From 759972fdb2b8af9210623b7468ac22fceb199d96 Mon Sep 17 00:00:00 2001 From: sn <3298564802@qq.com> Date: Sat, 2 Nov 2024 21:57:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 70 ++++++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/function.py b/function.py index 2525668..66d1a3e 100644 --- a/function.py +++ b/function.py @@ -3,6 +3,8 @@ import numpy as np import pandas as pd from sklearn.impute import SimpleImputer from sklearn.preprocessing import StandardScaler +from sklearn.model_selection import train_test_split +from sklearn.metrics import accuracy_score, classification_report """ 实验说明: @@ -34,83 +36,69 @@ def sigmoid(x): return 1 / (1 + np.exp(-x)) # train_logreg函数,用于训练逻辑回归模型 -def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.01): - m, n = X.shape # 获取样本数量和特征数量 - weights = np.zeros(n) # 初始化权重为0 - for _ in range(iterations): # 迭代指定次数 - linear_model = np.dot(X, weights) # 计算线性模型的输出 - y_predicted = sigmoid(linear_model) # 将线性输出转换为概率 - gradient = np.dot(X.T, (y_predicted - y)) / m + regularization * weights # 计算梯度 - weights -= learning_rate * gradient # 更新权重 - return weights # 返回训练好的权重 +def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.1): + m, n = X.shape + weights = np.zeros(n) + for _ in range(iterations): + linear_model = np.dot(X, weights) + y_predicted = sigmoid(linear_model) + gradient = np.dot(X.T, (y_predicted - y)) / m + regularization * weights + weights -= learning_rate * gradient + return weights # predict_logreg函数,用于使用训练好的权重进行预测 def predict_logreg(inX, weights): - linear_output = np.dot(inX, weights) # 计算线性输出 - probability = sigmoid(linear_output) # 将线性输出转换为概率 - return 1 if probability > 0.5 else 0 # 返回预测结果,大于0.5为1,否则为0 + linear_output = np.dot(inX, weights) + probability = sigmoid(linear_output) + return 1 if probability > 0.5 else 0 # preprocess_data函数,用于预处理数据 def preprocess_data(X, y): - # 合并目标变量的类别,将2和3都转换为0 y[y == 2] = 0 y[y == 3] = 0 - - # 删除特征3和特征24~28 X = np.delete(X, [3], axis=1) X = X[:, :23] - - return X, y # 返回预处理后的数据 + return X, y # load_data函数,用于加载数据 def load_data(filepath): - # 使用Pandas读取数据,可以更好地处理缺失值和字符串 try: data = pd.read_csv(filepath, header=None, sep=r'\s+', on_bad_lines='skip') except pd.errors.ParserError as e: print(f"Error parsing file: {e}") return None, None - - # 将'?'替换为np.nan data.replace('?', np.nan, inplace=True) - - # 使用SimpleImputer填充缺失值 imputer = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0) data.iloc[:, :-1] = imputer.fit_transform(data.iloc[:, :-1]) - - # 将所有列转换为数值类型 for col in data.columns[:-1]: data[col] = pd.to_numeric(data[col], errors='coerce') - - X = data.iloc[:, :-1].values # 特征数据 - y = data.iloc[:, -1].values.astype(int) # 目标变量 - - # 预处理数据 + X = data.iloc[:, :-1].values + y = data.iloc[:, -1].values.astype(int) X, y = preprocess_data(X, y) - - return X, y # 返回加载和预处理后的数据 + return X, y # main函数,程序的入口点 def main(): - # 假设data文件夹和function.py在同一目录下 - X_train, y_train = load_data("./data/horse-colic.data") - X_test, y_test = load_data("./data/horse-colic.test") - - if X_train is not None and X_test is not None: + X, y = load_data("./data/horse-colic.data") + if X is not None: + # 分割数据集为训练集和测试集 + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + # 特征缩放 scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) - + # 训练模型 weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, regularization=0.1) - + # 预测测试集 y_pred = [predict_logreg(x, weights) for x in X_test] - + # 计算准确率 - accuracy = np.mean(y_pred == y_test) + accuracy = accuracy_score(y_test, y_pred) print(f"准确率: {accuracy}") + print(classification_report(y_test, y_pred)) if __name__ == "__main__": - main() # 调用main函数,运行程序 \ No newline at end of file + main() \ No newline at end of file