摘要:关于 - 写文章 (jianshu.com)的代码理解
参考:
(1)为什么一些机器学习模型需要对数据进行归一化? - zhanlijun - 博客园 (cnblogs.com)
(2)(3条消息) 关于相关系数的一些理解误区_witforeveryang的专栏-CSDN博客_皮尔逊相关系数的缺点
1.机器学习过程
(1)Look at the big picture
(2)Get the data
(3)Discover and visualize the data to gain insights
(4)Prepared the data for machine learning algorithms
(5)Select a model and train it
(6)Fine-tune ur model
(7)Present ur solution
(8)Launch, monitor and maintain ur system
7.整理回顾
通过波士顿房价的代码,总算是画出了一些图,输出了一些结果。个人对机器学习的过程也有了一些新的理解。简单来说就是不停的调用各种包,常见的有model_selection用于数据集的分割,其中又有train_test_split包和KFold包两种不同的处理方式,当然还有第三种自助法,后续再说;preprocessing包用于数据预处理,可对数据进行标准化与归一化处理;linear_model线性模型里有线性回归模型;每一个机器学习算法都是一个类,使用时需要实例化,使用不同的训练集可以训练出不同的实例,他们的预测结果也是不一样的;可以使用metrics里的函数对模型进行评估,选出最优的模型,或者选出最好的参数(参数调整也是很重要的一步,同一个训练集可以训练出参数不同的多个模型,至于选什么参数最好,还需要后续的优化处理)。
7.1关于“为什么”
为什么要归一化?
维基百科里对这个问题有如下解释:1)归一化后加快了梯度下降求最优解的速度;2)归一化有可能提高精度。至于为什么有这些优点,为什么一些机器学习模型需要对数据进行归一化? - zhanlijun - 博客园 (cnblogs.com)这篇文章讲的很详细。
为什么要计算相关系数?
- 写文章 (jianshu.com)文章中给出了在csdn上找到的一个代码,后续的几篇理解里的代码与这篇文章中的代码不完全相同,这是因为有些内容个人认为不太准确。比如相关系数。阅读了(3条消息) 关于相关系数的一些理解误区_witforeveryang的专栏-CSDN博客_皮尔逊相关系数的缺点之后,对相关系数有了更好的理解,这篇文章大家可以参考,结论就是不一定所有的线性回归都需要计算相关系数。
7.2代码
所有代码以及归一化和计算相关系数后的评估指标的对比(我发现啥都不做得出的最终模型是最好的,真是非常amazing,不知道是不是我处理有问题,欢迎大家指正)
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn import datasets
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score, mean_squared_error
# In[2]:
plt.rcParams['font.sans-serif'] = ['SimHei']
# In[7]:
def figure(title, *datalist):
plt.figure(facecolor='gray', figsize=(20, 10))
for v in datalist:
plt.plot(v[0], '-', label=v[1], linewidth=2)
plt.plot(v[0], 'o')
plt.grid()
plt.title(title, fontsize=20)
plt.legend(fontsize=16)
plt.show()
# In[3]:
boston = datasets.load_boston()
data = pd.DataFrame(boston.data, columns=boston.feature_names)
data['Price'] = boston.target
data
# In[24]:
data.corr()['Price']
# In[21]:
new_data = data.T[abs(data.corr()['Price'])>=0.5].T
new_data
# In[15]:
x = boston.data
y = boston.target
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
lr1 = LinearRegression()
lr1.fit(x_train, y_train)
y_train_pred = lr1.predict(x_train)
y_test_pred = lr1.predict(x_test)
print("The mean_sqared_error for train set is: {:.4f}".format(mean_squared_error(y_train, y_train_pred)))
print("The mean_sqared_error for test set is: {:.4f}".format(mean_squared_error(y_test, y_test_pred)))
print("The r2_core for train set is: {:.4f}".format(lr1.score(x_train, y_train)))
print("The r2_core for test set is: {:.4f}".format(lr1.score(x_test, y_test)))
figure("预测值与真实值图模型的$R^2={:.4f}$".format(r2_score(y_test, y_test_pred)), [y_test, "ture"], [y_test_pred, "pred"])
# In[17]:
x = boston.data
y = boston.target
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
min_max_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
x_train = min_max_scaler.fit_transform(x_train)
x_test= min_max_scaler.fit_transform(x_test)
y_train = min_max_scaler.fit_transform(y_train.reshape(-1, 1))
y_test = min_max_scaler.fit_transform(y_test.reshape(-1, 1))
lr2 = LinearRegression()
lr2.fit(x_train, y_train)
y_train_pred = lr2.predict(x_train)
y_test_pred = lr2.predict(x_test)
print("The mean_sqared_error for train set is: {:.4f}".format(mean_squared_error(y_train, y_train_pred)))
print("The mean_sqared_error for test set is: {:.4f}".format(mean_squared_error(y_test, y_test_pred)))
print("The r2_core for train set is: {:.4f}".format(lr2.score(x_train, y_train)))
print("The r2_core for test set is: {:.4f}".format(lr2.score(x_test, y_test)))
figure("预测值与真实值图模型的$R^2={:.4f}$".format(r2_score(y_test, y_test_pred)), [y_test, "ture"], [y_test_pred, "pred"])
# In[22]:
x = np.array(new_data.iloc[:, :-1])
y = np.array(new_data.iloc[:, -1:])
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
lr1 = LinearRegression()
lr1.fit(x_train, y_train)
y_train_pred = lr1.predict(x_train)
y_test_pred = lr1.predict(x_test)
print("The mean_sqared_error for train set is: {:.4f}".format(mean_squared_error(y_train, y_train_pred)))
print("The mean_sqared_error for test set is: {:.4f}".format(mean_squared_error(y_test, y_test_pred)))
print("The r2_core for train set is: {:.4f}".format(lr1.score(x_train, y_train)))
print("The r2_core for test set is: {:.4f}".format(lr1.score(x_test, y_test)))
figure("预测值与真实值图模型的$R^2={:.4f}$".format(r2_score(y_test, y_test_pred)), [y_test, "ture"], [y_test_pred, "pred"])
# In[23]:
x = np.array(new_data.iloc[:, :-1])
y = np.array(new_data.iloc[:, -1:])
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
min_max_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
x_train = min_max_scaler.fit_transform(x_train)
x_test= min_max_scaler.fit_transform(x_test)
y_train = min_max_scaler.fit_transform(y_train.reshape(-1, 1))
y_test = min_max_scaler.fit_transform(y_test.reshape(-1, 1))
lr2 = LinearRegression()
lr2.fit(x_train, y_train)
y_train_pred = lr2.predict(x_train)
y_test_pred = lr2.predict(x_test)
print("The mean_sqared_error for train set is: {:.4f}".format(mean_squared_error(y_train, y_train_pred)))
print("The mean_sqared_error for test set is: {:.4f}".format(mean_squared_error(y_test, y_test_pred)))
print("The r2_core for train set is: {:.4f}".format(lr2.score(x_train, y_train)))
print("The r2_core for test set is: {:.4f}".format(lr2.score(x_test, y_test)))
figure("预测值与真实值图模型的$R^2={:.4f}$".format(r2_score(y_test, y_test_pred)), [y_test, "ture"], [y_test_pred, "pred"])
吐槽一下,没法插入文件可真是太难了
补充一下:
最后可以输出线性模型的系数:
# 线性回归的系数
print('线性回归的系数为:\n w = {} \n b = {}'.format(lr1.coef_, lr1.intercept_))