pandas-用于对分类d进行编码的Python“ TypeError:不可哈希类型:'slice'”
我正进入(状态
TypeError:无法散列的类型:'slice'
执行以下代码以在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])
7个解决方案
87 votes
X.values是一个数据框,无法通过切片术语(如X[:, 3])进行访问。您必须通过X.values或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])
piRSquared answered 2020-01-08T23:01:29Z
6 votes
如上所述在创建变量X或编码时使用值
# 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
Renu answered 2020-01-08T23:01:49Z
4 votes
创建矩阵X和Y向量时,请使用values。
X=dataset.iloc[:,4].values
Y=dataset.iloc[:,0:4].values
它肯定会解决您的问题。
Gurbaksh Singh answered 2020-01-08T23:02:13Z
2 votes
如果在创建矩阵X和Y向量时使用.Values,则可以解决此问题。
y=dataset.iloc[:, 4].values
X=dataset.iloc[:, 0:4].values
当您使用.Values时,它将创建一个创建的矩阵的Object表示,并且将删除轴。检查以下链接以获取更多信息
[https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.values.html]
chandan p answered 2020-01-08T23:02:43Z
0 votes
我收到以下代码相同的错误(TypeError:不可哈希类型:'slice'):
included_cols = [2,4,10]
dataset = dataset[:,included_cols] #Columns 2,4 and 10 are included.
通过将iloc放在数据集之后来解决以下代码:
included_cols = [2,4,10]
dataset = dataset.iloc[:,included_cols] #Columns 2,4 and 10 are included.
Sunitha G answered 2020-01-08T23:03:07Z
0 votes
尝试通过将标签编码器中的X [:,3]更改为X.iloc [:,3]
Anvesh answered 2020-01-08T23:03:27Z
-2 votes
您的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
价值点已经发布
Ali ÜSTÜNEL answered 2020-01-08T23:03:51Z