python numpy arange dtpye? why converting to integer was zero

2024/11/8 13:38:57

x = np.arange(0.3, 12.5, 0.6)

print(x)

[ 0.3 0.9 1.5 2.1 2.7 3.3 3.9 4.5 5.1 5.7 6.3 6.9 7.5 8.1 8.7 9.3 9.9 10.5 11.1 11.7 12.3]

x = np.arange(0.3, 12.5, 0.6,int)

print(x)

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

Answer

First let's skip the complexity of a float step, and use a simple integer start and stop:

In [141]: np.arange(0,5)
Out[141]: array([0, 1, 2, 3, 4])
In [142]: np.arange(0,5, dtype=int)
Out[142]: array([0, 1, 2, 3, 4])
In [143]: np.arange(0,5, dtype=float)
Out[143]: array([0., 1., 2., 3., 4.])
In [144]: np.arange(0,5, dtype=complex)
Out[144]: array([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j])
In [145]: np.arange(0,5, dtype='datetime64[D]')
Out[145]: 
array(['1970-01-01', '1970-01-02', '1970-01-03', '1970-01-04','1970-01-05'], dtype='datetime64[D]')

Even bool work, within a certain range:

In [149]: np.arange(0,1, dtype=bool)
Out[149]: array([False])
In [150]: np.arange(0,2, dtype=bool)
Out[150]: array([False,  True])
In [151]: np.arange(0,3, dtype=bool)
ValueError: no fill-function for data-type.
In [156]: np.arange(0,3).astype(bool)
Out[156]: array([False,  True,  True])

There are 2 possible boolean values, so asking for more should produce some sort of error.

arange is compiled code, so we can't readily examine its logic (but you are welcome to search for the C code on github).

The examples show that it does, in some sense convert the parameters into the corresponding dtype, and perform an iteration on that. It doesn't simply generate the range and convert to dtype at the end.

https://en.xdnf.cn/q/120608.html

Related Q&A

Where is my syntax error?

Im trying to see where a Python syntax error would be hiding. Both Django and pylint claim a syntax error at custom.py:41. Lines 41-42 read:(reading_threshold =int(request.POST[reading_threshold]))I do…

programming challenge: how does this algorithm (tied to Number Theory) work?

In order to work on my python skills, I am sometimes doing various challenges on the internet (eg on hackerrank). Googling for something else, I found this problem, and the accompanying solution on the…

Running total for list of dict

Have a python list of dict as following:Dict1 = [{date: 1, name: xyz, qty: 100},{date: 1, name: xyz, qty: 200},{date: 1, name: xyz, qty: 300},{date: 1, name: xyz2, qty: 30},{date: 2, name: xyz, qty: 10…

How to convert CSV file to a specific JSON format with nested objects?

I want to populate my json message with data from a CSV file. I want each row to be a "new" json object. I am using Python and will connect the the code to an API once done. Some of the data …

How do I repeat the program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

How to parse json data in Python? [duplicate]

This question already has answers here:How can I parse (read) and use JSON in Python?(5 answers)Closed 10 years ago.Please help me to parse this json in python.{ "IT" : [ { "firstName…

Why did they opt for the message most recent call last? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed last month.Improve …

AttributeError: NoneType object has no attribute current

class LoginScreen(Screen):def __init__(self,**kwargs):super(LoginScreen, self).__init__(**kwargs)print self,self.parent.currentclass AppScreenManager(ScreenManager):pass#Base Class class AppBaseClass(A…

Python urllib2 or requests post method [duplicate]

This question already has answers here:Submitting to a web form using python(3 answers)Closed 8 years ago.The community reviewed whether to reopen this question 9 months ago and left it closed:Original…

How to fix Django NoReverseMatch Error

I have built a simple blog app with Django and I have had some issue with NoReverseMatch. Here are my problem screenshots: https://prnt.sc/imqx70 https://prnt.sc/imqwptHere is my code on Github: https:…