<jupyter_start><jupyter_text>Random Forest regressor<jupyter_code>from sklearn.ensemble import RandomForestRegressor
random_regressor=RandomForestRegressor()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df= pd.read_csv('Data/Real-Data/Real_Combine.csv')
import seaborn as sns
sns.heatmap(df.isnull(),yticklabels=False,cbar=False,cmap='viridis')
df=df.dropna()
X= df.iloc[:,:-1]
y= df.iloc[:,-1]<jupyter_output><empty_output><jupyter_text>Train test split<jupyter_code>from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y)
random_regressor.fit(X_train,y_train)
print(random_regressor.score(X_train,y_train))
print(random_regressor.score(X_test,y_test))
from sklearn.model_selection import cross_val_score
val_score=cross_val_score(random_regressor,X,y,cv=5)
val_score.mean()
pred=random_regressor.predict(X_test)
sns.distplot(y_test-pred)<jupyter_output><empty_output><jupyter_text>HyperParameter Tuning<jupyter_code>from sklearn.model_selection import RandomizedSearchCV
n_estimators = [int(x) for x in np.linspace(start = 100, stop = 1200, num = 12)]
print(n_estimators)
max_features = ['auto', 'sqrt']
max_depth = [int(x) for x in np.linspace(5, 30, num = 6)]
min_samples_split = [2, 5, 10, 15, 100]
min_samples_leaf = [1, 2, 5, 10]
# Create the random grid
random_grid = {'n_estimators': n_estimators,
               'max_features': max_features,
               'max_depth': max_depth,
               'min_samples_split': min_samples_split,
               'min_samples_leaf': min_samples_leaf}

print(random_grid)
rf=RandomForestRegressor()
rf_random = RandomizedSearchCV(estimator=rf, param_distributions = random_grid,scoring='neg_mean_squared_error', n_iter = 100, cv = 5, verbose=2, random_state=42, n_jobs = 1)
rf_random.fit(X_train,y_train)
rf_random.best_params_
predictions=rf_random.predict(X_test)
sns.distplot(y_test-predictions)
from sklearn import metrics
print('MAE:', metrics.mean_absolute_error(y_test, predictions))
print('MSE:', metrics.mean_squared_error(y_test, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))
import pickle
pickle.dump(random_regressor,open("random_regressor.pkl","wb"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1201241134751773, 'fraction_numerical': 0.016843971631205674, 'mean_word_length': 8.028, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '169931', 'license': 'Apache-2.0', 'n_tokens_mistral': 884, 'n_tokens_neox': 831, 'n_words': 136, 'path': 'Random Forest.ipynb', 'repo_name': 'saiprasad1586/Air-Qulity-Index', 'size': '2256'}