为逻辑回归转换 Dataframe(一种热编码)
Transforming Dataframe for logistic regression (one hot encoding)
我正在尝试 运行 python 中的逻辑回归。我的数据由数字数据和分类数据组成。我想用性别、年龄和食物偏好来预测某人是否喜欢猫。
我想我需要在 Food_preference 上进行一次热编码(见下文)但不确定具体如何进行。能否请你帮忙?谢谢!
原始数据框
Name Gender Age Like_cats Food_preference
John M 30 Yes Apple
John M 30 Yes Orange
John M 30 Yes Steak
Amy F 20 No Apple
Amy F 20 No Grape
所需的数据帧
Name Gender Age Like_cats Apple Orange Steak Grape
John M 30 Yes 1 1 1 0
Amy F 20 No 1 0 0 1
您可以使用 LabelEncoder 将字符串特征转换为数字特征。
这是一个与您的数据结构相同的工作代码:
from sklearn.linear_model import LogisticRegression
import pandas as pd
from sklearn import preprocessing
import numpy as np
X = pd.DataFrame([['a', 0], ['b', 1], ['a', 5], ['b', 100]])
y = [0, 1, 0, 1]
X_n = [[]]*len(X.columns)
i = 0
for c in X.columns:
if type(X[c].iloc[0]) == str: # if features are string encode them
le = preprocessing.LabelEncoder()
le.fit( list(set(X[c])) )
X_n[i] = le.transform(X[c])
else: # already numeric features
X_n[i] = list(X[c])
i += 1
X_n = np.array(X_n).T # transposing to make rows as per sample feature
print(X_n)
clf = LogisticRegression(random_state=0).fit(X_n, y)
我正在尝试 运行 python 中的逻辑回归。我的数据由数字数据和分类数据组成。我想用性别、年龄和食物偏好来预测某人是否喜欢猫。
我想我需要在 Food_preference 上进行一次热编码(见下文)但不确定具体如何进行。能否请你帮忙?谢谢!
原始数据框
Name Gender Age Like_cats Food_preference
John M 30 Yes Apple
John M 30 Yes Orange
John M 30 Yes Steak
Amy F 20 No Apple
Amy F 20 No Grape
所需的数据帧
Name Gender Age Like_cats Apple Orange Steak Grape
John M 30 Yes 1 1 1 0
Amy F 20 No 1 0 0 1
您可以使用 LabelEncoder 将字符串特征转换为数字特征。
这是一个与您的数据结构相同的工作代码:
from sklearn.linear_model import LogisticRegression
import pandas as pd
from sklearn import preprocessing
import numpy as np
X = pd.DataFrame([['a', 0], ['b', 1], ['a', 5], ['b', 100]])
y = [0, 1, 0, 1]
X_n = [[]]*len(X.columns)
i = 0
for c in X.columns:
if type(X[c].iloc[0]) == str: # if features are string encode them
le = preprocessing.LabelEncoder()
le.fit( list(set(X[c])) )
X_n[i] = le.transform(X[c])
else: # already numeric features
X_n[i] = list(X[c])
i += 1
X_n = np.array(X_n).T # transposing to make rows as per sample feature
print(X_n)
clf = LogisticRegression(random_state=0).fit(X_n, y)