Python 3.7.4 lacks a proper linspace function?

In summary, the conversation discusses the creation of a Numpy array with linearly spaced integers between 0 and 3, using either the linspace function or the syntax of 0:0.01:3. The speaker also mentions the difference between including the endpoint or not and increasing the number of elements. They also ask if Python has a similar function and if the listener has read the documentation on this topic.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
Basically, I wanted to create a Numpy array with linearly spaced integers between 0 and 3, the increment being 0.01.

Yes, I know Numpy offers a linspace function. I used it like this: x = np.linspace(0, 3, num=300) (where np is numpy), and got this:

1566501564585.png


I know that the numbers cannot be exact (can you explain why?). Matlab returns something similar when the linspace function is used.

But then, in Matlab, I can write x = 0:0.01:3, which will give me this:

1566501758159.png


Does Python have something similar?
 
Technology news on Phys.org
  • #2
Have you read the docs on this function?

It seems like the only difference is between
Python:
import numpy as np

np.linspace(0, 3,300, False)
np.linspace(0, 3,300, True)
 
  • Like
Likes Wrichik Basu
  • #3
Another way is to include the endpoint, but increase num to 301. Just found this, btw.
 

1. What is a linspace function in Python 3.7.4?

A linspace function in Python 3.7.4 is a built-in function that generates evenly spaced numbers over a specified interval. It is commonly used in scientific computing and data analysis.

2. Why does Python 3.7.4 lack a proper linspace function?

Python 3.7.4 lacks a proper linspace function because it was not included in the standard library of the language. This means that it is not readily available for use and must be imported from an external module.

3. Can I still use the numpy.linspace() function in Python 3.7.4?

Yes, you can still use the numpy.linspace() function in Python 3.7.4. This function is included in the numpy package, which can be imported and used in Python 3.7.4.

4. Are there any alternatives to the linspace function in Python 3.7.4?

Yes, there are alternative functions and methods that can achieve similar results as the linspace function in Python 3.7.4. Some examples include using the range() function, creating a custom function, or using the numpy.arange() function.

5. Is there a plan to include a proper linspace function in future versions of Python?

There is no official plan to include a proper linspace function in future versions of Python. However, there are discussions and proposals in the community to add this function to the standard library in the future.

Similar threads

  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
895
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top