<jupyter_start><jupyter_text>Training the Dataset<jupyter_code>X = pd.concat( [ train_df.iloc[:,0:1], train_df.iloc[:,2:] ] ,axis=1)  #Feature = everything but Price (label)
y = train_df['Price'] # Price (label)
plt.figure(figsize=(20,20))
sns.heatmap(train_df.corr(), annot=True)
plt.show()<jupyter_output><empty_output><jupyter_text>Splitting data<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state = 42)<jupyter_output><empty_output><jupyter_text>ExtraTrees Regressor<jupyter_code>selection = ExtraTreesRegressor()
selection.fit(X_train,y_train)
selection.feature_importances_
feature_importance = pd.Series(selection.feature_importances_, index=X.columns).sort_values(axis=0, ascending=False)
feature_importance.head()
ET_prediction_y = selection.predict(X_test)
selection.score(X_train, y_train)
selection.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>RandomForest Regressor<jupyter_code>RandForest_Reg = RandomForestRegressor()
RandForest_Reg.fit(X_train,y_train)
RandForest_Reg.feature_importances_
rf_feature_importance = pd.Series(RandForest_Reg.feature_importances_, index=X.columns).sort_values(axis=0, ascending=False)
rf_feature_importance.head()
prediction_y = RandForest_Reg.predict(X_test)
RandForest_Reg.score(X_train, y_train)
RandForest_Reg.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Metrics Performance<jupyter_code>ET_MSE = metrics.mean_squared_error(y_test, ET_prediction_y)
ET_RMSE = np.sqrt(ET_MSE)
ET_MAE = metrics.mean_absolute_error(y_test, ET_prediction_y)
print("ExtraTrees MSE:",ET_MSE,"\n","ExtraTrees RMSE:",ET_RMSE,"\n",
      "ExtraTrees MAE:", ET_MAE)
MSE = metrics.mean_squared_error(y_test, prediction_y)
RMSE = np.sqrt(MSE)
MAE = metrics.mean_absolute_error(y_test, prediction_y)
print("RandomForest MSE:",MSE,"\n","RandomForest RMSE:",RMSE,"\n",
      "RandomForest MAE:", MAE)<jupyter_output>RandomForest MSE: 4344913.997189214 
 RandomForest RMSE: 2084.445729010284 
 RandomForest MAE: 1173.102337994559<jupyter_text>Cross Validation for Extra Trees<jupyter_code>et_par_dist = {'n_estimators':[10,50,100,200,300],
              'max_depth':[3,5,10,None],
              'bootstrap':[True,False],
              'max_features':['auto','sqrt','log2'],
              'min_samples_split':[2,5,10,15,20],
              'min_samples_leaf':[1,2,3]}
et_rand_search = RandomizedSearchCV(estimator=selection, param_distributions=et_par_dist, scoring='r2',
                                    n_jobs=-1, verbose=2, random_state=42, cv=5, n_iter=10)
et_rand_search.fit(X,y)
dff= pd.DataFrame(et_rand_search.cv_results_)
dff.tail()
et_rand_search.best_params_
et_rand_search.best_score_
post_CV_prediction = et_rand_search.predict(X_test)
et_rand_search.score(X_train,y_train)
et_rand_search.score(X_test,y_test)
print('Post CV ExtraTrees MSE:', metrics.mean_squared_error(y_test, post_CV_prediction))
print('Post CV ExtraTrees RMSE:', np.sqrt(metrics.mean_squared_error(y_test, post_CV_prediction)))
print('Post CV ExtraTrees MAE:', metrics.mean_absolute_error(y_test, post_CV_prediction))<jupyter_output>Post CV ExtraTrees MSE: 1624124.597477094
Post CV ExtraTrees RMSE: 1274.4114710238189
Post CV ExtraTrees MAE: 794.1491959676137<jupyter_text>To Pickle the model<jupyter_code>file= open('flight_et_cv.pkl','wb')
pickle.dump(et_rand_search,file)
model= open('flight_et_cv.pkl', 'rb')
etrees = pickle.load(model)
the_trees_that_predict=etrees.predict(X_test)
metrics.r2_score(y_test,the_trees_that_predict)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12749788910779622, 'fraction_numerical': 0.04193639178159302, 'mean_word_length': 8.37730870712401, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '440724', 'license': 'MIT', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1370, 'n_words': 168, 'path': 'Flight_Prediction_Model.ipynb', 'repo_name': 'chaturvediosmio/FlightPricePredict', 'size': '3553'}