You are on page 1of 10

Numpy Tutorial:-

1.What is Numpy?
2.Numpy v/s List
3.Numpy Operations
4.Numpy Special Functions
1. Numpy:-
*Numpy is the core library for scientific
computing in Python.
*It provides a high-performance
multidimensional array object, and tools for
working with these arrays.
E.g:-
import numpy as np
a=np.array([1,2,3])
print(a)
2. Numpy v/s List:-
Advantages of Numpy over List:-
1.Less Memory
2.Fast
3.Convenient
1.Less Memory:-
E.g:-
import numpy as np
import time
import sys

S=range(1000)
print(sys.getsizeof(5)*len(S ))

D=np.arange(1000)

print(D.size*D.itemsize)
2.Fast:-
Example:-
import numpy as np
import time
import sys
size=1000000
l1=range(size)
l2=range(size)
a1=np.arange(size)
a2=np.arange(size)
start=time.time()
result=[(x,y) for x,y in zip(l1,l2)]
print((time.time()-start)*1000)
start=time.time()
result=a1+a2
print((time.time()-start)*1000)
3. Numpy Operations:-
1.Find the dimension of the array.
2.Find the byte size of each element.
3.Find the data type of the elements.
1.Find the dimension of the array:-
Example:-
import numpy as n
a=n.array([1,2,3])
print(a.ndim)
print(a.itemsize)
print(a.dtype)

print(a.size) //Size tell us about the row only.


print(a.shape) //Shape tell us about the whole
array (row and column both)
Reshape:-Change the number of rows and
colums or A'.
Example:-
import numpy as np
a=np.array([(2,3,2,4),(3,4,3,5)])
print(a)
a=a.reshape(4,2)
print(a)

*To print particular element:-


print(a[0,2])
Output:-3

*To print particular column:-


Here we're going to print the column third.
print(a[0:,3])
*To print the particular column values from
the arrays:-
a=np.array([(1,2,3,4,5),(6,7,8,9,0),(7,8,9,4)])
print(a[0:2,3])
//It will print the index third values till the first
two arrays.
Output:-[4 9]

*To calculate the linespace between the


values:-
Example:-
import numpy as np
a=np.linespace(1,4,10)
print(a)
//this will give you ten values from 1 to 4 which
have equal space b/w all of them.
Minimum,Maximum & Sum of Numpy Array:-
Example:-
import numpy as np
a=np.array([1,2,3])
print(a.sum())

Output:-6

a=np.array([(1,3,4),(3,2,2)])
print(a.sum(axis=1)
output:-[4 5 6]
Finding the Square Roots & Standard
Deviations:-
Finding the Square Roots:-
print(np.sqrt(a))
Finding the Standard Deviatons:-
print(np.std(a))

Addition,Multiplication,Division,Subtraction:-
For all,we consider two arrays 'a' and 'b'.
Addition:-print(a+b)
Subtraction:-print(a-b)
Multiplication:-print(a*b)
Division:-print(a/b)

Vertical Stacking & Horizontal Stacking:-


print(np.vstack((a,b)))
print(np.hstack((a,b)))
*To convert the array into a single row:-
print(a.ravel()) or print(np.ravel(a))

Sine & Cosine Functions:-


Example:-
import numpy as np
import matplotlib.pylot as plt
x=np.arange(0,3*np.pi,0.1)
y=np.sin(x) [or y=np.cos(x)]
plt.plot(x,y)
plt.show()
Numpy Specail Functions:-
1.Exponential Function
2.Logarithmic Function
1.Exponential Function:-
Example:-
import numpy as np
import matplotlib.pylot as plt

a=np.array([1,3,4])
print(np.exp(a))

2.Logarithmic Functions:-
print(np.log(a))//Natural Log
print(np.log10(a))//Log with base 10

You might also like