Python实现逻辑回归(Logistic Regression)
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 读取数据
dataset = pd.read_csv('50_Startups.csv')
X = dataset.iloc[:, :-1].values # 特征
Y = dataset.iloc[:, 4].values # 目标变量,此处假设为第5列
# 使用sklearn的LabelBinarizer将标签二值化
label_binarizer = LabelBinarizer()
Y = label_binarizer.fit_transform(Y)
# 划分训练集和测试集
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
# 创建Logistic Regression模型
classifier = LogisticRegression()
classifier.fit(X_train, Y_train)
# 预测测试集结果
Y_pred = classifier.predict(X_test)
# 评估模型性能
accuracy = accuracy_score(Y_test, Y_pred)
print(f'Model Accuracy: {accuracy}')
这段代码使用了sklearn
库中的LogisticRegression
类来实现逻辑回归,并通过train_test_split
函数进行训练集和测试集的划分,最后使用accuracy_score
评估了模型的性能。这是实现逻辑回归的一个简单例子,适合入门学习。
评论已关闭