0

I'm trying:

target = keras.utils.to_categorical([0], num_classes)

This is giving me:

[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

What I want to do, however, is create something like:

[
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.],
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.],
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
...
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.],
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
]

where it will have 10,000 rows.

4 Answers 4

3

Using the function numpy.repeat should solve the problem:

target = np.array(target)
numpy.repeat(target , 10000, axis=0)

you specify the array, number of times you want to repeat each axis, and the axis.

Sign up to request clarification or add additional context in comments.

1 Comment

God, I missed this axis parameter, That's why I had to use tile and reshape
2
import keras
import numpy as np
num_classes = 10
num_rows = 10000

target = keras.utils.to_categorical(np.random.choice(num_classes,num_rows), num_classes)

Comments

1

Use np.tile and reshape. In the answer below, use n=10000 to get your desired answer

target = np.array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
n = 2
target_new = np.tile(target, 2).reshape(n, len(target[0]))

# array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
#        [1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])

Comments

1
Go with basic 

   import numpy as np
   np.arange(100).reshape(5,2,10)
   # output:
   array([[[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]],

       [[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]],

       [[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
        [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]],

       [[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
        [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]],

       [[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
        [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]])

run it.

you may add random, ones, zeros, empty etc it's on you how you wanna go 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.