<jupyter_start><jupyter_text>Random Forest using iris dataset<jupyter_code>from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pandas as pd 
import numpy as np
#setting random seed
np.random.seed(0)
iris = load_iris()
#creating a data frame
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df.head()
#Adding a new column for species name
df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
df.head()
#Adding a new column for train_test
df['is_train'] = np.random.uniform(0, 1, len(df)) <= 0.75#using 75% of data as training data
df.head()
#Creating Train and Test data
train, test = df[df['is_train'] == True], df[df['is_train'] == False]
#no. of data in train and test
print('No. of observations in trainig subset:', len(train))
print('No. of observations in test subset:', len(test))
#showing data upto 'species' column 
features = df.columns[:4]
features
#identify the species with digits
y = pd.factorize(train['species'])[0]
y
#create random forest classifier
forest = RandomForestClassifier(n_jobs=2, random_state=0)
#train
forest.fit(train[features], y)
#predicting
forest.predict(test[features])
#probability of predictions
forest.predict_proba(test[features])
#mapping names with plants according to the prediction
predictor = iris.target_names[forest.predict(test[features])]
predictor
test['species']<jupyter_output><empty_output><jupyter_text>Confusion Matrix  A confusion matrix is a table that is often used to describe the performance of a classification model (or "classifier") on a set of test data for which the true values are known. The confusion matrix itself is relatively simple to understand, but the related terminology can be confusing.  Link -- http://www.dataschool.io/simple-guide-to-confusion-matrix-terminology/<jupyter_code>#create a confusion matrix
pd.crosstab(test['species'], predictor, rownames=['Actual species'], colnames=['Predicted species'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11010558069381599, 'fraction_numerical': 0.006033182503770739, 'mean_word_length': 6.653846153846154, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '22946', 'license': 'MIT', 'n_tokens_mistral': 638, 'n_tokens_neox': 609, 'n_words': 200, 'path': 'scikit_Random_Forest(iris dataset).ipynb', 'repo_name': 'Ankit5678/Workflow_Jupyter', 'size': '1989'}