generated from LufsX/Experiment-LogReg
This commit is contained in:
+11
-4
@@ -6,6 +6,7 @@ from sklearn.preprocessing import PolynomialFeatures, StandardScaler
|
||||
id = "202210423104"
|
||||
name = "张钦诚"
|
||||
|
||||
|
||||
def load_data(filepath):
|
||||
data = []
|
||||
with open(filepath, 'r') as file:
|
||||
@@ -18,7 +19,12 @@ def load_data(filepath):
|
||||
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
|
||||
|
||||
# 将最后一列作为标签,其他列作为特征
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1]
|
||||
return X, y
|
||||
|
||||
|
||||
def preprocess_data(X):
|
||||
# 标准化特征
|
||||
@@ -30,18 +36,19 @@ def preprocess_data(X):
|
||||
X_poly = poly.fit_transform(X_standardized)
|
||||
return X_poly
|
||||
|
||||
|
||||
def train_logreg(X, y):
|
||||
model = RandomForestClassifier(n_estimators=113, max_depth=10, random_state=42)
|
||||
model.fit(X, y)
|
||||
return model
|
||||
|
||||
|
||||
def predict_logreg(model, X):
|
||||
return model.predict(X)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
data = load_data("./data/horse-colic.data")
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1]
|
||||
X, y = load_data("./data/horse-colic.data")
|
||||
|
||||
# 数据集划分
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
||||
|
||||
Reference in New Issue
Block a user