<jupyter_start><jupyter_text>Importing the dataset<jupyter_code>dataset = pd.read_csv('../../data/all_records_northeastern.csv')<jupyter_output><empty_output><jupyter_text>Dropping the column, Unnamed as it is not necessary<jupyter_code>dataset.drop(columns=['Unnamed: 0'],inplace=True)
dataset.head()<jupyter_output><empty_output><jupyter_text>Count of accept and reject in dataset<jupyter_code>dataset.status.value_counts()<jupyter_output><empty_output><jupyter_text>As we see from above stats, our data is baised so we need to resample the data, in order to balanced dataset<jupyter_code>balanced_data=resample(dataset[dataset.status=='accept'],replace=True,n_samples=1000,random_state=123)
balanced_data=balanced_data.append(dataset[dataset.status=='reject'])
balanced_data.status.value_counts()
encoded_dataset=balanced_data
encoded_dataset.head()<jupyter_output><empty_output><jupyter_text>Defining labels-X and Tragets-Y<jupyter_code>X=encoded_dataset[['gre_score_quant','gre_score_verbal','test_score_toefl','undergraduation_score','work_ex','papers_published']].copy()
Y=encoded_dataset[['status']].copy()<jupyter_output><empty_output><jupyter_text>Splitting the dataset_encoded into training and testing dataset<jupyter_code>X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2,random_state=1)<jupyter_output><empty_output><jupyter_text>Training the model<jupyter_code>def modeltraining(model,X_train,X_test,Y_train,Y_test):
    sc = StandardScaler()
    sc.fit(X_train)
    X_train = sc.transform(X_train)
    
    X_test = sc.transform(X_test)
    print(X_test)
    model.fit(X_train,Y_train)
    predicted_labels_test=model.predict(X_test)
    predicted_labels_train=model.predict(X_train)
    accuracy_test=accuracy_score(Y_test,predicted_labels_test)
    accuracy_train=accuracy_score(Y_train,predicted_labels_train)
    
    
    return model,predicted_labels_test,predicted_labels_train,accuracy_test,accuracy_train,sc<jupyter_output><empty_output><jupyter_text>Calling RandomForest model<jupyter_code>model=RandomForestClassifier()
model,predicted_labels_test,predicted_labels_train,accuracy_test,accuracy_train,sc=modeltraining(model,X_train,X_test,Y_train,Y_test)<jupyter_output>[[-0.1189225   0.73491402  0.39076816 -0.36262002 -0.62058721  1.89123818]
 [ 0.78723151 -0.2849141   0.43531487  0.59052431  0.41333471  1.89123818]
 [ 0.10761601 -0.48887972  0.12348788 -0.03768445  0.2308779  -0.59699686]
 ...
 [ 1.24030852  1.5507765   0.70259515  0.87213514 -1.04631976 -0.59699686]
 [ 1.01377002  1.5507765  -4.37573011 -2.09560971  1.14316194 -0.59699686]
 [ 0.33415451  2.16267337  0.5244083  -0.01602208  0.83906726 -0.59699686]]<jupyter_text>Accuracy of Test data<jupyter_code>accuracy_test<jupyter_output><empty_output><jupyter_text>Accuracy of Train data<jupyter_code>accuracy_train
model
print(classification_report(Y_test,predicted_labels_test))
print(classification_report(Y_train,predicted_labels_train))<jupyter_output>precision    recall  f1-score   support

      accept       0.98      1.00      0.99       801
      reject       1.00      0.98      0.99       862

   micro avg       0.99      0.99      0.99      1663
   macro avg       0.99      0.99      0.99      1663
weighted avg       0.99      0.99      0.99      1663<jupyter_text>Hypertuning the parameters using grid search, tune the max_depth and number of estimators<jupyter_code>kf = KFold(n_splits=5)
kf.get_n_splits(X)


param_grid = {"n_estimators": [10,15,20,30],
              "criterion": ['gini'],
             "max_depth": [10,15,20,25],
             "bootstrap": [True],
             "min_samples_leaf": [0.5,1,2]
              
             }

# run grid search
grid_search = GridSearchCV(model, param_grid, cv=5,return_train_score=True)
model,predicted_labels_test,predicted_labels_train,accuracy_test,accuracy_train,sc=modeltraining(grid_search,X_train,X_test,Y_train,Y_test)<jupyter_output>[[-0.1189225   0.73491402  0.39076816 -0.36262002 -0.62058721  1.89123818]
 [ 0.78723151 -0.2849141   0.43531487  0.59052431  0.41333471  1.89123818]
 [ 0.10761601 -0.48887972  0.12348788 -0.03768445  0.2308779  -0.59699686]
 ...
 [ 1.24030852  1.5507765   0.70259515  0.87213514 -1.04631976 -0.59699686]
 [ 1.01377002  1.5507765  -4.37573011 -2.09560971  1.14316194 -0.59699686]
 [ 0.33415451  2.16267337  0.5244083  -0.01602208  0.83906726 -0.59699686]]<jupyter_text>Accuracy of test data after grid search<jupyter_code>accuracy_test
accuracy_train
sc
grid_search.best_estimator_
grid_search.best_params_<jupyter_output><empty_output><jupyter_text>Confusion matrix:<jupyter_code>print(confusion_matrix(Y_test,predicted_labels_test))<jupyter_output>[[182  17]
 [ 47 170]]<jupyter_text>Saving the pickle file of model and scaler<jupyter_code># Dump the trained decision tree classifier with Pickle
rf_classifier_pkl_filename = r'..\..\model\university_random_forest_predict.pickel'
standard_scaler_filename = r'..\..\model\UniversityRFstandardScaler_rf_model.pickel'

random_forest_classifier_model_pkl = open(rf_classifier_pkl_filename, 'wb')
pickle.dump(model, random_forest_classifier_model_pkl)

random_forest_classifier_model_pkl.close()

sc_rf_classifier_scaler_pkl = open(standard_scaler_filename, 'wb')
pickle.dump(sc, sc_rf_classifier_scaler_pkl)

sc_rf_classifier_scaler_pkl.close()

random_forest_classifier_model_pkl = open(rf_classifier_pkl_filename, 'rb')
random_forest_classifier_model= pickle.load(random_forest_classifier_model_pkl)
random_forest_classifier_model_pkl.close()

sc_rf_classifier_scaler_pkl = open(standard_scaler_filename, 'rb')
standard_scaler_rf_classifier= pickle.load(sc_rf_classifier_scaler_pkl)
sc_rf_classifier_scaler_pkl.close()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup