how to convert nested list into numpy array? - numpy

In my code when I am taking nested array of list length of 100, I am getting the correct 3D numpy array as output, but when I write code to get numpy array of list length of 1000 or above, I am getting wrong numpy array (i.e. it shows it as single dimensional rather than 3 dimensional).
I tried using numpy.array(list) and numpy.asarray(list), but both have same output.
expected output is array with dimensions(1000,50,100) but actual output is array with dimensions(1000,1).

Related

how to find records starting with a particular letter in numpy array

i am beginner in numpy and created the following array
data=np.array([("abcd",111,9666),("atchd",222,6669),("cdef",444,8756)],dtype=[("name",str,10),("roll",int),("phone",int)])
print(data[data["name"][0]=='a']["name"])
I am trying to print records that have name property starting with character 'a' but this is returning empty array. pl update
The problem is, that you can not index the "string-axis" of your data, since it is not part of the numpy array. To achieve the behaviour you expect, you can utilize np.char.startswith :
import numpy as np
data=np.array(
[
("abcd",111,9666),
("atchd",222,6669),
("cdef",444,8756)
],
dtype=[
("name",str,10),("roll",int),("phone",int)
]
)
print(data[np.char.startswith(data["name"], 'a')]["name"])
# ['abcd' 'atchd']
Note that there are also other string operations supported by numpy.

correct way to create a 3d median numpy array

so I tried to create a 3d array using numpy via this line:
self.dark_median_roi=np.median(self.dark_roi, axis=3)
where self.dark_roi is a multidimensional array and I got this error:
IndexError: axis 3 out of bounds (2)
I'm guessing I went about creating a 3d array the wrong way. What is the correct way to create a median numpy array? This will be running/is trying to run on a Raspberry pi, so I would rather avoid using loops, especially with arrays.
Edit:
so I corrected some mistakes from earlier in the code that weren't noticeable at first until I started adding print statements so this is the error I'm getting now:
IndexError: axis 3 out of bounds (3)
and I tried changing the the axis flag to 2 and it created a 2d array
You have 3 axes: 0, 1 and 2.
If you mean the last one - enter axis=2.

Image in the form of Numpy array in a cell in Pyspark data frame

I would like to store a image represented as a numpy array in a Pyspark data frame.
When I try the I get an error data type not supported.
looking at the data types supported in Pyspark I don't see numpy, wondering if there's a way to store array.
I also tried numpy as string but the string for some reason is truncated contains ...
Any suggestions or solutions?

check if a numpy array is a numpy masked array

As output of a script, I have numpy masked array and standard numpy array. How do I easily check while running the script if an array is a masked (has data, mask attributes) one or not?
You can check explicitly if it is a masked array by isinstance(arr, np.ma.MaskedArray), or you can check for the attributes hasattr(arr, 'mask'). I'd probably recommend the first approach in general.

3 dots and a comma in NumPy array display

I was printing one list of values in Python, when I got this:
[ 0.00020885 0.00021386 0.0002141 ..., 0.0501399 0.12051606
0.12359095]
What is the problem here? The list should have at least size 20. What happened to the elements shown as ...?
The problem is that you are not printing a Python list, but a NumPy array. NumPy output can be configured using numpy.set_printoptions().
Data types matter. If you wonder about the behaviour of some object, first check its type.