NumPy stands for Numerical Python. NumPy is a python library used for working with arrays.
array data structure is a data structure consisting of a collection of elements (values or variables).
NumPy was created in 2005 by Travis Oliphant. It is an open source project and can be used freely.
NumPy is faster than lists because:
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 in computer science.
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
Once NumPy is installed, import it in your applications by adding the import keyword:
NumPy is used to work with arrays. The array object in NumPy is called ndarray.
We can create a NumPy ndarray object by using the array () function.
Now Lets start with the program:
Lets create a NumPy ndarray object:
import numpy as np arr = np.array([7,8,9,10]) print(arr) print(type(arr))
Type( ) is the built -in python function that tells us about the type of object passed, that arr is numpy.ndarray type.
We have multi dimensional array in NumPy.
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.
import numpy as np
a = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
here ndim attribute is 3 . that is the array is 3 dimensional.
Hope u understood the concept of array,
JOINING - NumPy Arrays
In NumPy Joining means putting contents of two or more arrays in a single array.
whereas in NumPy we join arrays by axes.
We pass a sequence of arrays that we want to join to the concatenate() function, along with the axis. If axis is not explicitly passed, it is taken as 0.
import numpy as np arr1 = np.array([11, 12, 13]) arr2 = np.array([14, 15, 16]) arr = np.concatenate((arr1, arr2)) print(arr)
[11 12 13 14 15 16]
Now, moving to
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].
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70]) print(arr[1:5:2])
OUTPUT: [20 40]
returns every other element from index 1 to index5.
Now, moving to DATATYPES in NumPy
DATA TYPES IN NumPy
i - integer
b - boolean
u - unsigned integer
f - float
c - complex float
m - timedelta
M - datetime
O - object
S - string
U - unicode string
V - fixed chunk of memory for other type ( void )
above is a list of all data types in NumPy and the characters used to represent them.
The NumPy array object has a property called dtype that returns the data type of the array:
Lets move to Copy and View in NumPy 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.
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 of the original array. Let me explain this with the example
import numpy as np arr = np.array([10, 20, 30, 40, 50]) x = arr.copy() arr = 60 print(arr) print(x)
[10 20 30 40 50]
[10 60 30 40 50]
import numpy as np arr = np.array([10, 20, 30, 40, 50]) x = arr.view() arr = 60 print(arr) print(x)
[10 60 30 40 50]
[10 60 30 40 50]
NumPy Array Shapes and Reshape
Shape of an Array
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 elements.
import numpy as np arr = np.array([[10, 20, 30, 40], [50, 60, 70, 80]]) print(arr.shape)
In the above the outcome is (2, 4), which means that the array has 2 dimensions, and each dimension has 4 elements.
Reshaping as name says its changing the shape of an array.
By reshaping we can add or remove dimensions or change number of elements in each dimension.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) newarr = arr.reshape(2, 3, 2) print(newarr)
[ 11 12]]]
For the above example, The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements.
We can Reshape into any shape, as long as the elements required for reshaping are equal in both shapes.
We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9 elements.
LIMITATIONS OF NumPy
· NumPy's np.concatenate([a1,a2]) operation does not actually link the two arrays but returns a new one, filled with the entries from both given arrays in sequence.
Reshaping the dimensionality of an array with np.reshape(...) is only possible as long as the number of elements in the array does not change. These circumstances originate from the fact that NumPy's arrays must be views on contiguous memory buffers.
Inserting or appending entries to an array is not as trivially possible as it is with Python's lists. The np.pad(...) routine to extend arrays actually creates new arrays of the desired shape and padding values, copies the given array into the new one and returns it.
THANK YOU FOR READING
Hope u liked it.......