diff --git a/function.py b/function.py index bcd504d..be724a8 100644 --- a/function.py +++ b/function.py @@ -1,102 +1,101 @@ import numpy as np import pandas as pd from sklearn.preprocessing import StandardScaler -from sklearn.model_selection import train_test_split, KFold -from sklearn.metrics import accuracy_score +from sklearn.model_selection import train_test_split +from imblearn.over_sampling import SMOTE # 导入SMOTE id = "202210423130" name = "董秋" + def sigmoid(z): return 1 / (1 + np.exp(-z)) + def train_logreg(X, y, learning_rate=0.01, iterations=1000, lambda_reg=0.01): m, n = X.shape weights = np.random.randn(n) * 0.01 + for i in range(iterations): indices = np.random.permutation(m) X = X[indices] y = y[indices] + z = np.dot(X, weights) predictions = sigmoid(z) gradient = np.dot(X.T, (predictions - y)) / m + gradient += (lambda_reg / m) * weights weights -= learning_rate * gradient learning_rate *= 0.99 + return weights + def predict_logreg(inX, weights): z = np.dot(inX, weights) prediction_prob = sigmoid(z) return 1 if prediction_prob >= 0.5 else 0 + def handle_outliers(df, threshold=3): z_scores = np.abs((df - df.mean()) / df.std()) df[z_scores > threshold] = np.nan df.fillna(df.mean(), inplace=True) return df + def load_data(filepath): data = [] labels = [] + with open(filepath, 'r') as f: for line in f: values = line.strip().replace('?', '0').split() try: values = [float(v) for v in values] if values[22] == 2 or values[22] == 3: - labels.append(0) + labels.append(0) # 死亡 elif values[22] == 1: - labels.append(1) + labels.append(1) # 生存 else: continue + values = [np.nan if v == 0 else v for v in values] data.append(values[:2] + values[3:23]) except ValueError: continue + data = np.array(data) labels = np.array(labels) + df = pd.DataFrame(data) df.fillna(df.mean(), inplace=True) df = handle_outliers(df) + return df.to_numpy(), labels + def preprocess_data(X): scaler = StandardScaler() return scaler.fit_transform(X) -def cross_validate_learning_rate(X, y, learning_rates, k=5, iterations=1000, lambda_reg=0.01): - best_lr = None - best_score = 0 - kf = KFold(n_splits=k, shuffle=True, random_state=42) - - for lr in learning_rates: - cv_scores = [] - for train_index, val_index in kf.split(X): - X_train, X_val = X[train_index], X[val_index] - y_train, y_val = y[train_index], y[val_index] - weights = train_logreg(X_train, y_train, learning_rate=lr, iterations=iterations, lambda_reg=lambda_reg) - y_pred = [predict_logreg(x, weights) for x in X_val] - cv_scores.append(accuracy_score(y_val, y_pred)) - - avg_score = np.mean(cv_scores) - print(f"Learning Rate: {lr}, Cross-Validation Accuracy: {avg_score:.4f}") - - if avg_score > best_score: - best_score = avg_score - best_lr = lr - - print(f"Best Learning Rate: {best_lr}, Best CV Accuracy: {best_score:.4f}") - return best_lr if __name__ == "__main__": + # 加载数据集 X, y = load_data("./data/horse-colic.data") X = preprocess_data(X) - learning_rates = [0.001, 0.01, 0.05, 0.1, 0.2] - best_learning_rate = cross_validate_learning_rate(X, y, learning_rates) - + # 使用 train_test_split 打乱并随机抽取数据 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) - weights = train_logreg(X_train, y_train, learning_rate=best_learning_rate, iterations=1000, lambda_reg=0.01) + + # 使用 SMOTE 进行数据集平衡 + smote = SMOTE(random_state=42) + X_train_balanced, y_train_balanced = smote.fit_resample(X_train, y_train) + + # 训练逻辑回归模型 + weights = train_logreg(X_train_balanced, y_train_balanced, learning_rate=0.1, iterations=1000, lambda_reg=0.01) + + # 测试单个数据 y_pred = predict_logreg(X_test[11], weights) print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")