Loading... - 数据准备 ```python import numpy as np from sklearn import linear_model import matplotlib.pyplot as plt # 数据准备 x = np.array([1,3,2,1,3]) y = np.array([14,24,18,17,27]) regr = linear_model.LinearRegression() # 模型训练 regr.fit(x.reshape(-1,1),y) ``` - 数据处理 ```python # 得到直线的斜率、截距、公式 y = a*x + b a,b = regr.coef_,regr.intercept_ print('a=',a,'b=',b) # 对回归模型打分 print('score=',regr.score(x.reshape(-1,1),y)) # 可视化模型 # 1.真实的点 plt.scatter(x,y,color='blue') # 2.拟合的直线 plt.plot(x,regr.predict(x.reshape(-1,1)),color='red',linewidth=4) plt.show() # 对数据进行预测 c = np.array([6]) print('y_pre=',regr.predict(c.reshape(-1,1))) ``` ### 多元线性回归 ```python from sklearn import datasets # 从scikit-learn导入数据集 load_data = datasets.load_boston() # 从数据集库加载波士顿数集 import numpy as np import pandas as pd from sklearn.model_selection import train_test_split #导入数据 data_x = load_data.data data_y = load_data.target x_train,x_verify,y_train,y_verify = train_test_split(data_x,data_y,train_size=0.8,test_size=0.2) ``` ```python # 建立线性回归模型及训练 lm = linear_model.LinearRegression() lm.fit(x_train,y_train) # 得到直线的斜率、截距、公式 y=a*x+b a,b = lm.coef_,lm.intercept_ print('a=',a,'\nb=',b) # 对回归模型进行检查,给实际观测点和回归方程的拟合程度打分 print('train_score=',lm.score(x_train,y_train)) # 对新输一局进行预测 predictions = lm.predict(x_verify) print('test_score=',lm.score(x_verify,y_verify)) ``` ### 全连接神经网络回归 ```python from sklearn.neural_network import MLPRegressor from sklearn.preprocessing import StandardScaler from sklearn import datasets #从scikit-learn导入数据集 load_data = datasets.load_boston() # 从数据集库加载波士顿数据集 import numpy as np import pandas as pd from sklearn.model_selection import train_test_split #导入数据 data_x = load_data.data data_y = load_data.target scaler = StandardScaler() # 标准化转换 scaler.fit(data_x) # 训练标准化对象 data_x = scaler.transform(data_x) # 转换数据集 x_train,x_verify,y_train,y_verify=train_test_split(data_x,data_y,train_size=0.8,test_size=0.2) # 神经网络回归 clf = MLPRegressor(solver='lbfgs',alpha=1e-5,hidden_layer_sizes=(5,2),random_state=1) clf.fit(x_train,y_train) # 查看训练模型得分 print('train_score=',clf.score(x_train,y_train)) # 模型预测 pred = clf.predict(x_verify) # 查看预测得分 print('test_score=',clf.score(x_verify,y_verify)) ``` ```python # 查看网络结构 index=0 for w in clf.coefs_: index += 1 print('第{}层网络层:'.format(index)) print('权重矩阵大小:',w.shape) print('系数矩阵:',w) ``` 最后修改:2021 年 09 月 27 日 © 禁止转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