Initial Placeholders
Example 1: For 1-Dimensional NumPy Arrays
Initial placeholders for a Numpy 1-dimension array can be created by using various Numpy functions.
Initial Placeholders for 1D Array |
Example |
---|---|
np.arange(1, 10) |
|
np.linspace(1, 10, 3) |
|
np.zeros(5, dtype=int) |
|
np.ones(5, dtype=int) |
|
np.random.rand(5) |
|
random.randint() |
np.random.randint(5, size=10) |
Python3
# create a NumPy array using numpy.arange() print (np.arange( 1 , 10 )) # create a NumPy array using numpy.linspace() print (np.linspace( 1 , 10 , 3 )) # create a NumPy array using numpy.zeros() print (np.zeros( 5 , dtype = int )) # create a NumPy array using numpy.ones() print (np.ones( 5 , dtype = int )) # create a NumPy array using numpy.random.rand() print (np.random.rand( 5 )) # create a NumPy array using numpy.random.randint() print (np.random.randint( 5 , size = 10 )) |
Output:
[1 2 3 4 5 6 7 8 9]
[ 1. 5.5 10. ]
[0 0 0 0 0]
[1 1 1 1 1]
[0.31447226 0.89090771 0.45908938 0.92006507 0.37757036]
[4 3 2 3 1 2 4 1 4 2]
Example 2: For N-dimensional Numpy Arrays
Initial placeholders for Numpy two dimension arrays can be created by using various NumPy functions.
Initial Placeholders for 2D Array |
Example |
---|---|
np.zeros([4, 3], dtype = np.int32) | |
np.ones([4, 3], dtype = np.int32) | |
np.full([2, 2], 67, dtype = int) | |
np.eye(4) |
Python3
# create a NumPy array using numpy.zeros() print (np.zeros([ 4 , 3 ], dtype = np.int32)) # create a NumPy array using numpy.ones() print (np.ones([ 4 , 3 ], dtype = np.int32)) # create a NumPy array using numpy.full() print (np.full([ 2 , 2 ], 67 , dtype = int )) # create a NumPy array using numpy.eye() print (np.eye( 4 )) |
Output:
[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]
[[1 1 1]
[1 1 1]
[1 1 1]
[1 1 1]]
[[67 67]
[67 67]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
NumPy Cheat Sheet: Beginner to Advanced (PDF)
NumPy stands for Numerical Python. It is one of the most important foundational packages for numerical computing & data analysis in Python. Most computational packages providing scientific functionality use NumPy’s array objects as the lingua franca for data exchange.
In this Numpy Cheat sheet for Data Analysis, we’ve covered the basics to advanced functions of Numpy including creating arrays, Inspecting properties as well as file handling, Manipulation of arrays, Mathematics Operations in Array and more with proper examples and output. By the end of this Numpy cheat sheet, you will gain a fundamental comprehension of NumPy and its application in Python for data analysis.