我写了一小段代码来使用sklearn进行线性回归.
我创建了一个2列的csv文件(列名为X,Y带有一些数字),当我读取文件时,我看到内容被正确读取 - 如下所示.
然而,当我尝试参考使用命令列我收到"unhashable型"的错误datafile[:,:]或datafile[:,-1]等.
当我尝试使用X作为响应时,Y作为sklearn线性回归的预测因子,我得到的值如下所示.
我在线查看但无法弄清楚我的代码或文件有什么问题.请帮忙.
import pandas as pd
datafile=pd.read_csv('samplelinear.csv')
datafile
X Y
0 0 1.440000
1 1 33.220000
. . .
print datafile.__class__
<class 'pandas.core.frame.DataFrame'>
datafile[:,:]
TypeError: unhashable type
datafile[:,:1]
TypeError: unhashable type
from sklearn.linear_model import LinearRegression
model=LinearRegression()
model.fit(datafile.X,datafile.Y)
ValueError: Found arrays with inconsistent numbers of samples: [ 1 14]
Run Code Online (Sandbox Code Playgroud)