generated from LufsX/Experiment-LogReg
66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
import numpy as np
|
|
from sklearn.model_selection import train_test_split, cross_val_score
|
|
from sklearn.ensemble import RandomForestClassifier
|
|
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
|
|
|
|
id = "202210423104"
|
|
name = "张钦诚"
|
|
|
|
def load_data(filepath):
|
|
data = []
|
|
with open(filepath, 'r') as file:
|
|
for line in file:
|
|
line_data = line.strip().split()
|
|
if line_data:
|
|
try:
|
|
data.append([float(x) if x != '?' else np.nan for x in line_data])
|
|
except ValueError as e:
|
|
print(f"Error processing line: {line}. Error: {e}")
|
|
data = np.array(data)
|
|
data = np.nan_to_num(data, nan=np.nanmean(data, axis=0))
|
|
return data
|
|
|
|
def preprocess_data(X):
|
|
# 标准化特征
|
|
scaler = StandardScaler()
|
|
X_standardized = scaler.fit_transform(X)
|
|
|
|
# 增加多项式特征
|
|
poly = PolynomialFeatures(degree=2, include_bias=False)
|
|
X_poly = poly.fit_transform(X_standardized)
|
|
return X_poly
|
|
|
|
def train_random_forest(X, y):
|
|
model = RandomForestClassifier(n_estimators=113, max_depth=10, random_state=42)
|
|
model.fit(X, y)
|
|
return model
|
|
|
|
def predict(model, X):
|
|
return model.predict(X)
|
|
|
|
if __name__ == "__main__":
|
|
data = load_data("./data/horse-colic.data")
|
|
X = data[:, :-1]
|
|
y = data[:, -1]
|
|
|
|
# 数据集划分
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
|
|
|
# 数据预处理
|
|
X_train_processed = preprocess_data(X_train)
|
|
X_test_processed = preprocess_data(X_test)
|
|
|
|
# 训练模型
|
|
model = train_random_forest(X_train_processed, y_train)
|
|
|
|
# 交叉验证
|
|
cv_scores = cross_val_score(model, X_train_processed, y_train, cv=5)
|
|
print(f"交叉验证准确率: {np.mean(cv_scores) * 100:.2f}%")
|
|
|
|
# 预测
|
|
y_pred = predict(model, X_test_processed)
|
|
|
|
# 计算正确率
|
|
accuracy = np.mean(y_pred == y_test)
|
|
print(f"测试出的正确率: {accuracy * 100:.2f}%")
|