上传文件至 /
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 33s

This commit is contained in:
2024-11-03 17:37:32 +08:00
parent 84423f7537
commit dde1cfc957
+72 -57
View File
@@ -1,69 +1,84 @@
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
id = "202210423104" id = "202210423104"
name = "张钦诚" name = "张钦诚"
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
def sigmoid(z):
return 1.0 / (1 + np.exp(-z))
def compute_gradient(X_batch, y_batch, weights, lambda_reg):
z = np.dot(X_batch, weights)
h = sigmoid(z)
gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch)
gradient += (lambda_reg / len(y_batch)) * weights # L2正则化
return gradient
def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1):
m, n = X.shape
weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化
best_weights = weights.copy()
best_accuracy = 0
for _ in range(iterations):
indices = np.random.permutation(m)
X_shuffled = X[indices]
y_shuffled = y[indices]
for j in range(0, m, batch_size):
X_batch = X_shuffled[j:j + batch_size]
y_batch = y_shuffled[j:j + batch_size]
gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg)
weights -= learning_rate * gradient
# 计算训练集准确率
train_accuracy = accuracy(X, y, weights)
if train_accuracy > best_accuracy:
best_accuracy = train_accuracy
best_weights = weights.copy()
# 动态调整学习率
if _ % 100 == 0 and train_accuracy > 0.95: # 达到高准确率时减小学习率
learning_rate *= 0.9
return best_weights
def predict_logreg(X, weights):
z = np.dot(X, weights)
h = sigmoid(z)
return np.where(h >= 0.5, 1, 0)
def load_data(filepath): def load_data(filepath):
data = [] data, labels = [], []
with open(filepath, 'r') as file: with open(filepath, 'r') as f:
for line in file: for line in f:
line_data = line.strip().split() line = line.strip().split()
if line_data: if len(line) >= 1 and line[-1].isdigit():
try: line_data = [0 if x == '?' else float(x) for x in line[:-1]]
data.append([float(x) if x != '?' else np.nan for x in line_data]) data.append(line_data)
except ValueError as e: labels.append(int(line[-1]))
print(f"Error processing line: {line}. Error: {e}")
data = np.array(data) data = np.array(data)
imputer = SimpleImputer(strategy='mean') labels = np.array(labels)
data = imputer.fit_transform(data)
X = data[:, :-1] # 特征 # 检查特征数量
y = data[:, -1] # 标签 num_features = data.shape[1]
return X, y
def preprocess_data(X): # 特征选择:去掉特征3(索引2)和特征24~28(索引23~26),确保索引有效
scaler = StandardScaler() indices_to_delete = [2] # 特征3
X_standardized = scaler.fit_transform(X) if num_features > 24: # 确保特征数量足够
poly = PolynomialFeatures(degree=2, include_bias=False) indices_to_delete += list(range(24, 27)) # 特征24到27(索引23到26)
X_poly = poly.fit_transform(X_standardized)
return X_poly
def train_logreg(X, y): # 删除特征
X_processed = preprocess_data(X) # 预处理训练数据 data = np.delete(data, indices_to_delete, axis=1)
model = RandomForestClassifier(
n_estimators=300, # 增加树的数量
max_depth=25, # 调整最大深度
min_samples_split=5,
class_weight='balanced', # 处理类别不平衡
random_state=42 # 固定随机种子
)
model.fit(X_processed, y)
return model # 返回训练好的模型
def predict_logreg(X, model): # 将标签二分类化,特征23(索引22)取值2和3合并为0,其余为1
if len(X.shape) == 1: target_variable = np.where((labels == 2) | (labels == 3), 0, 1)
X = X.reshape(1, -1) # 保持输入数据的形状为 (1, n)
X_processed = preprocess_data(X) # 预处理测试数据
return model.predict(X_processed)[0] # 确保 model 是训练好的模型
if __name__ == "__main__": return data, target_variable
# 数据加载和预处理保持不变
X, y = load_data("./data/horse-colic.data")
# 划分训练集和测试集时固定随机种子 def accuracy(X, y, weights):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) predictions = predict_logreg(X, weights)
return np.mean(predictions == y)
# 训练模型
model = train_logreg(X_train, y_train)
# 预测
y_pred = model.predict(preprocess_data(X_test))
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")