【问题标题】:Error in function return using python使用python返回函数时出错
【发布时间】:2018-02-23 07:55:01
【问题描述】:

def random_model_predictions(丢弃,特征): # 拟合模型

df_telecom_train, df_telecom_test = train_test_split(droping, test_size=0.25)

############## Random Forest Classifier ##############


# Set up our RandomForestClassifier instance and fit to data

clf = RandomForestClassifier(n_estimators=30)
clf = clf.fit(df_telecom_train[features], df_telecom_train["Churn"])
print(clf)

# Make predictions

prediction1 = clf.predict(df_telecom_test[features])
print(prediction1)

probs = clf.predict_proba(df_telecom_test[features])
print(probs)

# Accuracy

score1 = clf.score(df_telecom_test[features], df_telecom_test["Churn"])
print("Accuracy: ", score1)

# Confusion matrix

confusion_matrix1 = pd.DataFrame(
    confusion_matrix(df_telecom_test["Churn"], prediction1),
    columns=["Predicted False", "Predicted True"],
    index=["Actual False", "Actual True"]
)
print(confusion_matrix1)

return df_telecom_test

return df_telecom_train

return probs

return clf

deflogistic_model_prediction(df_telecom_train, features, df_telecom_test):

#############Logistic Regression############

# Import Library

from sklearn.linear_model import LogisticRegression

# Create logistic regression object

model2 = LogisticRegression()

# Train the model using the training sets and check score

model2.fit([features], df_telecom_train["Churn"])
score2 = model2.score(df_telecom_train[features], df_telecom_train["Churn"])
print("Accuracy: ", score2)

# Equation coefficient and Intercept

print('Coefficient: \n', model2.coef_)
print('Intercept: \n', model2.intercept_)

# Predict Output

prediction2 = model2.predict(df_telecom_test[features])
print(prediction2)

# Confusion matrix

confusion_matrix2 = pd.DataFrame(
    confusion_matrix(df_telecom_test["Churn"], prediction2),
    columns=["Predicted False", "Predicted True"],
    index=["Actual False", "Actual True"]
)
print(confusion_matrix2)

如果 name == 'ma​​in': df_telecom_test, df_telecom_train, probs, clf = random_model_predictions(丢弃,特征) logistic_model_prediction(df_telecom_train, features, df_telecom_test)

当我使用 2 个或更多返回类型时,它显示了这种类型的错误

ValueError: 必须只传递带有布尔值的 DataFrame

【问题讨论】:

  • 尝试:out2 = removing_outliers() in if __name__ == '__main__'

标签: python function return


【解决方案1】:

原因是out2if __name__ == "__main__": 中未定义,因为您没有保存来自removing_outliers() 的返回值。所以改成

if __name__ == "__main__":
     out2 = removing_outliers()
     removing_unwanted_columns(out2)

应该没问题

【讨论】:

  • 请帮我解决上述问题
  • 这是一个全新的问题。请ask a new question。同时提供错误的完整回溯。
猜你喜欢
  • 1970-01-01
  • 2019-04-08
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 2011-06-23
  • 2019-05-02
  • 1970-01-01
相关资源
最近更新 更多