Template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1437fce359 | ||
|
|
d66e4cd870 | ||
|
|
942b6f47f4 | ||
|
|
b31dade448 | ||
|
|
383d143d4e | ||
|
|
8b8f14440f | ||
|
|
d9b6dd8180 | ||
|
|
8e9a0a3b29 | ||
|
|
85c4a7cbd7 | ||
|
|
07daa3d4dd | ||
|
|
130bec1f67 | ||
|
|
cbc2162a6f | ||
|
|
c9b5c58a3f | ||
|
|
e45e048ef3 | ||
|
|
6d6d42b5b8 | ||
|
|
f91e807dcc | ||
|
|
fd39fb78f3 | ||
|
|
e553731774 | ||
|
|
a9d4f65cc6 | ||
|
|
1308465b88 | ||
|
|
6c364dc100 | ||
|
|
8a362fbfac | ||
|
|
767a8781bb | ||
|
|
f024fab4a8 | ||
|
|
e52eb8eabe | ||
|
|
b0c361f395 | ||
|
|
428e60319b | ||
|
|
937ae78001 | ||
|
|
7c4caae057 | ||
|
|
78d1c7ba3d | ||
|
|
5c62123573 | ||
|
|
a12eaee527 | ||
|
|
e062dd40bd | ||
|
|
3a40f5d6b2 | ||
|
|
73d12cd545 | ||
|
|
cae2705f28 | ||
|
|
f683f61a30 | ||
|
|
ff97284d3b | ||
|
|
b4cb68d77b | ||
|
|
e4bf84d286 | ||
|
|
e8e537be9a |
+50
-69
@@ -1,83 +1,64 @@
|
|||||||
# function.py
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
"""
|
id = "202210423224"
|
||||||
实验说明:
|
name = "吴杨洋"
|
||||||
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
|
||||||
|
|
||||||
注意事项:
|
def sigmoid(z):
|
||||||
- 在 function.py 中实现对率回归算法。
|
return 1 / (1 + np.exp(-np.clip(z, -500, 500)))
|
||||||
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
|
|
||||||
- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。
|
|
||||||
|
|
||||||
测评环境:
|
|
||||||
- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz
|
|
||||||
- OS: Ubuntu 22.04.4 LTS
|
|
||||||
- Python: 3.10.12
|
|
||||||
- numpy: 2.1.1
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
请在这里填写学号和姓名,不填写即成绩无效。
|
|
||||||
"""
|
|
||||||
id = "202210423xxx"
|
|
||||||
name = "test_name"
|
|
||||||
|
|
||||||
|
|
||||||
# 这里写自己的代码实现
|
|
||||||
def train_logreg(X, y, other_params="你自己设置一些其它参数"):
|
|
||||||
"""
|
|
||||||
对率回归训练函数。
|
|
||||||
|
|
||||||
Args:
|
|
||||||
- X (numpy array): 特征数据。
|
|
||||||
- y (numpy array): 标签数据。
|
|
||||||
- other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将**无法通过**自动评测,**成绩为零**。
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- numpy array: 训练得到的权重。
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
def train_logreg(X, y, numIter=1000, learning_rate=0.01, lambda_reg=0.01):
|
||||||
|
m, n = X.shape
|
||||||
|
weights = np.zeros(n)
|
||||||
|
for j in range(numIter):
|
||||||
|
for i in range(m):
|
||||||
|
z = np.dot(X[i], weights)
|
||||||
|
h = sigmoid(z)
|
||||||
|
error = y[i] - h
|
||||||
|
gradient = X[i] * error
|
||||||
|
reg_term = lambda_reg * weights
|
||||||
|
gradient += reg_term
|
||||||
|
weights -= learning_rate * gradient
|
||||||
|
return weights
|
||||||
|
|
||||||
def predict_logreg(inX, weights):
|
def predict_logreg(inX, weights):
|
||||||
"""
|
z = np.dot(inX, weights)
|
||||||
对率回归预测函数。
|
prob = sigmoid(z)
|
||||||
|
return 1 if prob >= 0.5 else 0
|
||||||
|
|
||||||
Args:
|
|
||||||
- inX (numpy array): 输入待分类的样本。
|
|
||||||
- weights (numpy array): 训练得到的权重。
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- int: 预测的分类标签。
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
# 这里也写自己的代码实现
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
"""
|
file = open(filepath)
|
||||||
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
|
fr = file.readlines()
|
||||||
|
data_list = []
|
||||||
|
y = []
|
||||||
|
for data in fr:
|
||||||
|
data = data.strip()
|
||||||
|
data = data.split()
|
||||||
|
if len(data) < 26: # 确保每行数据有足够的元素
|
||||||
|
continue
|
||||||
|
y.append(1 if data[22] in ['2', '3'] else 0) # 假设标签是 '2' 或 '3' 为正类
|
||||||
|
data_25 = data[24]
|
||||||
|
data = [0 if x == '?' else float(x) for x in data[:24] + data_25.split(',')] # 处理缺失值
|
||||||
|
data_list.append(data)
|
||||||
|
X = np.array(data_list)
|
||||||
|
return X, np.array(y)
|
||||||
|
|
||||||
Args:
|
def standardize(X):
|
||||||
- directory (str): 数据文件路径。
|
mean = X.mean(axis=0)
|
||||||
|
std = X.std(axis=0)
|
||||||
|
return (X - mean) / std, mean, std
|
||||||
|
|
||||||
Returns:
|
def main():
|
||||||
- np.array: 特征数据。
|
|
||||||
- np.array: 标签数据。
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
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")
|
X_test, y_test = load_data("./data/horse-colic.test")
|
||||||
|
|
||||||
# 训练对率回归模型
|
X_train, mean, std = standardize(X_train)
|
||||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
X_test = (X_test - mean) / std
|
||||||
|
|
||||||
# 测试单个数据
|
weights = train_logreg(X_train, y_train, numIter=1000, learning_rate=0.01, lambda_reg=0.1)
|
||||||
y_pred = predict_logreg(X_test[11], weights)
|
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
y_pred_test = [predict_logreg(x, weights) for x in X_test]
|
||||||
|
accuracy_test = np.mean(y_pred_test == y_test)
|
||||||
|
print(f"Test Accuracy: {accuracy_test:.2f}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
numpy==2.1.1
|
numpy==2.1.1
|
||||||
requests==2.32.3
|
requests==2.32.3
|
||||||
|
scipy==1.13.1
|
||||||
|
sklearn==
|
||||||
Reference in New Issue
Block a user