<jupyter_start><jupyter_text>Random Forest with Scikit LearnThe current notebook has a purpose to give a brief explanation for random forest solution and how to use it with scikit learn library  Loading libraries<jupyter_code># Load the libraries needed 
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.tree import export_graphviz
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn import model_selection
from sklearn.preprocessing import LabelEncoder
from sklearn import metrics

import pandas as pd
import numpy as np

import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
# Set random seed
np.random.seed(0)<jupyter_output><empty_output><jupyter_text>After loading the libraries used in this notebook. It will be assigned the data sets to local variables.  Data Wrangling & Exploration<jupyter_code>iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
print('The shape of iris dataset is :', df.shape)

df.columns
df.columns = ['sepal_length_cm','sepal_width_cm'
             ,'petal_length_cm','petal_width_cm'
             ,'species']<jupyter_output><empty_output><jupyter_text>Rename columns to have an easier access to data<jupyter_code>df.info()

df.head()<jupyter_output><empty_output><jupyter_text>Divide Data by Category<jupyter_code>setosa      = df[df['species']=='setosa']
versicolor  = df[df['species']=='versicolor']
virginica   = df[df['species']=='virginica']
print("Division of categories is : ", df.groupby('species').size())
setosa.describe()
versicolor.describe()
virginica.describe()


sns.set()
sns.pairplot(df[['sepal_length_cm','sepal_width_cm'
                ,'petal_length_cm', 'petal_width_cm', 'species']]
                ,hue="species", diag_kind="kde")<jupyter_output><empty_output><jupyter_text>Training Data<jupyter_code>X=df.iloc[:,0:4] # Features
y=df['species']  # Labels

# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) # 70% training and 30% test<jupyter_output><empty_output><jupyter_text>Simple training<jupyter_code>#RandomForestClassifier(bootstrap=True, class_weight=None, criterion='entropy',
#            max_depth=None, max_features='auto', max_leaf_nodes=None,
#            min_impurity_decrease=0.0, min_impurity_split=None,
#            min_samples_leaf=1, min_samples_split=2,
#            min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=None,
#            oob_score=False, random_state=0, verbose=0, warm_start=False)
predRF=RandomForestClassifier(n_estimators=100)

#Train the model using the training sets y_pred=clf.predict(X_test)
predRF.fit(X_train,y_train)
y_pred=predRF.predict(X_test)

# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
feature_imp = pd.Series(predRF.feature_importances_
                      ,index=X.columns).sort_values(ascending=False)

sns.barplot(x=feature_imp, y=feature_imp.index)
# Add labels to your graph
plt.xlabel('Feature Importance Score')
plt.ylabel('Features')
plt.title("Visualizing Important Features")
plt.legend()
plt.show()<jupyter_output>No handles with labels found to put in legend.<jupyter_text>DO NOT running during presentation.<jupyter_code>len(predRF.estimators_)
treeFilenma = ""
j = 1
for estimator in predRF.estimators_:
  treeFilename = "/content/RFTrees/tree" + str(j) + ".png"
  export_graphviz(estimator, out_file='/content/RFTrees/tree.dot'
                ,feature_names = iris.feature_names
                ,class_names = iris.target_names
                ,rounded = True
                ,proportion = False
                ,precision = 2
                ,filled = True)
  # Convert to png using system command (requires Graphviz)
  from subprocess import call
  call(['dot', '-Tpng', '/content/RFTrees/tree.dot', '-o', treeFilename, '-Gdpi=100'])
  print("\n Created filename ",treeFilename)
  j = j + 1

  

# Display in jupyter notebook
#from IPython.display import Image
#Image(filename = 'tree.png')
images = []
for img_path in glob.glob('/content/RFTrees/*.png'):
    images.append(mpimg.imread(img_path))

plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
    plt.subplot(len(images) / columns + 1, columns, i + 1)
    plt.imshow(image)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10872313527180784, 'fraction_numerical': 0.007374631268436578, 'mean_word_length': 5.1251612903225805, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '347247', 'license': 'MIT', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1552, 'n_words': 353, 'path': 'RandomForest.ipynb', 'repo_name': 'DavidAlzateOcampo/DataScienceNetwork', 'size': '4746'}