<jupyter_start><jupyter_text>Feature Engineering<jupyter_code>read_file.isna().sum()
read_file['Class']<jupyter_output><empty_output><jupyter_text>Feature Selection<jupyter_code>read_file.columns
X=read_file.drop('Class',axis=1)
y=read_file['Class']
y.shape
y.head
import seaborn as sns
# finding correlation between features
corr=X.corr()
corr.shape
#Plotting heatmap to find correlation between features
plt.figure(figsize=(12,5))
sns.heatmap(corr,vmin=0,vmax=1,annot=True,fmt=".1f",cmap='coolwarm',cbar=True,square= True)
plt.show()
# with the following features we can find highly correlated features
def correlation(dataset,threshold):
    col_corr=set()
    corr=dataset.corr()
    for i in range(len(corr.columns)):
        for j in range(i):
            if abs(corr.iloc[i,j])>threshold:
                colname=corr.columns[i]
                col_corr.add(colname)
    return col_corr
corr_features=correlation(X,0.2)
print(corr_features)
X_new=X.drop(corr_features,axis=1)
X_new<jupyter_output><empty_output><jupyter_text>Splitting Training and Test data<jupyter_code>from sklearn.model_selection import train_test_split
X_new_train,X_new_test,y_train,y_test=train_test_split(X_new,y,test_size=.30,random_state=1)
y_test.shape<jupyter_output><empty_output><jupyter_text>Naive Bayes<jupyter_code>from sklearn.naive_bayes import GaussianNB
naive=GaussianNB()
naive.fit(X_new_train,y_train)
predicted_naive=naive.predict(X_new_test)
import sklearn.metrics as metrics
cm_naive=metrics.confusion_matrix(y_test,predicted_naive)
accuracy_naive=metrics.accuracy_score(y_test,predicted_naive)
print(accuracy_naive)

print(cm_naive)
from sklearn.metrics import ConfusionMatrixDisplay
cmd=ConfusionMatrixDisplay(cm_naive,display_labels=['0','1'])
cmd.plot()
from sklearn.metrics import classification_report
print(classification_report(y_test,predicted_naive))
naive_probs=naive.predict_proba(X_new_test)
naive_probs=naive_probs[:,1]

from sklearn.metrics import roc_curve
naive_fpr,naive_tpr,_=roc_curve(y_test,naive_probs)
plt.plot(naive_fpr,naive_tpr,marker='.',label='Naive Bayes')
plt.xlabel('False Positive rate')
plt.ylabel('True positive rate')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>K-nearest Classifier<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn=KNeighborsClassifier()
knn.fit(X_new_train,y_train)
predicted_knn=knn.predict(X_new_test)
print(predicted_knn)
import sklearn.metrics as metrics
from  sklearn.metrics  import ConfusionMatrixDisplay
knn_cm=metrics.confusion_matrix(y_test,predicted_knn)
cmd=ConfusionMatrixDisplay(knn_cm,display_labels=['0','1'])
cmd.plot()


knn_accuracy=metrics.accuracy_score(y_test,predicted_knn)
print(knn_accuracy)
from sklearn.metrics import roc_curve
knn_probs=knn.predict_proba(X_new_test)
knn_probs=knn_probs[:,1]
knn_fpr,knn_tpr,_=roc_curve(y_test,knn_probs)
plt.plot(knn_fpr,knn_tpr,marker='.',label='K Nearest Neighbor')
plt.xlabel('False Positive only')
plt.ylabel('True Positive only')
plt.legend()
plt.show()

from sklearn.metrics import classification_report
print(classification_report(y_test,predicted_knn))<jupyter_output>precision    recall  f1-score   support

           0       0.86      0.81      0.84       235
           1       0.77      0.83      0.80       177

    accuracy                           0.82       412
   macro avg       0.81      0.82      0.82       412
weighted avg       0.82      0.82      0.82       412<jupyter_text>Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
lr=LogisticRegression()
lr.fit(X_new_train,y_train)
predicted_lr=lr.predict(X_new_test)
print(predicted_lr)
import sklearn.metrics as metrics
from sklearn.metrics import ConfusionMatrixDisplay
lr_cm=metrics.confusion_matrix(y_test,predicted_lr)
cmd=ConfusionMatrixDisplay(lr_cm,display_labels=['0','1'])
cmd.plot()

from sklearn.metrics import classification_report
print(classification_report(y_test,predicted_lr))
from sklearn.metrics import roc_curve
lr_probs=lr.predict_proba(X_new_test)
lr_probs=lr_probs[:,1]
lr_fpr,lr_tpr,_=roc_curve(y_test,lr_probs)
plt.plot(lr_fpr,lr_tpr,marker='.',label='Logistic Regression')
plt.xlabel('False Positive rate')
plt.ylabel('True positive only')
plt.legend()
plt.show()
lr_accuracy=metrics.accuracy_score(y_test,predicted_lr)
print(lr_accuracy)<jupyter_output>0.8422330097087378<jupyter_text>SVM classifier<jupyter_code>from sklearn import svm
svm=svm.SVC(kernel='linear',probability=True)
svm.fit(X_new_train,y_train)

predicted_svm=svm.predict(X_new_test)
print(predicted_svm)
from  sklearn.metrics import confusion_matrix
from sklearn.metrics import ConfusionMatrixDisplay
cm_svm=metrics.confusion_matrix(y_test,predicted_svm)
cmd=ConfusionMatrixDisplay(cm_svm,display_labels=['0','1'])
cmd.plot()

accuracy_svm=metrics.accuracy_score(y_test,predicted_svm)
print(accuracy_svm)
from sklearn.metrics import classification_report
print(classification_report(y_test,predicted_svm))
from sklearn.metrics import roc_curve
svm_probs=svm.predict_proba(X_new_test)
svm_probs=svm_probs[:,1]
svm_fpr,svm_tpr,_=roc_curve(y_test,svm_probs)
plt.plot(svm_fpr,svm_tpr,marker='.',label='Suport vector machine')
plt.xlabel('False positive only')
plt.ylabel('True Positive only')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>from sklearn.tree import DecisionTreeClassifier
dtree=DecisionTreeClassifier()
dtree.fit(X_new_train,y_train)
predicted_dtree=dtree.predict(X_new_test)
import sklearn.metrics as metrics
from sklearn.metrics import ConfusionMatrixDisplay
cm_dtree=metrics.confusion_matrix(y_test,predicted_dtree)
cmd=ConfusionMatrixDisplay(cm_dtree,display_labels=['0','1'])
cmd.plot()

accuracy_dtree=metrics.accuracy_score(y_test,predicted_dtree)
print(accuracy_dtree)
from sklearn.metrics import classification_report
print(classification_report(y_test,predicted_dtree))
y_test.shape
from sklearn.metrics import roc_curve
dtree_probs=dtree.predict_proba(X_new_test)
dtree_probs=dtree_probs[:,1]
dtree_fpr,dtree_tpr,_=roc_curve(y_test,dtree_probs)
plt.plot(dtree_fpr,dtree_tpr,marker='.',label='Decision Tree')
plt.xlabel('false positive rate')
plt.ylabel('True positive rate')
plt.plot()
plt.show()<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rf=RandomForestClassifier()
rf.fit(X_new_train,y_train)
predicted_rf=rf.predict(X_new_test)
import sklearn.metrics as metrics
from sklearn.metrics import confusion_matrix
cm_rf=metrics.confusion_matrix(y_test,predicted_rf)
cmd=ConfusionMatrixDisplay(cm_rf,display_labels=['0','1'])
cmd.plot()
accuracy_score=metrics.accuracy_score(y_test,predicted_rf)
print(accuracy_score)
from sklearn.metrics import classification_report
print(classification_report(y_test,predicted_rf))
from sklearn.metrics import roc_curve
rf_probs=rf.predict_proba(X_new_test)
rf_probs=rf_probs[:,1]
rf_fpr,rf_tpr,_=roc_curve(y_test,rf_probs)
plt.plot(rf_fpr,rf_tpr,marker='.',label='Random Forest')
plt.xlabel('False positive only')
plt.ylabel('True Positive only')
plt.legend()
plt.show()
results = pd.DataFrame({
    'Model': ['Navies Bayes','KNN', 'Logistic Regression','SVM Classifier' ,'Decision Tree','Random forest'],
    'Score': [0.8422,0.82,0.8422,0.847,0.80,.80]})

result_df = results.sort_values(by='Score', ascending=False)
result_df = result_df.set_index('Score')
result_df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11290322580645161, 'fraction_numerical': 0.01706989247311828, 'mean_word_length': 8.576576576576576, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '204630', 'license': 'MIT', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2766, 'n_words': 376, 'path': 'code/Banknote_authentication.ipynb', 'repo_name': 'Ratnabali-Pal/Machine-Learning-Assignments', 'size': '7440'}