如何提高 Scikit python 中逻辑回归模型的准确性?

How to increase the model accuracy of logistic regression in Scikit python?

我正在尝试使用 gre、gpa 和 ranks.But 等预测变量来预测 admit 变量,但预测精度非常低(0.66)。下面给出了数据集。 https://gist.github.com/abyalias/3de80ab7fb93dcecc565cee21bd9501a

请查找以下代码:

 In[73]: data.head(20)
 Out[73]: 

   admit  gre   gpa  rank_2  rank_3  rank_4
0      0  380  3.61     0.0     1.0     0.0
1      1  660  3.67     0.0     1.0     0.0
2      1  800  4.00     0.0     0.0     0.0
3      1  640  3.19     0.0     0.0     1.0
4      0  520  2.93     0.0     0.0     1.0
5      1  760  3.00     1.0     0.0     0.0
6      1  560  2.98     0.0     0.0     0.0

y = data['admit']
x = data[data.columns[1:]]

from sklearn.cross_validation import  train_test_split
xtrain,xtest,ytrain,ytest  = train_test_split(x,y,random_state=2)

ytrain=np.ravel(ytrain)

#modelling 
clf = LogisticRegression(penalty='l2')
clf.fit(xtrain,ytrain)
ypred_train = clf.predict(xtrain)
ypred_test = clf.predict(xtest)

In[38]: #checking the classification accuracy
accuracy_score(ytrain,ypred_train)
Out[38]: 0.70333333333333337
In[39]: accuracy_score(ytest,ypred_test)
Out[39]: 0.66000000000000003

In[78]: #confusion metrix...
from sklearn.metrics import confusion_matrix
confusion_matrix(ytest,ypred)

Out[78]: 
array([[62,  1],
       [33,  4]])

错误的是predicting.How提高模型精度?

由于机器学习更多地是关于对特征和模型进行试验,因此您的问题没有正确答案。我给你的一些建议是:

1. Feature Scaling and/or Normalization - 检查 gregpa 特征的比例。它们相差 2 个数量级。因此,您的 gre 特征最终将在 class 中支配其他特征,例如逻辑回归。在将所有特征放入机器学习模型之前,您可以将它们归一化到相同的比例。This 是关于 scikit-learn 中可用的各种特征缩放和归一化 classes 的很​​好指南。

2。 Class 不平衡 - 查找数据中的 class 不平衡。由于您正在处理 admit/reject 数据,因此拒绝的数量将明显高于承认的数量。 SkLearn 中的大多数 classifier 包括 LogisticRegression 都有一个 class_weight 参数。在 class 不平衡的情况下,将其设置为 balanced 也可能会很好地工作。

3。优化其他分数 - 您还可以优化其他指标,例如 Log LossF1-Score。在 class 不平衡的情况下,F1-Score 可能会有用。 This 是一个很好的指南,更多地讨论了评分。

4。超参数调整 - 网格搜索 - 您可以通过执行网格搜索来调整模型的超参数来提高准确性。例如在 LogisticRegression 的情况下,参数 C 是一个超参数。此外,您应该避免在网格搜索期间使用测试数据。而是执行交叉验证。仅使用您的测试数据来报告最终模型的最终数字。请注意,应该对您尝试的所有模型执行 GridSearch,因为只有这样您才能判断每个模型的最佳效果是什么。 Scikit-Learn 提供的 GridSearchCV class for this. This 文章也是一个很好的起点。

5.探索更多 classifier - Logistic 回归学习将您的 classes 分开的线性决策面。您的 2 classes 可能不是线性可分的。在这种情况下,如果您有大量数据,您可能需要查看其他 classifier,例如 Support Vector Machines which are able to learn more complex decision boundaries. You can also start looking at Tree-Based classifiers such as Decision Trees which can learn rules from your data. Think of them as a series of If-Else rules which the algorithm automatically learns from the data. Often, it is difficult to get the right Bias-Variance Tradeoff with Decision Trees, so I would recommend you to look at Random Forests

6.错误分析 - 对于你的每个模型,回头看看它们失败的案例。您可能最终会发现您的某些模型在参数 space 的一部分上运行良好,而其他模型在其他部分上运行得更好。如果是这种情况,那么 Ensemble Techniques such as VotingClassifier 技术通常会提供最好的结果。赢得 Kaggle 比赛的模型很多时候都是集成模型。

7.更多功能 _ 如果所有这些都失败了,那么这意味着您应该开始寻找更多功能。

希望对您有所帮助!