Last active
May 4, 2021 19:41
-
-
Save harrywang/ed1a8bc9311c4e4733b773406a045178 to your computer and use it in GitHub Desktop.
code snippet to show how to select features based on type: numerical vs. categorical
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example to show how to select features based on type: numerical vs. categorical | |
import pandas as pd | |
# load the data | |
df = pd.read_csv("housing.csv") | |
# find numerical features and categorical features based on the type of feature | |
df_num = df.select_dtypes(exclude ='object') | |
df_cat = df.select_dtypes(include ='object') | |
# select numerical features and categorical features | |
num_features = df_num.columns.tolist() | |
cat_features = df_cat.columns.tolist() | |
# we shouldn't include the target variable | |
# target variable: 'median_house_value' | |
num_features.remove('median_house_value') | |
# print the numerical features | |
print(num_features) | |
# print the categorical features | |
print(cat_features) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment