Numpy Tutorial

Creating NumPy Array

NumPy Array Manipulation

Matrix in NumPy

Operations on NumPy Array

Reshaping NumPy Array

Indexing NumPy Array

Arithmetic operations on NumPy Array

Linear Algebra in NumPy Array

NumPy and Random Data

Sorting and Searching in NumPy Array

Universal Functions

Working With Images

Projects and Applications with NumPy

Numpy - ndarray

NumPy's ndarray is its core object, representing a multi-dimensional array of homogeneous data. That means, in a single array, all elements must be of the same type.

1. Introduction:

An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the number of items in each dimension.

2. Basic Setup:

Start with importing the necessary library:

import numpy as np

3. Creating ndarray objects:

From a Python List or Tuple:

a = np.array([1, 2, 3])
print(a)            # Output: [1 2 3]
print(type(a))      # Output: <class 'numpy.ndarray'>
print(a.shape)      # Output: (3,)

Using Built-in Functions:

zeros_array = np.zeros((2,2))   # Create an array of zeros
print(zeros_array)              # Output: [[0. 0.]
                               #         [0. 0.]]

ones_array = np.ones((2,2))     # Create an array of ones
print(ones_array)               # Output: [[1. 1.]
                               #         [1. 1.]]

Creating Arrays with Specific Values:

full_array = np.full((2,2), 7)  # Create a constant array
print(full_array)               # Output: [[7 7]
                               #         [7 7]]

identity_array = np.eye(2)      # Create a 2x2 identity matrix
print(identity_array)           # Output: [[1. 0.]
                               #         [0. 1.]]

4. Array Indexing:

Slicing:

a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
b = a[:2, 1:3]    # first 2 rows, columns 1 and 2
print(b)          # Output: [[2 3]
                  #         [6 7]]

Integer Array Indexing:

a = np.array([[1,2], [3, 4], [5, 6]])
print(a[[0, 1, 2], [0, 1, 0]])  # Output: [1 4 5]

5. Data Types:

x = np.array([1, 2])                 # Let numpy choose the datatype
print(x.dtype)                       # Output: int64

x = np.array([1.0, 2.0])             # Let numpy choose the datatype
print(x.dtype)                       # Output: float64

x = np.array([1, 2], dtype=np.int64) # Force a particular datatype
print(x.dtype)                       # Output: int64

6. Array Math:

Basic mathematical functions operate element-wise on arrays:

x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])

print(np.add(x, y))          # Elementwise sum
print(np.subtract(x, y))     # Elementwise difference
print(np.multiply(x, y))     # Elementwise product
print(np.divide(x, y))       # Elementwise division

7. Conclusion:

The ndarray is at the heart of the NumPy library. With its efficient operations, you can perform complex computations on data, whether it's for data science, machine learning, or other fields requiring numerical computations on data. Mastering the ndarray will provide you with a strong foundation for all of NumPy's capabilities.

1. Introduction to ndarray in NumPy:

The NumPy ndarray (n-dimensional array) is a powerful data structure for efficient numerical operations. It provides a flexible container for homogeneous data.

import numpy as np

# Creating a 1D ndarray
array_1d = np.array([1, 2, 3, 4, 5])

print("1D ndarray:")
print(array_1d)

2. Python NumPy array creation and manipulation:

NumPy provides various functions for array creation and manipulation.

# Creating a 2D ndarray
array_2d = np.array([[1, 2, 3], [4, 5, 6]])

# Manipulating array elements
array_2d[1, 2] = 10
array_2d[:, 1] *= 2

print("2D ndarray:")
print(array_2d)

3. NumPy array indexing and slicing with ndarrays:

Indexing and slicing operations are fundamental for accessing and manipulating ndarray elements.

# Indexing and slicing
element = array_2d[0, 1]
subarray = array_2d[:, 1:]

print("Accessed Element:", element)
print("Sliced Subarray:")
print(subarray)

4. Creating and initializing ndarrays in NumPy:

NumPy provides functions for creating and initializing arrays with specific patterns.

# Creating arrays with specific patterns
zeros_array = np.zeros((2, 3))
ones_array = np.ones((3, 2))
random_array = np.random.random((2, 2))

print("Zeros Array:")
print(zeros_array)

print("\nOnes Array:")
print(ones_array)

print("\nRandom Array:")
print(random_array)

5. Vectorized operations with NumPy ndarrays:

Vectorized operations in NumPy enable efficient element-wise operations on arrays.

# Vectorized operations
array_a = np.array([1, 2, 3])
array_b = np.array([4, 5, 6])

result = array_a + array_b

print("Result of Vectorized Operation:")
print(result)

6. NumPy multidimensional arrays:

NumPy supports multidimensional arrays, enabling efficient manipulation of higher-dimensional data.

# Multidimensional arrays
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

print("3D ndarray:")
print(array_3d)

7. Advanced array manipulation in NumPy:

NumPy provides advanced array manipulation functions for reshaping, concatenating, and stacking arrays.

# Advanced array manipulation
reshaped_array = np.reshape(array_a, (3, 1))
concatenated_array = np.concatenate((array_a, array_b))
stacked_array = np.stack((array_a, array_b))

print("Reshaped Array:")
print(reshaped_array)

print("\nConcatenated Array:")
print(concatenated_array)

print("\nStacked Array:")
print(stacked_array)

8. NumPy ndarray vs list in Python:

NumPy ndarrays offer advantages over Python lists, such as efficient vectorized operations and memory management.

# NumPy ndarray vs list
list_a = [1, 2, 3]
list_b = [4, 5, 6]

array_a = np.array(list_a)
array_b = np.array(list_b)

result = array_a + array_b

print("Result using ndarray:")
print(result)

# Comparing with lists
result_list = [a + b for a, b in zip(list_a, list_b)]
print("\nResult using lists:")
print(result_list)