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

arange()

np.arange(1, 10)

linespace()

np.linspace(1, 10, 3)

zeros()

np.zeros(5, dtype=int)

ones()

np.ones(5, dtype=int)

random.rand()

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

zeros()

np.zeros([4, 3], dtype = np.int32)

ones()

np.ones([4, 3], dtype = np.int32)

full()

np.full([2, 2], 67, dtype = int)

eye()

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.

NumPy Cheat Sheet

Similar Reads

What is NumPy?

NumPy was initially created by Travis Oliphant in 2005 as an open-source project. NumPy is a powerful Python library that provides support for large, multi-dimensional arrays and matrices, along with a wide collection of mathematical functions to operate on these arrays. It is an essential tool for scientific computing and data analysis in Python....

NumPy Cheat Sheet 2023

1. Creating Arrays Commands...

1. Creating Arrays Commands

Arrays in NumPy are of fixed size and homogeneous in nature. They are faster and more efficient because they are written in C language and are stored in a continuous memory location which makes them easier to manipulate. NumPy arrays provide N-dimensional array objects that are used in linear algebra, Fourier Transformation, and random number capabilities. These array objects are much faster and more efficient than the Python Lists....

2. Initial Placeholders

...

3. Inspecting Properties

...

4. Saving and Loading File

Example 1: For 1-Dimensional NumPy Arrays...

5. Sorting Array

...

6. NumPy Array Manipulation

...

7. Combining and Splitting Commands

NumPy arrays possess some basic properties that can be used to get information about the array such as the size, length, shape, and datatype of the array. Numpy arrays can also be converted to a list and be change their datatype....

8. Indexing, Slicing and Subsetting

...

9. Copying and Viewing Array

...

10. NumPy Array Mathematics

...

Benefits of Using NumPy Cheat Sheet

Numpy arrays can be stored or loaded from a disk file with the ‘.npy‘ extension. There are various ways by which we can import a text file in a NumPy array....

Applications of NumPy

...

Feature of NumPy

...

Conclusion

...

NumPy Cheat Sheet – FAQs

...