Python“TypeError:unhashable type:'slice'”用于编码分类数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43290202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python "TypeError: unhashable type: 'slice'" for encoding categorical data
提问by kausik Chat
I am getting
我正进入(状态
TypeError: unhashable type: 'slice'
类型错误:不可散列的类型:“切片”
when executing the below code for encoding categorical data in Python. Can anyone please help?
在执行以下代码以在 Python 中编码分类数据时。有人可以帮忙吗?
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('50_Startups.csv')
y=dataset.iloc[:, 4]
X=dataset.iloc[:, 0:4]
# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 3] = labelencoder_X.fit_transform(X[:, 3])
回答by piRSquared
Xis a dataframe and can't be accessed via slice terminology like X[:, 3]. You must access via ilocor X.values. However, the way you constructed Xmade it a copy... so. I'd use values
X是一个数据框,不能通过像X[:, 3]. 您必须通过iloc或访问X.values。然而,你构建的方式X使它成为一个副本......所以。我会用values
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
# dataset = pd.read_csv('50_Startups.csv')
dataset = pd.DataFrame(np.random.rand(10, 10))
y=dataset.iloc[:, 4]
X=dataset.iloc[:, 0:4]
# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
# I changed this line
X.values[:, 3] = labelencoder_X.fit_transform(X.values[:, 3])
回答by Renu
use Values either while creating variable X or while encoding as mentioned above
如上所述,在创建变量 X 或编码时使用 Values
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
# dataset = pd.read_csv('50_Startups.csv')
dataset = pd.DataFrame(np.random.rand(10, 10))
y=dataset.iloc[:, 4].values
X=dataset.iloc[:, 0:4].values
回答by Gurbaksh Singh
While creating the matrix Xand Yvector use values.
在创建矩阵X和Y向量时使用values.
X=dataset.iloc[:,4].values
Y=dataset.iloc[:,0:4].values
It will definitely solve your problem.
它绝对会解决你的问题。
回答by chandan p
if you use .Values while creating the matrix X and Y vectors it will fix the problem.
如果在创建矩阵 X 和 Y 向量时使用 .Values 它将解决问题。
y=dataset.iloc[:, 4].values
X=dataset.iloc[:, 0:4].values
when you use .Values it creates a Object representation of the created matrix will be returned with the axes removed. Check the below link for more information
当您使用 .Values 时,它会创建所创建矩阵的 Object 表示,将在移除轴的情况下返回。查看以下链接以获取更多信息
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.values.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.values.html
回答by Sunitha G
I was getting same error (TypeError: unhashable type: 'slice') with below code:
我收到相同的错误(TypeError:unhashable type:'slice'),代码如下:
included_cols = [2,4,10]
dataset = dataset[:,included_cols] #Columns 2,4 and 10 are included.
Resolved with below code by putting iloc after dataset:
通过将 iloc 放在数据集之后使用以下代码解决:
included_cols = [2,4,10]
dataset = dataset.iloc[:,included_cols] #Columns 2,4 and 10 are included.
回答by Anvesh
Try by changing X[:,3] to X.iloc[:,3] in label encoder
尝试在标签编码器中将 X[:,3] 更改为 X.iloc[:,3]
回答by Ali üSTüNEL
Your x and y values ??are not running so first of all youre begin to write this point
你的 x 和 y 值 ?? 没有运行所以首先你开始写这一点
import numpy as np
import pandas as pd
import matplotlib as plt
dataframe=pd.read_csv(".\datasets\Position_Salaries.csv")
x=dataframe.iloc[:,1:2].values
y=dataframe.iloc[:,2].values
x1=dataframe.iloc[:,:-1].values
point of value have publish
价值点已发布

