簡體   English   中英

邏輯回歸模型系數

[英]Logistic regression model coefficient

我試圖對糖尿病進行邏輯回歸並得到模型的結果我假設每個變量都會有 1 個系數數,但結果給了我 3 個不同的系數數列表和 3 個不同的截距。 我嘗試了線性回歸,它為每個人提供 1

import pandas as pd
import sklearn
from sklearn.linear_model import LogisticRegression
import numpy as np
from sklearn import linear_model, preprocessing
data = pd.read_csv ('diabetestype.csv' , sep = ',')

le = preprocessing.LabelEncoder()
Age = list(data['Age']) #will take all buying to a list and transform into proper integer values 
BSf = list(data['BS Fast'])
BSp = list(data['BS pp'])
PR = list(data['Plasma R'])
PF = list(data['Plasma F'])
Hb = list(data['HbA1c'])
Type = le.fit_transform(list(data['Type']))

X = list(zip(Age, BSf,BSp,PR,PF,Hb))
y = list(Type)


x_train,x_test, y_train,y_test = sklearn.model_selection.train_test_split(X, y, test_size = 0.1)
# model = linear_model.LinearRegression()
model = LogisticRegression()
model.fit (x_train,y_train)
acc = model.score(x_test,y_test)
coef = model.coef_
inter = model.intercept_
prediction = model.predict(x_test)
for i in range (5):
    print ('predicted ', prediction[i],'variables  ', x_test[i] , 'actual', y_test[i])
print(acc)
print(coef, inter)

結果是————

predicted  1 variables (2, 9, 14, 6, 6, 10) actual 1
predicted  2 variables (33, 7, 0, 9, 8, 8) actual 2
predicted  0 variables (19, 4, 4, 3, 2, 0) actual 0
predicted  0 variables (7, 15, 9, 5, 5, 3) actual 0
predicted  0 variables (16, 4, 4, 3, 2, 0) actual 0
1.0
[[-0.02543341  0.3763792  -0.2116062  -1.36365511 -0.87416662 -1.8448327 ]
 [ 0.00940748 -1.12894486  1.50994009  1.1101098   1.23563738 -0.2574385 ]
 [ 0.01602593  0.75256566 -1.29833389  0.25354531 -0.36147076  2.1022712 ]] [ 28.79209663 -19.24933782  -9.54275881]
C:\Users\nk\anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py:764: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
  extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)

文檔

coef_: ndarray of shape (1, n_features) or (n_classes, n_features) (截取相同)

你有 3 個班級。

在這個最小的例子中,也有 3 個類:

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
clf = LogisticRegression(random_state=0).fit(X, y)
clf.predict(X[:2, :])

clf.predict_proba(X[:2, :])


clf.score(X, y)

set(y)  # >>>{0, 1, 2} --> there are 3 classes



clf.coef_ # >>> array([[-0.41874027,  0.96699274, -2.52102832, -1.08416599],
          #            [ 0.53123044, -0.31473365, -0.20002395, -0.94866082],
          #            [-0.11249017, -0.65225909,  2.72105226,  2.03282681]])

clf.coef_.shape # >>> (3, 4)

clf.intercept_ # >>> array([  9.84028024,   2.21683511, -12.05711535])

您需要能夠辨別樣本是否屬於哪個類別。 無論您測試哪個類,結果都將介於 0 或 1 之間。
例如,使用coef_的第一行,您檢查它是否屬於第 1 類...等。

暫無
暫無

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

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