Contents
Introduction to NumPy Arrays
NumPy, short for Numerical Python, is a Python library designed for efficient numerical computing. It provides multidimensional arrays and mathematical operations that are particularly useful when working with large datasets.
Python lists can store numerical values, but NumPy arrays support efficient element-wise calculations and many mathematical operations without requiring explicit loops.
Creating a NumPy Array
First, import NumPy using the conventional alias np.
import numpy as np
sales = np.array([12000, 15000, 18000, 22000, 25000])
print(sales)
print(type(sales))
Output:
[12000 15000 18000 22000 25000]
<class 'numpy.ndarray'>
The ndarray is NumPy's primary data structure. It can represent one-dimensional arrays, two-dimensional tables, and arrays with even more dimensions.
Understanding Array Dimensions
import numpy as np
one_dimensional = np.array([10, 20, 30])
two_dimensional = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(one_dimensional.ndim) # 1
print(two_dimensional.ndim) # 2
print(two_dimensional.shape) # (2, 3)
print(two_dimensional.size) # 6
The ndim attribute indicates the number of dimensions, shape gives the size along each dimension, and size returns the total number of elements.
These properties help you understand how numerical data is organized before performing calculations.
Python for Data Science
Vishalini A








