【学习笔记】人民邮电出版社《Python机器学习》学习记录。
以下程序调用了机器学习的第三方库scikit-learn,使用该库,方便简洁。
import numpy
import pandas
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.datasets import load_boston
boston_data = load_boston() #波士顿房价数据,sklearn自带数据库
boston_data_frame = pandas.DataFrame(boston_data.data, columns = boston_data.feature_names)
boston_data_x = boston_data_frame[boston_data_frame.columns[0:13]]
boston_data_y = pandas.DataFrame(boston_data.target, columns=['Price'])
abs_corr_matrix = boston_data_x.corr().abs() #协方差矩阵绝对值
print(abs_corr_matrix)
#ones构造全1矩阵;triu构造上三角矩阵,上三角全1,下三角全0;astype将0、1转化成False、True
print(numpy.triu(numpy.ones(abs_corr_matrix.shape), k=1).astype(numpy.bool))
uptri = abs_corr_matrix.where(numpy.triu(numpy.ones(abs_corr_matrix.shape), k=1).astype(numpy.bool))
#找出与其他特征的相关性大于0.75的所有特征,['NOX', 'DIS', 'TAX']
correlated_features = [column for column in uptri.columns if any(uptri[column] > 0.75)]
boston_data_x = boston_data_x.drop(correlated_features, axis=1) #数据中删除这些特征列,剩余10个特征
#分割数据
x_train, x_test, y_train, y_test = train_test_split(boston_data_x, boston_data_y, test_size=0.2)
linear_regression = LinearRegression() #创建线性回归对象
linear_regression.fit(x_train, y_train) #回归拟合
x_test=pandas.DataFrame([[1, 0,0,0,0,0,0,0,0,0]])
y_pred = linear_regression.predict(x_test) #预测值
print(linear_regression.coef_) #输出各个特征变量对应的系数
print(linear_regression.intercept_) #输出截距
print("均方误差:%.1f" % mean_squared_error(y_test, y_pred))