# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Random Forest
#
# In this lab you will learn the most important aspects of the random forest learning method. 
# Completing this lab and analyzing the code will give you a deeper understanding of these type of models.
# In our experiments we will mostly use the package sklearn from which we import RandomForestClassifier.
#

# +
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import seaborn as sns

from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import OneHotEncoder

from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, classification_report, confusion_matrix

# %matplotlib inline
# %load_ext autoreload
# %autoreload 2
# -

from sklearn.datasets import make_classification, make_regression

# ## Data Creation
#
# First of all, we create a data set containing 1000 samples with 2 features and two classes:

X, y = make_classification(n_samples = 1000,n_features=2, n_redundant=0, n_informative=2,
                           random_state=1, n_clusters_per_class=1)

# <b>Exercise 1:</b>
#
# Visualize the data set. It should look like this:
# <img src="figures/dataset.png" width="600"/>

# +
### WRITE YOUR CODE HERE ###

#pltclr=lambda x : "Black" if x==-1 else ("Red" if x==0 else "Blue") 
sns.scatterplot(X[:, 0], X[:, 1],hue=y, legend='full')
# -

# We split our data into train and test data. Then we can train our model (a random forest) on the train data and evaluate the model on the hold out test data. We split the data in a way that we train our model on 67% of the data and test our model on 33% of the data.

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.33, random_state=42)

# <b>Exercise 2:</b>
#
# Train a random forest on the training data and report the accuracy for this model on the train and test data using the default parameters of a random forest. What can you conclude from this? from sklearn.

# +
clf = RandomForestClassifier(n_estimators=100, min_samples_split=5,bootstrap=True)
### WRITE YOUR CODE HERE ###

rd=clf.fit(X_train,y_train )
rd

# +

y_pred=rd.predict(X_test)

print(confusion_matrix(y_test, y_pred))

print(f1_score(y_test, y_pred))
print(precision_score(y_test, y_pred))
print(recall_score(y_test, y_pred)) 
print(clf.feature_importances_)
# -

# ### Conclusion
#
# * F1 Score shows that it could predict with 91 % accuracy and Same shows in confusion.
# * Also X[:,2] playing importance role in prediction
#

# ## Decision Boundary
#
# Sometimes it is helpful to plot the decision boundary for a learned model. To do so, we create a grid of data points and calculate the probability of belonging to class 1. 

x_min, x_max = X[:, 0].min(), X[:, 0].max()
y_min, y_max = X[:, 1].min(), X[:, 1].max()
h = .1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)

# Then we can plot the boundary using the 'contourf' function of matplotlib.

cm = plt.cm.RdBu  # color map
plt.contourf(xx, yy, Z, alpha=.7, cmap=cm)
colors = ['red','blue']
for cur_class in [0,1]:
    plt.scatter(X[y==cur_class, 0], X[y == cur_class, 1], c=colors[cur_class],
                       edgecolors='k', alpha=0.6, label=cur_class)
plt.legend()
plt.show()

# ### Inference 
#
# * it is creating overlapping grid. We just need 4 grids to split this

# What can you conclude from the figure above?

# ## Parameter Selection
#
# The implementation of the random forest algorithm in sklearn has many parameter. The most important ones are the number of trees used (n_estimators) and the maximal depth of a single tree (max_depth). Investigate how the number of used trees effects the training and testing accuracy.
#
# <b>Exercise 3:</b>
#
# Plot a diagram that shows the training and testing accuracy depending on the number of trees (from 1 to 20) used. This plot should look like this:
# <img src="figures/num_trees.png" width="600"/>

# +
### WRITE YOUR CODE HERE ###

train_accuracy=[]
test_accuracy=[]

for i in range(20):
    clf = RandomForestClassifier(n_estimators=i+1)
    rd=clf.fit(X_train,y_train )
    y_test_pred=rd.predict(X_test)
    y_train_pred=rd.predict(X_train)
    test_accuracy.append(recall_score(y_test_pred, y_test)) 
    train_accuracy.append(recall_score(y_train_pred, y_train)) 

