generated from LufsX/Experiment-LogReg
This commit is contained in:
+29
-41
@@ -3,6 +3,8 @@ import numpy as np
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
from sklearn.impute import SimpleImputer
|
from sklearn.impute import SimpleImputer
|
||||||
from sklearn.preprocessing import StandardScaler
|
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))
|
return 1 / (1 + np.exp(-x))
|
||||||
|
|
||||||
# train_logreg函数,用于训练逻辑回归模型
|
# train_logreg函数,用于训练逻辑回归模型
|
||||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.01):
|
def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.1):
|
||||||
m, n = X.shape # 获取样本数量和特征数量
|
m, n = X.shape
|
||||||
weights = np.zeros(n) # 初始化权重为0
|
weights = np.zeros(n)
|
||||||
for _ in range(iterations): # 迭代指定次数
|
for _ in range(iterations):
|
||||||
linear_model = np.dot(X, weights) # 计算线性模型的输出
|
linear_model = np.dot(X, weights)
|
||||||
y_predicted = sigmoid(linear_model) # 将线性输出转换为概率
|
y_predicted = sigmoid(linear_model)
|
||||||
gradient = np.dot(X.T, (y_predicted - y)) / m + regularization * weights # 计算梯度
|
gradient = np.dot(X.T, (y_predicted - y)) / m + regularization * weights
|
||||||
weights -= learning_rate * gradient # 更新权重
|
weights -= learning_rate * gradient
|
||||||
return weights # 返回训练好的权重
|
return weights
|
||||||
|
|
||||||
# predict_logreg函数,用于使用训练好的权重进行预测
|
# predict_logreg函数,用于使用训练好的权重进行预测
|
||||||
def predict_logreg(inX, weights):
|
def predict_logreg(inX, weights):
|
||||||
linear_output = np.dot(inX, weights) # 计算线性输出
|
linear_output = np.dot(inX, weights)
|
||||||
probability = sigmoid(linear_output) # 将线性输出转换为概率
|
probability = sigmoid(linear_output)
|
||||||
return 1 if probability > 0.5 else 0 # 返回预测结果,大于0.5为1,否则为0
|
return 1 if probability > 0.5 else 0
|
||||||
|
|
||||||
# preprocess_data函数,用于预处理数据
|
# preprocess_data函数,用于预处理数据
|
||||||
def preprocess_data(X, y):
|
def preprocess_data(X, y):
|
||||||
# 合并目标变量的类别,将2和3都转换为0
|
|
||||||
y[y == 2] = 0
|
y[y == 2] = 0
|
||||||
y[y == 3] = 0
|
y[y == 3] = 0
|
||||||
|
|
||||||
# 删除特征3和特征24~28
|
|
||||||
X = np.delete(X, [3], axis=1)
|
X = np.delete(X, [3], axis=1)
|
||||||
X = X[:, :23]
|
X = X[:, :23]
|
||||||
|
return X, y
|
||||||
return X, y # 返回预处理后的数据
|
|
||||||
|
|
||||||
# load_data函数,用于加载数据
|
# load_data函数,用于加载数据
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
# 使用Pandas读取数据,可以更好地处理缺失值和字符串
|
|
||||||
try:
|
try:
|
||||||
data = pd.read_csv(filepath, header=None, sep=r'\s+', on_bad_lines='skip')
|
data = pd.read_csv(filepath, header=None, sep=r'\s+', on_bad_lines='skip')
|
||||||
except pd.errors.ParserError as e:
|
except pd.errors.ParserError as e:
|
||||||
print(f"Error parsing file: {e}")
|
print(f"Error parsing file: {e}")
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
# 将'?'替换为np.nan
|
|
||||||
data.replace('?', np.nan, inplace=True)
|
data.replace('?', np.nan, inplace=True)
|
||||||
|
|
||||||
# 使用SimpleImputer填充缺失值
|
|
||||||
imputer = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0)
|
imputer = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0)
|
||||||
data.iloc[:, :-1] = imputer.fit_transform(data.iloc[:, :-1])
|
data.iloc[:, :-1] = imputer.fit_transform(data.iloc[:, :-1])
|
||||||
|
|
||||||
# 将所有列转换为数值类型
|
|
||||||
for col in data.columns[:-1]:
|
for col in data.columns[:-1]:
|
||||||
data[col] = pd.to_numeric(data[col], errors='coerce')
|
data[col] = pd.to_numeric(data[col], errors='coerce')
|
||||||
|
X = data.iloc[:, :-1].values
|
||||||
X = data.iloc[:, :-1].values # 特征数据
|
y = data.iloc[:, -1].values.astype(int)
|
||||||
y = data.iloc[:, -1].values.astype(int) # 目标变量
|
|
||||||
|
|
||||||
# 预处理数据
|
|
||||||
X, y = preprocess_data(X, y)
|
X, y = preprocess_data(X, y)
|
||||||
|
return X, y
|
||||||
return X, y # 返回加载和预处理后的数据
|
|
||||||
|
|
||||||
# main函数,程序的入口点
|
# main函数,程序的入口点
|
||||||
def main():
|
def main():
|
||||||
# 假设data文件夹和function.py在同一目录下
|
X, y = load_data("./data/horse-colic.data")
|
||||||
X_train, y_train = load_data("./data/horse-colic.data")
|
if X is not None:
|
||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
# 分割数据集为训练集和测试集
|
||||||
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
||||||
if X_train is not None and X_test is not None:
|
|
||||||
# 特征缩放
|
# 特征缩放
|
||||||
scaler = StandardScaler()
|
scaler = StandardScaler()
|
||||||
X_train = scaler.fit_transform(X_train)
|
X_train = scaler.fit_transform(X_train)
|
||||||
X_test = scaler.transform(X_test)
|
X_test = scaler.transform(X_test)
|
||||||
|
|
||||||
# 训练模型
|
# 训练模型
|
||||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, regularization=0.1)
|
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]
|
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(f"准确率: {accuracy}")
|
||||||
|
print(classification_report(y_test, y_pred))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main() # 调用main函数,运行程序
|
main()
|
||||||
Reference in New Issue
Block a user