What is the difference between np.linspace and np.arange?
Question:
I have always used np.arange
. I recently came across np.linspace
. I am wondering what exactly is the difference between them… Looking at their documentation:
Return evenly spaced values within a given interval.
Return evenly spaced numbers over a specified interval.
The only difference I can see is linspace
having more options… Like choosing to include the last element.
Which one of these two would you recommend and why? And in which cases is np.linspace
superior?
Answers:
np.linspace
allows you to define how many values you get including the specified min and max value. It infers the stepsize:
>>> np.linspace(0,1,11)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
np.arange
allows you to define the stepsize and infers the number of steps(the number of values you get).
>>> np.arange(0,1,.1)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
contributions from user2357112:
np.arange
excludes the maximum value unless rounding error makes it do otherwise.
For example, the following results occur due to rounding error:
>>> numpy.arange(1, 1.3, 0.1)
array([1. , 1.1, 1.2, 1.3])
You can exclude the stop
value (in our case 1.3) using endpoint=False
:
>>> numpy.linspace(1, 1.3, 3, endpoint=False)
array([1. , 1.1, 1.2])
np.arange
– This is similar to built in range()
function
>>> np.arange(0,5,2)
[0 2 4]
np.linpace
– creates an array of defined evenly spaced value. For example, 2 values evenly spaced between 0 and 5
>>> np.linspace(0,5,2)
[0. 5.]
np.arange(start, stop, step)
np.linspace(start,stop,number)
Example:
np.arange(0,10,2) o/p --> array([0,2,4,6,8])
np.linspace(0,10,2) o/p --> array([0., 10.])
numpy.linspace
and numpy.arange
can produce two variables that appear the same but aren’t. This must be related to how data is stored internally. When I created ndarrays using arange
, the size in the memory scaled with the number of elements. However, the ndarray
created using linspace
remained the same size.
from sys import getsizeof
import numpy as np
arr_lnspc5 = np.linspace(1,5,5)
arr_lnspc20 = np.linspace(1,20,20)
arr_arange5 = np.arange(1,6,1.0)
arr_arange20 = np.arange(1,21,1.0)
print(f'lnspc5 ==============')
print(f'Val: {arr_lnspc5}')
print(f'Type: {type(arr_lnspc5)}')
print(f'Size: {getsizeof(arr_lnspc5)} Bytes n')
print(f'lnspc20 ==============')
print(f'Val: {arr_lnspc20}')
print(f'Type: {type(arr_lnspc20)}')
print(f'Size: {getsizeof(arr_lnspc20)} Bytes n')
print(f'arange5 ==============')
print(f'Val: {arr_arange5}')
print(f'Type: {type(arr_arange5)}')
print(f'Size: {getsizeof(arr_arange5)} Bytes n')
print(f'arange20 ==============')
print(f'Val: {arr_arange20}')
print(f'Type: {type(arr_arange20)}')
print(f'Size: {getsizeof(arr_arange20)} Bytes n')
The output I got was:
lnspc5 ==============
Val: [1. 2. 3. 4. 5.]
Type: <class 'numpy.ndarray'>
Size: 112 Bytes
lnspc20 ==============
Val: [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.]
Type: <class 'numpy.ndarray'>
Size: 112 Bytes
arange5 ==============
Val: [1. 2. 3. 4. 5.]
Type: <class 'numpy.ndarray'>
Size: 152 Bytes
arange20 ==============
Val: [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.]
Type: <class 'numpy.ndarray'>
Size: 272 Bytes
I have always used np.arange
. I recently came across np.linspace
. I am wondering what exactly is the difference between them… Looking at their documentation:
Return evenly spaced values within a given interval.
Return evenly spaced numbers over a specified interval.
The only difference I can see is linspace
having more options… Like choosing to include the last element.
Which one of these two would you recommend and why? And in which cases is np.linspace
superior?
np.linspace
allows you to define how many values you get including the specified min and max value. It infers the stepsize:
>>> np.linspace(0,1,11)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
np.arange
allows you to define the stepsize and infers the number of steps(the number of values you get).
>>> np.arange(0,1,.1)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
contributions from user2357112:
np.arange
excludes the maximum value unless rounding error makes it do otherwise.
For example, the following results occur due to rounding error:
>>> numpy.arange(1, 1.3, 0.1)
array([1. , 1.1, 1.2, 1.3])
You can exclude the stop
value (in our case 1.3) using endpoint=False
:
>>> numpy.linspace(1, 1.3, 3, endpoint=False)
array([1. , 1.1, 1.2])
np.arange
– This is similar to built in range()
function
>>> np.arange(0,5,2)
[0 2 4]
np.linpace
– creates an array of defined evenly spaced value. For example, 2 values evenly spaced between 0 and 5
>>> np.linspace(0,5,2)
[0. 5.]
np.arange(start, stop, step)
np.linspace(start,stop,number)
Example:
np.arange(0,10,2) o/p --> array([0,2,4,6,8])
np.linspace(0,10,2) o/p --> array([0., 10.])
numpy.linspace
and numpy.arange
can produce two variables that appear the same but aren’t. This must be related to how data is stored internally. When I created ndarrays using arange
, the size in the memory scaled with the number of elements. However, the ndarray
created using linspace
remained the same size.
from sys import getsizeof
import numpy as np
arr_lnspc5 = np.linspace(1,5,5)
arr_lnspc20 = np.linspace(1,20,20)
arr_arange5 = np.arange(1,6,1.0)
arr_arange20 = np.arange(1,21,1.0)
print(f'lnspc5 ==============')
print(f'Val: {arr_lnspc5}')
print(f'Type: {type(arr_lnspc5)}')
print(f'Size: {getsizeof(arr_lnspc5)} Bytes n')
print(f'lnspc20 ==============')
print(f'Val: {arr_lnspc20}')
print(f'Type: {type(arr_lnspc20)}')
print(f'Size: {getsizeof(arr_lnspc20)} Bytes n')
print(f'arange5 ==============')
print(f'Val: {arr_arange5}')
print(f'Type: {type(arr_arange5)}')
print(f'Size: {getsizeof(arr_arange5)} Bytes n')
print(f'arange20 ==============')
print(f'Val: {arr_arange20}')
print(f'Type: {type(arr_arange20)}')
print(f'Size: {getsizeof(arr_arange20)} Bytes n')
The output I got was:
lnspc5 ==============
Val: [1. 2. 3. 4. 5.]
Type: <class 'numpy.ndarray'>
Size: 112 Bytes
lnspc20 ==============
Val: [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.]
Type: <class 'numpy.ndarray'>
Size: 112 Bytes
arange5 ==============
Val: [1. 2. 3. 4. 5.]
Type: <class 'numpy.ndarray'>
Size: 152 Bytes
arange20 ==============
Val: [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.]
Type: <class 'numpy.ndarray'>
Size: 272 Bytes