更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 40s

This commit is contained in:
2024-11-05 19:29:07 +08:00
parent 6873e40eea
commit 38dcbc1841
+3 -3
View File
@@ -18,6 +18,7 @@ from sklearn.impute import SimpleImputer
- Python: 3.10.12
- numpy: 2.1.1
"""
# Sigmoid 函数
def sigmoid(z):
return 1 / (1 + np.exp(-z))
@@ -29,7 +30,7 @@ id = "202210423223"
name = "李顺潮"
# 对率回归训练函数
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
def train_logreg(X, y, learning_rate=0.01, iterations=10000):
X = np.hstack((np.ones((X.shape[0], 1)), X)) # 在特征前添加一列全为1的列
m, n = X.shape
weights = np.zeros(n)
@@ -58,7 +59,6 @@ def load_data(filepath):
imputer = SimpleImputer(strategy='mean') # 使用均值填充
data = pd.DataFrame(imputer.fit_transform(data))
X = data.iloc[:, :-1].to_numpy() # 特征
y = data.iloc[:, -1].to_numpy().astype(int) # 标签,确保y是整数类型
return X, y
@@ -81,7 +81,7 @@ if __name__ == "__main__":
# 训练对率回归模型
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=10000) #增加迭代次数
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=10000)
# 测试单个数据
y_pred = predict_logreg(X_test[11], weights)