Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have an array of the shape (100000, 1) with each element in an array of type positive integer and not greater than 6.

My goal is to convert each element into 1's and place these 1's in the new matrix of shape (100000, 6).

For example,

Input

X = np.array([[6],

              [2],

              [1],

              ...,

              [5],

              [4],

              [3]])

# shape of X is (100000, 1)

Output

Y = np.array([[1, 1, 1, 1, 1, 1],

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

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

              [       ...      ],

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

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

              [1, 1, 1, 0, 0, 0]])

# shape of Y is (100000, 6)

Is there any method that can achieve this without looping? Any help would be appreciated.

1 Answer

0 votes
by (36.8k points)

One way to achive is by using the numpy.flip with cumsum:

max_ = 6

np.flip(np.flip(np.eye(max_)[X.ravel()-1], 1).cumsum(1), 1)

Output:

array([[1., 1., 1., 1., 1., 1.],

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

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

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

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

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

Benchmark with the value 100k:

x_large = np.random.randint(1, 7, 100000)

max_ = 6

%timeit np.flip(np.flip(np.eye(max_)[x_large.ravel()-1], 1).cumsum(1), 1)

# 6.71 ms ± 68.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

 If you are a beginner and want to know more about Python the do check out the data science with python course

Browse Categories

...