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.
Related
Why are numpy arrays called homogeneous when you can have elements of different type in the same numpy array like this?
np.array([1,2,3,4,"a"])
I understand that I cannot perform some types of broadcasting operations like I cannot perform
np1*4 here and it results in an error.
but my question really is when it can have elements of different types, why it is called homogeneous?
Numpy automatically converts them to most applicable datatype.
e.g.,
>>> np.array([1,2,3,4,"a"]).dtype.type
numpy.str_
In short this means all elements are of string.
>>> np.array([1,2,3,4]).dtype.type
numpy.int64
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.
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).
I am giving myself an intro to plotting data and have come across some trouble. I am working on a line chart that I plan on making animated as soon as I figure out this problem.
I want a graph that looks like this:
However this code I have now:
`x=df_pre_2003['year']
y=df_pre_2003['nAllNeonic']
trace=go.Scatter(
x=x,
y=y
)
data=[trace]
ply.plot(data, filename='test.html')`
is giving me this:
So I added y=df_pre_2003['nAllNeonic'].sum()
but, now it says ValueError:
Invalid value of type 'builtins.float' received for the 'y' property of scatter
Received value: 1133180.4000000006
The 'y' property is an array that may be specified as a tuple,
list, numpy array, or pandas Series
Which I tried and it still did not work. The data types for year is int64 and nAllNeonic is float64.
It looks like you have to sort the values first based on the date. Now it's connecting a value in the year 1997 with a value in 1994.
df_pre_2003.sort_values(by = ['year'])
This is not to answer this question, but to share my similar case for any future research needs:
In my case the error message was coming when I tried to export the django models objects to use it in the plotly scatter chart, and the error was as follows:
The 'x' property is an array that may be specified as a tuple, list, numpy array, or pandas Series
The solution for this in my case was to export the django model info into pandas data frame then use the pandas data frame columns instead of the model fields name.
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.