Advertisement
Guest User

Untitled

a guest
May 17th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. for i in range(4):
  2. y_train= y[:,i]
  3. print('Train subject %d, class %s' % (subject, cols[i]))
  4. lr.fit(X_train[::sample,:],y_train[::sample])
  5. pred[:,i] = lr.predict_proba(X_test)[:,1]
  6.  
  7. prediction = lr.predict(X_train)
  8.  
  9. print(confusion_matrix(y_train, prediction))
  10.  
  11. import numpy as np
  12.  
  13.  
  14. def plot_confusion_matrix(cm,
  15. target_names,
  16. title='Confusion matrix',
  17. cmap=None,
  18. normalize=True):
  19. """
  20. given a sklearn confusion matrix (cm), make a nice plot
  21.  
  22. Arguments
  23. ---------
  24. cm: confusion matrix from sklearn.metrics.confusion_matrix
  25.  
  26. target_names: given classification classes such as [0, 1, 2]
  27. the class names, for example: ['high', 'medium', 'low']
  28.  
  29. title: the text to display at the top of the matrix
  30.  
  31. cmap: the gradient of the values displayed from matplotlib.pyplot.cm
  32. see http://matplotlib.org/examples/color/colormaps_reference.html
  33. plt.get_cmap('jet') or plt.cm.Blues
  34.  
  35. normalize: If False, plot the raw numbers
  36. If True, plot the proportions
  37.  
  38. Usage
  39. -----
  40. plot_confusion_matrix(cm = cm, # confusion matrix created by
  41. # sklearn.metrics.confusion_matrix
  42. normalize = True, # show proportions
  43. target_names = y_labels_vals, # list of names of the classes
  44. title = best_estimator_name) # title of graph
  45.  
  46. Citiation
  47. ---------
  48. http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
  49.  
  50. """
  51. import matplotlib.pyplot as plt
  52. import numpy as np
  53. import itertools
  54.  
  55. accuracy = np.trace(cm) / float(np.sum(cm))
  56. misclass = 1 - accuracy
  57.  
  58. if cmap is None:
  59. cmap = plt.get_cmap('Blues')
  60.  
  61. plt.figure(figsize=(8, 6))
  62. plt.imshow(cm, interpolation='nearest', cmap=cmap)
  63. plt.title(title)
  64. plt.colorbar()
  65.  
  66. if target_names is not None:
  67. tick_marks = np.arange(len(target_names))
  68. plt.xticks(tick_marks, target_names, rotation=45)
  69. plt.yticks(tick_marks, target_names)
  70.  
  71. if normalize:
  72. cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
  73.  
  74.  
  75. thresh = cm.max() / 1.5 if normalize else cm.max() / 2
  76. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
  77. if normalize:
  78. plt.text(j, i, "{:0.4f}".format(cm[i, j]),
  79. horizontalalignment="center",
  80. color="white" if cm[i, j] > thresh else "black")
  81. else:
  82. plt.text(j, i, "{:,}".format(cm[i, j]),
  83. horizontalalignment="center",
  84. color="white" if cm[i, j] > thresh else "black")
  85.  
  86.  
  87. plt.tight_layout()
  88. plt.ylabel('True label')
  89. plt.xlabel('Predicted labelnaccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
  90. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement