helloB - 3 months ago 4x

Python Question

I am trying to make a numpy array of the form

`([1.], [2.], ...)`

`[1, 2, 3]`

`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.]]
```

Source (Stackoverflow)

Comments