【问题标题】:Looping Logistic Regression over DataFrame in Python在 Python 中对 DataFrame 循环逻辑回归
【发布时间】:2020-12-18 11:12:50
【问题描述】:

我被困在这个循环的错误之处,以对具有 25 个特征的数据框执行逻辑回归。

当我重塑它给出错误: “ValueError:预期的 2D 数组,得到 1D 数组: 数组=[-12.36677125 -12.91946925 -12.89317629 -13.16951215 -12.20588875 -12.44694704 -12.71370778 -12.69351738 -12.89451587 -12.0776727 -12.63723271 -13.39461116 -12.52027792]。 如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果它包含单个样本,则使用 array.reshape(1, -1)。"

peptides = ['AYSLFSYNTQGR','IVLGQEQDSYGGK','EQLTPLIK','SPELQAEAK','SPELQAEAK','ALVQQMEQLR','SGVQQLIQYYQDQK','VVVHPDYR','GFVVAGPSR','CLCACPFK','VVEESELAR','FCDMPVFENSR','GYSIFSYATK',
'EPGCGCCSVCAR',
'LIQGAPTIR',
'YYLQGAK',
'ALGHLDLSGNR',
'DLLLPQPDLR',
'GPLQLER',
'IISIMDEK',
'LQDAEIAR',
'QINDYVEK',
'SVLGQLGITK',
'ADLSGITGAR',
'EQLSLLDR']

这是我想要交互的肽列表。它们应该是 X_train 的列标题。

LR_scores = []
logit_roc_auc =[]
y_pred = []
acc_score = []

for peptide in peptides:
    model=LogisticRegression()
    model.fit(X_train[peptide], y_train)
    score = model.score(X_test[peptide], y_test)
    y_pred=model.predict(X_test[peptide])
    acc_score = accuracy_score(y_test, y_pred)
    LR_scores.append(peptide,acc_score)
    
    #Classification Report
    print (classification_report(y_test,y_pred))
    
    #Confusion Matrix
    cnf_matrix = confusion_matrix(y_test,y_pred)
    print(cnf_matrix)
    
    #ROC_AUC Curves
    y_predict_proba = model.predict_proba(X_test[peptide])
    probabilities = np.array(y_predict_proba)[:, 1]
    fpr, tpr, thresholds = roc_curve(y_test, probabilities, pos_label=1)
    roc_auc = auc(fpr, tpr)
    logit_roc_auc = roc_auc_score(y_test, model.predict(X_test[peptide]))

感谢任何帮助。

Screenshot of Jupyter Notebook

This loop works with different input lists

【问题讨论】:

    标签: pandas numpy loops machine-learning logistic-regression


    【解决方案1】:

    在拟合模型时,X 应该是一个二维数组,而 y 是一个一维数组。

    X_train[peptide] 返回一个一维数组的序列。你可以 -

    X_train[peptide].shape
    #Output  = (nrows,)
    

    你可以这样做 -

    X_train[[peptide]].shape
    #Output = (nrows,1)
    

    X_train[peptide].to_numpy().reshape(-1,1)
    #Output = (nrows,1)
    

    这应该可行 -

    如果出现另一个错误,则代码存在多个问题。请将该错误也发布在 cmets 中。

    for peptide in peptides:
        model=LogisticRegression()
        model.fit(X_train[[peptide]], y_train)
        score = model.score(X_test[[peptide]], y_test)
        y_pred=model.predict(X_test[[peptide]])
        acc_score = accuracy_score(y_test, y_pred)
        LR_scores.append(peptide,acc_score)
        
        #Classification Report
        print (classification_report(y_test,y_pred))
        
        #Confusion Matrix
        cnf_matrix = confusion_matrix(y_test,y_pred)
        print(cnf_matrix)
        
        #ROC_AUC Curves
        y_predict_proba = model.predict_proba(X_test[[peptide]])
        probabilities = np.array(y_predict_proba)[:, 1]
        fpr, tpr, thresholds = roc_curve(y_test, probabilities, pos_label=1)
        roc_auc = auc(fpr, tpr)
        logit_roc_auc = roc_auc_score(y_test, model.predict(X_test[[peptide]]))
    

    【讨论】:

    • 我试过了,但我最终得到了这个错误:: Found input variables with contrast numbers of samples: [26, 13]
    • 这行得通吗?您正在使用使用 2D 训练数据和 1D y 数据的多个函数。请确保您在所有这些更改中都进行了这些更改
    • 我已经尝试了所有这些都没有成功。最近,是产生此错误的中间错误:预期 2D 数组,得到 1D 数组:array=[13. 1.]。如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果它包含单个样本,则使用 array.reshape(1, -1)。
    • 我只将这些更改应用于 X_train 或 X_test 值
    • 我正在使用这个循环,但输入列表是由“combos = list(combinations(X_train.columns,4))”组成的肽组合。然后我使用“组合中的组合”,代码工作正常。我不确定发生了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 2020-03-22
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多