2016-07-27 130 views
1

我一直在嘗試爲分類問題實施邏輯迴歸,但它給了我非常奇怪的結果。我已經獲得了體面的結果與梯度提升和隨機森林,所以我想到基本,看看我能達到什麼最好。你能幫我指出我做錯了什麼導致了這種過度配合? 你可以從 https://www.kaggle.com/c/santander-customer-satisfaction/dataLogistic迴歸Python

這裏的數據是我的代碼:

import pandas as pd 
import numpy as np 
train = pd.read_csv("path") 
test = pd.read_csv("path") 
test["TARGET"] = 0 
fullData = pd.concat([train,test], ignore_index = True) 

remove1 = [] 
for col in fullData.columns: 
if fullData[col].std() == 0: 
    remove1.append(col) 

fullData.drop(remove1, axis=1, inplace=True) 
import numpy as np 
remove = [] 
cols = fullData.columns 
for i in range(len(cols)-1): 
v = fullData[cols[i]].values 
    for j in range(i+1,len(cols)): 
    if np.array_equal(v,fullData[cols[j]].values): 
     remove.append(cols[j]) 

fullData.drop(remove, axis=1, inplace=True) 

from sklearn.cross_validation import train_test_split 
X_train, X_test = train_test_split(fullData, test_size=0.20, random_state=1729) 
print(X_train.shape, X_test.shape) 

y_train = X_train["TARGET"].values 
X = X_train.drop(["TARGET","ID"],axis=1,inplace = False) 

from sklearn.ensemble import ExtraTreesClassifier 
clf = ExtraTreesClassifier(random_state=1729) 
selector = clf.fit(X, y_train) 

from sklearn.feature_selection import SelectFromModel 
fs = SelectFromModel(selector, prefit=True) 
X_t = X_test.drop(["TARGET","ID"],axis=1,inplace = False) 
X_t = fs.transform(X_t) 
X_tr = X_train.drop(["TARGET","ID"],axis=1,inplace = False) 
X_tr = fs.transform(X_tr) 


from sklearn.linear_model import LogisticRegression 
log = LogisticRegression(penalty ='l2', C = 1, random_state = 1, 
        ) 
from sklearn import cross_validation 
scores = cross_validation.cross_val_score(log,X_tr,y_train,cv = 10) 

print(scores.mean()) 
log.fit(X_tr,y_train) 
predictions = log.predict(X_t) 
predictions = predictions.astype(int) 
print(predictions.mean()) 

回答

0

您沒有配置的參數C - 很好,在技術上你,但只爲默認值 - 這是一個通常嫌疑人過度配合。你可以看看GridSearchCV,並用C參數的幾個值(例如從10^-5到10^5)來查看它是否可以緩解你的問題。將懲罰規則更改爲'l1'也可能有所幫助。

此外,這場競賽還有幾個挑戰:它是一個不平衡的數據集,訓練集和私人LB之間的分佈有些不同。所有這些如果要與你對抗,特別是使用簡單的算法,如LR。

+0

感謝您的回覆。是的,在這裏使用LR很困難,過度配合的分數徘徊在0.98左右,無論我選擇C還是罰款。我使用logisticRegressionCV並使用Cs的列表而不是單個值,但沒有影響。我應該採用其他數據集來展示邏輯迴歸的力量。 –