我试图将分类字符串列转换为几个虚拟变量二进制列,但我得到了一个值错误。
以下是代码:
import sys, os
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from dateutil import parser
import math
import traceback
import logging
datasetMod = pd.read_csv('data.csv')
X = datasetMod.iloc[:, 3:6].values
y = datasetMod.iloc[:, 1].values
print(X[:, 0])
# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
try:
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
这是错误:
<class 'ValueError'> multipleLinearRegression.py 23
该列的print语句的结果是:
['Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Weekend' 'Workday' 'Workday' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday' 'Workday'
'Workday' 'Workday' 'Workday' 'Workday' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend' 'Weekend'
'Weekend' 'Weekend' 'Weekend' 'Weekend']
字符串本身似乎没有任何问题,中间没有空格,没有数字符号。所以我不明白为什么我得到一个值类型不能将字符串转换为浮点错误。
任何帮助都将受到高度赞赏。
更新
onehotencoder现在工作得有点好,但最终结果是object类型,而它应该是float64类型:
labelencoder_X = LabelEncoder()
X[:, 1] = labelencoder_X.fit_transform(X[:, 1])
X[:, 2] = labelencoder_X.fit_transform(X[:, 2])
X[:, 3] = labelencoder_X.fit_transform(X[:, 3])
onehotencoder = OneHotEncoder(categorical_features = [1,2,3])
onehotencoder.fit(X[:, 1])
onehotencoder.fit(X[:, 2])
onehotencoder.fit(X[:, 3])
onehotencoder.transform(X[:, 1])
onehotencoder.transform(X[:, 2])
onehotencoder.transform(X[:, 3])
X = onehotencoder.toArray()
更新2
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 1] = labelencoder_X.fit_transform(X[:, 1])
X[:, 2] = labelencoder_X.fit_transform(X[:, 2])
X[:, 3] = labelencoder_X.fit_transform(X[:, 3])
onehotencoder = OneHotEncoder(categorical_features = [1,2,3])
X[:, 1] = onehotencoder.fit_transform(X[:, 1]).toarray()
X[:, 2] = onehotencoder.fit_transform(X[:, 2]).toarray()
X[:, 3] = onehotencoder.fit_transform(X[:, 3]).toarray()
print(X.dtype) #object
最终代码
由于categorical_features
已经指定了索引,我可以在整个矩阵X
上使用fit_transform()。感谢@mkos的耐心等待!
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 1] = labelencoder_X.fit_transform(X[:, 1])
X[:, 2] = labelencoder_X.fit_transform(X[:, 2])
X[:, 3] = labelencoder_X.fit_transform(X[:, 3])
onehotencoder = OneHotEncoder(categorical_features = [1,2,3])
X = onehotencoder.fit_transform(X)
答案 0 :(得分:2)
这应该可以解决问题:
onehotencoder = OneHotEncoder(categorical_features = [1,2,3])
X = onehotencoder.fit_transform(X)
你可以用以下方式打印:
print(X.toArray())
将X
作为稀疏矩阵并不错,因为它可以节省内存。如果您想查看它,则可以使用np.array
将其转换为常规toArray()
。