0% found this document useful (0 votes)
1 views

Exploring basics of NumPy Methods

The document provides an overview of fundamental NumPy methods for numerical computations in Python, including array creation, reshaping, mathematical operations, indexing, concatenation, random number generation, and linear algebra methods. Key functions like np.array(), np.zeros(), np.ones(), np.reshape(), and np.dot() are highlighted with examples. It serves as a basic guide for users to understand and utilize NumPy for handling large datasets and performing various operations.

Uploaded by

parthmanjrekar12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Exploring basics of NumPy Methods

The document provides an overview of fundamental NumPy methods for numerical computations in Python, including array creation, reshaping, mathematical operations, indexing, concatenation, random number generation, and linear algebra methods. Key functions like np.array(), np.zeros(), np.ones(), np.reshape(), and np.dot() are highlighted with examples. It serves as a basic guide for users to understand and utilize NumPy for handling large datasets and performing various operations.

Uploaded by

parthmanjrekar12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

‭Exploring basics of NumPy Methods.


‭ umPy is a powerful Python library used for numerical computations, especially when dealing‬
N
‭with large datasets or arrays. It provides various methods to perform operations like‬
‭mathematical calculations, reshaping, and manipulating arrays. Here's an exploration of some‬
‭fundamental NumPy methods:‬

‭1. Creating Arrays‬


np.array()‬
‭ ‭: Creates an array from a Python list or‬‭tuple.‬

import numpy as np‬



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

print(arr)‬

np.zeros()‬
‭ ‭: Creates an array of zeros.‬

zeros_arr = np.zeros((2, 3))


‭ # 2x3 matrix of zeros‬
print(zeros_arr)‬

np.ones()‬
‭ ‭: Creates an array of ones.‬

ones_arr = np.ones((3, 2))


‭ # 3x2 matrix of ones‬
print(ones_arr)‬

np.arange()‬
‭ ‭: Generates an array with a range of values.‬

range_arr = np.arange(0, 10, 2)


‭ # [0, 2, 4, 6, 8]‬
print(range_arr)‬

np.linspace()‬
‭ ‭: Creates an array of evenly spaced values‬‭over a specified range.‬

linspace_arr = np.linspace(0, 1, 5)
‭ # 5 values between 0 and 1‬
print(linspace_arr)‬

‭2. Array Reshaping‬
np.reshape()‬
‭ ‭: Changes the shape of an array without‬‭changing its data.‬

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



reshaped_arr = arr.reshape((2, 3))
‭ # 2x3 matrix‬
print(reshaped_arr)‬

np.flatten()‬
‭ ‭: Converts a multi-dimensional array into‬‭a 1D array.‬

flattened_arr = reshaped_arr.flatten()‬

print(flattened_arr)‬

‭3. Mathematical Operations‬


‭Element-wise operations‬‭: NumPy allows element-wise‬‭mathematical operations on arrays.‬

arr1 = np.array([1, 2, 3])‬



arr2 = np.array([4, 5, 6])‬

sum_arr = arr1 + arr2‬

print(sum_arr)
‭ # [5, 7, 9]‬

‭p.add()‬
n np.subtract()‬
‭,‬‭ np.multiply()‬
‭,‬‭ np.divide()‬
‭,‬‭ ‭:‬‭These functions can be used‬
‭for element-wise operations as well.‬

multiply_arr = np.multiply(arr1, arr2)


‭ # [4, 10, 18]‬
print(multiply_arr)‬

np.dot()‬
‭ ‭: Computes the dot product of two arrays.‬

dot_product = np.dot(arr1, arr2)


‭ # 32 (1*4 + 2*5 + 3*6)‬
print(dot_product)‬

np.sum()‬
‭ np.mean()‬
‭,‬‭ np.std()‬
‭,‬‭ ‭: Statistical operations.‬

sum_arr = np.sum(arr1)
‭ # 6‬
mean_arr = np.mean(arr1)
‭ # 2.0‬
std_arr = np.std(arr1)
‭ # 0.8165‬
print(sum_arr, mean_arr, std_arr)‬

‭4. Array Indexing & Slicing‬


‭Basic indexing‬‭: You can access individual elements‬‭or subarrays.‬

arr = np.array([10, 20, 30, 40, 50])‬



print(arr[0])
‭ # 10‬

‭Slicing‬‭: Access a portion of the array.‬

sliced_arr = arr[1:4]
‭ # [20, 30, 40]‬
print(sliced_arr)‬

‭Advanced indexing‬‭: Using boolean masks or array indices‬‭to index arrays.‬

arr = np.array([10, 20, 30, 40, 50])‬



mask = arr > 30‬

print(arr[mask])
‭ # [40, 50]‬

‭5. Array Concatenation and Splitting‬


np.concatenate()‬
‭ ‭: Concatenate arrays along an existing‬‭axis.‬

arr1 = np.array([1, 2, 3])‬



arr2 = np.array([4, 5, 6])‬

concat_arr = np.concatenate((arr1, arr2))
‭ # [1, 2, 3, 4, 5, 6]‬
print(concat_arr)‬

np.split()‬
‭ ‭: Split an array into multiple sub-arrays.‬
arr = np.array([1, 2, 3, 4, 5, 6])‬

split_arr = np.split(arr, 3)
‭ # [array([1, 2]), array([3, 4]),‬
array([5, 6])]‬

print(split_arr)‬

‭6. Random Number Generation‬


np.random.rand()‬
‭ ‭: Generates an array of random numbers‬‭between 0 and 1.‬

random_arr = np.random.rand(2, 3)
‭ # 2x3 array of random floats‬
print(random_arr)‬

np.random.randint()‬
‭ ‭: Generates random integers.‬
random_int_arr = np.random.randint(0, 10, (2, 3))
‭ # 2x3 array with‬
random integers between 0 and 10‬

print(random_int_arr)‬

‭7. Linear Algebra Methods‬


np.linalg.inv()‬
‭ ‭: Computes the inverse of a matrix.‬

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



inv_matrix = np.linalg.inv(matrix)‬

print(inv_matrix)‬

np.linalg.det()‬
‭ ‭: Computes the determinant of a matrix.‬
determinant = np.linalg.det(matrix)‬

print(determinant)
‭ # -2.0‬

You might also like