generated from LufsX/Experiment-LogReg
This commit is contained in:
+18
-4
@@ -1,5 +1,6 @@
|
|||||||
# function.py
|
# function.py
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from sklearn.preprocessing import StandardScaler
|
||||||
|
|
||||||
"""
|
"""
|
||||||
实验说明:
|
实验说明:
|
||||||
@@ -26,7 +27,6 @@ name = "廖子豪"
|
|||||||
|
|
||||||
|
|
||||||
# 这里写自己的代码实现
|
# 这里写自己的代码实现
|
||||||
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
"""
|
"""
|
||||||
从文件中加载数据。
|
从文件中加载数据。
|
||||||
@@ -65,7 +65,7 @@ def load_data(filepath):
|
|||||||
print(f"Error loading data: {e}")
|
print(f"Error loading data: {e}")
|
||||||
# 返回空数组而不是None,以避免后续代码中出现错误
|
# 返回空数组而不是None,以避免后续代码中出现错误
|
||||||
return np.empty((0, 0)), np.empty(0)
|
return np.empty((0, 0)), np.empty(0)
|
||||||
|
|
||||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||||
"""
|
"""
|
||||||
对率回归训练函数。
|
对率回归训练函数。
|
||||||
@@ -80,7 +80,9 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
|||||||
- numpy array: 训练得到的权重。
|
- numpy array: 训练得到的权重。
|
||||||
"""
|
"""
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.zeros(n)
|
# 添加偏置项
|
||||||
|
X = np.c_[np.ones((m, 1)), X]
|
||||||
|
weights = np.zeros(n + 1)
|
||||||
for _ in range(iterations):
|
for _ in range(iterations):
|
||||||
for i in range(m):
|
for i in range(m):
|
||||||
z = np.dot(X[i], weights)
|
z = np.dot(X[i], weights)
|
||||||
@@ -99,6 +101,8 @@ def predict_logreg(inX, weights):
|
|||||||
Returns:
|
Returns:
|
||||||
- int: 预测的分类标签。
|
- int: 预测的分类标签。
|
||||||
"""
|
"""
|
||||||
|
# 添加偏置项
|
||||||
|
inX = np.c_[np.ones((1, 1)), inX]
|
||||||
z = np.dot(inX, weights)
|
z = np.dot(inX, weights)
|
||||||
y_pred = 1 / (1 + np.exp(-z))
|
y_pred = 1 / (1 + np.exp(-z))
|
||||||
return 1 if y_pred > 0.5 else 0
|
return 1 if y_pred > 0.5 else 0
|
||||||
@@ -110,9 +114,19 @@ if __name__ == "__main__":
|
|||||||
# 加载测试集
|
# 加载测试集
|
||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
X_test, y_test = load_data("./data/horse-colic.test")
|
||||||
|
|
||||||
|
# 特征缩放
|
||||||
|
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)
|
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||||
|
|
||||||
# 测试单个数据
|
# 测试单个数据
|
||||||
y_pred = predict_logreg(X_test[11], weights)
|
y_pred = predict_logreg(X_test[11], weights)
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||||
|
|
||||||
|
# 评估模型
|
||||||
|
y_pred_all = [predict_logreg(X_test[i], weights) for i in range(len(X_test))]
|
||||||
|
accuracy = np.mean(y_pred_all == y_test)
|
||||||
|
print(f"Accuracy: {accuracy}")
|
||||||
Reference in New Issue
Block a user