NumPy Array Creation
There are several ways for creating a NumPy arrays. Here you will learn what are those methods.
1. Converting python array-like objects like lists, tuples to NumPy arrays.
Using array(), the numerical data arranged in an array-like structure in Python can be converted into ndarray or a NumPy array.
NumPy is employed to figure with arrays & the array object in NumPy is named ndarray. To create an array we can pass a list as well as a tuple or an array-like object into the array(), it will be converted into a ndarray.
Let’s see the example code:
#This python code demonstrates how we can convert a python array-like objects into a numpy array or ndarray.
Code:
import numpy as np
ar1 = np.array([9 ,8 ,7, 6])
ar2 = np.array((9 ,8 ,7, 6))
print(ar1)
#output:[9 8 7 6]
print(type(ar1))
#output: <class 'numpy.ndarray>
print(ar2)
#output:[9 8 7 6]
print(type(ar2))
#output: <class 'numpy.ndarray>
2. Using built-in functions of NumPy:
a). zeros(shape):
This function creates an array filled with ‘0’ values. its default dtype is float64.
Let’s see the example code:
#This python code demonstrates how we can create an array using the zeros(shape) function.
Code:
import numpy as np
#creating ndarray of shape(7,10) using zeros(shape) function
arr= np.zeros((7,10))
print(arr)
#output:
'''[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]'''
#print default dtype
print(arr.dtype)
#output:float64
b). ones(shape):
This function creates an array filled with ‘1’ values . its default dtype is float64.
Let’s see the example code:
#This python code demonstrates how we can create an array using ones(shape) function.
Code:
import numpy as np
#creating ndarray of shape(7,10) using ones(shape) function
arr=np.ones((7,10))
print(arr)
#output:
'''[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
'''
#print default dtype
print(arr.dtype)
#output: float64
c) arange() function:
this function creates a numpy arrays with regularly incrementing values. Let’s see the example code:
#This python code demonstrates how we can create an array using arange function.
Code:
import numpy as np
# this will create an array with values starting with 0 and upto 6(excluding 6)
n = np.arange(6)
print(n)
#output: [0 1 2 3 4 5]
#this will create an array with values starting with 2 and upto 20(excluding 20)
n1 = np.arange(2,20)
print(n1)
#output:[ 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
#we can also specify the data type of the array elements.
n2 = np.arange(2.1 ,20, dtype =float)
print(n2)
#output:[ 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.]
#this will create an array starting from 5.1 upto 6(excluded) with difference 0.1
n3= np.arange(5.1,6, 0.1)
print(n3)
#output: [5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9]
d) linspace() function:
this function is an advanced version of arange(start, stop, difference).
this function will create an array with a specified number of elements
, spaced equally between the specified beginning and the end value. This also guarantees the no. of elements.
Code:
import numpy as np
#this will create an array starting with 5 upto 20(excluded), this guarantees no. of elemnts, here 10 elements will be created.
arr = np.linspace(5. , 20. , 10)
print(arr)
#output:[ 5. 6.66666667 8.33333333 10. 11.66666667 13.33333333
# 15. 16.66666667 18.33333333 20. ]
#print the dtype of the array
print(arr.dtype)
#output: float64
e). indices():
this function creates a set of the array representing the indices of a grid.
syntax:
numpy.indices(shape, dtype, sparse=false)
Shape- dimension of the grid
Dtype-data type of the array elements, this parameter is optional.
Sparse-this accepts a Boolean value, if true then it will return a tuple of arrays else, a single array of grid indices.
Let’s see example code with default sparse:
import numpy as np
out = np.indices((2,4))
#print the shape of the grid
print(out.shape)
#output: (2,2,4)
#row idices
print(out[0])
#output: [[0 0 0 0][1 1 1 1]]
# comumn indices
print(out[1])
#output: [[0 1 2 3] [0 1 2 3]]
Let’s see example code with Sparse=True:
import numpy as np
#setting sparse to true will result grid in sparse representation
out = np.indices((2,4),sparse= True)
#As we passes True to sparse it will retun a tuple, so lets chech the type of 'out' variable
print(type(out))
#output:
#this will return tuple of row indices and tuple of column indices
print(out)
#output:(array([[0], [1]]), array([[0, 1, 2, 3]]))
Hope you like this article, don’t forget to comment and share...
Happy Learning


