gpt4 book ai didi

python - 如何使用 SciKit Learn 转换一次编码多个特征

转载 作者:行者123 更新时间:2023-11-28 20:31:55 26 4
gpt4 key购买 nike

我正在尝试对一些分类特征进行编码,以便能够将它们用作机器学习模型中的特征,目前我有以下代码:

data_path = '/Users/novikov/Assignment2/epl-training.csv'
data = pd.read_csv(data_path)
data['Date'] = pd.to_datetime(data['Date'])

le = preprocessing.LabelEncoder()


data['HomeTeam'] = le.fit_transform(data.HomeTeam.values)
data['AwayTeam'] = le.fit_transform(data.AwayTeam.values)
data['FTR'] = le.fit_transform(data.FTR.values)
data['HTR'] = le.fit_transform(data.HTR.values)
data['Referee'] = le.fit_transform(data.Referee.values)

这很好用,但是这并不理想,因为如果有 100 个特征要编码,手动完成会花费很长时间。我如何自动化该过程?我试过实现一个循环:

label_encode = ['HomeTeam', 'AwayTeam', 'FTR', 'HTR', 'Referee']

for feature in label_encode:
method = 'data.' + feature + '.values'
data[feature] = le.fit_transform(method)

但是我得到 ValueError: bad input shape ():

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-1b8fb6164d2d> in <module>()
11 method = 'data.' + feature + '.values'
12 print(method)
---> 13 data[feature] = le.fit_transform(method)

/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/label.py in fit_transform(self, y)
109 y : array-like of shape [n_samples]
110 """
--> 111 y = column_or_1d(y, warn=True)
112 self.classes_, y = np.unique(y, return_inverse=True)
113 return y

/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in column_or_1d(y, warn)
612 return np.ravel(y)
613
--> 614 raise ValueError("bad input shape {0}".format(shape))
615
616

ValueError: bad input shape ()

此代码的所有变体(比如只放置 data.feature.values)似乎都不起作用。除了手写之外,必须有一种方法可以做到这一点。

最佳答案

当然,method = 'data.' + feature + '.values' 将不起作用 - 它本身就是一个字符串!试试看

method = data[feature].values

for feature in label_encode:
data[feature] = le.fit_transform(data[feature].values)

关于python - 如何使用 SciKit Learn 转换一次编码多个特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53484270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com