Skip to content

Instantly share code, notes, and snippets.

@harrywang
Last active May 4, 2021 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrywang/ed1a8bc9311c4e4733b773406a045178 to your computer and use it in GitHub Desktop.
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
# 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