generated from LufsX/Experiment-LogReg
This commit is contained in:
+3
-3
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user