亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

簡單示例中的錯誤邏輯回歸

簡單示例中的錯誤邏輯回歸

楊__羊羊 2021-07-07 13:04:01
我正在嘗試使用一個簡單的邏輯回歸示例 sklearn.linear_model.LogisticRegression這是代碼:import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as snsfrom sklearn.linear_model import LogisticRegressionfrom sklearn import metrics# some randomly generated data with two well differentiated groups x1 = np.random.normal(loc=15, scale=2, size=(30,1))y1 = np.random.normal(loc=10, scale=2, size=(30,1))x2 = np.random.normal(loc=25, scale=2, size=(30,1))y2 = np.random.normal(loc=20, scale=2, size=(30,1))data1 = np.concatenate([x1, y1, np.zeros(shape=(30,1))], axis=1)data2 = np.concatenate([x2, y2, np.ones(shape=(30,1))], axis=1)dfa = pd.DataFrame(data=data1, columns=["F1", "F2", "group"])dfb = pd.DataFrame(data=data2, columns=["F1", "F2", "group"])df = pd.concat([dfa, dfb], ignore_index=True)# the actual fittingfeatures = [item for item in df.columns if item not in ("group")]logreg = LogisticRegression(verbose=1)logreg.fit(df[features], df.group)# plotting and checking the resulttheta = logreg.coef_[0,:] # parametersy0 = logreg.intercept_    # interceptprint("Theta =", theta)print("Intercept = ", y0)xdb = np.arange(0, 30, 0.2)  # dummy x vector for decision boundaryydb = -(y0+theta[0]*xdb) / theta[1] # decision boundary y valuesfig = plt.figure()ax = fig.add_subplot(111)colors = {0 : "red", 1 : "blue"}for i, group in df.groupby("group"):    plt.plot(group["F1"], group["F2"],             MarkerFaceColor = colors[i], Marker = "o", LineStyle="",             MarkerEdgeColor=colors[i])plt.plot(xdb, ydb, LineStyle="--", Color="b")令人震驚的是,結果圖如下所示:事實上,準確度可以計算為:predictions = logreg.predict(df[features])metrics.accuracy_score(predictions, df["group"])結果是 0.966...我一定是做錯了什么,只是想不通是什么。任何幫助深表感謝!
查看完整描述

1 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

這是由于正則化。線的最佳值是截距值約為 -16,但由于正則化,它無法達到該水平。

Logistic 回歸最小化損失函數,即誤差和權重值的組合。在這種情況下,當我們增加 C 模型的值時,將更多地關注減少錯誤(從而找到更好的決策邊界)而不是權重。結果在適當的決策邊界。

盡管正則化在大多數現實世界場景中非常重要。在某些情況下,重要的是不要使用一個。

進行以下更改

logreg = LogisticRegression(verbose=1, C=100)

輸出如下

http://img1.sycdn.imooc.com//60f7e94f0001bd7605260397.jpg

閱讀有關正則化的更多信息以更好地理解這一點


查看完整回答
反對 回復 2021-07-21
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號