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)
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,...]
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')
I have the following array of strings as a numpy array:
filepaths = np.array(['Padma','Meghna','Jamuna'])
And I want to concatenate so that I should get the following :
Padma-Meghna-Jamuna
I am doing the following, but not getting as expected:
np.array([np.core.defchararray.join('-',a) for a in filepaths])
array(['P-a-d-m-a', 'M-e-g-h-n-a', 'J-a-m-u-n-a'],
dtype='|S11')
Some hint would be helpful here
The defchararray.join operates on each element of the array, individually.
In [94]: '-'.join('padma')
Out[94]: 'p-a-d-m-a'
Just treat your array like a list:
In [93]: '-'.join(np.array(['Padma','Meghna','Jamuna']))
Out[93]: 'Padma-Meghna-Jamuna'
I've got an 18x18 2d numpy array (it's a confusion matrix)...and I need/would like to display it as a table in an ipython notebook.
When I simply print it out, it displays with overlap--the rows are so long they take up two lines.
Is there a library that will allow me to print this array in a sort of spreadsheet format?
You can use Pandas for that.
import pandas as pd
print pd.DataFrame(yourArray)
Note:
Konstantinos proposal holds only for 1-D and 2-D arrays!
You can use numpy.array2string():
from pprint import pprint
import numpy as np
array = np.array([[1,2,3], [4,5,6]])
print(np.array2string(array).replace('[[',' [').replace(']]',']'))
Output:
[1 2 3]
[4 5 6]
See also: Printing Lists as Tabular Data