<jupyter_start><jupyter_text>Chapter 6 - Other Popular Machine Learning Methods Segment 6 - Ensemble methods with random forestThis is a classification problem, where in we will be estimating the species label for iris flowers.<jupyter_code>import numpy as np
import pandas as pd

import sklearn.datasets as datasets
from sklearn.model_selection import train_test_split 
from sklearn import metrics
from sklearn.ensemble import RandomForestClassifier
iris = datasets.load_iris()

df = pd.DataFrame(iris.data, columns=iris.feature_names)
y = pd.DataFrame(iris.target)

y.columns = ['labels']

print(df.head())
y[0:5]<jupyter_output>sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)
0                5.1               3.5                1.4               0.2
1                4.9               3.0                1.4               0.2
2                4.7               3.2                1.3               0.2
3                4.6               3.1                1.5               0.2
4                5.0               3.6                1.4               0.2<jupyter_text>The data set contains information on the:- sepal length (cm)- sepal width (cm)  - petal length (cm)  - petal width (cm)- species type<jupyter_code>df.isnull().any()==True
print(y.labels.value_counts())<jupyter_output>2    50
1    50
0    50
Name: labels, dtype: int64<jupyter_text>Preparing the data for training the model<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=.2, random_state=17)<jupyter_output><empty_output><jupyter_text>Build a Random Forest model<jupyter_code>classifier = RandomForestClassifier(n_estimators=200, random_state=0)

y_train_array = np.ravel(y_train)

classifier.fit(X_train, y_train_array)

y_pred = classifier.predict(X_test)<jupyter_output><empty_output><jupyter_text>Evaluating the model on the test data<jupyter_code>print(metrics.classification_report(y_test, y_pred))
y_test_array = np.ravel(y_test)
print(y_test_array)
print(y_pred)<jupyter_output>[0 1 2 1 2 2 1 2 1 2 2 0 1 0 2 0 0 2 2 2 1 0 2 1 1 1 1 1 0 1]
{'fraction_non_alphanumeric': 0.08614051973051011, 'fraction_numerical': 0.04667949951876805, 'mean_word_length': 2.65377855887522, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '80591', 'license': 'Apache-2.0', 'n_tokens_mistral': 758, 'n_tokens_neox': 694, 'n_words': 195, 'path': 'LinkedIn/Ex_Files_Python_Data_Science_EssT_Pt2/Exercise Files/06_06_end/06_06_end.ipynb', 'repo_name': 'weboctave/ml-bootcamp', 'size': '2078'}