Adding new feature (column) to given numpy array [duplicate] - python

This question already has answers here:
How do I add an extra column to a NumPy array?
(17 answers)
Closed 2 years ago.
lets say I have a numpy array
Y =
array([[4.96473614, 6.02336215],
[2.56213959, 2.74621531],
[4.36170066, 5.60413956],
...,
[5.93540184, 3.62864816],
[2.34483661, 2.37333593],
[6.3250522 , 7.09102362]])
with
Y.shape
(1000,2)
(for example 2dim datapoints with x and y)
How can I easily add a third column to this given array (a z-value) such that
Y.shape
(1000,3)
?

Assume that you have:
Y array of shape (3,2) (3 rows instead of 1000):
array([[4.96473614, 6.02336215],
[2.56213959, 2.74621531],
[4.36170066, 5.60413956]])
Y2 array (1-D) of shape (3,):
array([10, 20, 30])
To get your result, you should:
first convert Y2 to (3,1) shape (3 rows, 1 column),
hstack them.
The code to do it is:
Y = np.hstack([Y, Y2[:, np.newaxis]])
The result is:
array([[ 4.96473614, 6.02336215, 10. ],
[ 2.56213959, 2.74621531, 20. ],
[ 4.36170066, 5.60413956, 30. ]])

I hope this helps:
Create the desired column in a (1000,1) array.
Call ArrayName.append(DesiredColumn, axis = 1)

Related

Changing shape of multidimensional array in Python [duplicate]

This question already has answers here:
How to change array shapes in in numpy?
(5 answers)
Closed 8 months ago.
I have an array B with shape (1,9,1). Is it possible to convert into a new array B1 with shape (1,3,3)? The desired output is attached.
import numpy as np
B=np.array([[[0.67873113],
[1.24442563],
[0.02109 ],
[0.76788408],
[2.00615422],
[3.07375839],
[1.037729 ],
[5.03294753],
[0.0105 ]]])
print("B shape =",B.shape)
The desired output is
B1=np.array([[[0.67873113,1.24442563,0.02109 ],
[0.76788408,2.00615422,3.07375839],
[1.037729,5.03294753,0.0105 ]]])
B1 shape = (1, 3, 3)
You can just reshape it. See here
B.reshape((1,3,3))
>>> array([[[0.67873113, 1.24442563, 0.02109 ],
[0.76788408, 2.00615422, 3.07375839],
[1.037729 , 5.03294753, 0.0105 ]]])

Numpy.Append(): ValueError: could not broadcast input array from shape (4) into shape (3) [duplicate]

This question already has answers here:
How do I add an extra column to a NumPy array?
(17 answers)
Closed 2 years ago.
I met with a problem when doing appending in NumPy. array_1 will throw an error: ValueError: could not broadcast input array from shape (4) into shape (3) but the bottom one does not, where am I doing it wrong? I need to write a loop to append arrays to each array in array_1.
The blunt way is to convert my 2d-arrays to a 2d-list, but as a keen learner, I am really curious about how to do it properly.
array_1 = np.array([[1,2,3], [4,5,6]])
array_1[0] = np.append(array_1[0], 1)
array_2 = np.array([[1,2,3]])
array_2 = np.append(array_2, 1)
Change it to this:
array_1 = np.array([[1,2,3], [4,5,6]])
array_1 = [np.append(array_1[0], 1), array_1[1]]

Turn numpy list into column vector [duplicate]

This question already has answers here:
Numpy reshape 1d to 2d array with 1 column
(7 answers)
Closed 4 years ago.
I can't turn a numpy array into a vector. What I want to do is transform this:
>> x.shape
(784,)
into this:
>> x.shape
(784,1)
x was created in the normal numpy.array() way.
Note: I want to change, not create a new array.
You can add a new axis to a vector in multiple ways: e.g. using np.reshape
x = np.zeros(784)
x0 = x.reshape(784, 1) # shape: (784, 1)
or using np.newaxis while slicing:
x = np.zeros(784)
x0 = x[:,None] # short-hand for x[:,np.newaxis], shape: (784, 1)
Vectors, arrays and lists are totally different data structures, especially in Python. In your question you mentioned I can't turn a numpy array into a vector. Actually what you are trying to do is exactly the opposite, means you want to turn a vector of shape (784,) to an array of shape(784, 1). Anyways #cheersmate gave you the correct answer.

numpy vectorized assignment of sequences [duplicate]

This question already has answers here:
Vectorized NumPy linspace for multiple start and stop values
(4 answers)
Closed 6 years ago.
Is there a vectorized assigment of elements to sequences in numpy like in this discussion?
for instance:
xx = np.array([1,2], dtype=object)
expanded = np.arange(xx, xx+2)
instead of loops:
xx = np.array([1,2], dtype=object)
expanded = np.array([np.arange(x, x+2) for x in xx]).flatten()
This would be for mapping a scalar heuristic to the neighboring cells in a matrix that determined it (e.g. the range of cells that had the peak overlap from a correlation() operation).
Like this?
>>> xx = np.array([3,8,19])
>>> (xx[:,None]+np.arange(2)[None,:]).flatten()
array([ 3, 4, 8, 9, 19, 20])
The xx[:,None] operation turns the length n vector into an nx1 matrix, and the np.arange(2)[None,:]) operation creates a length 1x2 matrix containing [0., 1.]. Added together using array broadcasting gives an nx2 matrix, which is then flattened into a length 2n vector.

numpy get std between datasets

I have a dataset array A. A is nĂ—2. It can be plotted on the x and y axis.
A[:,1] gets me all of the Y values ans A[:,0] gets me all the x values.
Now, I have a few other dataset arrays that are similar to A. X values are the same for these similar arrays. How do I calculate the standard deviation of the datasets? There should be a std value for each X. In the end my result std should have a length of n.
I can do this the manual way with loops but I'm not sure how to do this using NumPy in a pythonic and simple manner.
here are some sample data:
A=[[0,2.54],[1,254.5],[2,-43]]
B=[[0,3.34],[1,154.5],[2,-93]]
std_Array=[std(2.54,3.54),std(254.5,154.5),std(-43,-93)]
Suppose your arrays are all the same shape and they are in a list. Then to get the standard deviation of the first column of each you can do
arrays = [np.random.rand(10, 2) for _ in range(8)]
np.dstack(arrays).std(axis=0)[0]
This stacks the 2-D arrays into a 3-D array an then takes the std along the first axis, giving a 2 X 8 (the number of arrays). The first row of the result is the std. devs. of the 8 sets of x-values.
If you post some sample data perhaps we could help more.
Is this pythonic enough?
std_Array = numpy.std((A,B), axis = 0)[:,1]
li_arr = [np.array(x)[: , 1] for x in [A , B]]
This will produce numpy arrays with specifi columns you want to add the result will be
[array([ 2.54, 254.5 , -43. ]), array([ 3.34, 154.5 , -93. ])]
then you stack the values using column_stack
arr = np.column_stack(li_arr)
this will be the result stacking
array([[ 2.54, 3.34],
[ 254.5 , 154.5 ],
[ -43. , -93. ]])
and then finally
np.std(arr , axis = 1)

Categories