NumPy
NumPy is short for Numerical Python. It is a Python library/package used for working with arrays which contains classes, functions, variables , a large library of mathematical functions etc for working with scientific calculation. It can be used to create an βnβ dimensional array where βnβ is any integer.
Why NumPy
In Python we have lists that serve the purpose of arrays, but they are slow. NumPy aims to provide an array object that is up to 50x faster than a traditional Python list.
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.
What makes NumPy arrays 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 behavior is called locality of reference. This is the main reason why NumPy is faster than lists. It is also optimized to work with the latest CPU architectures.
Installing NumPy
To install NumPy, you can use pip, the Python package installer. Open your terminal or command prompt and enter the following command:
Importing NumPy
There are two ways to import NumPy. Code Example :
# this will import the entire NumPy module.
import numpy as np
# this will import all class, objects, variables etc from the NumPy package
from numpy import*
alias: In Python aliases are an alternate name for referring to the same thing.
Creating NumPy Arrays
The array object in NumPy is called ndarray. We can create a NumPy ndarray object by using the array() function. NumPy arrays can be created in a number of ways. Here are some of the most common methods:
- Using the numpy.array() function to create an array from a list/tuple:
- Using the numpy.random.randint(): Returns an array of random integers between the two given numbers
NumPy Array Dimensions
A dimension in arrays is one level of array depth (nested arrays). nested array: are arrays that have arrays as their elements. 0-D Arrays 1-D Arrays 2-D Arrays 3-D Arrays.
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
Code Example:
An array that has 0-D arrays as its elements is called a uni-dimensional or 1-D array. These are the most common types of arrays.
Code Example :
An array that has 1-D arrays as its elements is called a 2-D array. These are often used to represent matrix or 2nd order tensors.
Code Example :
An array that has 2-D arrays (matrices) as its elements is called 3-D array. These are often used to represent a 3rd order tensor.
Code Example :
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array has.
Code Example :
An array can have any number of dimensions. When the array is created, you can define the number of dimensions by using the ndim argument.Code Example :
Arrays type and shape
NumPy Array Data Type :The NumPy array object has a property called dtype that returns the data type of the array.
Code Example :
NumPy Array Shape : The shape of an array is the number of elements in each dimension. NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elementsCode example :
NumPy Array Indexing
Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
Code Example :
To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element.Code Example :
To access elements from 3-D arrays we can use comma separated integers representing the dimensions and the index of the element.Code Example :
Use negative indexing to access an array from the end Code ExampleNumPy Array Slicing
Slicing in python means taking elements from one given index to another given index. We pass slice instead of index like this [start: end]. We can also define the step, like this [start:end:step].
- If we don't pass start its considered 0
- If we don't pass end its considered length of array in that dimension
- If we don't pass step its considered 1
Note: The result includes the start index, but excludes the end index. Use the minus operator to refer to an index from the end.
Code Example :
arr=np.array([1,2,3,4,5])
print (arr[0:5]) #slice arrays from index 0 to 5 excluding 5 print (arr[:4]) #slice from beginning to 4 excluding 4
print (arr[2:]) #slice from index 2 onwards
print (arr[:-3]) #slice from index -3
arr=np.array([1,2,3,4,5])
print (arr[:4:1]) #slice from beginning to 4 step of 1 print (arr[0::3]) #slice from index 0 onwards step of 3 print (arr[:-1:2]) #slice from index -1 step of 2
Array Operations in NumPy
Element-wise Operations : NumPy allows you to perform element-wise operations on arrays. Here are some examples:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Element-wise addition
c = a + b
print(c) # Output: [5 7 9]
# Element-wise multiplication
d = a * b
print(d) # Output: [ 4 10 18]
Matrix Multiplication : NumPy allows you to perform matrix multiplication using the numpy.dot() function. Here is an example:
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# Matrix multiplication
c = np.dot(a, b)
print(c)
NumPy Array Reshaping : Reshaping means changing the shape of an array. The shape of an array is the number of elements in each dimension. By reshaping we can add or remove dimensions or change number of elements in each dimension.
Code Example :
# we can do it this way
arr=np.array([1,2,3,4,5,6,7,8,9,10])
arr2= arr.reshape(5,2)
print (arr)
print (arr2)
# or this way
a = np.array([1, 2, 3, 4, 5, 6])
b = np.reshape(a, (2, 3))
print (a)
print (b)
Transposing Arrays : NumPy provides a way to transpose arrays using the numpy.transpose() function. Here is an example:
Aggregation Functions : NumPy provides several aggregation functions that can be used to compute statistics on arrays. Here are some examples:a = np.array([1, 2, 3, 4, 5])
# Computing the sum of the elements in the array
print(np.sum(a)) # Output: 15
# Computing the mean of the elements in the array
print(np.mean(a)) # Output: 3.0
# Computing the standard deviation of the elements in the array
print(np.std(a)) # Output: 1.41421356
NumPy Array Copy and View
The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a view, or link to, the original array.
The copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original array will not affect the copy.
The view does not own the data and any changes made to the view will affect the original array, and any changes made to the original array will affect the view.
As mentioned, copies owns the data, and views does not own the data, so how can we check if it owns the data or not?
Every NumPy array has the attribute base that returns None if the array owns the data. Otherwise, the base attribute refers to the original object
Code example :