Python 3.7.4 lacks a proper linspace function?

  • Context: Python 
  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    Function Python
Click For Summary
SUMMARY

The discussion centers on the absence of a direct equivalent to Matlab's colon operator in Python for generating linearly spaced integers. Users can utilize Numpy's linspace function, as demonstrated with x = np.linspace(0, 3, num=300), to achieve similar results. However, the precision of floating-point arithmetic in Python may lead to slight discrepancies in the output. The alternative approach using np.linspace(0, 3, 300, False) versus np.linspace(0, 3, 300, True) highlights the importance of endpoint inclusion in generating arrays.

PREREQUISITES
  • Familiarity with Python 3.7.4 programming
  • Understanding of Numpy library functions
  • Knowledge of floating-point arithmetic and its implications
  • Basic experience with Matlab syntax and functionality
NEXT STEPS
  • Explore Numpy's linspace and arange functions for array generation
  • Investigate floating-point precision issues in Python
  • Learn about array manipulation techniques in Numpy
  • Compare array generation methods between Python and Matlab
USEFUL FOR

Data scientists, Python developers, and anyone transitioning from Matlab to Python who seeks to understand array generation techniques in Numpy.

Wrichik Basu
Science Advisor
Insights Author
Gold Member
Messages
2,188
Reaction score
2,694
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
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   Reactions: Wrichik Basu
Another way is to include the endpoint, but increase num to 301. Just found this, btw.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K