Exploring basics of NumPy Methods
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:
np.zeros()
: Creates an array of zeros.
np.ones()
: Creates an array of ones.
np.arange()
: Generates an array with a range of values.
np.linspace()
: Creates an array of evenly spaced valuesover 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 withoutchanging its data.
np.flatten()
: Converts a multi-dimensional array intoa 1D array.
flattened_arr = reshaped_arr.flatten()
print(flattened_arr)
p.add()
n np.subtract()
, np.multiply()
, np.divide()
, :These functions can be used
for element-wise operations as well.
np.dot()
: Computes the dot product of two arrays.
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)
sliced_arr = arr[1:4]
# [20, 30, 40]
print(sliced_arr)
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)
np.linalg.det()
: Computes the determinant of a matrix.
determinant = np.linalg.det(matrix)
print(determinant)
# -2.0