【发布时间】: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 == 'main': 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()inif __name__ == '__main__'