【发布时间】:2020-12-22 03:50:57
【问题描述】:
我尝试对糖尿病进行 Logistic 回归并获得模型的结果,我假设每个变量都有 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)
【问题讨论】:
标签: python scikit-learn logistic-regression coefficients