<jupyter_start><jupyter_text>Random Forest Classifiers---Using a Random Forest to make predictions about a categorical variable using numerical inputs. `scikit-learn` has the iris dataset if you need it.<jupyter_code>from sklearn import datasets
iris = datasets.load_iris()<jupyter_output><empty_output><jupyter_text>Check it out:<jupyter_code>iris.target_names
iris.feature_names<jupyter_output><empty_output><jupyter_text>We'll need `pandas` of course, as well as a classifier from `sklearn` and the `train_test_split` function.<jupyter_code>import pandas as pd

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>So our `DataFrame` is<jupyter_code>data = pd.DataFrame({
    'sepal_length' : iris.data[:, 0],
    'sepal_width' : iris.data[:, 1],
    'petal_length' : iris.data[:, 2],
    'petal_width' : iris.data[:, 3],
    'species' : iris.target,
})
data.head()<jupyter_output><empty_output><jupyter_text>Splitting features from target<jupyter_code>x = data.loc[:, :'petal_width']
y = data.loc[:, 'species']<jupyter_output><empty_output><jupyter_text>70/30 split into train/test data<jupyter_code>xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3)<jupyter_output><empty_output><jupyter_text>Making and training our classifier on train data<jupyter_code>classifier = RandomForestClassifier(n_estimators=100)
classifier.fit(xtrain, ytrain)<jupyter_output><empty_output><jupyter_text>We can make predictions:<jupyter_code>predictions = classifier.predict(xtest)<jupyter_output><empty_output><jupyter_text>Or check our classifier's accuracy on the test set:<jupyter_code>classifier.score(xtest, ytest)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11448196908986835, 'fraction_numerical': 0.007441327990841442, 'mean_word_length': 8.71111111111111, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '398006', 'license': 'MIT', 'n_tokens_mistral': 643, 'n_tokens_neox': 625, 'n_words': 119, 'path': 'iris/RandomForest.ipynb', 'repo_name': 'benharmonics/DataScience', 'size': '1747'}