Numpy
Numpy
Numpy
1.Create 1-D array called zeros having 10 elements and all the elements are
set to zero.
import numpy as np
array1=np.zeros((10,))
print(array1)
Output:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
1a. Find the dimensions, shape, size, data type of the items and itemsize of
array zeros
import numpy as np
array1=np.zeros((10,))
print(array1)
print(array1.ndim)
print(array1.shape)
print(array1.dtype)
print(array1.itemsize)
print(array1.size)
Output:
===========
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
1
(10,)
float64
8
10
1b. Split the array zeros at array index 2, 5, 7, 8 and store the resulting
arrays in zerosA, zerosB, zerosC and zerosD and print them.
import numpy as np
X=np.zeros([10])
a,b,c,d,e=np.split(x,[2,5,7,8])
print(a)
print(b)
print(c)
print(d)
print(e)
Output:
=================
[0. 0.]
[0. 0. 0.]
[0. 0.]
[0.]
[0. 0.]
2. Create 1-D array called vowels having the elements ‘a’,‘e’, ‘i’, ‘o’ and ‘u’.
import numpy as np
array2=np.array(['a','e','i','o','u'])
print(array2)
Output:
['a' 'e' 'i' 'o' 'u']
2b. Display the 2nd and 3rd element of the array vowels.
import numpy as np
array2=np.array(['a','e','i','o','u'])
print(array2[1:3])
Output:
['e' 'i']
2c. Find the dimensions, shape, size, data type of the items and itemsize of
array vowels.
import numpy as np
array2=np.array(['a','e','i','o','u'])
print(array2)
print(array2.ndim)
print(array2.shape)
print(array2.size)
print(array2.dtype)
print(array2.itemsize)
Output:
===========
['a' 'e' 'i' 'o' 'u']
1
(5,)
5
<U1
4
3. Create 2-D array called ones having 2 rows and 5 columns and all the
elements are set to 1 and dtype as int.
import numpy as np
array3=np.ones([2,5],dtype=int)
print(array3)
output:
===========
[[1 1 1 1 1]
[1 1 1 1 1]]
3a. Reshape the array ones to have all the 10 elements in a single row.
import numpy as np
array3=np.ones([2,5],dtype=int)
print(array3)
print(array3.reshape(1,10))
Output:
[[1 1 1 1 1]
[1 1 1 1 1]]
[[1 1 1 1 1 1 1 1 1 1]]
3b. Find the dimensions, shape, size, data type of the items and itemsize of
array ones.
import numpy as np
array3=np.ones([2,5],dtype=int)
print(array3)
print(array3.ndim)
print(array3.shape)
print(array3.size)
print(array3.dtype)
Output:
[[1 1 1 1 1]
[1 1 1 1 1]]
2
(2, 5)
10
int32
4. Use nested Python lists to create a 2-D array called myarray1 having 3
rows and 3 columns and store
the following data:
2.7 -2 -19
0 3.4 99.9
10.6 0 13
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(my_array1)
Output:
===========
[[ 2.7 -2. -19. ]
[ 0. 3.4 99.9]
[ 10.6 0. 13. ]]
4a. Display the elements in the 1st column of the 2nd and 3rd row of the array
myarray1.
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(my_array1[1:2,0:1])
print(my_array1[2:3,0:1])
Output:
[[0.]]
[[10.6]]
4b. Display the elements in the 1st and 2nd column of the array myarray1.
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(my_array1[0:1,0:2])
print(my_array1[1:2,0:2])
print(my_array1[2:3,0:2])
Output:
===========
[[ 2.7 -2. ]]
[[0. 3.4]]
[[10.6 0. ]]
4c. Display all elements in the 2nd and 3rd row of the array myarray1.
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(my_array1[1:3,0:3])
Output:
===========
[[ 0. 3.4 99.9]
[10.6 0. 13. ]]
4d. Find the dimensions, shape, size, data type of the items and itemsize of
array myarray1
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(my_array1.ndim)
print(my_array1.size)
print(my_array1.dtype)
print(my_array1.shape)
print(my_array1.itemsize)
Output:`
===========
2
9
float64
(3, 3)
8
4e. Find the cube of all elements of myarray1 and divide the resulting array
by 2
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print((my_array1**3)/2)
Output:
===========
[[ 9.841500e+00 -4.000000e+00 -3.429500e+03]
[ 0.000000e+00 1.965200e+01 4.985015e+05]
[ 5.955080e+02 0.000000e+00 1.098500e+03]]
4f. Sort the myarray1 such that it brings the lowest value of the column in the
first row and soon
import numpy as np
my_array1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(np.sort(my_array1,axis=0))
Output:
===========
[[ 0. -2. -19. ]
[ 2.7 0. 13. ]
[ 10.6 3.4 99.9]]
5. A 2-D array called myarray2 using arange() having 3 rows and 5 columns
with start value = 4, step size 4and dtype as float.
import numpy as np
myarray2=np.arange(4,61,4)
a=myarray2.reshape(3,5)
a=a.astype(float)
print(a)
Output:
[[ 4. 8. 12. 16. 20.]
[24. 28. 32. 36. 40.]
[44. 48. 52. 56. 60.]]
5a. Find the dimensions, shape, size, data type of the items and itemsize of
array myarray2.
import numpy as np
myarray2=np.arange(4,61,4)
a=myarray2.reshape(3,5)
a=a.astype(float)
print(a.ndim)
print(a.shape)
print(a.size)
print(a.dtype)
print(a.itemsize)
Output:
2
(3, 5)
15
float64
8
5b. Find the square root of all elements of myarray2 and divide the resulting
array by 2. The result should be rounded to two places of decimals.
import numpy as np
myarray2=np.arange(4,61,4)
a=myarray2.reshape(3,5)
a=a.astype(float)
b=np.sqrt(a)
c=b/2
d=np.round(c,2)
print(d)
Output:
===========
[[1. 1.41 1.73 2. 2.24]
[2.45 2.65 2.83 3. 3.16]
[3.32 3.46 3.61 3.74 3.87]]
5d. Use NumPy. split() to split the array myarray2 into 5 arrays columnwise.
Store your resulting arrays in myarray2A, myarray2B, myarray2C,
myarray2D and myarray2E. Print the arraysmyarray2A, myarray2B,
myarray2C, myarray2D and myarray2E
import numpy as np
myarray2=np.arange(4,61,4)
x=myarray2.reshape(3,5)
x=x.astype(float)
print(x)
a,b,c,d,e=np.split(x,5,axis=1)
print(a)
print(b)
print(c)
print(d)
print(e)
Output:
===========
[[ 4. 8. 12. 16. 20.]
[24. 28. 32. 36. 40.]
[44. 48. 52. 56. 60.]]
[[ 4.]
[24.]
[44.]]
[[ 8.]
[28.]
[48.]]
[[12.]
[32.]
[52.]]
[[16.]
[36.]
[56.]]
[[20.]
[40.]
[60.]]
A= 2.7 -2 -19
0 3.4 99.9
10.6 0 13
4 8 12 16 20
B= 24 28 32 36 40
44 48 52 56 60
import numpy as np
a=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
b=np.array([[4,8,12,16,20],[24,28,32,36,40],[44,48,52,56,60]])
print(a)
print(b)
Output:
===========
[[ 2.7 -2. -19. ]
[ 0. 3.4 99.9]
[ 10.6 0. 13. ]]
[[ 4 8 12 16 20]
[24 28 32 36 40]
[44 48 52 56 60]]
6.b. Subtract array A from B array and store the result in a new array.
import numpy as np
a=np.array([[2.7,-2,-19,0,0],[0,3.4,99.9,0,0],[10.6,0,13,0,0]])
b=np.array([[4,8,12,16,20],[24,28,32,36,40],[44,48,52,56,60]])
d=a-b
print(d)
Output:
[[ -1.3 -10. -31. -16. -20. ]
[-24. -24.6 67.9 -36. -40. ]
[-33.4 -48. -39. -56. -60. ]]
Output:
[[ 10.8 -16. -228. 0. 0. ]
[ 0. 95.2 3196.8 0. 0. ]
[ 466.4 0. 676. 0. 0. ]]
6.d.Do the matrix multiplication of arrayA and B array and store the result in a new array C.
import numpy as np
a=np.array([[2.7,-2,-19,0,0],[0,3.4,99.9,0,0],[10.6,0,13,0,0]])
b=np.array([[4,8,12,16,20],[24,28,32,36,40],[44,48,52,56,60]])
output:
[[ 0.675 -0.25 -1.58333333 0. 0. ]
[ 0. 0.12142857 3.121875 0. 0. ]
[ 0.24090909 0. 0.25 0. 0. ]]
7. Create a 2-D array called myarray4 using arange() having 14 rows and 3
columns with start value = -1, step size 0.25 having. Split this array row wise
into 3 equal parts and print the result.
import numpy as np
myarray4=np.arange(-1,9.5,0.25)
n=myarray4.reshape(14,3)
print(n)
a,b,c,d=np.split(n,[4,8,12],axis=0)
print(a)
print(b)
print(c)
print(d)
Output:
[[-1. -0.75 -0.5 ]
[-0.25 0. 0.25]
[ 0.5 0.75 1. ]
[ 1.25 1.5 1.75]
[ 2. 2.25 2.5 ]
[ 2.75 3. 3.25]
[ 3.5 3.75 4. ]
[ 4.25 4.5 4.75]
[ 5. 5.25 5.5 ]
[ 5.75 6. 6.25]
[ 6.5 6.75 7. ]
[ 7.25 7.5 7.75]
[ 8. 8.25 8.5 ]
[ 8.75 9. 9.25]]
========
[[-1. -0.75 -0.5 ]
[-0.25 0. 0.25]
[ 0.5 0.75 1. ]
[ 1.25 1.5 1.75]]
[[2. 2.25 2.5 ]
[2.75 3. 3.25]
[3.5 3.75 4. ]
[4.25 4.5 4.75]]
[[5. 5.25 5.5 ]
[5.75 6. 6.25]
[6.5 6.75 7. ]
[7.25 7.5 7.75]]
[[8. 8.25 8.5 ]
[8.75 9. 9.25]]
output:
[[-1. -0.75 -0.5 ]
[-0.25 0. 0.25]
[ 0.5 0.75 1. ]
[ 1.25 1.5 1.75]
[ 2. 2.25 2.5 ]
[ 2.75 3. 3.25]
[ 3.5 3.75 4. ]
[ 4.25 4.5 4.75]
[ 5. 5.25 5.5 ]
[ 5.75 6. 6.25]
[ 6.5 6.75 7. ]
[ 7.25 7.5 7.75]
[ 8. 8.25 8.5 ]
[ 8.75 9. 9.25]]
9.25
output:
[[-1. -0.75 -0.5 ]
[-0.25 0. 0.25]
[ 0.5 0.75 1. ]
[ 1.25 1.5 1.75]
[ 2. 2.25 2.5 ]
[ 2.75 3. 3.25]
[ 3.5 3.75 4. ]
[ 4.25 4.5 4.75]
[ 5. 5.25 5.5 ]
[ 5.75 6. 6.25]
[ 6.5 6.75 7. ]
[ 7.25 7.5 7.75]
[ 8. 8.25 8.5 ]
[ 8.75 9. 9.25]]
[-0.75 0. 0.75 1.5 2.25 3. 3.75 4.5 5.25 6. 6.75 7.5
8.25 9. ]
7.g.Find the standard deviation column wise.
import numpy as np
myarray4=np.arange(-1,9.5,0.25)
n=myarray4.reshape(14,3)
print(n)
print(np.std(n,axis=0))
output:
[[-1. -0.75 -0.5 ]
[-0.25 0. 0.25]
[ 0.5 0.75 1. ]
[ 1.25 1.5 1.75]
[ 2. 2.25 2.5 ]
[ 2.75 3. 3.25]
[ 3.5 3.75 4. ]
[ 4.25 4.5 4.75]
[ 5. 5.25 5.5 ]
[ 5.75 6. 6.25]
[ 6.5 6.75 7. ]
[ 7.25 7.5 7.75]
[ 8. 8.25 8.5 ]
[ 8.75 9. 9.25]]
[3.02334666 3.02334666 3.02334666]
8. Calculate and find the solution using financial functions.
What is the future value after 10 years of saving 100/- now, with an additional monthly savings of
100/-. Assume the interest rate is 5% (annually) compounded monthly?
a.
import numpy_financial as npf
pv=int(input("Enter present value"))
rate=int(input("enter rate of interest"))
rate=(rate/100)/12
time=int(input("enter tenure"))
time=time*12
amt=int(input("enter amount"))
fv=npf.fv(rate,time,-amt,-pv)
print("future value is ",fv)
output:
Enter present value100
enter rate of interest5
enter tenure10
enter amount100
future value is 15692.928894335748
9. What is the present value (e.g., the initial investment) of an investment that needs to
total 15692.93 after 10 years of saving 100 every month? Assume the interest rate is 5%
(annually) compounded monthly.
Output:
Enter amount to be borrowed200000
enter rate of interest7.5
enter tenure15
monthly payment is -1854.0247200054619
11. If you only had $150/month to pay towards the loan, how long would it take to pay-off a
loan of $8,000 at 7% annual interest?
Output:
Enter loan amount8000
enter rate of interest7
enter monthly payment150
number of months to pay 64
12. Write a program in python to find the rate of interest of an investment that future value
is 15692.93 after 10 years of saving 100 every month?
def my_fv1(rate,nper,pmt,pv):
for i in range(1,nper+1):
if(i==1):
pmt=(pv+pmt)+(pmt*(0.05/12))
else:
pmt=100+pmt+(pmt*(0.05/12))
print(pmt)
#main
rate = float(input("Enter rate of interest(annually):"))
rate = rate / 12
print(rate)
nper = int(input("Enter no of years:"))
nper = nper * 12
print(nper)
pmt = int(input("Enter payment:"))
pv = int(input("Enter present value:"))
my_fv1(rate,nper,pmt,pv)
output:
Enter rate of interest(annually):5
Enter no of years:5
Enter payment:100
Enter present value:100
6928.944151934662