Num Py
Num Py
Num Py
1. **int8, int16, int32, int64**: Signed integer types with 8, 16, 32,
and 64 bits respectively.
2. **uint8, uint16, uint32, uint64**: Unsigned integer types with 8,
16, 32, and 64 bits respectively.
3. **float16, float32, float64**: Floating-point types with 16, 32, and
64 bits respectively.
4. **complex64, complex128**: Complex number types with 64 and
128 bits respectively.
5. **bool**: Boolean type representing True or False values.
6. **object**: Object type, allowing any Python object to be stored in
an array.
7. **string_**: Variable-length string type.
8. **unicode_**: Variable-length unicode type.
output
int32
float64
complex128
bool
In addition to these standard data types, NumPy also allows creating
custom data types using the `dtype` class. This allows you to define
structured data types or specify the byte order of the elements, among
other things. Custom data types provide more flexibility in
representing and manipulating complex data structures.
Reshaping arrays
Reshaping arrays in NumPy refers to changing the shape or
dimensions of an existing array without modifying its data. Reshaping
allows you to transform an array from one shape to another, as long as
the total number of elements remains the same. Here's an overview of
reshaping arrays in NumPy:
1. Reshaping 1-D arrays:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
# Reshape to a 2x3 array
reshaped_arr = arr.reshape((2, 3))
print(reshaped_arr)
# Output:
# [[1 2 3]
# [4 5 6]]
2. Reshaping multi-dimensional arrays:
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
# Reshape to a 1-D array
reshaped_arr = arr.reshape((6,))
print(reshaped_arr) # Output: [1 2 3 4 5 6]
# Reshape to a 3x2 array
reshaped_arr = arr.reshape((3, 2))
print(reshaped_arr)
# Output:
# [[1 2]
# [3 4]
# [5 6]]
3. Reshaping with -1 parameter:
The -1 parameter can be used in the reshape function to
automatically infer the size of one dimension based on the given
shape and the size of the other dimensions.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
# Reshape to a 2x3 array (inferred from the size of the array)
reshaped_arr = arr.reshape((2, -1))
print(reshaped_arr)
# Output:
# [[1 2 3]
# [4 5 6]]
# Reshape to a 3x2 array (inferred from the size of the array)
reshaped_arr = arr.reshape((-1, 2))
print(reshaped_arr)
# Output:
# [[1 2]
# [3 4]
# [5 6]]
4. Flattening arrays:
Flattening an array means converting a multi-dimensional array into
a 1-D array.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Flattening the array
flattened_arr = arr.flatten()
print(flattened_arr) # Output: [1 2 3 4 5 6]
Reshaping arrays can be useful when we need to change the
dimensions of an array to match a specific requirement or perform
operations that require a different shape. NumPy provides the
`reshape` function to modify the shape of arrays efficiently.
Array Concatenation:
1. `np.concatenate`: The `np.concatenate` function is used to
concatenate arrays along a specified axis. It takes a sequence of arrays
as input and concatenates them into a single array.
- Syntax: `np.concatenate((array1, array2, ...), axis=0)`
- `array1`, `array2`, ...: The arrays to be concatenated.
- `axis`: The axis along which the arrays will be concatenated. By
default, it is 0 (concatenation along the first axis).
- Example:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.concatenate((arr1, arr2))
print(result)
Output:
[1 2 3 4 5 6]
2. `np.vstack` and `np.hstack`: These functions are used for vertical
and horizontal stacking of arrays, respectively.
- Syntax: `np.vstack((array1, array2, ...))` or `np.hstack((array1,
array2, ...))`
- Example:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result_vstack = np.vstack((arr1, arr2))
result_hstack = np.hstack((arr1, arr2))
print(result_vstack)
print(result_hstack)
Output:
[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6]
Array Splitting:
1. `np.split`: The `np.split` function splits an array into multiple
subarrays of equal size. It takes an array, the indices or sections at
which to split, and the axis along which to split.
- Syntax: `np.split(array, indices_or_sections, axis=0)`
- `array`: The array to be split.
- `indices_or_sections`: An integer specifying the number of
equally-sized subarrays or a sequence of indices indicating the split
positions.
- `axis`: The axis along which the array will be split. By default, it is
0 (splitting along the first axis).
- Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
result = np.split(arr, 3)
print(result)
Output:
[array([1, 2]), array([3, 4]), array([5, 6])]
2. `np.vsplit` and `np.hsplit`: These functions split an array vertically
and horizontally, respectively.
- Syntax: `np.vsplit(array, indices_or_sections)` or `np.hsplit(array,
indices_or_sections)`
- `array`: The array to be split.
- `indices_or_sections`: An integer specifying the number of
equally-sized subarrays or a sequence of indices indicating the split
positions.
- Example:
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
result_vsplit = np.vsplit(arr, 3)
result_hsplit = np.hsplit(arr, 3)
print(result_vsplit)
print(result_hsplit)
Output:
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]
[array([[1],
[4],
[7]]),
array([[2],
[5],
[8]]),
array([[3],
[6],
[9]])]
These numpy functions provide flexible ways to concatenate arrays
along different axes and split arrays into multiple smaller arrays.
Array aggregations
Array aggregations in numpy involve performing operations to
summarize or compute statistical metrics on arrays. These operations
help in analyzing data and extracting useful insights. 1. `np.sum`: This
function calculates the sum of array elements along the specified axis
or over the entire array.
- Syntax: `np.sum(array, axis=None)`
- `array`: The input array.
- `axis`: The axis along which the sum is calculated. By default, it
sums over all elements.
- Example:
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
total_sum = np.sum(arr)
row_sum = np.sum(arr, axis=1)
col_sum = np.sum(arr, axis=0)
print(total_sum)
print(row_sum)
print(col_sum)
Output:
21
[6 15]
[5 7 9]
2. `np.mean`: This function calculates the arithmetic mean of array
elements along the specified axis or over the entire array.
- Syntax: `np.mean(array, axis=None)`
- `array`: The input array.
- `axis`: The axis along which the mean is calculated. By default, it
calculates the mean over all elements.
- Example:
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
overall_mean = np.mean(arr)
row_mean = np.mean(arr, axis=1)
col_mean = np.mean(arr, axis=0)
print(overall_mean)
print(row_mean)
print(col_mean)
Output:
3.5
[2. 5.]
[2.5 3.5 4.5]
3. `np.min` and `np.max`: These functions compute the minimum and
maximum values of array elements along the specified axis or over
the entire array.
- Syntax: `np.min(array, axis=None)` and `np.max(array,
axis=None)`
- `array`: The input array.
- `axis`: The axis along which the minimum and maximum are
calculated. By default, they are calculated over all elements.
- Example:
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
min_value = np.min(arr)
max_value = np.max(arr)
row_min = np.min(arr, axis=1)
col_max = np.max(arr, axis=0)
print(min_value)
print(max_value)
print(row_min)
print(col_max)
Output:
1
6
[1 4]
[4 5 6]
Computations on arrays in numpy involve performing mathematical
operations on arrays and applying functions element-wise. These
operations allow for efficient computation and manipulation of array
data.
1. Element-wise Operations:
- Arithmetic Operations: Numpy supports basic arithmetic
operations such as addition (`+`), subtraction (`-`), multiplication
(`*`), division (`/`), and exponentiation (`**`) on arrays. These
operations are performed element-wise.
- Example:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Element-wise addition
result_add = arr1 + arr2
print(result_add)
# Element-wise multiplication
result_mul = arr1 * arr2
print(result_mul)
Output:
[5 7 9]
[4 10 18]
2. Universal Functions (ufuncs):
- Numpy provides a wide range of universal functions (ufuncs) that
operate element-wise on arrays. These functions can perform
mathematical operations, trigonometric calculations, logarithmic
operations, etc.
- Example:
import numpy as np
arr = np.array([1, 2, 3])
# Square root
result_sqrt = np.sqrt(arr)
print(result_sqrt)
# Exponential function
result_exp = np.exp(arr)
print(result_exp)
Output:
[1. 1.41421356 1.73205081]
[ 2.71828183 7.3890561 20.08553692]
3. Aggregation Functions:
- Numpy's aggregation functions (e.g., `np.sum`, `np.mean`,
`np.min`, `np.max`) can be applied element-wise to arrays, computing
summary statistics over the entire array or along specific axes.
- Example:
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Sum of all elements
total_sum = np.sum(arr)
print(total_sum)
# Mean along axis 0
mean_axis0 = np.mean(arr, axis=0)
print(mean_axis0)
Output:
21
[2.5 3.5 4.5]
NumPy's structured arrays
NumPy's structured arrays, also known as structured data types, allow
for storing and manipulating heterogeneous data in a tabular format
similar to a database table or spreadsheet. A structured array can
contain elements of different data types organized into named fields.
This provides a way to work with structured data within the NumPy
framework.
1. Defining a Structured Data Type:
- Structured data types are defined using the `np.dtype` function,
specifying the names and data types of each field.
- Example:
import numpy as np
# Define a structured data type with two fields: 'name' and 'age'
dt = np.dtype([('name', np.str_, 16), ('age', np.int32)])
print(dt)
Output:
[('name', '<U16'), ('age', '<i4')]
2. Creating a Structured Array:
- A structured array is created by providing data and specifying the
structured data type.
- Example:
import numpy as np
# Define a structured data type with two fields: 'name' and 'age'
dt = np.dtype([('name', np.str_, 16), ('age', np.int32)])
# Create a structured array using the defined data type
arr = np.array([('Alice', 25), ('Bob', 30)], dtype=dt)
print(arr)
Output:
[('Alice', 25) ('Bob', 30)]
3. Accessing Structured Array Fields:
- Fields of a structured array can be accessed using field names or
indices.
- Example:
import numpy as np
# Define a structured data type with two fields: 'name' and 'age'
dt = np.dtype([('name', np.str_, 16), ('age', np.int32)])
# Create a structured array using the defined data type
arr = np.array([('Alice', 25), ('Bob', 30)], dtype=dt)
# Access the 'name' field
print(arr['name'])
# Access the 'age' field
print(arr['age'])
Output:
['Alice' 'Bob']
[25 30]
4. Performing Operations on Structured Arrays:
- Structured arrays support various operations, including filtering,
slicing, sorting, and aggregation.
- Example:
import numpy as np
# Define a structured data type with two fields: 'name' and 'age'
dt = np.dtype([('name', np.str_, 16), ('age', np.int32)])
# Create a structured array using the defined data type
arr = np.array([('Alice', 25), ('Bob', 30), ('Charlie', 35)], dtype=dt)
# Filter the structured array based on age
filtered_arr = arr[arr['age'] > 30]
print(filtered_arr)