类型错误:不可散列的类型 [英] TypeError: unhashable type

查看:44
本文介绍了类型错误:不可散列的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一小段代码来使用 sklearn 进行线性回归.

I wrote a small piece of code to do linear regression using sklearn.

我创建了一个 2 列 csv 文件(列名 X、Y 带有一些数字)和当我阅读文件时,我看到内容已正确读取 - 如下所示.

I created a 2 column csv file (column names X,Y with some numbers) and when I read the file I see that the content is properly read - as shown below.

但是,当我尝试使用命令 datafile[:,:]datafile[:,-1] 引用列时,出现unhashable type"错误代码>等等.

However, I am getting "unhashable type" error when I try to refer to a column using the commands datafile[:,:] or datafile[:,-1] etc..

当我尝试在 sklearn 的线性回归中使用 X 作为响应,Y 作为预测变量时,出现如下所示的值错误.

And when I try to use X as response, Y as predictor in sklearn's linear regression, I am getting Value error as shown below.

我在网上查看,但无法弄清楚我的代码或文件有什么问题.请帮忙.

I looked online but not able to figure out what is wrong with my code or file. Please help.

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]

推荐答案

如果您想使用切片语法从数据框中进行选择,您必须使用

If you want to use the slice syntax to select from a dataframe you have to use

data.iloc[:,:1]

对于你的第二个问题,X 输入需要是一个矩阵,而不是一个向量,所以要么包含更多的列,要么使用语法:

For your second problem, the X input needs to be a matrix, not a vector, so either include more columns or use the syntax:

model.fit(pd.DataFrame(datafile.X), datafile.Y)

这篇关于类型错误:不可散列的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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