generated from LufsX/Experiment-LogReg
This commit is contained in:
+75
-76
@@ -1,5 +1,8 @@
|
|||||||
# function.py
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from sklearn.impute import SimpleImputer
|
||||||
|
from sklearn.preprocessing import StandardScaler
|
||||||
|
import csv
|
||||||
|
|
||||||
"""
|
"""
|
||||||
实验说明:
|
实验说明:
|
||||||
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
||||||
@@ -18,92 +21,88 @@ import numpy as np
|
|||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
202210423219李燕如
|
请在这里填写学号和姓名,不填写即成绩无效。
|
||||||
"""
|
"""
|
||||||
id = "202210423219"
|
id = "202210423219"
|
||||||
name = "李燕如"
|
name = "李燕如"
|
||||||
|
|
||||||
|
|
||||||
# 这里写自己的代码实现
|
# 这里写自己的代码实现
|
||||||
def load_data(filepath):
|
def sigmoid(x):
|
||||||
"""
|
return 1 / (1 + np.exp(-x))
|
||||||
从文件中加载数据。
|
|
||||||
|
|
||||||
Args:
|
|
||||||
- filepath (str): 数据文件路径。
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- np.array: 特征数据。
|
|
||||||
- np.array: 标签数据。
|
|
||||||
"""
|
|
||||||
data = np.genfromtxt(filepath, delimiter=',', skip_header=1, filling_values=np.nan)
|
|
||||||
|
|
||||||
# 分离特征和标签
|
|
||||||
X = data[:, :-1]
|
|
||||||
y = data[:, -1].flatten() # 确保y是一维数组
|
|
||||||
|
|
||||||
# 删除包含NaN的行
|
|
||||||
mask = ~np.isnan(X).any(axis=1) & ~np.isnan(y)
|
|
||||||
X = X[mask]
|
|
||||||
y = y[mask]
|
|
||||||
|
|
||||||
return X, y
|
|
||||||
|
|
||||||
def sigmoid(z):
|
|
||||||
"""
|
|
||||||
Sigmoid函数。
|
|
||||||
|
|
||||||
Args:
|
|
||||||
- z (numpy array): 输入值。
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- numpy array: Sigmoid函数的输出值。
|
|
||||||
"""
|
|
||||||
return 1 / (1 + np.exp(-z))
|
|
||||||
|
|
||||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
|
||||||
"""
|
|
||||||
对率回归训练函数。
|
|
||||||
|
|
||||||
Args:
|
|
||||||
- X (numpy array): 特征数据。
|
|
||||||
- y (numpy array): 标签数据。
|
|
||||||
- learning_rate (float): 学习率,默认为0.01。
|
|
||||||
- iterations (int): 迭代次数,默认为1000。
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- numpy array: 训练得到的权重。
|
|
||||||
"""
|
|
||||||
# 添加截距项
|
|
||||||
X = np.c_[np.ones(X.shape[0]), X]
|
|
||||||
# 初始化权重
|
|
||||||
weights = np.zeros(X.shape[1])
|
|
||||||
|
|
||||||
# 检查y是否为空
|
|
||||||
if len(y) == 0:
|
|
||||||
raise ValueError("标签数组y为空")
|
|
||||||
|
|
||||||
|
def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.01):
|
||||||
|
m, n = X.shape
|
||||||
|
weights = np.zeros(n)
|
||||||
for _ in range(iterations):
|
for _ in range(iterations):
|
||||||
# 计算预测值
|
linear_model = np.dot(X, weights)
|
||||||
predictions = sigmoid(np.dot(X, weights))
|
y_predicted = sigmoid(linear_model)
|
||||||
# 计算梯度
|
gradient = np.dot(X.T, (y_predicted - y)) / m + regularization * weights
|
||||||
gradient = np.dot(X.T, (predictions - y)) / max(len(y), 1) # 防止除以零
|
|
||||||
# 更新权重
|
|
||||||
weights -= learning_rate * gradient
|
weights -= learning_rate * gradient
|
||||||
|
|
||||||
return weights
|
return weights
|
||||||
|
|
||||||
def predict_logreg(inX, weights):
|
def predict_logreg(inX, weights):
|
||||||
"""
|
linear_output = np.dot(inX, weights)
|
||||||
对率回归预测函数。
|
probability = sigmoid(linear_output)
|
||||||
|
return 1 if probability > 0.5 else 0
|
||||||
|
|
||||||
Args:
|
def preprocess_data(X, y):
|
||||||
- inX (numpy array): 输入待分类的样本。
|
# 合并目标变量的类别
|
||||||
- weights (numpy array): 训练得到的权重。
|
y[y == 2] = 0
|
||||||
|
y[y == 3] = 0
|
||||||
|
|
||||||
Returns:
|
# 删除特征3和特征24~28
|
||||||
- int: 预测的分类标签。
|
X = np.delete(X, [3], axis=1)
|
||||||
"""
|
X = X[:, :23]
|
||||||
inX = np.c_[1, inX] # 添加截距项
|
|
||||||
prob = sigmoid(np.dot(inX, weights))
|
return X, y
|
||||||
return 1 if prob > 0.5 else 0
|
|
||||||
|
def load_data(filepath):
|
||||||
|
# 使用csv模块读取数据
|
||||||
|
data = []
|
||||||
|
with open(filepath, 'r') as file:
|
||||||
|
reader = csv.reader(file, delimiter=' ')
|
||||||
|
for row in reader:
|
||||||
|
if row and row[0] != '?': # 忽略以'?'开头的行
|
||||||
|
row = [float(x) if x != '?' else np.nan for x in row]
|
||||||
|
data.append(row)
|
||||||
|
|
||||||
|
# 将'?'替换为np.nan
|
||||||
|
data = np.array(data, dtype=float)
|
||||||
|
|
||||||
|
# 使用SimpleImputer填充缺失值
|
||||||
|
imputer = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0)
|
||||||
|
data = imputer.fit_transform(data)
|
||||||
|
|
||||||
|
# 将所有列转换为数值类型
|
||||||
|
X = data[:, :-1]
|
||||||
|
y = data[:, -1].astype(int)
|
||||||
|
|
||||||
|
# 预处理数据
|
||||||
|
X, y = preprocess_data(X, y)
|
||||||
|
|
||||||
|
return X, y
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# 更新文件路径以匹配实际位置
|
||||||
|
X_train, y_train = load_data("C:/Users/HP/Desktop/机器学习/Experiment-LogReg/data/horse-colic.data")
|
||||||
|
X_test, y_test = load_data("C:/Users/HP/Desktop/机器学习/Experiment-LogReg/data/horse-colic.test")
|
||||||
|
|
||||||
|
if X_train is not None and X_test is not None:
|
||||||
|
# 特征缩放
|
||||||
|
scaler = StandardScaler()
|
||||||
|
X_train = scaler.fit_transform(X_train)
|
||||||
|
X_test = scaler.transform(X_test)
|
||||||
|
|
||||||
|
# 训练模型
|
||||||
|
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, regularization=0.1)
|
||||||
|
|
||||||
|
# 预测测试集
|
||||||
|
y_pred = [predict_logreg(x, weights) for x in X_test]
|
||||||
|
|
||||||
|
# 计算准确率
|
||||||
|
accuracy = np.mean(y_pred == y_test)
|
||||||
|
print(f"准确率: {accuracy}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user