仅逻辑回归预测1

时间:2018-12-30 18:19:49

标签: python machine-learning scikit-learn classification logistic-regression

我是10年级学生,从事一项科学竞赛项目,该项目涉及根据患者数据预测依从性。我将一周分为21个时隙,一天中的每个时间三个(1个是星期一早晨,2个是星期一下午,依此类推)分成day_time变量。还有一个“ day”变量(1-7)和一个“ time”变量(1-3(早晨,船尾,夜晚)。坚持值是二进制的(0表示他们没有服用药物,1表示他们服用了药物) 。我创建了一个具有30周数据的csv,并为每个时隙分配了1个遵从性,除了3个选择时隙,其中包括“下午”时隙(1-3个中的2个),“星期四”时间(1-7中有4个)和周日晚上时段(1-21中有21)的时间段。这些时间段全为0,除了1或2个例外。但是,当我将Logistic回归模型拟合到数据时,该模型预测每个依从性值为1,导致准确性很差。我正在使用Scikit-learn,并使用了class_weight ='balanced'参数,但这使准确性降低了。是的,该模型开始预测的不只是1,准确性差得多(此处低于0.5)。

我只是为了好玩,我模拟了前10个时隙为1,其余11个为0的数据,并且这种模式在整个30周内都重复进行。逻辑回归在这里具有100%的准确性。这使我相信该模型对我的第一个数据集的处理效果很差,因为0小于1。但是后来,我模拟了一周中具有大致相同数量的0和1的数据,但是这次却不是连续的。重复此精确模式30次,然后该模型再次具有约0.5的精度。我不知道是什么原因导致如此糟糕的准确性。班级权重或门槛有问题吗?我不应该使用逻辑回归吗? (希望我可以,因为有关我的模型的权利应在本星期五到期)

我正在为模型使用scikit软件包。

这里是指向我作为csv下载的Google表格的链接,用于我的数据:[https://docs.google.com/spreadsheets/d/1AYJQUt8LcmI3cEKwuOZB2m8EGLAKu5djYQ_Q05Y_PLo/edit?usp=sharing][1]

这是我的代码:

import pandas as pd
%pylab inline
df = pd.read_csv("/Users/neelashabhattacharjee/scfair/patientdata.csv")
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
x = scaler.fit_transform(x)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)

def base_rate_model(x):
     y = np.ones(x.shape[0])
     return y
y_base_rate = base_rate_model(x_test)
from sklearn.metrics import accuracy_score
print("Base rate accuracy is %2.2f" % accuracy_score(y_test, y_base_rate))

from sklearn.linear_model import LogisticRegression
model = LogisticRegression(penalty = 'l2', C = 1, class_weight = None)
model.fit(x_train, y_train)
print("Logistic accuracy is %2.2f" % accuracy_score(y_test, model.predict(x_test)))

from sklearn.metrics import roc_auc_score
from sklearn.metrics import classification_report
print("Base Model:")
base_roc_auc = roc_auc_score(y_test, base_rate_model(x_test))
print("Base Rate AUC = %2.2f" % base_roc_auc)

logit_roc_auc = roc_auc_score(y_test, model.predict(x_test))
print("Logistic AUC = %2.2f" % logit_roc_auc)

# Just some code to help me look more at the predictions of the model and compare it to the actual data

# Display because I am using jupyter notebook

predictions = model.predict(x_test)

display(predictions)

display(y_test)

# How many ones being predicted by model vs real # of ones

ones = 0
for num in y_test:
    if num == 1:
        ones = ones + 1

print("Actual # of ones: {}".format(ones))

predones = 0
for num in predictions:
    if num == 1:
        predones = predones + 1
print("Predicted # of ones: {}".format(predones))

0 个答案:

没有答案