代码如下:

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

np.random.seed(5)  # 随机种子

a, b = 0, 2  # 类别区间

# 生成10个样本数据和对应的标签
X = np.random.randint(a, b, 20).reshape((10, 2))
y = np.random.randint(a, b, 5).reshape((5, 1))
y = np.concatenate([y, y], axis=0)

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=5)

# 使用逻辑回归拟合数据
model = LogisticRegression()
model.fit(X_train, y_train)

# 计算精度
acc = model.score(X_test, y_test)

print("accu of model is {:.2f}".format(acc))

输出结果为:

accu of model is 0.60
使用nprandomrandint生成2维的样本数据10个。train_test_split用来分隔训练集和测试集训练数据集比例05 random_state = 5。 使用逻辑回归的方式进行训练拟合和测试。 使用sklearn的LogisticRegression都使用默认参数和train_test_split。精度使用score显示2位小数。 如下是数据集生成方式: 样本nprandomra

原文地址: https://www.cveoy.top/t/topic/bt2T 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录