generated from LufsX/Experiment-LogReg
This commit is contained in:
+19
-3
@@ -1,6 +1,6 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
|
||||
from sklearn.model_selection import train_test_split, GridSearchCV
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
|
||||
@@ -68,15 +68,31 @@ def load_data(filepath):
|
||||
else:
|
||||
continue
|
||||
values = [0 if np.isnan(v) else v for v in values]
|
||||
data.append(values[:2] + values[3:23])
|
||||
data.append(values[:2] + values[3:23]) # 根据数据结构选择特征
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
return np.array(data), np.array(labels)
|
||||
|
||||
def preprocess_data(X):
|
||||
# 标准化
|
||||
scaler = StandardScaler()
|
||||
return scaler.fit_transform(X)
|
||||
X_scaled = scaler.fit_transform(X)
|
||||
|
||||
# 创建多项式特征
|
||||
poly = PolynomialFeatures(degree=2, interaction_only=False, include_bias=False)
|
||||
X_poly = poly.fit_transform(X_scaled)
|
||||
|
||||
# 例如,可以对某个特征进行分箱处理
|
||||
# 假设 X_scaled[:, 0] 是一个连续特征,比如年龄
|
||||
age_bins = pd.cut(X_scaled[:, 0], bins=[0, 30, 60, 100], labels=["年轻", "中年", "老年"])
|
||||
# 将分箱结果转换为独热编码
|
||||
age_dummies = pd.get_dummies(age_bins, drop_first=True)
|
||||
|
||||
# 将独热编码的结果与原特征组合
|
||||
X_combined = np.hstack((X_poly, age_dummies.values))
|
||||
|
||||
return X_combined
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 加载数据集
|
||||
|
||||
Reference in New Issue
Block a user