plt.plot(train_accuracy,label='train_accuracy')
plt.plot(test_accuracy,label='test_accuracy')

plt.legend(loc='upper right')
# -

# ## Churn Data Set
# Lets revisit the churn data set from the first tutorial.

churn_df = pd.read_csv('telecom_churn.csv')
label = churn_df['Churn']
churn_df = churn_df.drop(columns=['Churn'])

# <b>Exercise 4:</b>
#
# Create a data set containing only the numeric values. <b>Optional:</b> Try to convert all non numeric values to numeric values using a one hot encoding or by binning them. 

# +
### WRITE YOUR CODE HERE ###
enc = OneHotEncoder(handle_unknown='ignore')

encodedval=pd.DataFrame(enc.fit_transform(churn_df[['International plan','Voice mail plan']]).toarray() , columns=['International plan - No','International plan - Yes','Voice mail plan - No','Voice mail plan - Yes'])

numeric_col=list(pd.Series([x if churn_df[x].dtype==np.float64 or churn_df[x].dtype == np.int64 else np.NaN for x in churn_df.columns]).dropna())

x=pd.concat([churn_df[numeric_col],encodedval],  ignore_index=True, axis=1)
numeric_col.extend(['International plan - No','International plan - Yes','Voice mail plan - No','Voice mail plan - Yes'])
x.columns=numeric_col
# -

# <b>Exercise 5:</b>
#
# Train a model on this data set and visualize the most important features in a figure. This should look like this (The scaling and order of features can be different):
# <img src="figures/importance.png" width="600"/>
#
# <b>Hint</b>: The method feature_importance_ should be used.
# What can you conclude?

# ### WRITE YOUR CODE HERE ###
clf = RandomForestClassifier(n_estimators=100, min_samples_split=5,bootstrap=True)
rd=clf.fit(x,label)
pd.Series(clf.feature_importances_, index =numeric_col).sort_values().plot(kind='barh', figsize=(12,8))
plt.title('Impotant Features');

# <b>Exercise 6:</b>
#
# If we want to use a random forest to solve regression problems we can use the RandomForestRegressor from sklearn.
# * Generate an easy regression data set using make_regression with 10 features. (use function make_regression)
# * Split the data set into a train and test set.
# * Train a model and report the training and testing mean square error (can be calculated using sklearn.metrics.mean_squared_error)

# +
### WRITE YOUR CODE HERE ###
# X,y = make_regression(n_samples=100,n_features=10)

from matplotlib import pyplot
# generate regression dataset
X, y = make_regression(n_samples=100, n_features=10)
# -

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.33, random_state=42)

from sklearn.ensemble import RandomForestRegressor
regr = RandomForestRegressor(n_estimators=10)
regr.fit(X_train, y_train)

pred=regr.predict(X_test)
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
print(mean_squared_error(y_test,pred))
print(r2_score(y_test,pred))

estimators = np.arange(10, 200, 10)
scores = []
for n in estimators:
    regr.set_params(n_estimators=n)
    regr.fit(X_train, y_train)
    scores.append(regr.score(X_test, y_test))
plt.title("Effect of n_estimators")
plt.xlabel("n_estimator")
plt.ylabel("score")
plt.plot(estimators, scores)

regr = RandomForestRegressor(n_estimators=26)
regr.fit(X_train, y_train)
pd.Series(regr.feature_importances_).sort_values().plot(kind='barh', figsize=(12,8))
plt.title('Impotant Features');

regr = RandomForestRegressor(n_estimators=10, max_depth=None, min_samples_leaf=2, min_samples_split=5)
regr.fit(X_train, y_train)
pred=regr.predict(X_test)
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
print(mean_squared_error(y_test,pred))
print(r2_score(y_test,pred))
#print(regr.score(X_test, y_test))



StarcoderdataJupyterScriptsDedupFiltered