回归算法与聚类算法学习
一、目的
- 1.熟练掌握线性回归算法
- 2.了解并掌握K邻居回归算法
- 3.了解并掌握集成模型(回归)算法
- 4.熟练掌握K均值聚类算法
- 5.熟练掌握回归评价方法
- 6.熟练掌握聚类评价方法
二、题目内容及解析
按下面步骤要求完成实验:
1“使用线性回归算法实现美国波士顿房价预测算法”实例,并在Jupyter环境重现所有结果,要求对每一条Python语句均清楚了解其语法和用法,并重点理解回归算法的评价方法。包括代码【34-38】
1.代码:
from sklearn.datasets import load_boston
boston = load_boston()
print (boston.DESCR)
from sklearn.model_selection import train_test_split
import numpy as np
X = boston.data
y = boston.target
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state = 33,test_size = 0.5)
print("The max target value is",np.max(boston.target))
print("The min target value is",np.min(boston.target))
print("The average target value is",np.mean(boston.target))
from sklearn.preprocessing import StandardScaler
ss_X = StandardScaler()
ss_y = StandardScaler()
X_train.shape
X_test.shape
y_train=y_train.reshape(-1,1)
y_test=y_test.reshape(-1,1)
X_train = ss_X.fit_transform(X_train)
X_test = ss_X.transform(X_test)
y_train = ss_y.fit_transform(y_train)
y_test= ss_y.fit_transform(y_test)
#线性回归
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X_train,y_train)
lr_y_predict = lr.predict(X_test)
# SGD回归
from sklearn.linear_model import SGDRegressor
sgdr = SGDRegressor()
sgdr.fit(X_train,y_train)
sgdr_y_predict = sgdr.predict(X_test)
print('The value of default measurement of LinearRegression is',lr.score(X_test,y_test))
结果:
2“使用K近邻(回归)算法实现美国波士顿房价预测算法”实例,并在Jupyter环境重现所有结果,要求对每一条Python语句均清楚了解其语法和用法。包括代码【41-42】
代码: mport numpy as np
from sklearn import datasets
from sklearn.metrics import mean_squared_error,explained_variance_score,r2_score
from sklearn