使用Colaboratory的必要框架

安装字体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import subprocess
FontNotInstalled = subprocess.run('fc-list | grep "Noto Serif CJK JP"', shell=True)

if FontNotInstalled.returncode:
subprocess.run('apt install -y fonts-noto-cjk', shell=True)
import matplotlib
cache_dir = matplotlib.get_cachedir()
import shutil
shutil.rmtree(cache_dir)
import os
os._exit(0)
else:
pass

from matplotlib import rcParams
rcParams['font.family'] = 'Noto Serif CJK JP'
rcParams['axes.unicode_minus'] = False

挂载 Google Drive

1
2
from google.colab import drive
drive.mount('/content/drive')

设置工作目录

1
2
import os
os.chdir('/content/drive/MyDrive/Colaboratory/机器学习/实验四')

导出 Notebook 为 LaTeX

1
2
3
%%capture
!sudo apt install -y pandoc texlive-xetex
!jupyter nbconvert --to latex 'PCA算法和K-means算法.ipynb' --output-dir='LaTeX'