What Is NumPy
What Is NumPy
NumPy is a Python library used for working with arrays. It also has functions for working in
domain of linear algebra, fourier transform, and matrices. NumPy was created in 2005 by
Travis Oliphant. It is an open source project and you can use it freely. NumPy stands for
Numerical Python.
Why Use NumPy?
In Python we have lists that serve the purpose of arrays, but they are slow to process. NumPy
aims to provide an array object that is up to 50x faster than traditional Python lists. The array
object in NumPy is called ndarray, it provides a lot of supporting functions that make
working with ndarray very easy. Arrays are very frequently used in data science, where speed
and resources are very important.
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in memory unlike lists, so processes can
access and manipulate them very efficiently. This behaviour is called locality of reference in
computer science. This is the main reason why NumPy is faster than lists. Also, it is
optimized to work with latest CPU architectures.
Which Language is NumPy written in?
NumPy is a Python library and is written partially in Python, but most of the parts that require
fast computation are written in C or C++.
Installation of NumPy
If you have Python and PIP already installed on a system, then installation of NumPy is very
easy.
Install it using this command:
C:\Users\Your Name>pip install numpy
If this command fails, then use a python distribution that already has NumPy installed like,
Anaconda, Spyder etc.
Import NumPy
Once NumPy is installed, import it in your applications by adding the import keyword:
import numpy
Now NumPy is imported and ready to use.
import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
NumPy as np
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same thing.
Create an alias with the as keyword while importing:
import numpy as np
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Checking NumPy Version
The version string is stored under __version__ attribute.
Example
import numpy as np
print(np.__version__)
Key Features:
Array Creation:
np.array(): Creates an array from a Python list or tuple.
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
# Output: [1 2 3]
np.arange(): Creates an array with evenly spaced values within a given interval.
arange_arr = np.arange(10, 20, 2)
print(arange_arr)
# Output: [10 12 14 16 18]
np.linspace(): Creates an array with evenly spaced values over a specified interval.
linspace_arr = np.linspace(0, 10, 5)
print(linspace_arr)
# Output: [ 0. 2.5 5. 7.5 10. ]
Array Operations:
o Arithmetic operations: +, -, *, /, //, %, **.
o Comparison operations: ==, !=, <, >, <=, >=.
o Logical operations: &, |, ~.
o Universal functions: np.sin(), np.cos(), np.exp(), np.log(), etc.
Example
pip install numpy
import numpy as np
# 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
print("2D Array:\n", array_2d)
# Transpose of a matrix
transposed = np.transpose(array_2d)
print("Transposed Array:\n", transposed)
# Element-wise addition
add_result = array_a + array_b
print("Addition:", add_result)
# Element-wise multiplication
mul_result = array_a * array_b
print("Multiplication:", mul_result)
# Dot product
dot_product = np.dot(array_a, array_b)
print("Dot Product:", dot_product)
# Statistical operations
random_array = np.random.randint(1, 100, size=10)
print("\nRandom Array:", random_array)
print("Mean:", np.mean(random_array))
print("Median:", np.median(random_array))
print("Standard Deviation:", np.std(random_array))
# Step 4: Broadcasting
print("\n### Broadcasting ###")
broadcast_result = array_1d + 5
print("Broadcasting Result (Adding 5):", broadcast_result)
Sample Output
### Array Creation ###
1D Array: [1 2 3 4 5]
2D Array:
[[1 2 3]
[4 5 6]]
Zeros Array:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Range Array: [10 12 14 16 18]
Linspace Array: [0. 0.25 0.5 0.75 1. ]