End-to-End Machine Learning
Joshua Stapleton
Machine Learning Engineer
Creating features
Techniques
Benefits
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import Normalizer
# Split the data
X_train, X_test = train_test_split(df, test_size=0.2, random_state=42)
# Createnormalizer object, fit on training data, normalize, and transform test set
norm = Normalizer()
X_train_norm = norm.fit_transform(X_train)
X_test_norm = norm.transform(X_test)
from sklearn.preprocessing import StandardScaler
# Split the data
X_train, X_test = train_test_split(df, test_size=0.2, random_state=42)
# Create a scaler object and fit training data to standardize it
sc = StandardScaler()
X_train_stzd = sc.fit_transform(X_train)
# Only standardize the test data
X_test_stzd = sc.transform(X_test)
from sklearn.ensemble import RandomForestClassifier from sklearn.feature_selection import SelectFromModel from sklearn.model_selection import train_test_split
# Splitting data into train and test subsets first to avoid data leakage X_train, X_test, y_train, y_test = train_test_split( heart_disease_df_X, heart_disease_df_y, test_size=0.2, random_state=42)
# Define and fit the random forest model rf = RandomForestClassifier(n_jobs=-1, class_weight='balanced', max_depth=5) rf.fit(X_train, y_train)
# Define and run feature selection model = SelectFromModel(rf, prefit=True) features_bool = model.get_support() features = heart_disease_df.columns[features_bool]
End-to-End Machine Learning