<jupyter_start><jupyter_text>Training the Model<jupyter_code>from sklearn.svm import SVC
model=SVC(C=1.0,kernel='rbf',gamma=0.01,probability=True)
model.fit(x_train,y_train)
print('Trained Succesfully')
model.score(x_train,y_train)
model.score(x_test,y_test)<jupyter_output><empty_output><jupyter_text>Model Evaluation-Confusion Matrix-Classification Report-Kappa Score-ROC and AUC<jupyter_code>from sklearn import metrics
y_pred=model.predict(x_test)
y_prob=model.predict_proba(x_test)
cm=metrics.confusion_matrix(y_test,y_pred)

cm=np.concatenate((cm,cm.sum(axis=0).reshape(1,-1)),axis=0)
cm=np.concatenate((cm,cm.sum(axis=1).reshape(-1,1)),axis=1)
plt.imshow(cm)
for i in range(3):
    for j in range(3):
        plt.text(i,j,'%d'%cm[i,j])
plt.xticks([0,1])
plt.yticks([0,1])
plt.xlabel('Predicted Values')
plt.ylabel('True Values')


cr=metrics.classification_report(y_test,y_pred,target_names=['Male','Female'],output_dict=True)
df=pd.DataFrame(cr).T
df
#kappa should be more than 60%
metrics.cohen_kappa_score(y_test,y_pred)
#roc and auc
plt.figure(figsize=(10,6))
fpr,tpr,thresh=metrics.roc_curve(y_test,y_prob[:,1])
plt.plot(fpr,tpr,'--')
plt.plot([0,1],[0,1],'b--')
for i in range(0,len(thresh),20):
    plt.plot(fpr[i],tpr[i],'^')
    plt.text(fpr[i],tpr[i],'%0.2f'%thresh[i])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')<jupyter_output><empty_output><jupyter_text>Hyper Parameter Tuning<jupyter_code>model_tune=SVC()
from sklearn.model_selection import GridSearchCV
param_grid={'C':[1,10,20,30,50,100],'kernel':['rbf','poly'],'gamma':[0.1,0.05,0.01,0.001,0.002,0.005],'coef0':[0,1]}
model_grid=GridSearchCV(model_tune,param_grid=param_grid,scoring='accuracy',cv=5,verbose=1)
model_grid.fit(X,y)
model_grid.best_estimator_
model_grid.best_params_
model_grid.best_score_
#with best parameter build the Ml Model
model_best=SVC(C=1,kernel='rbf',gamma=0.05,probability=True)
model_best.fit(x_train,y_train)
model_best.score(x_test,y_test)
y_pred=model_best.predict(x_test)
y_prob=model_best.predict_proba(x_test)
cm=metrics.confusion_matrix(y_test,y_pred)

cm=np.concatenate((cm,cm.sum(axis=0).reshape(1,-1)),axis=0)
cm=np.concatenate((cm,cm.sum(axis=1).reshape(-1,1)),axis=1)
plt.imshow(cm)
for i in range(3):
    for j in range(3):
        plt.text(i,j,'%d'%cm[i,j])
plt.xticks([0,1])
plt.yticks([0,1])
plt.xlabel('Predicted Values')
plt.ylabel('True Values')

cr=metrics.classification_report(y_test,y_pred,target_names=['Male','Female'],output_dict=True)
df=pd.DataFrame(cr).T
df
#roc and auc
plt.figure(figsize=(10,6))
fpr,tpr,thresh=metrics.roc_curve(y_test,y_prob[:,1])
plt.plot(fpr,tpr,'--')
plt.plot([0,1],[0,1],'b--')
for i in range(0,len(thresh),20):
    plt.plot(fpr[i],tpr[i],'^')
    plt.text(fpr[i],tpr[i],'%0.2f'%thresh[i])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
#kappa should be more than 60%
metrics.cohen_kappa_score(y_test,y_pred)
#save machine learning model
import pickle

pickle.dump(model_best,open('D:/data science/Module-2/data/model_svm.pickle','wb'))
pickle.dump(mean,open('D:/data science/Module-2/data/mean_preprocess.pickle','wb'))<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup