Skip to content

Instantly share code, notes, and snippets.

@Eligijus112
Created June 15, 2021 06:20
Show Gist options
  • Save Eligijus112/def0ce9db3ca999852b39249c73e2832 to your computer and use it in GitHub Desktop.
Save Eligijus112/def0ce9db3ca999852b39249c73e2832 to your computer and use it in GitHub Desktop.
A snippet on how to use the custom RandomForest class
# Data wrangling
import pandas as pd
# Accuracy metrics
from sklearn.metrics import precision_score, recall_score
# Reading data for classification
d = pd.read_csv("data/random_forest/telecom_churn.csv")
# Setting the features used
features = [
'AccountWeeks',
'DataUsage',
'DayMins',
'DayCalls',
'MonthlyCharge',
'OverageFee',
'RoamMins'
]
# Initiating the random forest object
rf = RandomForestClassifier(
Y=d['Churn'],
X=d[features],
min_samples_split=5,
max_depth=4,
n_trees=10,
X_features_fraction=0.5
)
# Growing the random forest
rf.grow_random_forest()
# Printing out the trees
rf.print_trees()
# Making predictions
yhat = rf.predict(d[features])
d['yhat'] = yhat
# Measurring accuracy
print(f"The training precision: {precision_score(d['Churn'], d['yhat'])}")
print(f"The training recall: {recall_score(d['Churn'], d['yhat'])}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment