<jupyter_start><jupyter_text>Feature Engineering<jupyter_code>data.isna().sum()
data['quality']<jupyter_output><empty_output><jupyter_text>Feature selection<jupyter_code>data.columns
X=data.drop('quality',axis=1)
y=data['quality']
y.shape
y.head
y.unique()
import seaborn as sns
# finding correlation beteen features
corr=X.corr()
corr.shape
#Plotting Heatmap to find correlation among features
plt.figure(figsize=(9,7))
sns.heatmap(corr,vmin=0,vmax=1,annot=True,fmt=".1f",cbar=True,square=True,cmap='coolwarm')
plt.show()
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,.5)
print(corr_features)
X_new=X.drop(corr_features,axis=1)
X_new
y.shape<jupyter_output><empty_output><jupyter_text>Split the train and test data set<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=.20,random_state=1)<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)
print(predicted_naive)
import sklearn.metrics as metrics
from sklearn.metrics import ConfusionMatrixDisplay
cm_naive=metrics.confusion_matrix(y_test,predicted_naive)
cmd=ConfusionMatrixDisplay(cm_naive,display_labels=['3','4','5','6','7','8','9'])
cmd.plot()
from sklearn.metrics import classification_report
print(classification_report(y_test,predicted_naive))
from sklearn.metrics import roc_curve
naive_probs=naive.predict_proba(X_new_test) 

fpr = {}
tpr = {}
thresh ={}

n_class = 7

for i in range(n_class):    
    fpr[i], tpr[i], thresh[i] = roc_curve(y_test, naive_probs[:,i], pos_label=i+3)
print(fpr[0])
print(tpr[0])
    
# plotting    
plt.plot(fpr[0], tpr[0], linestyle='--',color='orange', label='Class 3 vs Rest')
plt.plot(fpr[1], tpr[1], linestyle='--',color='green', label='Class 4 vs Rest')
plt.plot(fpr[2], tpr[2], linestyle='--',color='blue', label='Class 5 vs Rest')
plt.plot(fpr[3], tpr[3], linestyle='--',color='red', label='Class 6 vs Rest')
plt.plot(fpr[4], tpr[4], linestyle='--',color='yellow', label='Class 7 vs Rest')
plt.plot(fpr[5], tpr[5], linestyle='--',color='pink', label='Class 8 vs Rest')
plt.plot(fpr[6], tpr[6], linestyle='--',color='purple', label='Class 9 vs Rest')

plt.title('Multiclass ROC curve')
accuracy_naive=metrics.accuracy_score(y_test,predicted_naive)
print(accuracy_naive)<jupyter_output>0.4316326530612245<jupyter_text>K-Nearest Neighnour<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)
from sklearn.metrics import confusion_matrix
from sklearn.metrics import ConfusionMatrixDisplay
cm_knn=metrics.confusion_matrix(y_test,predicted_knn)
cmd=ConfusionMatrixDisplay(cm_knn,display_labels=['3','4','5','6','7','8','9'])
cmd.plot()

accuracy_knn=metrics.accuracy_score(y_test,predicted_knn)
print(accuracy_knn)
from sklearn.metrics import roc_curve
knn_probs=knn.predict_proba(X_new_test)
fpr={}
tpr={}
thresh={}
n_classes=7
for i in range(n_classes):
    fpr[i],tpr[i],thresh[i]=roc_curve(y_test,knn_probs[:,i],pos_label=i+3)
## Plotting
plt.plot(fpr[0],tpr[0],linestyle='--',color='green',label='class 3 vs rest')
plt.plot(fpr[1],tpr[1],linestyle='--',color='red',label='class 4 vs rest')
plt.plot(fpr[2],tpr[2],linestyle='--',color='blue',label='class 5 vs rest')
plt.plot(fpr[3],tpr[3],linestyle='--',color='orange',label='class 6 vs rest')
plt.plot(fpr[4],tpr[4],linestyle='--',color='pink',label='class 7 vs rest')
plt.plot(fpr[5],tpr[5],linestyle='--',color='yellow',label='class 8 vs rest')
plt.plot(fpr[6],tpr[6],linestyle='--',color='purple',label='class 9 vs rest')
plt.title('Multiclass ROC curve')<jupyter_output>C:\Users\arif\.conda\envs\ratnabali\lib\site-packages\sklearn\metrics\_ranking.py:951: UndefinedMetricWarning: No positive samples in y_true, true positive value should be meaningless
  UndefinedMetricWarning)<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)
