File size: 2,094 Bytes
98b0379 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.decomposition import PCA
from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
def select_features_pca(df, n_components):
df_numeric = df.select_dtypes(include=[float, int])
imputer = SimpleImputer(strategy='mean')
df_imputed = imputer.fit_transform(df_numeric)
scaler = StandardScaler()
df_scaled = scaler.fit_transform(df_imputed)
pca = PCA(n_components=n_components)
components = pca.fit_transform(df_scaled)
feature_names = df_numeric.columns
important_features = feature_names[:n_components]
return df[important_features], important_features
def select_features_rfe(df, n_features_to_select):
df_numeric = df.select_dtypes(include=[float, int])
imputer = SimpleImputer(strategy='mean')
df_imputed = imputer.fit_transform(df_numeric)
scaler = StandardScaler()
df_scaled = scaler.fit_transform(df_imputed)
model = LinearRegression()
rfe = RFE(estimator=model, n_features_to_select=n_features_to_select)
rfe.fit(df_scaled, df_scaled[:, 0])
important_features = [df_numeric.columns[i] for i in range(len(rfe.support_)) if rfe.support_[i]]
return df[important_features], important_features
def select_features_rf(df, n_features_to_select):
df_numeric = df.select_dtypes(include=[float, int])
imputer = SimpleImputer(strategy='mean')
df_imputed = imputer.fit_transform(df_numeric)
scaler = StandardScaler()
df_scaled = scaler.fit_transform(df_imputed)
model = RandomForestRegressor()
model.fit(df_scaled, df_scaled[:, 0])
importances = model.feature_importances_
indices = importances.argsort()[-n_features_to_select:][::-1]
important_features = [df_numeric.columns[i] for i in indices]
return df[important_features], important_features
|