In Python, I can concatenate two arrays like below,
myArray = []
myArray += [["Value1", "Value2"]]
I can then access the data by indexing the 2 dimensions of this array like so
print(myArray[0][0])
which will output:
Value1
How would I go about achieving the same thing with Numpy?
I tried the numpy append function but that only ever results in single dimensional arrays for me, no matter how many square brackets I put around the value I'm trying to append.
If you know the dimension of the final array then you could use np.vstack
>>> import numpy as np
>>> a = np.array([]).reshape(0,2)
>>> b = np.array([['Value1', 'Value2']])
>>> np.vstack([a,b])
array([['Value1', 'Value2']], dtype='<U32')
Related
I have a numpy array with n row and p columns.
I want to check if a given row is in my array and find the index.
For exemple I have a numpy array like this :
[[1,0,8,7,2,2],[1,3,7,0,3,0],[1,7,1,0,1,0],[1,9,1,0,6,0],[1,8,1,7,9,0],....]
I want to check if this array [6,0,5,8,2,1] is in my numpy array or and where.
Is there a numpy function for that ?
I'm sorry for asking naive question but I'm quite confuse right now.
You can use == and .all(axis=1) to match entire rows, then use numpy.where() to get the index:
import numpy as np
a = np.array([[1,0,8,7,2,2],[1,3,7,0,3,0],[1,7,1,0,1,0],[1,9,1,0,6,0],[1,8,1,7,9,0], [6,0,5,8,2,1]])
b = np.array([6,0,5,8,2,1])
print(np.where((a==b).all(axis=1)))
Output:
(array([5], dtype=int32),)
I have a numpy multidimensional array with shape = (12,2,3,3)
import numpy as np
arr = np.arange(12*2*3*3).reshape((12,2,3,3))
I need to select those elements based on the 2nd dimension where the dindices are stored in another list
indices = [0,1,0,0,1,1,0,1,1,0,1,1]
in one array, and the rest in another array.
the output in either case should be in another array of shape (12,3,3)
arr2 = np.empty((arr.shape[0],*arr.shape[-2:]))
I could do it using a for loop
for i, ii in enumerate(indices):
arr2[i] = arr[i, indices[ii],...]
However, I am searching for a one liner.
When I try indexing using the list as indices
test = arr[:,indices,...]
I get test of shape (12,12,3,3) instead of (12,3,3). Could you help me please?
You can use np.arange for indexing the first dimension:
test = arr[np.arange(arr.shape[0]),indices,...]
or just the python range function:
test = arr[range(arr.shape[0]),indices,...]
I have an numpy array like this
Input
array([['ATS1, ATS2', 'P_CD'],
['ATS1,ATS2,ATS3', 'C_CD']], dtype=object)
I would like to convert this numpy array as stated below
Expected output
array([['ATS1' , 'ATS2', 'P_CD'],
['ATS1','ATS2','ATS3', 'C_CD']], dtype=object)
As you can notice above, I would like to split the string with a delimeter and make it as a separate entry
Any suggestions on how to achieve using python?
You can use re.split and join
This is just changing the type but this results in numpy array of lists as inside sub lists can be of variable length so they will not be of numpy array types.
import numpy as np
import re
arr = np.array([['ATS1, ATS2', 'P_CD'],
['ATS1,ATS2,ATS3', 'C_CD']], dtype=object)
arr = np.array([re.split('[-,]','-'.join(ele)) for ele in arr] ,dtype=object)
print(arr)
This question already has answers here:
Appending to numpy arrays
(3 answers)
Closed 3 years ago.
I have a for loop where I create numpy array a. I want to have a results numpy array that I append array a to every loop. So the final structure of the results array should be [a,a,a,etc...], such that I can get into a new array [len(a),Len(a),etc..]
I can't figure out how to do this. I've tried np.append, and can't figure out how to do this (I'm confused what the axis parameter does). I'm new to numpy so any help is appreciated. I don't want to flatten the arrays - I want it to behave like appending a python list to a python list.
Sorry about the formatting - I'm on a phone right now.
The simplest (and quickest) way is to collect the arrays in a list, then use np.concatenate to join them all together.
Example test data
import numpy as np
a = np.random.rand(4,5)
b = np.random.rand(4,5)
c = np.random.rand(4,5)
d = np.random.rand(4,5)
lst = [a,b,c,d]
You can concatenate along either axis — by default it is along the 0-axis.
>>> result = np.concatenate(lst)
>>> result.shape
(16, 5)
...along the 1st axis.
>>> result = np.concatenate(lst, axis=1)
>>> result.shape
(4, 20)
The arrays need to match in the other axis, i.e. if your concatenating them vertically, they must be the same width.
Right, perhaps I should be using the normal Python lists for this, but here goes:
I want a 9 by 4 multidimensional array/matrix (whatever really) that I want to store arrays in. These arrays will be 1-dimensional and of length 4096.
So, I want to be able to go something like
column = 0 #column to insert into
row = 7 #row to insert into
storageMatrix[column,row][0] = NEW_VALUE
storageMatrix[column,row][4092] = NEW_VALUE_2
etc..
I appreciate I could be doing something a bit silly/unnecessary here, but it will make it ALOT easier for me to have it structured like this in my code (as there's alot of these, and alot of analysis to be done later).
Thanks!
Note that to leverage the full power of numpy, you'd be much better off with a 3-dimensional numpy array. Breaking apart the 3-d array into a 2-d array with 1-d values
may complicate your code and force you to use loops instead of built-in numpy functions.
It may be worth investing the time to refactor your code to use the superior 3-d numpy arrays.
However, if that's not an option, then:
import numpy as np
storageMatrix=np.empty((4,9),dtype='object')
By setting the dtype to 'object', we are telling numpy to allow each element of storageMatrix to be an arbitrary Python object.
Now you must initialize each element of the numpy array to be an 1-d numpy array:
storageMatrix[column,row]=np.arange(4096)
And then you can access the array elements like this:
storageMatrix[column,row][0] = 1
storageMatrix[column,row][4092] = 2
The Tentative NumPy Tutorial says you can declare a 2D array using the comma operator:
x = ones( (3,4) )
and index into a 2D array like this:
>>> x[1,2] = 20
>>> x[1,:] # x's second row
array([ 1, 1, 20, 1])
>>> x[0] = a # change first row of x
>>> x
array([[10, 20, -7, -3],
[ 1, 1, 20, 1],
[ 1, 1, 1, 1]])