from sklearn.metrics import confusion_matrix
from sklearn.metrics import ConfusionMatrixDisplay
cm_dtree=metrics.confusion_matrix(y_test,predicted_dtree)
cmd=metrics.ConfusionMatrixDisplay(cm_dtree,display_labels=['3','4','5','6','7','8','9'])
cmd.plot()
from sklearn.metrics import classification_report
print(classification_report(y_test,predicted_dtree))
accuracy_dtree=metrics.accuracy_score(y_test,predicted_dtree)
print(accuracy_dtree)
from sklearn.metrics import roc_curve
fpr={}
tpr={}
thresh={}
dtree_probs=dtree.predict_proba(X_new_test)

n_class=7
for i in range(n_class):
    fpr[i],tpr[i],thresh[i]=roc_curve(y_test,dtree_probs[:,i],pos_label=i+3)
 # Plotting
plt.plot(fpr[0],tpr[0],linestyle='--',color='green',label='class 3 vs rest')
plt.plot(fpr[1],tpr[1],linestyle='--',color='red',label='class 4 vs rest')
plt.plot(fpr[2],tpr[2],linestyle='--',color='blue',label='class 5 vs rest')
plt.plot(fpr[3],tpr[3],linestyle='--',color='orange',label='class 6 vs rest')
plt.plot(fpr[4],tpr[4],linestyle='--',color='pink',label='class 7 vs rest')
plt.plot(fpr[5],tpr[5],linestyle='--',color='yellow',label='class 8 vs rest')
plt.plot(fpr[6],tpr[6],linestyle='--',color='purple',label='class 9 vs rest')
plt.title('Multiclass ROC curve')<jupyter_output>C:\Users\arif\.conda\envs\ratnabali\lib\site-packages\sklearn\metrics\_ranking.py:951: UndefinedMetricWarning: No positive samples in y_true, true positive value should be meaningless
  UndefinedMetricWarning)<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=metrics.ConfusionMatrixDisplay(cm_rf,display_labels=['3','4','5','6','7','8','9'])
cmd.plot()
accuracy_rf=metrics.accuracy_score(y_test,predicted_rf)
print(accuracy_rf)
from sklearn.metrics import classification_report
print(classification_report(y_test,predicted_rf))
from sklearn.metrics import roc_curve
fpr={}
tpr={}
thresh={}
n_class=7
rf_probs=rf.predict_proba(X_new_test)
for i in range(n_class):
    fpr[i],tpr[i],thresh[i]=roc_curve(y_test,rf_probs[:,i],pos_label=i+3)
#Plotting
plt.plot(fpr[0],tpr[0],linestyle='--',color='green',label='class 3 vs rest')
plt.plot(fpr[1],tpr[1],linestyle='--',color='red',label='class 4 vs rest')
plt.plot(fpr[2],tpr[2],linestyle='--',color='blue',label='class 5 vs rest')
plt.plot(fpr[3],tpr[3],linestyle='--',color='orange',label='class 6 vs rest')
plt.plot(fpr[4],tpr[4],linestyle='--',color='pink',label='class 7 vs rest')
plt.plot(fpr[5],tpr[5],linestyle='--',color='yellow',label='class 8 vs rest')
plt.plot(fpr[6],tpr[6],linestyle='--',color='purple',label='class 9 vs rest')
plt.title('Multiclass ROC curve')   



results = pd.DataFrame({
    'Model': ['Navies Bayes','KNN' ,'Decision Tree','Random forest'],
    'Score': [.431632,.43877,.6030,.660204]})

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.17326667543744245, 'fraction_numerical': 0.023286409682936455, 'mean_word_length': 9.23149394347241, 'pattern_counts': {'":': 0, '<': 26, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '252409', 'license': 'MIT', 'n_tokens_mistral': 3074, 'n_tokens_neox': 2914, 'n_words': 414, 'path': 'code/WineQuality_v1.ipynb', 'repo_name': 'Ratnabali-Pal/Machine-Learning-Assignments', 'size': '7601'}