python线性拟合误差分析_Python数据分析-线性回归、逻辑回
'''运⾏后会报错,因为这⾥输⼊的特征只有1个。'''
#第1步:导⼊线性回归
from sklearn.linear_model import LinearRegression
# 第2步:创建模型:线性回归
model=LinearRegression()
#第3步:训练模型
model.fit(x_train,y_train)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
7 model=LinearRegression()
8 #第3步:训练模型python 定义数组
----> 9 model.fit(x_train,y_train)
D:\ana\lib\site-packages\sklearn\linear_model\base.py in fit(self, X, y, sample_weight)
480 n_jobs_ = self.n_jobs
481 X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 482 y_numeric=True, multi_output=True)
483
484 if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:
D:\ana\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, dtype, order, cop
y, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
571 X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
572 ensure_2d, allow_nd, ensure_min_samples,
--> 573 ensure_min_features, warn_on_dtype, estimator)
574 if multi_output:
575 y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,
D:\ana\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
439 "Reshape your data either shape(-1, 1) if "
440 "your data has a single feature shape(1, -1) "
-
-> 441 "if it contains a single sample.".format(array))
442 array = np.atleast_2d(array)
443 # To ensure that array flags are maintained
ValueError: Expected 2D array, got 1D array instead:
array=[ 2. 4.75 3.25 4.25 5.5 5. 1.75 2.75 4.5 1. 0.5 4. 3.5
0.75 3. 2.5 ].
Reshape your data either shape(-1, 1) if your data has a single feature shape(1, -1) if it contains a single sample.
'''上⾯的报错内容,最后⼀⾏是这样提⽰我们的:Reshape your data either shape(-1, 1) if your data has a single feature shape(1, -1) if it contains a single sample.上⾯报错的内容翻译过来就是:如果你输⼊的数据只有1个特征,需要⽤shape(-1, 1)来改变数组的形状import numpy as np#定义2⾏*3列的数组aarr=np.array([[1,2,3],[5,6,7]])aarr.shape(2, 3)#改变数组形成为3⾏*2列shape(3,2)barrarray([[1, 2],[3, 5],[6, 7]])barr.shape(3, 2)'''
reshape⾏的参数是-1表⽰什么呢?例如reshape(-1,列数)
如果⾏的参数是-1,就会根据所给的列数,⾃动按照原始数组的⼤⼩形成⼀个新的数组,
例如reshape(-1,1)就是改变成1列的数组,这个数组的长度是根据原始数组的⼤⼩来⾃动形成的。
原始数组总共是2⾏*3列=6个数,那么这⾥就会形成6⾏*1列的数组
'''shape(-1,1)carr.shape(6, 1)'''
reshape列的参数是-1表⽰什么呢?例如reshape(⾏数,-1)
如果列的参数是-1,就会根据所给的⾏数,⾃动按照原始数组的⼤⼩形成⼀个新的数组,
例如reshape(1,-1)就是改变成1⾏的数组,这个数组的列数是根据原始数组的⼤⼩来⾃动形成的。
原始数组总共是2⾏*3列=6个数,那么这⾥就会形成1⾏*6列的数组
'''shape(1,-1)darr.shape(1, 6)'''
理解了reshape后,我们再来看下逻辑回归模型
sklearn要求输⼊的特征必须是⼆维数组的类型,但是因为我们⽬前只有1个特征,所以需要⽤安装错误提⽰⽤reshape转⾏成⼆维数组的类型。
错误提⽰信息:Reshape your data either shape(-1, 1) if your data has a single feature
'''#将训练数据特征转换成⼆维数组XX⾏*1列x_train=x_shape(-1,1)#将测试数据特征转换成⼆维数组⾏数*1列
x_test=x_shape(-1,1)#第1步:导⼊线性回归from sklearn.linear_model import LinearRegression# 第2步:创建模型:线性回归model=LinearRegression()#第3步:训练模型model.fit(x_train,y_train)LinearRegression(copy_X=True,
fit_intercept=True, n_jobs=1, normalize=False)'''
最佳拟合线:z= + x
截距intercept:a
回归系数:b
'''#截距a=model.intercept_#回归系数f_print('最佳拟合线:截距a=',a,',回归系数b=',b)最佳拟合线:截距a=
8.30405982906 ,回归系数b= [ 16.20683761]#绘图import matplotlib.pyplot as plt#训练数据散点图
plt.scatter(x_train,y_train,color='blue',label='train data')#训练数据的预测值y_train_pred=model.predict(x_train)#绘制最佳拟合线plt.plot(x_train,y_train_pred,color='black',linewidth=3,label='best line')#添加图标标签
plt.legend(loc=2)plt.xlabel('Hours')plt.ylabel('Score')#显⽰图像plt.show()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。