image_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score

from sklearn.metrics import confusion_matrix, precision_score, plot_confusion_matrix

from sklearn.metrics import precision_recall_curve, plot_precision_recall_curve

from sklearn.preprocessing import LabelEncoder

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

data = pd.read_csv('/kaggle/input/factors-affecting-campus-placement/Placement_Data_Full_Class.csv')
data

data.isna().sum()

data.drop(['salary', 'sl_no'], axis=1, inplace=True)
data.isna().sum()

data.nunique()

corr = data.corr()
corr.style.background_gradient(cmap='coolwarm').set_precision(2)

unique_vals = data.nunique()
col_log = data.columns
for i in range(0, len(unique_vals)):
    coln = str(col_log[i])
    if int(unique_vals[i]) < 5 and coln != 'status':
        data = pd.concat([data.drop(coln, axis=1), pd.get_dummies(data[coln], prefix=coln)], axis=1)

data_y = pd.DataFrame(data['status'])
data_x = data.drop('status', axis=1)
status_encoder = LabelEncoder()
data_y = status_encoder.fit_transform(data_y)

best_score = -1
best_estimators = 0
for i in range(10, 250):
    model = RandomForestClassifier(n_estimators=i, random_state=0)
    model.fit(train_x, train_y)
    pred = model.predict(test_x)
    score = accuracy_score(pred, test_y)
    if score > best_score:
        best_score = score
        best_estimators = i

model = RandomForestClassifier(n_estimators=best_estimators, random_state=0)

model.fit(train_x, train_y)

pred = model.predict(test_x)
score = accuracy_score(pred, test_y)

from sklearn.metrics import confusion_matrix, precision_score, plot_confusion_matrix
import matplotlib.pyplot as plt

from sklearn.metrics import precision_recall_curve, plot_precision_recall_curve
y_pred_prob = model.predict_proba(val_x)[:, 1]
precision, recall, thresholds = precision_recall_curve(val_y, y_pred_prob)

df = pd.DataFrame(data={'Precision': precision[:-1], 'Recall': recall[:-1], 'Thresholds': thresholds})
df

targets = df.loc[(df['Precision'] >= 1) & (df['Thresholds'] != 1)]
targets

best = -1
thresh_best = -1
y_test_prob = model.predict_proba(test_x)[:, 1]
for target in targets.to_numpy():
    true_prediction = (y_test_prob > target[2]).astype(int)
    score = precision_score(test_y, true_prediction)
    if score > best:
        best = score
        thresh_best = target[2]
    print('Score for threshold %f: %f' % (target[2], score * 100))
print('Best precision score of %f achieved with threshold %f.' % (best, thresh_best))