generated from LufsX/Experiment-LogReg
96 lines
2.4 KiB
Python
96 lines
2.4 KiB
Python
# function.py
|
|
import numpy as np
|
|
import pandas as pd
|
|
|
|
"""
|
|
请在这里填写学号和姓名,不填写即成绩无效。
|
|
"""
|
|
id = "202210423107"
|
|
name = "刘燚燊"
|
|
|
|
|
|
def load_data(filepath):
|
|
"""
|
|
加载数据集并进行预处理。
|
|
参数:
|
|
filepath: 数据文件路径
|
|
返回:
|
|
特征和目标变量的数组
|
|
"""
|
|
# 读取数据
|
|
data = pd.read_csv(filepath, header=None)
|
|
|
|
# 替换缺失值
|
|
data.replace('?', np.nan, inplace=True)
|
|
data.fillna(0, inplace=True)
|
|
|
|
# 处理目标变量
|
|
target_col = data.shape[1] - 1 # 目标变量为最后一列
|
|
data[target_col] = data[target_col].replace({2: 0, 3: 0})
|
|
|
|
# 去掉不需要的特征
|
|
data.drop(columns=[2, 24, 25, 26, 27], inplace=True, errors='ignore')
|
|
|
|
# 转换数据为数值类型
|
|
data = data.apply(pd.to_numeric, errors='coerce')
|
|
|
|
# 分离特征与目标变量
|
|
X = data.drop(columns=[target_col]).values # 特征
|
|
y = data[target_col].values # 目标变量
|
|
|
|
return X, y
|
|
|
|
|
|
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
|
"""
|
|
训练逻辑回归模型。
|
|
参数:
|
|
X: 特征数据
|
|
y: 目标变量
|
|
learning_rate: 学习率
|
|
iterations: 训练迭代次数
|
|
返回:
|
|
权重向量
|
|
"""
|
|
m, n = X.shape
|
|
weights = np.zeros(n) # 初始化权重
|
|
for i in range(iterations):
|
|
# 计算预测值
|
|
linear_model = np.dot(X, weights)
|
|
y_predicted = 1 / (1 + np.exp(-linear_model))
|
|
|
|
# 更新权重
|
|
gradient = np.dot(X.T, (y_predicted - y)) / m
|
|
weights -= learning_rate * gradient
|
|
|
|
return weights
|
|
|
|
|
|
def predict_logreg(inX, weights):
|
|
"""
|
|
使用训练好的模型进行预测。
|
|
参数:
|
|
inX: 单个输入数据
|
|
weights: 权重向量
|
|
返回:
|
|
预测结果
|
|
"""
|
|
linear_model = np.dot(inX, weights)
|
|
y_predicted = 1 / (1 + np.exp(-linear_model))
|
|
return 1 if y_predicted >= 0.5 else 0 # 返回二分类结果
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# 加载训练集
|
|
X_train, y_train = load_data("./data/horse-colic.data")
|
|
|
|
# 加载测试集
|
|
X_test, y_test = load_data("./data/horse-colic.test")
|
|
|
|
# 训练对率回归模型
|
|
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
|
|
|
# 测试单个数据
|
|
y_pred = predict_logreg(X_test[11], weights)
|
|
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|