Introduction to NumPy

Python tutorial · PySpark.in

What is NumPy?

NumPy (Numerical Python) is an open-source Python library used for scientific computing and numerical operations.
It provides support for multi-dimensional arrays, matrices, and a large collection of high-level mathematical functions to operate on these arrays efficiently.

NumPy is the core foundation of many scientific and data-related libraries such as:

Why NumPy over Python Lists?

NumPy is preferred over Python lists because it provides faster execution, lower memory consumption, and optimized mathematical operations through vectorization and efficient memory management. Python lists are general-purpose containers, whereas NumPy arrays are specialized for numerical computation.

Key Reasons to Use NumPy

1.Speed

2.Memory Efficiency

3.Vectorized Operations

4.Mathematical & Scientific Support

Importing NumPy

NumPy is commonly imported using the alias np.

```

Hide

import numpy as np

```

Example

```

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr)

```

NumPy Array vs Python List

Python List Example

```

list1 = [1, 2, 3, 4]

list2 = [5, 6, 7, 8]

result = []

for i in range(len(list1)):

result.append(list1[i] + list2[i])

print(result)

```

NumPy Array Example

```

import numpy as np

a = np.array([1, 2, 3, 4])

b = np.array([5, 6, 7, 8])

print(a + b)

```

Speed Comparison (Practical)

```

import numpy as np

import time

# Python list

list_data = list(range(1000000))

start = time.time()

sum([x * x for x in list_data])

print("List time:", time.time() - start)

# NumPy array

array_data = np.arange(1000000)

start = time.time()

np.sum(array_data ** 2)

print("NumPy time:", time.time() - start)

```

Result

Memory Comparison

```

import sys

import numpy as np

list_data = [1, 2, 3, 4]

array_data = np.array([1, 2, 3, 4])

print(sys.getsizeof(list_data))

print(array_data.nbytes)

```

Observation

Tabular Comparison

Feature

Python List

NumPy Array

Speed

Slower

Faster

Memory

High

Low

Data Type

Mixed

Same type

Operations

Loop required

Vectorized

Mathematical Functions

Limited

Extensive

Usage

General purpose

Scientific computing

Practical Applications of NumPy

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges