Traditional_machine_learning_tutorial

Scikit-learn开发环境的搭建

使用conda或mamba创建一个新的虚拟环境,安装指定版本的python和sklearn.

此处示例是使用mamba创建和安装,conda的命令相同,把mamba替换为conda即可。

mamba create -n sklearn python==3.12
mamba activate sklearn
pip install scikit-learn==1.8

我是习惯使用pip来安装机器学习和深度学习的包,因为mamba安装的时候有时候会安装错误。 现在可以使用uv来安装,速度比pip更快。uv pip install scikit-learn==1.8

我遇到报错说gcc版本太低,编译失败,就先升级到gcc11 mamba install -c conda-forge gxx_linux-64=11

如果pip安装scikit-learn失败,那就直接使用mamba安装。

mamba install -c conda-forge scikit-learn==1.8

安装其他常用包和可视化包

uv pip install pandas matplotlib seaborn

安装jupyter环境的包(如果使用jupyter notebook,则需要安装下面的包)

jupyter使用指南https://www.jianshu.com/p/d2ac1b9e6b24

uv pip install jupyter notebook

mamba install ipykernel

把conda环境名为sklearn的环境添加到jupyter的内核
python -m ipykernel install --user --name sklearn --display-name "sklearn"

开发环境

使用Google colab的注意事项

  1. 环境里默认运行的命令是python,所以要运行系统命令安装软件,需要在命令前面添加! 例如:
    • !ls -lh
    • !pip install pandas
    • `` 2.google colab保存的文件的默认地址
      临时目录

      /content/

挂载google Drive(谷歌云盘)后的路径
from google.cobal import drive
drive.mount('/content/drive/')


#将文件永久保存到google drive云盘
df.to_csv('/content/drive/MyDrive/output.csv')

测试一下是否安装成功sklean

from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(random_state=0)
X = [[ 1,  2,  3],  # 2 samples, 3 features
     [11, 12, 13]]
y = [0, 1]  # classes of each sample
clf.fit(X, y)