Customer-Segmentation / feature_selection.py
simran0608's picture
Upload 9 files
98b0379 verified
raw
history blame
2.09 kB
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