A2 Rahil
A2 Rahil
NFSU gandhinagar
import numpy as np
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
myarray1=np.array([[2.7,-2,-19],[0,3.4,99.9],[10.6,0,13]])
print(myarray1)
A 2-D array called myarray2 using arange() having 3 rows and 5 columns with start value = 4, step size 4 and dtype as float.
myarray2=np.arange(4,(4*3*5)+1,4,dtype="float").reshape(3,5)
print(myarray2)
Q1. Find the dimensions, shape, size, data type of the arrays myarray1 and myarray2.
print("Array 1:")
print("Dimension of myarray1: "+str(myarray1.ndim))
print("Shape of myarray1: "+str(myarray1.shape))
print("Size of myarray1: "+str(myarray1.size))
print("datatype of myarray1: "+str(myarray1.dtype)+"\n")
print("Array 2:")
print("Dimension of myarray2: "+str(myarray2.ndim))
print("Shape of myarray2: "+str(myarray2.shape))
print("Size of myarray2: "+str(myarray2.size))
print("datatype of myarray2: "+str(myarray2.dtype))
Array 1:
Dimension of myarray1: 2
Shape of myarray1: (3, 3)
Size of myarray1: 9
datatype of myarray1: float64
Array 2:
Dimension of myarray2: 2
Shape of myarray2: (3, 5)
Size of myarray2: 15
datatype of myarray2: float64
np.delete(myarray1,2,axis=1)
Q3. Display all elements in the 2nd and 3rd row of the array myarray1.
print(myarray1[1:3,:])
[[ 0. 3.4 99.9]
[10.6 0. 13. ]]
Q4. Display the elements in the 1st column of the 2nd and 3rd row of the array myarray1.
print(myarray1[1:3,0])
[ 0. 10.6]
Q5. Do the matrix multiplication of myarray1 and myarray2 and store the result in a new array myarray3.
myarray3 = np.matmul(myarray1,myarray2)
print(myarray3)
resarray = pow(myarray1,3)
print("Cube of myarray1: ")
print(resarray)
resarray2 = resarray/2
print("\nResultant array divided by 2: ")
print(resarray2)
Cube of myarray1:
[[ 1.96830000e+01 -8.00000000e+00 -6.85900000e+03]
[ 0.00000000e+00 3.93040000e+01 9.97002999e+05]
[ 1.19101600e+03 0.00000000e+00 2.19700000e+03]]
Q7. Sort the array myarray1 such that it brings the lowest value of the column in the first row and so on.
myarray1.sort(axis=1)
print(myarray1)
Q8 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 arrays myarray2A, myarray2B, myarray2C, myarray2D and myarray2E.
myarray2A,myarray2B,myarray2C,myarray2D,myarray2E = np.split(myarray2,5,axis=1)
print("myarray2A: \n",myarray2A)
print("\nmyarray2B: \n",myarray2B)
print("\nmyarray2C: \n",myarray2C)
print("\nmyarray2D: \n",myarray2D)
print("\nmyarray2E: \n",myarray2E)
myarray2A:
[[ 4.]
[24.]
[44.]]
myarray2B:
[[ 8.]
[28.]
[48.]]
myarray2C:
[[12.]
[32.]
[52.]]
myarray2D:
[[16.]
[36.]
[56.]]
myarray2E:
[[20.]
[40.]
[60.]]
Q9. Using the myarray1 ,write commands for the following a) Find the sum of all elements. b) Find the sum of all elements row wise. c) Find
the min of all elements column wise. d) Find the mean of all elements in each row. e) Find the standard deviation column wise.
print("a) sum of all elements: "+str(myarray1.sum()))
print("\nb) sum of all elements in:")
sumarr = myarray1.sum(axis=1)
x=0
for i in sumarr:
print("row["+str(x)+"]: "+str(i))
x+=1
minarr = myarray1.min(axis=0)
x=0
print("\nc) min of all elements in:")
for i in sumarr:
print("column["+str(x)+"]: "+str(i))
x+=1
meanarr = myarray1.mean(axis=1)
x=0
print("\nd) mean of all elements in:")
for i in meanarr:
print("row["+str(x)+"]: "+str(i))
x+=1
stdarr = myarray1.std(axis=0)
x=0
print("\ne) standard deviation of all elements in:")
for i in stdarr:
print("col["+str(x)+"]: "+str(i))
x+=1
Load Iris Dataset. Using the Matplotlib write commands for the following:
from sklearn.datasets import load_iris
import pandas as pd
import matplotlib.pyplot as plt
iris = load_iris()
data=pd.DataFrame(iris['data'],columns=['petal length','petal width','sepal length','sepal width'])
data['species']=iris['target']
print(data.describe())
plt.plot(data['sepal length'],data['sepal width'],c="#Fe5216")
plt.title("Line Graph for Sepal Length v/s Sepal Width")
plt.xlabel("Sepal Length")
plt.ylabel("Sepal Width")
plt.show()
plt.hist(data['sepal length'],color="#Fe5216",edgecolor="black")
plt.title("Histogram of sepal length")
plt.xlabel("Sepal Length")
plt.show()