使用logisticRegression模型跑出來得結果會不相同
2019/09/19 下午 07:04
機器學習共學討論版
herohsu
觀看數:60
回答數:2
收藏數:0
ml100-3
ml100-3-d12
# 空值補 -1, 做羅吉斯迴歸
df_m1 = df.fillna(-1)
train_X = df_m1[:train_num]
estimator = LogisticRegression(solver='lbfgs', multi_class='auto')
cross1 = cross_val_score(estimator, train_X, train_Y, cv=5).mean()
print( 'The accuracy fills with -1 when na found: ', cross1 )
df_m2 = df.fillna(0)
train_X2 = df_m2[:train_num]
#estimator = LogisticRegression(solver='lbfgs', multi_class='auto')
cross2 = cross_val_score(estimator, train_X2, train_Y, cv=5).mean()
print( 'The accuracy fills with 0 when na found: ', cross2 )
df_m3 = df.fillna(df.mean())
train_X3 = df_m3[:train_num]
#estimator = LogisticRegression(solver='lbfgs', multi_class='auto')
cross3 = cross_val_score(estimator, train_X3, train_Y, cv=5).mean()
print( 'The accuracy fills with mean when na found: ', cross3 )
'''
The accuracy fills with -1 when na found: 0.6982644788418415
The accuracy fills with 0 when na found: 0.6993817972775958
The accuracy fills with mean when na found: 0.6959413955734954
效果最好當fillna = 0
'''
我發現我的答案和解答不同,我fillna(0)時得到最好結果
每次進行logisticRegression我會存入不同df, 然後再依序去算, 可是我看不出來哪裡有問題
請幫忙看看??
回答列表
-
2019/09/19 下午 11:26Wei-po Tsai贊同數:0不贊同數:0留言數:1
Hi!同學你好:
============================================
我是偽菠菜(Wei-Po Tsai),是個剛學Python的新手,
對你提到的問題很有興趣,所以研究了一下,請多指教。
============================================
你的寫法沒有問題喔!
會與解答的結果不同,是因爲你使用了solver = 'lbfgs' 的演算法。
如果拿掉solver = 'lbfgs' 的話,結果就會與解答相同囉!
請再試試看吧!
Edited By:偽菠菜(Wei-Po Tsai)
-
2019/09/20 下午 00:10陳明佑 (Ming You Chen)贊同數:1不贊同數:0留言數:1
謝謝偽波菜同學的回答, 這邊做點補充:
之前的同學就有提過一些類似的問題,
經過確認的結果是 :
不僅是"參數"或者"隨機種子"的不同, 可能造成不同的結果
連"sklearn版本"如果不同, 結果也可能會很不相同
尤其是在一些初始條件影響很大的模型 (如梯度提升機)