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])
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.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.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.array([ 6, 12, 18])
array([-2, -4, -6])
array([ 8, 32, 72])
array([0.5, 0.5, 0.5])
array([1.41421356, 2. , 2.44948974])
array([ 7.3890561 , 54.59815003, 403.42879349])
array([0.69314718, 1.38629436, 1.79175947])
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.4.0
4.0
1.632993161855452
2.6666666666666665
2
6