Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

NumPy Fundamentals

1. Introduction

NumPy is a powerful library for numerical computing in Python. It provides support for arrays, matrices, and a host of mathematical functions to operate on these data structures.

Key features of NumPy include:

  • Support for multidimensional arrays and matrices
  • Mathematical functions for operations on arrays
  • Tools for integrating C/C++ and Fortran code
  • Linear algebra, Fourier transforms, and random number generation

2. Installation

To install NumPy, you can use pip, the package installer for Python. Run the following command in your terminal:

pip install numpy

3. NumPy Arrays (ndarrays)

NumPy's main object is the ndarray, which is a fast and flexible container for large data sets in Python.

Creating Arrays

You can create NumPy arrays from lists or tuples:

import numpy as np

# Create a NumPy array from a list
arr = np.array([1, 2, 3, 4, 5])
print(arr)  # Output: [1 2 3 4 5]

Array Attributes

Common attributes of ndarrays include:

  • shape: The dimensions of the array
  • dtype: The data type of the array elements
  • ndim: The number of dimensions of the array

Example:

print(arr.shape)  # Output: (5,)
print(arr.dtype)   # Output: int64
print(arr.ndim)    # Output: 1

4. Array Operations

NumPy provides a range of mathematical operations that can be performed on arrays. This includes:

  • Arithmetic operations (addition, subtraction, multiplication, division)
  • Statistical operations (mean, median, variance)
  • Linear algebra operations (dot product, matrix multiplication)

Example of Basic Operations:

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# Element-wise addition
result = arr1 + arr2
print(result)  # Output: [5 7 9]

5. Indexing and Slicing

Indexing and slicing in NumPy is similar to Python lists but with more powerful features.

Indexing

You can access elements of an array using indices:

arr = np.array([10, 20, 30, 40, 50])
print(arr[0])  # Output: 10
print(arr[1:4])  # Output: [20 30 40]

Slicing

To slice an array, specify a range of indices:

slice = arr[1:4]
print(slice)  # Output: [20 30 40]

6. FAQ

What is NumPy?

NumPy is a library for Python that provides support for arrays and matrices, along with a collection of mathematical functions to operate on them.

Why should I use NumPy?

NumPy is optimized for performance and can handle large datasets efficiently. It also provides a convenient way to perform complex mathematical operations.

Is NumPy compatible with other libraries?

Yes, NumPy is compatible with many other Python libraries, including Pandas, Matplotlib, and SciPy, making it a fundamental tool in data science and machine learning.