Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 应为2D数组,但改为1D数组:数组=[1 3 5 6 7 8 9]?_Python_Arrays_Scikit Learn_Linear Regression - Fatal编程技术网

Python 应为2D数组,但改为1D数组:数组=[1 3 5 6 7 8 9]?

Python 应为2D数组,但改为1D数组:数组=[1 3 5 6 7 8 9]?,python,arrays,scikit-learn,linear-regression,Python,Arrays,Scikit Learn,Linear Regression,我在调用fit函数时遇到了这个错误:regr.fit(x,y) ValueError:应为2D数组,而应为1D数组: 数组=[1 3 5 6 7 8 9]。 使用数组重塑数据。如果数据具有单个特征或数组,则重塑(-1,1)。如果数据包含单个样本,则重塑(1,-1)。以下是更正的代码: x=[1,3,5,6,7,8,9] y=[4,5,6,9,3,4,6] def linear_model_main(X_parameters,Y_parameters,predict_value): # C

我在调用fit函数时遇到了这个错误:
regr.fit(x,y)

ValueError:应为2D数组,而应为1D数组: 数组=[1 3 5 6 7 8 9]。
使用数组重塑数据。如果数据具有单个特征或数组,则重塑(-1,1)。如果数据包含单个样本,则重塑(1,-1)。

以下是更正的代码:

x=[1,3,5,6,7,8,9]
y=[4,5,6,9,3,4,6]

def linear_model_main(X_parameters,Y_parameters,predict_value):
 
 # Create linear regression object
 regr = linear_model.LinearRegression()
 regr.fit(x, y)
 predict_outcome = regr.predict(predict_value)
 predictions = {}
 predictions['intercept'] = regr.intercept
 predictions['coefficient'] = regr.coef
 predictions['predicted_value'] = predict_outcome
 predicted_value = predict_outcome
 #return predicted_value
 return predictions



predictvalue = 7000
result = linear_model_main(x,y,predictvalue)
print ("Intercept value " , result['intercept'])
print ("coefficient" , result['coefficient'])
print ("Predicted value: ",result['predicted_value'])
你犯了几个错误,我将在下面解释:

1-首先,您需要将输入转换为NumPy数组,而不是使用1×n数组,您需要n×1数组。您得到的错误就是因为这个(这就是scikit学习模型的设计方式)

2-秒,您错过了属性名称末尾的下划线,如“intercept\ux”

3-预测值也应为n×1数组

解决这些问题后,结果如下(点为输入,虚线为线性模型):

编辑:这是绘图的代码:

from sklearn import linear_model
import numpy as np

x=[1,3,5,6,7,8,9]
y=[4,5,6,9,3,4,6]

def linear_model_main(X_parameters,Y_parameters,predict_value):
    # Create linear regression object
    regr = linear_model.LinearRegression()
    regr.fit(np.array(x).reshape(-1,1), np.array(y).reshape(-1,1))
    predict_outcome = regr.predict(np.array(predict_value).reshape(-1,1))
    predictions = {}
    predictions['intercept'] = regr.intercept_
    predictions['coefficient'] = regr.coef_
    predictions['predicted_value'] = predict_outcome
    predicted_value = predict_outcome
    #return predicted_value
    return predictions



predictvalue = 7000
result = linear_model_main(x,y,predictvalue)
print ("Intercept value " , result['intercept'])
print ("coefficient" , result['coefficient'])
print ("Predicted value: ",result['predicted_value'])

你在让我们猜错误发生在哪里。请更新问题以包含完整的错误回溯消息。感谢提供解决方案。我还有一个疑问,我们如何用其他值绘制预编辑值我们的预测值将在线(因为这是一个线性回归。我用这张图来绘制:
plt.scatter(x,y)
axes=plt.gca()x\u vals=np.array(axes.get\u xlim())y\u vals=result['intercept'[0]+result['coefficient']*x_vals plt.plot(x_vals,y_vals,'-')plt.show()
评论中很难看。我将编辑此帖子并将其放在那里。如果这解决了您的问题,请将答案标记为正确答案。是的,先生,已解决。您能解释什么是get_xlim(),和y轴表达式,以便我能有清楚的理解是的,为了绘制一条线,我们需要该线的一些点。通过
get_xlim()
我们得到x轴上的值,然后将这些值放入直线公式中,得到一些点来绘制直线。
plt.scatter(x,y)

axes = plt.gca()
x_vals = np.array(axes.get_xlim())
y_vals = result['intercept'][0] + result['coefficient'][0] * x_vals
plt.plot(x_vals, y_vals, '--')
plt.show()