generated from LufsX/Experiment-LogReg
This commit is contained in:
+42
-10
@@ -1,5 +1,6 @@
|
|||||||
# function.py
|
# function.py
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
"""
|
"""
|
||||||
实验说明:
|
实验说明:
|
||||||
@@ -23,8 +24,12 @@ import numpy as np
|
|||||||
姓名请直接**填写中文**
|
姓名请直接**填写中文**
|
||||||
例如: name = "张三"
|
例如: name = "张三"
|
||||||
"""
|
"""
|
||||||
id = "202210423xxx"
|
id = "202210423223"
|
||||||
name = "test_name"
|
name = "李顺潮"
|
||||||
|
|
||||||
|
def sigmoid(z):
|
||||||
|
"""Sigmoid函数,用于对率回归的预测。"""
|
||||||
|
return 1 / (1 + np.exp(-z))
|
||||||
|
|
||||||
|
|
||||||
# 这里写自己的代码实现
|
# 这里写自己的代码实现
|
||||||
@@ -40,6 +45,19 @@ def train_logreg(X, y, other_params="你自己设置一些其它参数"):
|
|||||||
Returns:
|
Returns:
|
||||||
- numpy array: 训练得到的权重。
|
- numpy array: 训练得到的权重。
|
||||||
"""
|
"""
|
||||||
|
# 初始化权重
|
||||||
|
weights = np.zeros(X.shape[1])
|
||||||
|
learning_rate = 0.01
|
||||||
|
iterations = 1000
|
||||||
|
|
||||||
|
for _ in range(iterations):
|
||||||
|
z = np.dot(X, weights)
|
||||||
|
predictions = sigmoid(z)
|
||||||
|
error = y - predictions
|
||||||
|
gradient = np.dot(X.T, error)
|
||||||
|
weights += learning_rate * gradient
|
||||||
|
|
||||||
|
return weights
|
||||||
|
|
||||||
|
|
||||||
def predict_logreg(inX, weights):
|
def predict_logreg(inX, weights):
|
||||||
@@ -53,6 +71,10 @@ def predict_logreg(inX, weights):
|
|||||||
Returns:
|
Returns:
|
||||||
- int: 预测的分类标签。
|
- int: 预测的分类标签。
|
||||||
"""
|
"""
|
||||||
|
# 计算预测值
|
||||||
|
z = np.dot(inX, weights)
|
||||||
|
probability = sigmoid(z)
|
||||||
|
return 1 if probability >= 0.5 else 0
|
||||||
|
|
||||||
|
|
||||||
# 这里也写自己的代码实现
|
# 这里也写自己的代码实现
|
||||||
@@ -67,6 +89,18 @@ def load_data(filepath):
|
|||||||
- np.array: 特征数据。
|
- np.array: 特征数据。
|
||||||
- np.array: 标签数据。
|
- np.array: 标签数据。
|
||||||
"""
|
"""
|
||||||
|
# 使用pandas加载数据
|
||||||
|
df = pd.read_csv(filepath, header=None)
|
||||||
|
|
||||||
|
# 替换缺失值和非数值数据,这里假设我们用0来填补缺失数据
|
||||||
|
df.replace('?', np.nan, inplace=True)
|
||||||
|
df.fillna(0, inplace=True)
|
||||||
|
|
||||||
|
# 将数据拆分为特征和标签
|
||||||
|
X = df.iloc[:, :-1].values # 特征
|
||||||
|
y = df.iloc[:, -1].values # 标签
|
||||||
|
|
||||||
|
return X, y
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@@ -74,12 +108,10 @@ if __name__ == "__main__":
|
|||||||
# 加载训练集
|
# 加载训练集
|
||||||
X_train, y_train = load_data("./data/horse-colic.data")
|
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)
|
||||||
|
|
||||||
# 训练对率回归模型
|
# 测试单个样本的预测
|
||||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
sample = X_train[0].reshape(1, -1) # 取出第一行作为样本
|
||||||
|
prediction = predict_logreg(sample, weights)
|
||||||
# 测试单个数据
|
print(f"样本预测: {prediction}")
|
||||||
y_pred = predict_logreg(X_test[11], weights)
|
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user