Files
Experiment-LogReg/function.py
T
lzh 45d9232ec5
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 23s
333
2024-11-02 21:16:41 +08:00

79 lines
2.6 KiB
Python

import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, roc_auc_score, f1_score
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, VotingClassifier
from sklearn.linear_model import LogisticRegression
"""
请在这里填写学号和姓名,不填写即成绩无效。
"""
id = "202210423221"
name = "廖子豪"
def load_data(filepath):
"""Load and preprocess data from specified file."""
with open(filepath) as file:
fr = file.readlines()
data_list = []
y = []
for data in fr:
data = data.strip().split(' ')
# Keep the necessary fields
y.append(data[22]) # Target value
# Remove unnecessary columns
data = [data[i] for i in range(len(data)) if i not in [2, 22, 24, 25, 26]]
# Convert to float, replacing '?' with 0
data_processed = [0 if elem == '?' else float(elem) for elem in data]
data_list.append(data_processed)
y_train = np.array([1 if label in ['2', '3'] else 0 for label in y])
X = np.array(data_list)
# Add bias term (intercept)
X = np.insert(X, 0, 1, axis=1)
scaler = StandardScaler()
X = scaler.fit_transform(X)
return X, y_train
def train_logreg(X, y, numIter=1000, learning_rate=0.01, batch_size=128, lambda_reg=0.01):
"""Train a logistic regression model using mini-batch gradient descent."""
m, n = X.shape
weights = np.zeros(n)
for j in range(numIter):
# Shuffle data indices
dataIndex = np.random.permutation(m)
for i in range(0, m, batch_size):
end = min(i + batch_size, m)
batch_X = X[dataIndex[i:end]]
batch_y = y[dataIndex[i:end]]
# Compute predictions
z = np.dot(batch_X, weights)
h = 1 / (1 + np.exp(-z)) # Sigmoid
# Compute gradient
error = batch_y - h
gradient = np.dot(batch_X.T, error) / len(batch_y)
reg_term = 2 * lambda_reg * weights
gradient -= reg_term / len(batch_y) # Regularization
# Update weights
weights += learning_rate * gradient
return weights
def predict_logreg(inX, weights):
"""Make a prediction using the logistic regression model."""
z = np.dot(inX, weights)
prob = 1 / (1 + np.exp(-z))
return 1 if prob >= 0.5 else 0