- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试预测可能出现在“退出”列中的两个值中的一个。我有干净的数据(大约 20 列和 4k 行包含有关客户的典型信息,例如“性别”、“年龄”...)。在训练数据集中,大约 20% 的客户被评为“1”。我制作了两个模型 - svm 和随机森林 - 但两者对测试数据集的预测大多为“0”(几乎每次)。两个模型的召回率为 0。我附加了我认为可能会犯一些愚蠢错误的代码。有什么想法为什么在 80% 的准确率下召回率如此之低吗?
def ml_model():
print('sklearn: %s' % sklearn.__version__)
df = pd.read_csv('clean_data.csv')
df.head()
feat = df.drop(columns=['target'], axis=1)
label = df["target"]
x_train, x_test, y_train, y_test = train_test_split(feat, label, test_size=0.3)
sc_x = StandardScaler()
x_train = sc_x.fit_transform(x_train)
# SVC method
support_vector_classifier = SVC(probability=True)
# Grid search
rand_list = {"C": stats.uniform(0.1, 10),
"gamma": stats.uniform(0.1, 1)}
auc = make_scorer(roc_auc_score)
rand_search_svc = RandomizedSearchCV(support_vector_classifier, param_distributions=rand_list, n_iter=100, n_jobs=4, cv=3, random_state=42,
scoring=auc)
rand_search_svc.fit(x_train, y_train)
support_vector_classifier = rand_search_svc.best_estimator_
cross_val_svc = cross_val_score(estimator=support_vector_classifier, X=x_train, y=y_train, cv=10, n_jobs=-1)
print("Cross Validation Accuracy for SVM: ", round(cross_val_svc.mean() * 100, 2), "%")
predicted_y = support_vector_classifier.predict(x_test)
tn, fp, fn, tp = confusion_matrix(y_test, predicted_y).ravel()
precision_score = tp / (tp + fp)
recall_score = tp / (tp + fn)
print("Recall score SVC: ", recall_score)
# Random forests
random_forest_classifier = RandomForestClassifier()
# Grid search
param_dist = {"max_depth": [3, None],
"max_features": sp_randint(1, 11),
"min_samples_split": sp_randint(2, 11),
"bootstrap": [True, False],
"criterion": ["gini", "entropy"]}
rand_search_rf = RandomizedSearchCV(random_forest_classifier, param_distributions=param_dist,
n_iter=100, cv=5, iid=False)
rand_search_rf.fit(x_train, y_train)
random_forest_classifier = rand_search_rf.best_estimator_
cross_val_rfc = cross_val_score(estimator=random_forest_classifier, X=x_train, y=y_train, cv=10, n_jobs=-1)
print("Cross Validation Accuracy for RF: ", round(cross_val_rfc.mean() * 100, 2), "%")
predicted_y = random_forest_classifier.predict(x_test)
tn, fp, fn, tp = confusion_matrix(y_test, predicted_y).ravel()
precision_score = tp / (tp + fp)
recall_score = tp / (tp + fn)
print("Recall score RF: ", recall_score)
new_data = pd.read_csv('new_data.csv')
new_data = cleaning_data_to_predict(new_data)
if round(cross_val_svc.mean() * 100, 2) > round(cross_val_rfc.mean() * 100, 2):
predictions = support_vector_classifier.predict(new_data)
predictions_proba = support_vector_classifier.predict_proba(new_data)
else:
predictions = random_forest_classifier.predict(new_data)
predictions_proba = random_forest_classifier.predict_proba(new_data)
f = open("output.txt", "w+")
for i in range(len(predictions.tolist())):
print("id: ", i, "probability: ", predictions_proba.tolist()[i][1], "exit: ", predictions.tolist()[i], file=open("output.txt", "a"))
最佳答案
如果我没有错过的话,你忘了扩展你的测试集。因此,您还需要对其进行扩展。请注意,您应该只是改造它,不要再次安装它。见下文。
x_test = sc_x.transform(x_test)
关于python - SVM 和随机森林,召回率 = 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58826840/
我们正在运行 MarkLogic 9.0-11 版本 3 节点集群,并且 MarkLogic 安装在“/var/opt/MarkLogic/”目录中,我们创建了“/var/opt/MarkLogic/
我有一片任意高度的森林,大致像这样: let data = [ { "id": 2, "name": "AAA", "parent_id": null, "short_name": "A" },
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 7 年前。 Improve
我有一个巨大的深度字典,代表森林(许多非二叉树),我想处理森林并创建一个包含森林所有可能关系的文本文件,例如给定字典: {'a': {'b': {'c': {}, 'd': {}}, 'g': {}}
在我的 Android 应用程序中,我包含了谷歌地图。现在我想获取有关您周围地区的信息。例如,你是在公园/森林/海滩……所以我基本上想要一个用“水”回答输入坐标 53°33'40.9"N 10°00'
如果我有下表: Member_Key Member_Name col1 Mem1 col2 Mem2 col3 Mem3 col4
继续我的老问题: Writing nested dictionary (forest) of a huge depth to a text file 现在我想把森林遍历写成BFS风格:我有一个巨大的深
我有一个多域环境(事件目录林),例如subdomain1.mydomain.com, subdomain2.mydomain.com 其中 mydomain.com 是根 AD 域 (GC) 和 su
我想知道是否有可能在 Google map 或 Bing Mag 2D/3D map 上恢复地形类型(山脉、森林、水域、平原等...) 。为了根据玩家在现实世界中的位置生成 map !我认为可用 AP
我是一名优秀的程序员,十分优秀!