Guest User

Untitled

a guest
Oct 20th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. def to_categorical(y, num_classes=None):
  2. """Converts a class vector (integers) to binary class matrix.
  3. E.g. for use with categorical_crossentropy.
  4. # Arguments
  5. y: class vector to be converted into a matrix
  6. (integers from 0 to num_classes).
  7. num_classes: total number of classes.
  8. # Returns
  9. A binary matrix representation of the input.
  10. """
  11. y = np.array(y, dtype='int').ravel()
  12. if not num_classes:
  13. num_classes = np.max(y) + 1
  14. n = y.shape[0]
  15. categorical = np.zeros((n, num_classes))
  16. categorical[np.arange(n), y] = 1
  17. return categorical
  18.  
  19.  
  20. def normalize(x, axis=-1, order=2):
  21. """Normalizes a Numpy array.
  22. # Arguments
  23. x: Numpy array to normalize.
  24. axis: axis along which to normalize.
  25. order: Normalization order (e.g. 2 for L2 norm).
  26. # Returns
  27. A normalized copy of the array.
  28. """
  29. l2 = np.atleast_1d(np.linalg.norm(x, order, axis))
  30. l2[l2 == 0] = 1
  31. return x / np.expand_dims(l2, axis)
Add Comment
Please, Sign In to add comment