簡體   English   中英

如何獲得多項式邏輯回歸系數?

[英]How to get coefficients of multinomial logistic regression?

我需要使用sklearn計算多元邏輯回歸的系數:

X =

x1          x2          x3   x4         x5    x6
0.300000    0.100000    0.0  0.0000     0.5   0.0
0.000000    0.006000    0.0  0.0000     0.2   0.0
0.010000    0.678000    0.0  0.0000     2.0   0.0
0.000000    0.333000    1.0  12.3966    0.1   4.0
0.200000    0.005000    1.0  0.4050     1.0   0.0
0.000000    0.340000    1.0  15.7025    0.5   0.0
0.000000    0.440000    1.0  8.2645     0.0   4.0
0.500000    0.055000    1.0  18.1818    0.0   4.0

y的值在[1; 4]。

y =

1
2
1
3
4
1
2
3

這是我的工作:

import pandas as pd
from sklearn import linear_modelion
from sklearn.metrics import mean_squared_error, r2_score
import numpy as np

h = .02

logreg = linear_model.LogisticRegression(C=1e5)

logreg.fit(X, y)

# print the coefficients
print(logreg.intercept_)
print(logreg.coef_)

但是,我得到的輸出6列logreg.intercept_和6列的輸出logreg.coef_我怎樣才能得到每個功能1個系數,如a - f值?

y = a*x1 + b*x2 + c*x3 + d*x4 + e*x5 + f*x6

另外,可能我做錯了,因為y_pred = logreg.predict(X)給我所有行的值1

查看在線文檔

coef_ :數組,形狀(1, n_features)(n_classes, n_features)

決策函數中要素的系數。

當給定問題為二元時 (1, n_features)的形狀為(1, n_features)

正如@Xochipilli在評論中已經提到的那樣,您將擁有(n_classes, n_features)或在您的情況下(4,6)系數和4個截距(每個類一個)

可能我做錯了,因為y_pred = logreg.predict(X)給我所有行的值1

是的,您不應嘗試使用用於訓練模型進行預測的數據。 將您的數據分為訓練和測試數據集,使用訓練數據集訓練模型,並使用測試數據集檢查其准確性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM