Numpy
- NumPy is nothing but the library of Linear Algebra for Python, the main reason is that it is more important in Data Science using Python.
- Numpy is mathematical function mainly use the array method instead of lists, to perform the faster operation.
First we will study the basics of Numpy, to get it first started with its install it
If you have installed Anaconda already then simply type following command in your Anaconda prompt:
conda install numpy
After installation of Numpy the first step is to import the Numpy library
using following command:
import numpy as np
Numpy Arrays
In NumPy the arrays are the main data type in these library. Numpy arrays mainly divided into in two types:
1. Vectors: Vectors are nothing but the 1-d arrays.
2. Matrices: Matrices are nothing but the 2-d arrays.
Creating Numpy Arrays
From a Python List
Ex.1:
my_list1 = [1,2,3]
np.array(my_list1)
It displays following output:
array([1, 2, 3])
Ex.2:
my_matrix1 = [[1,2,3],[4,5,6],[7,8,9]]
np.array(my_matrix1)
It displays following output:
array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
Built-in Methods
There are many built in types to generate Arrays.
1. arange
In this method, it return evenly spaced values within a given interval.
import numpy as np
np.arange(0,10)
It displays following output:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,11,2)
It displays following output:
array([ 0, 2, 4, 6, 8, 10])
2. zeros and ones
This method is used to generate arrays of zero's or one's.
For example:
import numpy as np
np.zeros((5,5))
It displays following output:
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
3. linspace
It return evenly spaced numbers over a given interval.
For example:
import numpy as np
np.linspace(0,15,4)
It displays following output:
array([ 0., 5., 10., 15.])
4. eye
It is used to creates an identity matrix.
For example:
import numpy as np
np.eye(3)
It displays following output:
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
5. Random
In Numpy there are many ways to create a random number arrays:
rand
It is used to create an array of given shape and elaborate it with random samples from a uniform distribution over (0,1).
For example:
import numpy as np
np.random.rand(3,2)
It display following output:
array([[0.23440635, 0.37017193],
[0.4850963 , 0.45149511],
[0.94609105, 0.15344959]])
Array Attributes and Methods:
Let us discuss with some important array attributes and methods:
1. Reshape
It returns an array that containing the same data with a new shape.
For example:
import numpy as np
arr = np.arange(20)
arr.reshape(5,4)
It display following output:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
2. min, max, argmin,argmax
In this method, it is used to find min or max values. Also argmin and argmax used to find their index locations.
For example:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
a
It displays following output:
array([1, 2, 3, 4, 5])
a.min()
1
a.max()
5
a.argmin()
0
a.argmax()
4
No comments:
Post a Comment