需求:
假设现在需要对数据进行二分类,小于0.5的,打上0的标记,大于0.5的,打上1的标记,怎么做?
分析:
这是一个简单的二分类问题,使用逻辑回归模型。
代码:
# 导入所需的库,如需安装:pip install scikit-learn matplotlib
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix
# 创建一个随机的二分类数据集
np.random.seed(42)
X = np.random.rand(100, 1)
print("X:\n", X)
y = (X > 0.5).astype(int)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print("X_train:\n", X_train)
# 创建并训练逻辑回归模型
model = LogisticRegressi