Sklearn 线性回归 - “IndexError: tuple index out of range" [英] Sklearn Linear Regression - "IndexError: tuple index out of range"

查看:133
本文介绍了Sklearn 线性回归 - “IndexError: tuple index out of range"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt导入 scipy.interpolate 作为 interp从 sklearn 导入 linear_modelin_file = 打开(路径,r")文本 = np.loadtxt(in_file)in_file.close()x = np.array(text[:,0])y = np.array(text[:,1])

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

<块引用>

IndexError:元组索引超出范围

regr = linear_model.LinearRegression()regr.fit(x,y)

我做错了什么?

解决方案

Linear Regression 期望 X 作为二维数组,内部需要 X.shape[1] 初始化一个 np.ones 数组.因此,将 X 转换为 nx1 数组 就行了.所以,替换:

regr.fit(x,y)

作者:

regr.fit(x[:,np.newaxis],y)

这将解决问题.演示:

<预><代码>>>>从 sklearn 导入数据集>>>从 sklearn 导入 linear_model>>>clf = linear_model.LinearRegression()>>>iris=datasets.load_iris()>>>X=iris.data[:,3]>>>Y=虹膜.目标>>>clf.fit(X,Y) # 这会抛出错误回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/usr/lib/python2.7/dist-packages/sklearn/linear_model/base.py",第363行,合适X, y, self.fit_intercept, self.normalize, self.copy_X)文件/usr/lib/python2.7/dist-packages/sklearn/linear_model/base.py",第103行,在center_dataX_std = np.ones(X.shape[1])IndexError:元组索引超出范围>>>clf.fit(X[:,np.newaxis],Y) # 这将正常工作线性回归(copy_X=True,fit_intercept=True,normalize=False)

要绘制回归线,请使用以下代码:

<预><代码>>>>从 matplotlib 导入 pyplot 作为 plt>>>plt.scatter(X, Y, color='red')<matplotlib.collections.PathCollection 对象在 0x7f76640e97d0>>>>plt.plot(X, clf.predict(X[:,np.newaxis]), color='blue')<matplotlib.lines.Line2D 对象在 0x7f7663f9eb90>>>>plt.show()

I have a ".dat" file in which are saved values of X and Y (so a tuple (n,2) where n is the number of rows).

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interp
from sklearn import linear_model

in_file = open(path,"r")
text = np.loadtxt(in_file)
in_file.close()
x = np.array(text[:,0])
y = np.array(text[:,1])

I created an instance for linear_model.LinearRegression(), but when I invoke the .fit(x,y) method I get

IndexError: tuple index out of range

regr = linear_model.LinearRegression()
regr.fit(x,y)

What did I do wrong?

解决方案

Linear Regression expects X as an array with two dimensions and internally requires X.shape[1] to initialize an np.ones array. So converting X to an nx1 array would do the trick. So, replace:

regr.fit(x,y)

by:

regr.fit(x[:,np.newaxis],y)

This will fix the problem. Demo:

>>> 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)

To plot the regression line use the below code:

>>> 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()

这篇关于Sklearn 线性回归 - “IndexError: tuple index out of range"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