<jupyter_start><jupyter_text>* @author: <EMAIL>  * @site: e-smartdata.org<jupyter_code>from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
iris = load_iris()
data = iris.data
targets = iris.target
feature_names = iris.feature_names
target_names = iris.target_names
all_data = np.c_[data, targets]
df = pd.DataFrame(all_data, columns=feature_names + ['target'])
df.head()
data = data[:, [2, 3]]
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(data, targets)

print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
print('y_train shape:', y_train.shape)
print('y_test shape:', y_test.shape)
from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier(n_estimators=10)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from mlxtend.plotting import plot_decision_regions

plot_decision_regions(X_train, y_train, clf)
plt.xlabel(feature_names[2])
plt.ylabel(feature_names[3])
plot_decision_regions(X_test, y_test, clf)
from sklearn.model_selection import GridSearchCV

param_grid = [{'n_estimators': np.arange(1, 100)}]

gs = GridSearchCV(clf, param_grid=param_grid, scoring='accuracy', n_jobs=-1)
gs.fit(X_train, y_train)
gs.best_score_
model = gs.best_estimator_
from mlxtend.plotting import plot_decision_regions

plot_decision_regions(X_train, y_train, model)
y_pred = model.predict(X_test)

from sklearn.metrics import accuracy_score

acc = accuracy_score(y_test, y_pred)
print(acc)
model.feature_importances_<jupyter_output><empty_output><jupyter_text>All features<jupyter_code>data = iris.data
targets = iris.target
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(data, targets)

print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
print('y_train shape:', y_train.shape)
print('y_test shape:', y_test.shape)
clf = RandomForestClassifier(n_estimators=100)
clf.fit(X_train, y_train)
from sklearn.model_selection import GridSearchCV

param_grid = [{'n_estimators': np.arange(1, 100)}]

gs = GridSearchCV(clf, param_grid=param_grid, scoring='accuracy', n_jobs=-1)
gs.fit(X_train, y_train)
model = gs.best_estimator_

y_pred = model.predict(X_test)

accuracy_score(y_test, y_pred)
model.feature_importances_
sns.barplot(x=feature_names, y=model.feature_importances_)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11426256077795786, 'fraction_numerical': 0.007698541329011345, 'mean_word_length': 8.077205882352942, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '406995', 'license': 'MIT', 'n_tokens_mistral': 970, 'n_tokens_neox': 944, 'n_words': 181, 'path': 'random_forest/01_iris_classification.ipynb', 'repo_name': 'krakowiakpawel9/decision_tree_course', 'size': '2486'}