Code
import numpy as np
# Create a NumPy array from a list or tuple:
1, 2, 3]) np.array([
array([1, 2, 3])
Code
# Create an array with a range of numbers:
10) np.arange(
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
NumPy (Numerical Python) is a fundamental package for numerical computing in Python. It provides support for multi-dimensional arrays, mathematical functions, linear algebra operations, and random number generation, making it an essential tool for scientific computing, data analysis, and machine learning.
1. Efficient Array Handling: Supports ndarray, a powerful multi-dimensional array object that is more efficient than Python lists.
2. Vectorized Operations: Eliminates the need for explicit loops by applying operations element-wise.
3. Broadcasting: Allows arithmetic operations on arrays of different shapes without explicit looping.
4. Mathematical Functions: Provides a wide range of functions for algebra, statistics, trigonometry, and more.
5. Random Number Generation: Generates pseudo-random numbers for simulations and machine learning.
6. Integration with Other Libraries: Works seamlessly with pandas, matplotlib, scikit-learn, and TensorFlow.
To install NumPy, use: pip install numpy
Or, if using Anaconda: conda install numpy
np.array([1, 2, 3])
: Create a NumPy array from a list or tuple.np.arange(10)
: Create an array with a range of numbers.import numpy as np
# Create a NumPy array from a list or tuple:
1, 2, 3]) np.array([
array([1, 2, 3])
# Create an array with a range of numbers:
10) np.arange(
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.concatenate((a1, a2), axis=0)
: Join a sequence of arrays along an existing axis.np.split(array, indices_or_sections)
: Split an array into multiple sub-arrays.= np.array([1, 2, 3,])
a1 = np.array([4, 5, 6])
a2 = np.concatenate((a1, a2), axis=0)
a3 a3
array([1, 2, 3, 4, 5, 6])
2,4]) np.split(a3, [
[array([1, 2]), array([3, 4]), array([5, 6])]
np.add(a, b)
, np.subtract(a, b)
, np.multiply(a, b)
, np.divide(a, b)
: Perform element-wise addition, subtraction, multiplication, and division.np.sqrt(a)
: Square root of each element in the array.np.exp(a)
: Calculate the exponential of all elements in the array.np.log(a)
: Natural logarithm of each element in the array.np.power(a, b)
: Elements of a
raised to the powers from b
, element-wise.Python
= np.array([2, 4, 6,])
a = np.array([4, 8, 12])
b
np.add(a, b)
array([ 6, 12, 18])
np.subtract(a, b)
array([-2, -4, -6])
np.multiply(a, b)
array([ 8, 32, 72])
np.divide(a, b)
array([0.5, 0.5, 0.5])
np.sqrt(a)
array([1.41421356, 2. , 2.44948974])
np.exp(a)
array([ 7.3890561 , 54.59815003, 403.42879349])
np.log(a)
array([0.69314718, 1.38629436, 1.79175947])
np.power(a, b)
array([ 16, 65536, 2176782336])
np.mean(a)
: Compute the arithmetic mean along the specified axis.np.median(a)
: Compute the median along the specified axis.np.std(a)
: Compute the standard deviation along the specified axis.np.var(a)
: Compute the variance along the specified axis.np.min(a)
, np.max(a)
: Find the minimum or maximum values.np.argmin(a)
, np.argmax(a)
: Find the indices of the minimum or maximum values.Python
np.mean(a)
4.0
np.median(a)
4.0
np.std(a)
1.632993161855452
np.var(a)
2.6666666666666665
min(a) np.
2
max(a) np.
6