generated from LufsX/Experiment-LogReg
This commit is contained in:
+3
-14
@@ -16,13 +16,8 @@ import numpy as np
|
|||||||
- numpy: 2.1.1
|
- numpy: 2.1.1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
"""
|
id = "202210423104"
|
||||||
请在这里填写学号和姓名,不填写即成绩无效。
|
name = "张钦诚"
|
||||||
姓名请直接**填写中文**
|
|
||||||
例如: name = "张三"
|
|
||||||
"""
|
|
||||||
id = "202210423xxx"
|
|
||||||
name = "test_name"
|
|
||||||
|
|
||||||
|
|
||||||
# 训练对率回归模型
|
# 训练对率回归模型
|
||||||
@@ -46,9 +41,7 @@ def predict_logreg(inX, weights):
|
|||||||
|
|
||||||
# 数据加载函数
|
# 数据加载函数
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
data = np.genfromtxt(filepath, delimiter=' ', dtype=float, filling_values=np.nan)
|
data = np.genfromtxt(filepath, delimiter=',', dtype=float, filling_values=np.nan) # 根据实际分隔符调整
|
||||||
|
|
||||||
# 去掉包含NaN的行
|
|
||||||
data = data[~np.isnan(data).any(axis=1)]
|
data = data[~np.isnan(data).any(axis=1)]
|
||||||
|
|
||||||
X = data[:, :-1] # 特征
|
X = data[:, :-1] # 特征
|
||||||
@@ -67,12 +60,8 @@ if __name__ == "__main__":
|
|||||||
print(f"X_train shape: {X_train.shape}, y_train shape: {y_train.shape}")
|
print(f"X_train shape: {X_train.shape}, y_train shape: {y_train.shape}")
|
||||||
print(f"X_test shape: {X_test.shape}, y_test shape: {y_test.shape}")
|
print(f"X_test shape: {X_test.shape}, y_test shape: {y_test.shape}")
|
||||||
|
|
||||||
# 确保测试集有足够的数据进行预测
|
|
||||||
if X_test.shape[0] > 0:
|
if X_test.shape[0] > 0:
|
||||||
# 训练对率回归模型
|
|
||||||
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)
|
||||||
|
|
||||||
# 测试单个数据,确保索引有效
|
|
||||||
index_to_test = 0 if X_test.shape[0] == 1 else 11
|
index_to_test = 0 if X_test.shape[0] == 1 else 11
|
||||||
y_pred = predict_logreg(X_test[index_to_test], weights)
|
y_pred = predict_logreg(X_test[index_to_test], weights)
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[index_to_test]}")
|
print(f"预测结果: {y_pred}, 实际结果: {y_test[index_to_test]}")
|
||||||
|
|||||||
Reference in New Issue
Block a user