generated from LufsX/Experiment-LogReg
This commit is contained in:
+9
-11
@@ -1,6 +1,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from sklearn.preprocessing import StandardScaler
|
from sklearn.preprocessing import StandardScaler
|
||||||
from sklearn.linear_model import LogisticRegression
|
from sklearn.linear_model import LogisticRegression
|
||||||
|
from sklearn.model_selection import train_test_split, GridSearchCV
|
||||||
|
|
||||||
"""
|
"""
|
||||||
实验说明:
|
实验说明:
|
||||||
@@ -25,7 +26,6 @@ id = "202210423221"
|
|||||||
name = "廖子豪"
|
name = "廖子豪"
|
||||||
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
# 使用 numpy 的 genfromtxt 函数加载数据,自动处理缺失值
|
|
||||||
data = np.genfromtxt(filepath, delimiter=' ', missing_values='?', filling_values=0)
|
data = np.genfromtxt(filepath, delimiter=' ', missing_values='?', filling_values=0)
|
||||||
labels = data[:, -1].astype(int)
|
labels = data[:, -1].astype(int)
|
||||||
features = data[:, :-1]
|
features = data[:, :-1]
|
||||||
@@ -33,30 +33,28 @@ def load_data(filepath):
|
|||||||
# 将标签转换为二分类问题
|
# 将标签转换为二分类问题
|
||||||
labels = np.where((labels == 2) | (labels == 3), 1, 0)
|
labels = np.where((labels == 2) | (labels == 3), 1, 0)
|
||||||
|
|
||||||
# 删除不相关特征(如果需要)
|
|
||||||
# features = np.delete(features, [2] + list(range(23, 28)), axis=1)
|
|
||||||
|
|
||||||
# 数据标准化
|
# 数据标准化
|
||||||
scaler = StandardScaler()
|
scaler = StandardScaler()
|
||||||
features = scaler.fit_transform(features)
|
features = scaler.fit_transform(features)
|
||||||
|
|
||||||
return features, labels
|
return features, labels, scaler
|
||||||
|
|
||||||
def train_logreg(X, y, random_state=42):
|
def train_logreg(X, y, random_state=42):
|
||||||
# 创建逻辑回归模型,使用liblinear求解器
|
# 使用 GridSearchCV 进行参数调优
|
||||||
|
param_grid = {'C': [0.01, 0.1, 1, 10, 100], 'penalty': ['l1', 'l2']}
|
||||||
model = LogisticRegression(solver='liblinear', random_state=random_state)
|
model = LogisticRegression(solver='liblinear', random_state=random_state)
|
||||||
model.fit(X, y)
|
grid_search = GridSearchCV(model, param_grid, cv=5)
|
||||||
return model
|
grid_search.fit(X, y)
|
||||||
|
return grid_search.best_estimator_
|
||||||
|
|
||||||
def predict_logreg(inX, model):
|
def predict_logreg(inX, model):
|
||||||
# 使用模型进行预测
|
|
||||||
return int(model.predict([inX])[0])
|
return int(model.predict([inX])[0])
|
||||||
|
|
||||||
# 示例使用
|
# 示例使用
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 加载训练数据和测试数据
|
# 加载训练数据和测试数据
|
||||||
X_train, y_train = load_data("./data/horse-colic.data")
|
X_train, y_train, scaler = load_data("./data/horse-colic.data")
|
||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
X_test, y_test, _ = load_data("./data/horse-colic.test") # 使用相同的scaler
|
||||||
|
|
||||||
# 训练模型
|
# 训练模型
|
||||||
model = train_logreg(X_train, y_train, random_state=42)
|
model = train_logreg(X_train, y_train, random_state=42)
|
||||||
|
|||||||
Reference in New Issue
Block a user