generated from LufsX/Experiment-LogReg
This commit is contained in:
+40
-43
@@ -31,24 +31,21 @@ name = "严锡好"
|
|||||||
|
|
||||||
|
|
||||||
# 这里写自己的代码实现
|
# 这里写自己的代码实现
|
||||||
# sigmoid函数,用于逻辑回归模型中将线性输出转换为概率
|
def sigmoid(z):
|
||||||
def sigmoid(x):
|
return 1.0 / (1 + np.exp(-z))
|
||||||
return 1 / (1 + np.exp(-x))
|
|
||||||
|
|
||||||
# 计算梯度的函数
|
|
||||||
def compute_gradient(X_batch, y_batch, weights, lambda_reg):
|
def compute_gradient(X_batch, y_batch, weights, lambda_reg):
|
||||||
z = np.dot(X_batch, weights)
|
z = np.dot(X_batch, weights)
|
||||||
h = sigmoid(z)
|
h = sigmoid(z)
|
||||||
gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch)
|
gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch)
|
||||||
gradient += lambda_reg * weights # L2正则化
|
gradient += (lambda_reg / len(y_batch)) * weights # L2正则化
|
||||||
return gradient
|
return gradient
|
||||||
|
|
||||||
# train_logreg函数,用于训练逻辑回归模型
|
def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1):
|
||||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=64, lambda_reg=0.1):
|
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.zeros(n)
|
weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化
|
||||||
best_accuracy = 0
|
|
||||||
best_weights = weights.copy()
|
best_weights = weights.copy()
|
||||||
|
best_accuracy = 0
|
||||||
|
|
||||||
for i in range(iterations):
|
for i in range(iterations):
|
||||||
indices = np.random.permutation(m)
|
indices = np.random.permutation(m)
|
||||||
@@ -61,25 +58,21 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=64, lambd
|
|||||||
gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg)
|
gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg)
|
||||||
weights -= learning_rate * gradient
|
weights -= learning_rate * gradient
|
||||||
|
|
||||||
# 计算训练集准确率
|
|
||||||
accuracy = accuracy_score(y, predict_logreg(X, weights))
|
accuracy = accuracy_score(y, predict_logreg(X, weights))
|
||||||
if accuracy > best_accuracy:
|
if accuracy > best_accuracy:
|
||||||
best_accuracy = accuracy
|
best_accuracy = accuracy
|
||||||
best_weights = weights.copy()
|
best_weights = weights.copy()
|
||||||
|
|
||||||
# 动态调整学习率
|
if i % 1000 == 0 and accuracy > 0.95: # 动态调整学习率
|
||||||
if i % 100 == 0 and accuracy > 0.95: # 达到高准确率时减小学习率
|
|
||||||
learning_rate *= 0.9
|
learning_rate *= 0.9
|
||||||
|
|
||||||
return best_weights
|
return best_weights
|
||||||
|
|
||||||
# predict_logreg函数,用于使用训练好的权重进行预测
|
def predict_logreg(X, weights):
|
||||||
def predict_logreg(inX, weights):
|
z = np.dot(X, weights)
|
||||||
linear_output = np.dot(inX, weights)
|
h = sigmoid(z)
|
||||||
probability = sigmoid(linear_output)
|
return np.where(h >= 0.5, 1, 0)
|
||||||
return np.where(probability > 0.5, 1, 0)
|
|
||||||
|
|
||||||
# preprocess_data函数,用于预处理数据
|
|
||||||
def preprocess_data(X, y):
|
def preprocess_data(X, y):
|
||||||
y[y == 2] = 0
|
y[y == 2] = 0
|
||||||
y[y == 3] = 0
|
y[y == 3] = 0
|
||||||
@@ -87,7 +80,6 @@ def preprocess_data(X, y):
|
|||||||
X = X[:, :23]
|
X = X[:, :23]
|
||||||
return X, y
|
return X, y
|
||||||
|
|
||||||
# load_data函数,用于加载数据
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
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')
|
||||||
@@ -104,28 +96,33 @@ def load_data(filepath):
|
|||||||
X, y = preprocess_data(X, y)
|
X, y = preprocess_data(X, y)
|
||||||
return X, y
|
return X, y
|
||||||
|
|
||||||
# main函数,程序的入口点
|
|
||||||
def main():
|
|
||||||
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, batch_size=64, lambda_reg=0.1)
|
|
||||||
|
|
||||||
# 预测测试集
|
|
||||||
y_pred = predict_logreg(X_test, weights)
|
|
||||||
|
|
||||||
# 计算准确率
|
|
||||||
accuracy = accuracy_score(y_test, y_pred)
|
|
||||||
print(f"准确率: {accuracy}")
|
|
||||||
print(classification_report(y_test, y_pred))
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
# 加载数据
|
||||||
|
X, y = load_data("./data/horse-colic.data")
|
||||||
|
|
||||||
|
# 拆分训练集和验证集
|
||||||
|
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
|
||||||
|
|
||||||
|
# 数据归一化
|
||||||
|
scaler = MinMaxScaler()
|
||||||
|
X_train = scaler.fit_transform(X_train)
|
||||||
|
X_val = scaler.transform(X_val)
|
||||||
|
|
||||||
|
# 训练模型
|
||||||
|
final_weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=5000, batch_size=64, lambda_reg=0.1)
|
||||||
|
|
||||||
|
# 验证模型
|
||||||
|
y_pred = predict_logreg(X_val, final_weights)
|
||||||
|
val_accuracy = accuracy_score(y_val, y_pred)
|
||||||
|
print(f"验证集的准确率: {val_accuracy:.4f}")
|
||||||
|
print(classification_report(y_val, y_pred))
|
||||||
|
|
||||||
|
# 加载测试集
|
||||||
|
X_test, y_test = load_data("./data/horse-colic.test")
|
||||||
|
X_test = scaler.transform(X_test)
|
||||||
|
|
||||||
|
# 测试模型
|
||||||
|
y_test_pred = predict_logreg(X_test, final_weights)
|
||||||
|
test_accuracy = accuracy_score(y_test, y_test_pred)
|
||||||
|
print(f"最终测试集的准确率: {test_accuracy:.4f}")
|
||||||
|
print(classification_report(y_test, y_test_pred))
|
||||||
Reference in New Issue
Block a user