For a comprehensive introcution of NumPy, refer to:

**Understanding the Basics of NumPy Arrays**

**Creation**

Let’s understand how to create a basic NumPy array.

```
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
```

This code will output a 1-dimensional array with the numbers 1 through 5. Simple, right?

**Attributes**

Every NumPy array has attributes that give information about its nature.

`ndim`

: Number of dimensions.

`ndim`

: Number of dimensions.```
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim) # Output: 2
```

`shape`

: Size along each dimension.

`shape`

: Size along each dimension.```
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # Output: (2, 3)
```

`size`

: Total number of elements.

`size`

: Total number of elements.```
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size) # Output: 6
```

`dtype`

: Data type of the elements.

`dtype`

: Data type of the elements.```
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.dtype) # Output: int64 (or similar, depending on the platform)
```

**Data Types**

Unlike Python lists, NumPy arrays are homogeneous. This means all elements of an array **must be of the same type**.

Common data types include `int32`

, `float64`

, and many more. You can specify the data type at array creation using the `dtype`

argument.

**Basic Operations**

NumPy arrays, at their core, are like supercharged Python lists. They’re designed to make mathematical operations straightforward and intuitive.

**For a further instruction, refer to:**

**Element-wise operations**

Think of this as giving a small instruction to every item in your list, all at once. For instance, if you want to add 1 to every item:

```
arr = np.array([1, 2, 3])
print(arr + 1) # Outputs [2, 3, 4]
```

Here, instead of looping through each item, NumPy lets you add 1 to all items with a simple operation.

**Array with array operations**

Imagine you have two lists, and you want to add their respective items together. With regular lists, you’d need loops. With NumPy, it’s just:

```
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2) # Outputs [5, 7, 9]
```

**Reshaping and Resizing**

Sometimes, you’ll want to change how your data looks without actually changing the data itself. That’s where reshaping comes in.

**reshape()**

**reshape()**

Imagine you have a line of 9 people and you ask them to form 3 rows of 3 people each. That’s what `reshape`

does to your data.

```
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
matrix = arr.reshape(3, 3)
print(matrix) # Outputs [[1 2 3][4 5 6][7 8 9]]
```

**ravel()**

**ravel()**

Now, if you want them back in a single line, you’d use `ravel`

.

```
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
matrix = arr.reshape(3, 3)
flattened = matrix.ravel()
print(flattened) # Outputs [1 2 3 4 5 6 7 8 9]
```

**Indexing and Slicing**

Remember how you’d pick items from a list using an index? NumPy arrays work the same way, but with some extra magic for multi-dimensional arrays.

**For a further instruction, refer to:**

**Basic Indexing**

Just like picking an item from a list.

```
arr = np.array([1, 2, 3, 4, 5])
print(arr[2]) # Outputs 3
```

- arr[2] means
**the 3rd item**in the list because it start couting from zero. Thus arr[0] means the 1st item, and arr[5] means the 6th item.

**Multi-dimensional Indexing**

Think of a grid on a board game. To pick a spot, you’d specify a row and a column. That’s how you pick items from a 2D NumPy array.

```
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix[1, 2]) # Outputs 6
```

- matrix[1, 2] means
**“the 2nd row, the 3rd item”**.

**Boolean Indexing**

This is like playing a game of “True or False”. You ask a question, and NumPy gives you items that answer “True”.

```
arr = np.array([1, 2, 3, 4, 5])
print(arr[arr > 3]) # Outputs [4, 5]
```

Here, NumPy gives you all items** greater than 3**.

**Advanced Array Manipulations**

**Broadcasting**

Imagine you’re trying to add a single number to a whole list of numbers.

Instead of manually adding that number to each item, NumPy automatically “broadcasts” that number across the entire list for you.

```
arr = np.array([1, 2, 3])
print(arr + 5) # Outputs [6, 7, 8]
```

Here, the number 5 is “broadcasted” across the array, adding it to each element.

**Mathematical Functions**

NumPy comes with a treasure trove of mathematical functions. Think of these as your advanced calculator tools.

**np.sum()**

**np.sum()**

Adds up all the numbers in your array.

```
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
total = np.sum(arr)
print(f"Total sum of the array: {total}") # Outputs 55
```

**np.mean()**

**np.mean()**

Gives you the average of your numbers.

```
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
average = np.mean(arr)
print(f"Average of the array: {average}") # Outputs 5.5
```

`np.max()`

and `np.min()`

`np.max()`

and `np.min()`

Find the highest and lowest numbers, respectively.

```
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
maximum = np.max(arr)
print(f"Maximum value in the array: {maximum}") # Outputs 10
minimum = np.min(arr)
print(f"Minimum value in the array: {minimum}") # Outputs 1
```

**Aggregation Functions**

When you have a lot of data, sometimes you want to summarize it. That’s where aggregation comes in.

`np.cumsum()`

: This gives you a running total of your numbers.

```
arr = np.array([1, 2, 3, 4])
print(np.cumsum(arr)) # Outputs [1, 3, 6, 10]
```