0% found this document useful (0 votes)
38 views5 pages

A2 Rahil

The document is an assignment submission for a course on Artificial Intelligence. It contains the student's name, course details, and a series of questions answered using NumPy to perform operations on 2D arrays like creating, manipulating, and analyzing arrays. Matrix operations like multiplication and splitting arrays are demonstrated. Visualizations are created using Matplotlib by plotting graphs from iris dataset features.

Uploaded by

harsh upadhyay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

A2 Rahil

The document is an assignment submission for a course on Artificial Intelligence. It contains the student's name, course details, and a series of questions answered using NumPy to perform operations on 2D arrays like creating, manipulating, and analyzing arrays. Matrix operations like multiplication and splitting arrays are demonstrated. Visualizations are created using Matplotlib by plotting graphs from iris dataset features.

Uploaded by

harsh upadhyay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 2

Name: Rahil Tilak Poladiya

Subject: Artificial Intelligence

Msc. Cyber Security Sem-1

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)

[[ 2.7 -2. -19. ]


[ 0. 3.4 99.9]
[ 10.6 0. 13. ]]

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)

[[ 4. 8. 12. 16. 20.]


[24. 28. 32. 36. 40.]
[44. 48. 52. 56. 60.]]

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

Q2. Drop column whose index = 2 from the array myarray1

np.delete(myarray1,2,axis=1)

array([[ 2.7, -2. ],


[ 0. , 3.4],
[10.6, 0. ]])

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)

[[ -873.2 -946.4 -1019.6 -1092.8 -1166. ]


[ 4477.2 4890.4 5303.6 5716.8 6130. ]
[ 614.4 708.8 803.2 897.6 992. ]]
Q6. Find the cube of all elements of myarray1 and divide the resulting array by 2.

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]]

Resultant array divided by 2:


[[ 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]]

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)

[[-19. -2. 2.7]


[ 0. 3.4 99.9]
[ 0. 10.6 13. ]]

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

a) sum of all elements: 108.60000000000001

b) sum of all elements in:


row[0]: -18.3
row[1]: 103.30000000000001
row[2]: 23.6

c) min of all elements in:


column[0]: -18.3
column[1]: 103.30000000000001
column[2]: 23.6

d) mean of all elements in:


row[0]: -6.1000000000000005
row[1]: 34.43333333333334
row[2]: 7.866666666666667

e) standard deviation of all elements in:


col[0]: 8.956685895029603
col[1]: 5.161395160225576
col[2]: 43.59604977008302

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())

petal length petal width sepal length sepal width species


count 150.000000 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.199333 1.000000
std 0.828066 0.435866 1.765298 0.762238 0.819232
min 4.300000 2.000000 1.000000 0.100000 0.000000
25% 5.100000 2.800000 1.600000 0.300000 0.000000
50% 5.800000 3.000000 4.350000 1.300000 1.000000
75% 6.400000 3.300000 5.100000 1.800000 2.000000
max 7.900000 4.400000 6.900000 2.500000 2.000000

1) Draw a line Graph for Sepal length vs Sepal width

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()

2) Draw a Bar Graph for Petal Length vs Petal Width


plt.bar(data['petal length'],data['petal width'],color="#Fe5216",edgecolor="black")
plt.title("Bar Graph for Petal Length v/s Petal Width")
plt.xlabel("Petal Length")
plt.ylabel("Petal Width")
plt.show()

3) Draw a Histogram Graph for Sepal Length

plt.hist(data['sepal length'],color="#Fe5216",edgecolor="black")
plt.title("Histogram of sepal length")
plt.xlabel("Sepal Length")
plt.show()

check 0s completed at 11:58 PM

You might also like