![](/img/trans.png)
[英]How to predict single instance with Logistic Regression using sci-kit learn?
[英]Prevent overfitting in Logistic Regression using Sci-Kit Learn
我使用邏輯回歸訓練了一個模型來預測姓名字段和描述字段是否屬於男性、女性或品牌的個人資料。 我的訓練准確度約為 99%,而我的測試准確度約為 83%。 我嘗試通過調整 C 參數來實現正則化,但幾乎沒有注意到改進。 我的訓練集中有大約 5,000 個示例。 這是我只需要更多數據的情況,還是我可以在 Sci-Kit Learn 中做其他事情來提高我的測試准確度?
過擬合是一個多方面的問題。 它可能是你的訓練/測試/驗證拆分(從 50/40/10 到 90/9/1 的任何事情都可能改變事情)。 您可能需要調整輸入。 嘗試集成方法,或減少特征數量。 你可能有異常值把東西扔掉
再說一次,它可能不是這些,或所有這些,或這些的某種組合。
對於初學者,嘗試將測試分數繪制為測試拆分大小的函數,然后看看你會得到什么
#The 'C' value in Logistic Regresion works very similar as the Support
#Vector Machine (SVM) algorithm, when I use SVM I like to use #Gridsearch
#to find the best posible fit values for 'C' and 'gamma',
#maybe this can give you some light:
# For SVC You can remove the gamma and kernel keys
# param_grid = {'C': [0.1,1, 10, 100, 1000],
# 'gamma': [1,0.1,0.01,0.001,0.0001],
# 'kernel': ['rbf']}
param_grid = {'C': [0.1,1, 10, 100, 1000]}
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report,confusion_matrix
# Train and fit your model to see initial values
X_train, X_test, y_train, y_test = train_test_split(df_feat, np.ravel(df_target), test_size=0.30, random_state=101)
model = SVC()
model.fit(X_train,y_train)
predictions = model.predict(X_test)
print(confusion_matrix(y_test,predictions))
print(classification_report(y_test,predictions))
# Find the best 'C' value
grid = GridSearchCV(SVC(),param_grid,refit=True,verbose=3)
grid.best_params_
c_val = grid.best_estimator_.C
#Then you can re-run predictions on this grid object just like you would with a normal model.
grid_predictions = grid.predict(X_test)
# use the best 'C' value found by GridSearch and reload your LogisticRegression module
logmodel = LogisticRegression(C=c_val)
logmodel.fit(X_train,y_train)
print(confusion_matrix(y_test,grid_predictions))
print(classification_report(y_test,grid_predictions))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.