helloB helloB - 4 months ago 6
Python Question

How to make a numpy array of form ([1.], [2.], [3.]...) from list?

I am trying to make a numpy array of the form

([1.], [2.], ...)
from a list
[1, 2, 3]
so I can use it as an input for
sklearn's linear_model
.

This command

np.array(test_list)


produces this kind of array:

array([1, 2, 3, 4])


whereas I want

array ([1], [2], [3], [4])

Answer

You could just reshape:

 import  numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr.reshape(arr.size, 1).astype(float))

Which would give you:

[[ 1.]
[ 2.]
[ 3.]
[ 4.]
[ 5.]]