【发布时间】:2018-03-23 21:56:51
【问题描述】:
我想估计用于医疗数据逻辑回归的 sigmoidal/逻辑的最佳参数(在最后提到:斜率和截距)。这是我对python所做的:
import numpy as np
from sklearn import preprocessing, svm, neighbors
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn import preprocessing, svm, utils
from scipy.io import loadmat
import pandas as pd
我有 Apache.mat 文件,其中包含 4 列:Apache 评分 (0-72)、患者人数、死亡人数、比例(死亡人数与患者人数的比率)
datamat = loadmat('Apache.mat')
data = pd.DataFrame(np.hstack((datamat['apacheII'], datamat['NoPatients'],
datamat['NoDeaths'], datamat['proportion'])))
data.columns = ['apacheII', 'NoPatients', 'NoDeaths', 'proportion']
在这里,我创建了要使用的数据框。
x = np.array(data.drop(['NoPatients', 'NoDeaths', 'proportion'],1))
我已经删除了不需要的列,现在只留下了 'x' 中的 ApacheII 分数
#scaling the data (normalizing)
x = preprocessing.scale(x)
y = np.array(data['proportion'])
现在,我使用 LabelEncoder() 函数对 'y' 进行编码,以便与 LogisticRegression() 兼容。
lab_enc = preprocessing.LabelEncoder()
encoded = np.array(lab_enc.fit_transform(y))
clf = LogisticRegression()
clf.fit(x, encoded)
print(clf.coef_)
print(clf.intercept_)
输出如下:
[[-0.49124107]
[-0.23528893]
[-0.19035795]
[-0.30312848]
[-0.25783808]
[-0.37161079]
[-0.12332468]
[-0.16797195]
[-0.05660718]
[-0.21279785]
[-0.22142453]
[-0.10105617]
[-0.14562868]
[ 0.00991192]
[-0.012247 ]
[ 0.03206243]
[ 0.07635461]
[ 0.20951544]
[ 0.12067417]
[-0.03441851]
[ 0.16504852]
[ 0.09850035]
[ 0.23179558]
[ 0.05420914]
[ 1.47513463]]
[-1.79691975 -2.35677113 -2.35090141 -2.3679202 -2.36017388 -2.38191049
-2.34441678 -2.34843121 -2.34070389 -2.35368047 -1.57944984 -2.3428732
-2.3462668 -2.33974088 -2.33975687 -2.34002906 -2.34151792 -2.35329447
-2.34422478 -2.34007746 -2.34814388 -2.34271603 -2.35632459 -2.34062229
-1.72511457]
我只是想找出逻辑回归中常用的 sigmoidal 函数的参数。如何找到 sigmoidal 参数(即截距和斜率)?
这里是 sigmoidal 函数(如果需要参考):
def sigmoid(x, x0, k):
y = 1 / (1 + np.exp(-k*(x-x0)))
return y
【问题讨论】:
-
如果
proportion是连续变量,我认为对于这个问题,你应该寻找岭回归而不是逻辑回归。 -
是的,你是正确的@GergesDib。谢谢。但在这里我只是想弄清楚逻辑函数的参数,即使它不是最好的回归模型。任何帮助表示赞赏。
-
我想你已经找到它们了,它们是
lr.coef_和lr.intercept_。有什么问题? -
我预测
lr.coef_和lr.intercept_应该有一个值,这就是我所需要的。但我得到了一堆价值观。你能帮忙吗? -
x.shape和y.shape是什么?
标签: python machine-learning scikit-learn logistic-regression