MATLAB Answers

sir, can you please help me to convert this python code into matlab

7 views (last 30 days)
#false positive rate,fpr= FP/(TN+FP) OR fpr=1-specificty, tpr=sensitivity
y_pred_knn_p =knn.predict_proba(X_test)[:,1]
models=[y_pred_knn_p]
label=['KNN']
# plotting ROC curves
plt.figure(figsize=(10, 8))
m=np.arange(1)
for m in m:
fpr, tpr,thresholds= metrics.roc_curve(y_test,models[m])
print('model:',label[m])
print('thresholds:',np.round(thresholds,3))
print('tpr: ',np.round(tpr,3))
print('fpr: ',np.round(fpr,3))
plt.plot(fpr,tpr,label=label[m])
plt.xlim([0.0,1.0])
plt.ylim([0.0,1.0])
plt.title('ROC curve for Cancer classifer')
plt.xlabel('False positive rate (1-specificity)')
plt.ylabel('True positive rate (sensitivity)')
plt.legend(loc=4,)

  0 Comments

Sign in to comment.

Accepted Answer

ANKUR KUMAR
ANKUR KUMAR on 28 Sep 2018
Edited: ANKUR KUMAR on 30 Sep 2018
Commented lines are the python code and uncommented lines are the matlab codes. There are few python functions which you have use like predict_proba and roc_curve for which you have to write these function in matlab.
% y_pred_knn_p =knn.predict_proba(X_test)[:,1]
y_pred_knn_p= predict_proba(X_test); y_pred_knn_p=y_pred_knn_p(:,2); % make sure that you must have predict_proba function. python reads first row or column as 0 but matlab starts with 1.
% models=[y_pred_knn_p]
models=[y_pred_knn_p];
% label=['KNN']
label={'KNN'}
% % # plotting ROC curves
% plt.figure(figsize=(10, 8))
figure()
% m=np.arange(1)
m=1;
% for m in m:
for mm=1:length(m)
mmm=m(mm);
% fpr, tpr,thresholds= metrics.roc_curve(y_test,models[mmm])
[fpr, tpr,thresholds]= roc_curve(y_test,models{mmm}) %if models is in cells then use models{m} otherwise model(:,mmm) can also be use as per your need.
% print('model:',label[mmm])
disp(strcat('model:',label{mmm})) % one can use sprintf to the same
% print('thresholds:',np.round(thresholds,3))
disp(strcat('thresholds:',num2str(round(thresholds,3))))
% print('tpr: ',np.round(tpr,3))
% print('fpr: ',np.round(fpr,3))
% plt.plot(fpr,tpr,label=label[m])
plot(fpr,tpr)
leg=legend(label{mmm})
% plt.xlim([0.0,1.0])
xlim([0 1])
% plt.ylim([0.0,1.0])
ylim([0 1])
% plt.title('ROC curve for Cancer classifer')
title('ROC curve for Cancer classifer')
% plt.xlabel('False positive rate (1-specificity)')
xlabel('False positive rate (1-specificity)')
% plt.ylabel('True positive rate (sensitivity)')
ylabel('True positive rate (sensitivity)')
% plt.legend(loc=4,)
leg.Location='southeast';
end
Hope this helps you. If you are still facing problem, then comeup with some maltab code so that you can get help a bit more effectively.

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!