How To Create Numpy Array ?
Table Of Contents:
- np.array( )
- np.zeros( )
- np.ones( )
- np.empty( )
- np.arange( )
- np.linspace( )
(1) np.array( )
- To create a NumPy array, you can use the function
np.array()
. - All you need to do to create a simple array is, pass a list to it.
- you can also specify the type of data in your list.
Example-1
import numpy as np
lst = [1,2,3,4,5,6]
a = np.array(lst)
a
Output:
array([1, 2, 3, 4, 5, 6])
(2) np.zeros( )
- You can also create an array which will contain only zeros.
- np.zeros( ) will take an integer value, that will tell how many zeros you want.
Example-1
import numpy as np
a = np.zeros(5)
a
Output:
array([0., 0., 0., 0., 0.])
(3) np.ones( )
- You can also create an array which will contain only ones.
- np.ones( ) will take an integer value, that will tell how many numbers of ‘ones’ you want.
Example-1
import numpy as np
a = np.ones(5)
a
Output:
array([1., 1., 1., 1., 1.])
(4) np.empty( )
- ‘np.empty()’ function will create an array whose initial content is random.
- You choose ‘np.empty()’ over ‘np.zeros()’ for speed reasons.
- Don’t forget to fill in every element afterwards!
Example-1
import numpy as np
a = np.empty(2)
a
array([ 1.05889299e-168, -2.76573256e-223])
a[0] = 25
a[1] = 50
a
array([25., 50.])
(5) np.arange( )
- ‘np.arange( )’ is used to create a range of elements.
Syntax:
np.arange(first_number, last_number, step_size)
Parameters:
- first_number = Starting number of your range.
- last_number = ending_number of your range.
- step_size = What should be the difference between two conjugative values?
Example-1
import numpy as np
a = np.arange(10)
a
Output:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
(5) np.linspace( )
- Return evenly spaced numbers over a specified interval.
- You must mention the interval’s ‘Start’ and ‘End’ points.
- The ‘num’ parameter value will tell how many numbers you want to display.
Syntax:
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
Parameters:
- start: array_like – The starting value of the sequence.
- stop: array_like – The end value of the sequence, unless endpoint is set to False. In that case, the sequence consists of all but the last of
num + 1
evenly spaced samples, so that stop is excluded. Note that the step size changes when endpoint is False. - num: int, optional – Number of samples to generate. Default is 50. Must be non-negative.
- endpoint: bool, optional – If True, stop is the last sample. Otherwise, it is not included. Default is True.
- retstep: bool, optional – If True, return (samples, step), where step is the spacing between samples.
- dtype: dtype, optional – The type of the output array. If
dtype
is not given, the data type is inferred from start and stop. The inferred dtype will never be an integer; float is chosen even if the arguments would produce an array of integers. - axis: int, optional – The axis in the result to store the samples. Relevant only if start or stop are array-like. By default (0), the samples will be along a new axis inserted at the beginning. Use -1 to get an axis at the end.
Example-1
import numpy as np
np.linspace(0, 10, num=5)
Output:
array([ 0. , 2.5, 5. , 7.5, 10. ])
Example-2
import numpy as np
np.linspace(0, 10, num=10)
Output:
array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444,
5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ])