2

I am new to python, am not aware of data types.

I want the output to be in the form

[[ 0.3120883 ]
 [ 0.36910208]
 [ 0.99886361]
 ..., 
 [-0.10729821]
 [ 0.08311962]
 [ 1.67302086]]

But currently my output is the form

[-0.13562086 -0.11107482  0.1600553  ..., -0.3161786  -0.23419835
  0.45029903]

How to convert it?

2 Answers 2

6

You can numpy.reshape it to (-1,1) to get the result in the form you want. Example -

narray = narray.reshape((-1,1))

Demo -

In [19]: import numpy as np

In [20]: narray = np.arange(10)

In [21]: narray
Out[21]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [22]: narray.reshape((-1,1))
Out[22]:
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])

Basically what you are doing is to change the shape of the array from something like - (n,) to (n,1) , you do this by using reshape() , in it you can pass -1 as one of the arguments. As given in documentation -

newshape : int or tuple of ints

The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

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

Comments

2

An alternative is to add a new dimension like this:

import numpy
a = numpy.arange(10)
a
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

a[:,None]
    array([[0],
           [1],
           [2],
           [3],
           [4],
           [5],
           [6],
           [7],
           [8],
           [9]])

a[None,:]
    array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

I don't remember if None indexing is discouraged or not.

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.