Python Sklearn线性回归-“;索引器:元组索引超出范围“;

Python Sklearn线性回归-“;索引器:元组索引超出范围“;,python,scikit-learn,Python,Scikit Learn,我有一个“.dat”文件,其中保存了X和Y的值(因此是一个元组(n,2),其中n是行数) 我为linear\u model.LinearRegression()创建了一个实例,但是当我调用.fit(x,y)方法时,我得到了 索引器错误:元组索引超出范围 我做错了什么?线性回归期望将X作为二维数组,并在内部要求X.shape[1]初始化np.one数组。因此,将X转换为nx1数组就可以了。因此,替换: regr.fit(x,y) 作者: 这将解决问题。演示: >>> from

我有一个“.dat”文件,其中保存了X和Y的值(因此是一个元组(n,2),其中n是行数)

我为
linear\u model.LinearRegression()
创建了一个实例,但是当我调用
.fit(x,y)
方法时,我得到了

索引器错误:元组索引超出范围


我做错了什么?

线性回归期望将
X
作为二维数组,并在内部要求
X.shape[1]
初始化
np.one
数组。因此,将
X
转换为nx1数组就可以了。因此,替换:

regr.fit(x,y)
作者:

这将解决问题。演示:

>>> from sklearn import datasets
>>> from sklearn import linear_model
>>> clf = linear_model.LinearRegression()
>>> iris=datasets.load_iris()
>>> X=iris.data[:,3]
>>> Y=iris.target
>>> clf.fit(X,Y)  # This will throw an error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/sklearn/linear_model/base.py", line 363, in fit
    X, y, self.fit_intercept, self.normalize, self.copy_X)
  File "/usr/lib/python2.7/dist-packages/sklearn/linear_model/base.py", line 103, in center_data
    X_std = np.ones(X.shape[1])
IndexError: tuple index out of range
>>> clf.fit(X[:,np.newaxis],Y)  # This will work properly
LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
来自sklearn导入数据集的
>>
>>>从sklearn导入线性_模型
>>>clf=线性模型。线性回归()
>>>iris=数据集。加载\u iris()
>>>X=虹膜。数据[:,3]
>>>Y=iris.target
>>>clf.fit(X,Y)#这将抛出一个错误
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/lib/python2.7/dist-packages/sklearn/linear_-model/base.py”,第363行,适合
十、 y,self.fit\u截距,self.normalize,self.copy\u X)
文件“/usr/lib/python2.7/dist packages/sklearn/linear\u model/base.py”,第103行,在中心数据中
X_标准=np.one(X.shape[1])
索引器错误:元组索引超出范围
>>>clf.fit(X[:,np.newaxis],Y)#这将正常工作
线性回归(复制X=真,拟合截距=真,规格化=假)
要绘制回归线,请使用以下代码:

>>> from matplotlib import pyplot as plt
>>> plt.scatter(X, Y, color='red')
<matplotlib.collections.PathCollection object at 0x7f76640e97d0>
>>> plt.plot(X, clf.predict(X[:,np.newaxis]), color='blue')
<matplotlib.lines.Line2D object at 0x7f7663f9eb90>
>>> plt.show()
>>从matplotlib导入pyplot作为plt
>>>plt.散射(X,Y,color='red')
>>>plt.plot(X,clf.predict(X[:,np.newaxis]),color='blue')
>>>plt.show()

对不起,我完全误解了你的问题:(我已经删除了答案,如果我能得到修复,那么我将取消删除编辑后的答案。但是你能提供更多信息吗?比如你的完整代码?这是你需要的代码,没有其他重要的。真的吗?什么是
线性模型
?你是怎么得到它的?现在真的就这些了,谢谢你的帮助。x和Y的长度相同吗?谢谢非常感谢您的帮助!另一个问题:现在我从线性回归中只得到一个系数是正常的吗?我如何绘制它的线?@JackLametta,这绝对正常。这些系数用于预测给定Y值的X值。我已将代码上传到绘图线。
regr.fit(x[:,np.newaxis],y)
>>> from sklearn import datasets
>>> from sklearn import linear_model
>>> clf = linear_model.LinearRegression()
>>> iris=datasets.load_iris()
>>> X=iris.data[:,3]
>>> Y=iris.target
>>> clf.fit(X,Y)  # This will throw an error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/sklearn/linear_model/base.py", line 363, in fit
    X, y, self.fit_intercept, self.normalize, self.copy_X)
  File "/usr/lib/python2.7/dist-packages/sklearn/linear_model/base.py", line 103, in center_data
    X_std = np.ones(X.shape[1])
IndexError: tuple index out of range
>>> clf.fit(X[:,np.newaxis],Y)  # This will work properly
LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
>>> from matplotlib import pyplot as plt
>>> plt.scatter(X, Y, color='red')
<matplotlib.collections.PathCollection object at 0x7f76640e97d0>
>>> plt.plot(X, clf.predict(X[:,np.newaxis]), color='blue')
<matplotlib.lines.Line2D object at 0x7f7663f9eb90>
>>> plt.show()