python - 如何将二元类 Logistic 回归与 Python 合并

标签 python machine-learning scikit-learn logistic-regression

我有一个包含许多分类列的视频游戏数据集。 我对所有这些列进行了二值化。 现在我想用逻辑回归来预测一列(称为Rating),但该列现在实际上被二值化为4列(Rating_Everyone、Rating_Everyone10+、Rating_Teen和Rating_Mature)。 因此,我应用了四次逻辑回归,这是我的代码:

df2 = pd.read_csv('../MQPI/docs/Video_Games_Sales_as_at_22_Dec_2016.csv', encoding="utf-8")
y = df2['Rating_Everyone'].values
df2 = df2.drop(['Rating_Everyone'], axis=1)
df2 = df2.drop(['Rating_Everyone10'], axis=1)
df2 = df2.drop(['Rating_Teen'], axis=1)
df2 = df2.drop(['Rating_Mature'], axis=1)
X = df2.values
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.20)

log_reg = LogisticRegression(penalty='l1', dual=False, C=1.0, fit_intercept=False, intercept_scaling=1,
                             class_weight=None, random_state=None, solver='liblinear', max_iter=100,
                             multi_class='ovr',
                             verbose=0, warm_start=False, n_jobs=-1)
log_reg.fit(Xtrain, ytrain)
y_val_l = log_reg.predict(Xtest)
ris = accuracy_score(ytest, y_val_l)

print("Logistic Regression Rating_Everyone accuracy: ", ris)

再说一遍:

y = df2['Rating_Everyone10'].values
df2 = df2.drop(['Rating_Everyone'], axis=1)
df2 = df2.drop(['Rating_Everyone10'], axis=1)
df2 = df2.drop(['Rating_Teen'], axis=1)
df2 = df2.drop(['Rating_Mature'], axis=1)
X = df2.values
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.20)

log_reg = LogisticRegression(penalty='l1', dual=False, C=1.0, fit_intercept=False, intercept_scaling=1,
                             class_weight=None, random_state=None, solver='liblinear', max_iter=100,
                             multi_class='ovr',
                             verbose=0, warm_start=False, n_jobs=-1)
log_reg.fit(Xtrain, ytrain)
y_val_l = log_reg.predict(Xtest)
ris = accuracy_score(ytest, y_val_l)

print("Logistic Regression Rating_Everyone accuracy: ", ris)

Rating_Teen 和Rating_Mature 依此类推。 您能告诉我如何将所有这四个结果合并为一个结果,或者如何更好地解决这个多类逻辑回归问题?

最佳答案

LogisticRegression模型本质上是处理多类问题:

Below is a summary of the classifiers supported by scikit-learn grouped by strategy; you don’t need the meta-estimators in this class if you’re using one of these, unless you want custom multiclass

behavior: Inherently multiclass: Naive Bayes, LDA and QDA, Decision Trees, Random Forests, Nearest Neighbors, setting multi_class='multinomial' in sklearn.linear_model.LogisticRegression.

作为基本模型,没有类别权重(您可能需要这样做,因为样本可能不会在评级上平衡)设置 multi_class='multinomial' 并将求解器更改为 ' lbfgs' 或一个 支持多类问题的其他求解器:

For multiclass problems, only ‘newton-cg’, ‘sag’ and ‘lbfgs’ handle multinomial loss; ‘liblinear’ is limited to one-versus-rest schemes

因此,您不必按照现有方式拆分数据集。相反,提供原始评级列作为标签。

这是一个最小的例子:

X = np.random.randn(10, 10)
y = np.random.randint(1, 4, size=10) # 3 classes simulating ratings
lg = LogisticRegression(multi_class='multinomial', solver='lbfgs')
lg.fit(X, y)
lg.predict(X)

编辑:回复评论。

td;lr:我希望模型能够自行学习这种交互。如果不是,您可以将该信息编码为功能。因此没有明显需要对类进行二值化。

我的理解是,你有电影的功能,并且你有电影的 MPAA 评级作为标签(你试图预测)。这是一个多类问题,您可以使用逻辑回归开始建模(您知道这一点)。这就是我在上面提出的模型。

现在您认识到类之间存在隐含的距离。我使用这些信息的方式是作为模型的一个特征。然而,我首先倾向于看到模型会自己学习这一点。

关于python - 如何将二元类 Logistic 回归与 Python 合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43312907/

相关文章:

python - pandas如何计算索引?

python - Scipy 稀疏 Cumsum

python - 使用 tf.optimizers.Adam.minimize() 时,对象不可调用

machine-learning - 自动编码器无法捕获小伪影

python - 导出 Scikit Learn 随机森林以在 Hadoop 平台上使用

python - 在 GEKKO 中设置动态模拟的初始值和稳态结果

python - 如何在SqlAlchemy中使用 "CONVERT"函数?

python - python 中的机器学习 scikit learn 问题

python - 如何在 sklearn 中缩放单个样本进行预测?

python - 如何从 numpy 矩阵传递到 numpy 数组?