10 examples of 'onehotencoder' in Python

Every line of 'onehotencoder' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
29def one_hot_encoder(a):
30 length = len(a)
31 b = np.zeros( (length, 10) )
32 b[np.arange(length), a] = 1
33 return b
15def encode_one_hot(s):
16 all = []
17 for c in s:
18 if c not in characters:
19 continue
20 x = np.zeros((INPUT_VOCAB_SIZE))
21 index = char_indices[c]
22 x[index] = 1
23 all.append(x)
24 return all
26def one_hot_encode(X, K):
27 # input is N x D
28 # output is N x D x K
29 N, D = X.shape
30 Y = np.zeros((N, D, K))
31 for n, d in zip(*X.nonzero()):
32 # 0.5...5 --> 1..10 --> 0..9
33 k = int(X[n,d]*2 - 1)
34 Y[n,d,k] = 1
35 return Y
26def onehot(self,arr):
27 n, w, h = arr.shape
28 arr = arr.reshape(n, -1)
29 arr = self.onehotencoder.fit_transform(arr)
30 arr = arr.reshape(n, w, h, self.k)
31 arr = arr.transpose(0, 3, 1, 2)
32 return arr
259def encode(self, label_str, on_value=1, off_value=0): # pylint: disable=arguments-differ
260 e = np.full(self.vocab_size, off_value, dtype=np.int32)
261 e[self._class_labels.index(label_str)] = on_value
262 return e.tolist()
22def decode_one_hot(x):
23 s = []
24 for onehot in x:
25 one_index = np.argmax(onehot)
26 s.append(indices_char[one_index])
27 return ''.join(s)
4def one_hot_encode_y(y, n_classes=135):
5 y = np.asarray(y)
6 if n_classes == 1:
7 return reshape_add_axis(y, len(y.shape)-1)
8 else:
9 from keras.utils import to_categorical
10 shape = y.shape
11 y = to_categorical(y, num_classes=n_classes).astype(np.uint8)
12 y = y.reshape(shape + (n_classes,))
13 return y
55def one_hot_class(inp, n_class):
56 """The function to make the input to n-class one-hot vectors.
57 Args:
58 inp: the input numpy array.
59 n_class: the number of classes.
60 Return:
61 The reorganized n-class one-shot array.
62 """
63 n_sample = inp.shape[0]
64 out = np.zeros((n_sample, n_class))
65 for idx in range(n_sample):
66 out[idx, inp[idx]] = 1
67 return out
21def one_hot(*,
22 index: Union[None, int, Sequence[int]] = None,
23 shape: Union[int, Sequence[int]],
24 value: Any = 1,
25 dtype: Type[np.number]) -> np.ndarray:
26 """Returns a numpy array with all 0s and a single non-zero entry(default 1).
27
28 Args:
29 index: The index that should store the `value` argument instead of 0.
30 If not specified, defaults to the start of the array.
31 shape: The shape of the array.
32 value: The hot value to place at `index` in the result.
33 dtype: The dtype of the array.
34
35 Returns:
36 The created numpy array.
37 """
38 if index is None:
39 index = 0 if isinstance(shape, int) else (0,) * len(shape)
40 result = np.zeros(shape=shape, dtype=dtype)
41 result[index] = value
42 return result
2317def one_hot_encoding(labels, num_classes=None):
2318 """One-hot encodes the multiclass labels.
2319
2320 Example usage:
2321 labels = tf.constant([1, 4], dtype=tf.int32)
2322 one_hot = OneHotEncoding(labels, num_classes=5)
2323 one_hot.eval() # evaluates to [0, 1, 0, 0, 1]
2324
2325 Args:
2326 labels: A tensor of shape [None] corresponding to the labels.
2327 num_classes: Number of classes in the dataset.
2328 Returns:
2329 onehot_labels: a tensor of shape [num_classes] corresponding to the one hot
2330 encoding of the labels.
2331 Raises:
2332 ValueError: if num_classes is not specified.
2333 """
2334 with tf.name_scope('OneHotEncoding', values=[labels]):
2335 if num_classes is None:
2336 raise ValueError('num_classes must be specified')
2337
2338 labels = tf.one_hot(labels, num_classes, 1, 0)
2339 return tf.reduce_max(labels, 0)

Related snippets