在 Scikit learn 中,当执行 X,Y = make_moons(500,noise = 0.2)
并在打印 X
和 Y
之后,我看到它们就像具有一堆条目但没有逗号的数组?我有想要使用的数据而不是 Scikit learn 卫星 dataset,但我不明白这些 Scikit learn 数据集是什么数据类型以及如何使我的数据遵循这种数据类型。
回答1
第一个 X 是一个二维数组:
array([[-6.72300890e-01, 7.40277997e-01],
[ 9.60230259e-02, 9.95379113e-01],
[ 3.20515776e-02, 9.99486216e-01],
[ 8.71318704e-01, 4.90717552e-01],
....
[ 1.61911895e-01, -4.55349012e-02]])
其中包含点的 x 轴和 y 轴位置。
元组的第二部分:y,是一个包含标签的数组(0 或 1 用于二进制分类)。
array([0, 0, 0, 0, 1, ... ])
要在简单的分类任务中使用此数据,您可以执行以下操作:
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
# Create dataset
X, y = make_moons(500,noise = 0.2)
# Split dataset in a train part and a test part
train_X, test_X, train_y, test_y = train_test_split(X, y)
# Create the Logistic Regression classifier
log_reg = LogisticRegression()
# Fit the logistic regression classifier
log_reg.fit(train_X, train_y)
# Use the trained model to predit con the train and predict samples
train_y_pred = log_reg.predict(train_X)
test_y_pred = log_reg.predict(test_X)
# Print classification report on the training data
print(classification_report(train_y, train_y_pred))
# Print classification report on the test data
print(classification_report(test_y, test_y_pred))
结果是:
关于训练数据
precision recall f1-score support
0 0.88 0.87 0.88 193
1 0.86 0.88 0.87 182
accuracy 0.87 375
macro avg 0.87 0.87 0.87 375
weighted avg 0.87 0.87 0.87 375
关于测试数据
precision recall f1-score support
0 0.81 0.89 0.85 57
1 0.90 0.82 0.86 68
accuracy 0.86 125
macro avg 0.86 0.86 0.86 125
weighted avg 0.86 0.86 0.86 125
正如我们所见,训练集和测试集的 f1_score 差别不大,模型没有过拟合。