forked from LufsX/Experiment-LogReg
This commit is contained in:
+8
-12
@@ -85,7 +85,13 @@ def load_data(filepath):
|
||||
for line in lines:
|
||||
parts = line.strip().split(',')
|
||||
label = int(parts[-1])
|
||||
features.append([float(x) for x in parts[:-1]])
|
||||
feature_list = []
|
||||
for x in parts[:-1]:
|
||||
if x == '?':
|
||||
feature_list.append(0) # 用0代替缺失值
|
||||
else:
|
||||
feature_list.append(float(x))
|
||||
features.append(feature_list)
|
||||
labels.append(label)
|
||||
return np.array(features), np.array(labels)
|
||||
|
||||
@@ -99,14 +105,4 @@ if __name__ == "__main__":
|
||||
# 预测
|
||||
predictions = [predict_logreg(X_train[i], weights) for i in range(X_train.shape[0])]
|
||||
# 打印预测结果
|
||||
print(predictions)
|
||||
|
||||
# 加载测试集
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||
print(predictions)
|
||||
Reference in New Issue
Block a user