Python: 线性回归,调整numpy数组以用于模型

抱歉问一个新手问题…这是我的代码:

from __future__ import divisionimport sklearnimport numpy as npfrom scipy import stats from sklearn.linear_model import LinearRegressionimport matplotlib.pyplot as pltX =np.array([6,8,10,14,18])Y = np.array([7,9,13,17.5,18])X = np.reshape(X,(1,5))Y = np.reshape(Y,(1,5))print Xprint Yplt.figure()plt.title('Pizza Price as a function of Pizza Diameter')plt.xlabel('Pizza Diameter (Inches)')plt.ylabel('Pizza Price (Dollars)')axis = plt.axis([0, 25, 0 ,25])m, b = np.polyfit(X,Y,1)plt.grid(True)plt.plot(X,Y, 'k.')plt.plot(X, m*X + b, '-')#plt.show()#training data#x= [[6],[8],[10],[14],[18]]#y= [[7],[9],[13],[17.5],[18]]# create and fit linear regression modelmodel = LinearRegression()model.fit(X,Y)print 'A 12" pizza should cost $% .2f' % model.predict(19)#work out cost function, which is residual sum of squaresprint 'Residual sum of squares: %.2f' % np.mean((model.predict(x)- y) ** 2)#work out variance (AKA Mean squared error)xMean = np.mean(x)print 'Variance is: %.2f' %np.var([x], ddof=1)#work out covariance (this is whether the x axis data and y axis data correlate with eachother)#When a and b are 1-dimensional sequences, numpy.cov(x,y)[0][1] calculates covarianceprint 'Covariance is: %.2f' %np.cov(X, Y, ddof = 1)[0][1]#test the model on new test data, printing the r squared coefficientX_test = [[8], [9], [11], [16], [12]]y_test = [[11], [8.5], [15], [18], [11]]print 'R squared for model on test data is: %.2f' %model.score(X_test,y_test)

基本上,有些函数对我的变量X和Y有效,而有些则无效。

例如,按照当前的代码,它会抛出以下错误:

TypeError: expected 1D vector for x 

在这一行:

m, b = np.polyfit(X,Y,1)

然而,当我注释掉调整变量的这两行,像这样:

#X = np.reshape(X,(1,5))#Y = np.reshape(Y,(1,5))

我会得到以下错误:

ValueError: Found input variables with inconsistent numbers of samples: [1, 5]

在这一行:

model.fit(X,Y)

那么,我该如何调整数组,使其在脚本中的所有函数中都能正常工作,而不需要使用结构略有不同的相同数据的不同数组呢?

谢谢你的帮助!


回答:

修改这些行

X = np.reshape(X,(5))Y = np.reshape(Y,(5))

或者干脆删除这两行enter image description here

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注