in Education by
I want to encode a 1-D numpy array: x = array([1,0,3]) As a 2-D 1-hot array y = array([[0,1,0,0], [1,0,0,0], [0,0,0,1]]) Suggest me some faster technique other than looping. Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
@Anisha, Refer to the following code it may help. >>> x = [1, 0, 3] >>> z = np.max(x) + 1 >>> np.eye(z)[x] array([[ 0., 1., 0., 0.], [ 1., 0., 0., 0.], [ 0., 0., 0., 1.]]) Alernatively, you can use: >>> x = np.array([1, 0, 3]) >>> z= np.zeros((3, 4)) >>> z[np.arange(3), x] = 1 >>> y array([[ 0., 1., 0., 0.], [ 1., 0., 0., 0.], [ 0., 0., 0., 1.]])

Related questions

0 votes
    What exactly is the difference between groupby("x").count and groupby("x").size in Pandas? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I wish to divide pandas dataframe to 3 separate sets. I know by using train_test_split from sklearn.cross_validation, ... ? kindly help Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    What is the procedure to find the indices of an array on NumPy where some condition is true?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    I have a dataset that only contains y-values in one column. I want to insert a column of x- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have the 4D array, which is defined as follows: B = np.array( [[[[0.5000, 0.5625], [0.5000, ... loops and ideally one function call. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    How do you think I can get the intersection of the below-given array? a=np.array([[[[0,0],[0,1]],[[1,1],[1,1]]] ... , [[0, 1], [1, 1]]] Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have a numpy array (dtype bool) representing an array of bits. For example, the array np.array( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have a variable of z storing features of length and height of image files where z is z = [ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have a variable of z storing features of length and height of image files where z is z = [ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    When I try to generate a model with acumos I get this error : File "/Users/fredericchantrel/.pyenv/ ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    In [31]: print(np.poly1d((3,2))) 3 x + 2 In [32]: a = np.array(( np.poly1d( ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I need to encode a signed integer as hexadecimal using via the two's complement notation. For example I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    Is there a built-in method for converting a date to a datetime in Python, for example getting the datetime for the ... .month, d.day)? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Imagine that you have: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] What is the simplest way ... 42, 'food' : 'spam'} Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to convert the bytes value back to string? I have used binascii.b2a_qp() method, ... doing it manually. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
...