generated from LufsX/Experiment-LogReg
This commit is contained in:
+52
-22
@@ -15,41 +15,61 @@ import numpy as np
|
|||||||
- numpy: 2.1.1
|
- numpy: 2.1.1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
id = "202210423213"
|
id = "202210423xxx"
|
||||||
name = "熊浩燃"
|
name = "test_name"
|
||||||
|
|
||||||
# 对率回归训练函数
|
# 对率回归训练函数
|
||||||
def train_logreg(X, y, learning_rate=0.001, iterations=1000):
|
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||||
# 标准化特征数据
|
"""
|
||||||
X = (X - np.mean(X, axis=0)) / np.std(X, axis=0)
|
对率回归训练函数。
|
||||||
|
Args:
|
||||||
|
- X (numpy array): 特征数据。
|
||||||
|
- y (numpy array): 标签数据。
|
||||||
|
- learning_rate (float): 学习率,默认为0.01。
|
||||||
|
- iterations (int): 迭代次数,默认为1000。
|
||||||
|
Returns:
|
||||||
|
- numpy array: 训练得到的权重。
|
||||||
|
"""
|
||||||
|
# 初始化权重
|
||||||
weights = np.zeros(X.shape[1])
|
weights = np.zeros(X.shape[1])
|
||||||
|
|
||||||
for i in range(iterations):
|
for i in range(iterations):
|
||||||
|
# 预测值:通过对率回归模型得到
|
||||||
z = np.dot(X, weights)
|
z = np.dot(X, weights)
|
||||||
predictions = 1 / (1 + np.exp(-z))
|
predictions = 1 / (1 + np.exp(-z))
|
||||||
|
|
||||||
# 防止 log(0) 引发 NaN
|
# 计算梯度并更新权重
|
||||||
predictions = np.clip(predictions, 1e-10, 1 - 1e-10)
|
|
||||||
|
|
||||||
gradient = np.dot(X.T, (predictions - y)) / y.size
|
gradient = np.dot(X.T, (predictions - y)) / y.size
|
||||||
weights -= learning_rate * gradient
|
weights -= learning_rate * gradient
|
||||||
|
|
||||||
# 输出损失帮助调试
|
# 可选:输出损失,帮助调试
|
||||||
if i % 100 == 0:
|
if i % 100 == 0:
|
||||||
loss = -np.mean(y * np.log(predictions) + (1 - y) * np.log(1 - predictions))
|
loss = -np.mean(y * np.log(predictions) + (1 - y) * np.log(1 - predictions))
|
||||||
print(f"迭代 {i},损失: {loss}")
|
print(f"迭代 {i},损失: {loss}")
|
||||||
|
|
||||||
return weights
|
return weights
|
||||||
|
|
||||||
# 数据加载函数
|
# 对率回归预测函数
|
||||||
|
def predict_logreg(inX, weights):
|
||||||
|
"""
|
||||||
|
对率回归预测函数。
|
||||||
|
Args:
|
||||||
|
- inX (numpy array): 输入待分类的样本。
|
||||||
|
- weights (numpy array): 训练得到的权重。
|
||||||
|
Returns:
|
||||||
|
- int: 预测的分类标签。
|
||||||
|
"""
|
||||||
|
probability = 1 / (1 + np.exp(-np.dot(inX, weights)))
|
||||||
|
return 1 if probability >= 0.5 else 0
|
||||||
|
|
||||||
|
# 数据加载函数,包含缺失值填充
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
"""
|
"""
|
||||||
从文件中加载数据。
|
从文件中加载数据,并填充缺失值。
|
||||||
Args:
|
Args:
|
||||||
- filepath (str): 数据文件路径。
|
- filepath (str): 数据文件路径。
|
||||||
Returns:
|
Returns:
|
||||||
- numpy array: 特征数据。
|
- numpy array: 填充后的特征数据。
|
||||||
- numpy array: 标签数据。
|
- numpy array: 标签数据。
|
||||||
"""
|
"""
|
||||||
data = []
|
data = []
|
||||||
@@ -58,14 +78,24 @@ def load_data(filepath):
|
|||||||
for line in file:
|
for line in file:
|
||||||
# 去除无效行,并将数据转换为float格式
|
# 去除无效行,并将数据转换为float格式
|
||||||
fields = line.strip().split()
|
fields = line.strip().split()
|
||||||
if '?' not in fields:
|
|
||||||
# 以最后一列作为标签,其余列作为特征
|
# 将 '?' 替换为 None
|
||||||
features = [float(x) for x in fields[:-1]]
|
features = [float(x) if x != '?' else None for x in fields[:-1]]
|
||||||
label = float(fields[-1])
|
label = float(fields[-1])
|
||||||
data.append(features)
|
data.append(features)
|
||||||
labels.append(label)
|
labels.append(label)
|
||||||
|
|
||||||
|
# 转换为numpy数组并填充缺失值
|
||||||
|
data = np.array(data, dtype=float)
|
||||||
|
labels = np.array(labels, dtype=float)
|
||||||
|
|
||||||
|
# 处理缺失值,使用平均值填充
|
||||||
|
col_means = np.nanmean(data, axis=0)
|
||||||
|
indices = np.where(np.isnan(data))
|
||||||
|
data[indices] = np.take(col_means, indices[1])
|
||||||
|
|
||||||
|
return data, labels
|
||||||
|
|
||||||
return np.array(data), np.array(labels)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 加载训练集
|
# 加载训练集
|
||||||
|
|||||||
Reference in New Issue
Block a user