【发布时间】:2020-04-17 15:11:22
【问题描述】:
我有 4 个连续变量 x_1 到 x_4,每个变量通过原始数据的最小-最大缩放分布在 [0, 1] 范围内。我正在使用 LogisticRegression() 将类的标签预测为“1”或“0”。
什么不工作?好吧,我的 LogisticRegression() 预测所有分类为“1”类型。
split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=0)
for train_indices, test_indices in split.split(numerical_data, y):
x_train = numerical_data[train_indices]
y_train = y[train_indices]
x_test = numerical_data[test_indices]
y_test = y[test_indices]
reg = LogisticRegression()
reg.fit(x_train, y_train)
y_pred = reg.predict(x_test)
print(classification_report_without_support(y_test, y_pred))
我有以下问题
- LogisticRegression 是否适合这项工作?因为它可以很好地处理一次性编码数据。
- 它是否处理连续数据?我想是的。
- 我为 LogisticRegression 设置的任何参数是否不正确?你能推荐一些更好或更整洁的东西吗?
- 最后,我是不是做错了什么?
输出
precision recall f1-score
0 0.00 0.00 0.00
1 0.90 1.00 0.95
accuracy 0.90
macro avg 0.45 0.50 0.47
weighted avg 0.80 0.90 0.85
UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, msg_start, len(result))
SMOTE + same settings for LogisticRegressionCV
precision recall f1-score
0 0.63 0.73 0.67
1 0.68 0.57 0.62
accuracy 0.65
macro avg 0.65 0.65 0.65
weighted avg 0.65 0.65 0.65
带有 LogisticRegression 的 SMOTE 代码。
os = SMOTE(random_state=0)
x_train, x_test, y_train, y_test = train_test_split(numerical_data, y, test_size=0.2, random_state=0)
os_data_x, os_data_y = os.fit_sample(x_train, y_train)
os_data_X = pd.DataFrame(data=os_data_x,columns=['x1', 'x2', 'x3', 'x4'] )
os_data_Y = pd.DataFrame(data=os_data_y,columns=['y'])
x_train, x_test, y_train, y_test = train_test_split(os_data_X, os_data_Y.values.ravel(), test_size=0.2, random_state=0)
reg.fit(x_train, y_train)
y_pred = reg.predict(x_test)
print(classification_report_without_support(y_test, y_pred))
Accuracy of classifier on test set: 0.71
precision recall f1-score
0 0.14 0.70 0.24
1 0.95 0.57 0.71
accuracy 0.58
macro avg 0.55 0.63 0.47
weighted avg 0.87 0.58 0.67
【问题讨论】:
标签: machine-learning scikit-learn classification logistic-regression