1. 安装sklearn
pycharm-file-settings-project interpreter-最右侧绿色添加按钮-搜索sklearn-install packages
2. sklearn数据集
http://scikit-learn.org/stable/modules/classes.html#module-sklearn.datasets
load_:表示下载数据
make_:表示创造数据
from sklearn import datasets loaded_data = datasets.load_boston() data_X = loaded_data.data data_y = loaded_data.target
3. 定义模型
http://scikit-learn.org/stable/modules/classes.html#module-sklearn.cluster
from sklearn.linear_model import LinearRegression model = LinearRegression() model.fit(data_X, data_y) # 训练模型 print(model.predict(data_X[:4, :])) # 打印输出值 print(data_y[:4]) # 输出结果 # [ 30.00821269 25.0298606 30.5702317 28.60814055] # [ 24. 21.6 34.7 33.4]
4. 数据可视化
使用matplotlib
import matplotlib.pyplot as plt plt.scatter(X, y) plt.show()