I have created the following array, called X
:
array([[6.575],
[6.421],
[7.185],
[6.998],
[6.43 ],
[6.012],
[6.172],
[5.631],
[6.004],
[6.377],
[6.03 ]])
and I would like to create
array([[6.575, 1],
[6.421, 1],
[7.185, 1],
[6.998, 1],
[6.43, 1],
[6.012, 1],
[6.172, 1],
[5.631, 1],
[6.004, 1],
[6.377, 1],
[6.03, 1]])
I have tried X = np.array( [ [value,1] for value in X ] )
but I get the error:
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (506, 2) + inhomogeneous part.
Aside from ignorance I am not sure where I am going wrong. Any ideas?
Do not use list comprehensions/loops to work with numpy arrays. You should combine expand the scalar with broadcast_to
and combine it to X with hstack
:
out = np.hstack([X, np.broadcast_to(1, X.shape)])
Output:
array([[6.575, 1. ],
[6.421, 1. ],
[7.185, 1. ],
[6.998, 1. ],
[6.43 , 1. ],
[6.012, 1. ],
[6.172, 1. ],
[5.631, 1. ],
[6.004, 1. ],
[6.377, 1. ],
[6.03 , 1. ]])
Now, just to answer the source of your error, this this because your intermediate list comprehension generates items of the form [array([6.575]), 1]
instead of [6.575, 1]
. The correct approach should have been:
np.array([[value, 1] for value in X[:, 0]])
# or
np.array([[*value, 1] for value in X])
But, again, do not do this in numpy.