<jupyter_start><jupyter_text>Confusion Matrix<jupyter_code>from sklearn.metrics import classification_report, accuracy_score,confusion_matrix,roc_curve,plot_roc_curve
cm = confusion_matrix(ytest,pred)
cm
print(classification_report(ytest,pred))
clf.score(xtest,ytest)
accuracy1 = accuracy_score(ytest,pred) # pred is predicted y ; ytest is actual
print('Acc for Decision Tree is',accuracy1)
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted Values')
plt.ylabel('Actual Values')<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rclf = RandomForestClassifier()
rclf.fit(xtrain,ytrain)
pred2 = rclf.predict(xtest)
pred2
cm2 = confusion_matrix(ytest,pred2)
cm2
rclf.score(xtest,ytest)<jupyter_output><empty_output><jupyter_text>Hyper Parameter Tuning / Optimization  Randomized Search CV<jupyter_code>from sklearn.model_selection import RandomizedSearchCV
#Update Parameters

n_estimators = [int(x) for x in np.linspace(start=100, stop=500, num=15)]
n_estimators
np.random.randint(100,500,15)
#Update Parameters

n_estimators = [int(x) for x in np.linspace(start=100, stop=500, num=15)]
max_features = ['auto','sqrt','log2']
max_depth = [int(x) for x in np.linspace(start=10, stop=100, num=15)]
min_samples_split = [2,4,5,6,7,9,10]
min_samples_leaf = [2,3,5,6,7,9,12]
criterion = ['entropy','gini']

grids = {
    'n_estimators' : n_estimators,
    'max_features' : max_features,
    'max_depth' : max_depth,
    'min_samples_split' : min_samples_split,
    'min_samples_leaf' : min_samples_leaf,
    'criterion' :  criterion
}

print(grids)
rnf = RandomForestClassifier()
rmcv = RandomizedSearchCV(rnf,grids,n_iter=200,cv=3)
rmcv.fit(xtrain,ytrain)
rmcv.cv_results_
result = pd.DataFrame(rmcv.cv_results_)
result
rmcv.best_score_
rmcv.best_params_
rfc = rmcv.best_estimator_
rfc.score(xtest,ytest)
rfc.score(x,y)<jupyter_output><empty_output><jupyter_text>XGBoost Classifier<jupyter_code>!pip install xgboost
import xgboost
from xgboost import XGBClassifier
xgb = XGBClassifier()
xgb.fit(xtrain,ytrain)
xgb.score(xtest,ytest)
xgb.score(x,y)
rclf.score(x,y)
clf.score(x,y)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1239060340856748, 'fraction_numerical': 0.027176416397973285, 'mean_word_length': 8.610619469026549, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '507806', 'license': 'MIT', 'n_tokens_mistral': 904, 'n_tokens_neox': 867, 'n_words': 137, 'path': 'Class-10/Class - 10.ipynb', 'repo_name': 'Anikcb/Machine_Learning_Daffodil', 'size': '2171'}