0% found this document useful (0 votes)
6 views

AI Lab Record for Class x

Uploaded by

Vansh Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

AI Lab Record for Class x

Uploaded by

Vansh Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

In [2]:

"""Question1: Draw a line chart with two lists X and Y with following values
x = [10,20,30,40,50]
y = [10,8,15,41,17] """

import matplotlib.pyplot as plt


#plt is alias name to matplotlib.pyplot
# Define X and Y through lists
x = [10,20,30,40,50]
y = [10,8,15,41,17]
plt.plot(x, y) # Plot is a method to plot a line chart
plt.show() # to display the graph we required show method

In [10]:
"""Question2: Draw a line chart with two lists X and Y with following values
x = [10,20,30,40,35]
y = ["a","b","c","d","e"]
and add plot(),show(),xlabel(), ylabel(), title() are called methods """

import matplotlib.pyplot as plt


""" here plt is alias name/altenate name to "matplotlib.pyplot"
matplotlib is a library/ pre-defined package
pyplot is a module, this module consists of many plot related methods.
"""
x = [10,20,30,40,35]
y = ["a","b","c","d","e"]
plt.plot(y,x) # Plot the line chart y ys x
plt.title(" X ys Y ")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.show() # display the chart
In [1]:
# question3: draw a scatter plot with x and y list values

import matplotlib.pyplot as plt


# Define X and Y through lists
x = [10,20,30,4]
y = ['a','b','c','d']
#list can able to store sequence of heterogenous/different data
plt.scatter(x, y)# Plot the scatter chart
plt.xlabel("X-axis") # add X-axis label
plt.ylabel("Y-axis") # add Y-axis label
plt.title("X vs Y") # add title
plt.show()# display the chart

In [4]:
# question4: draw a scatter plot with x and y as array values
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6] )
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
#arrays can able to store sequence of homogeneous/similar type data
plt.scatter(x, y)
plt.xlabel("X-axis") # add X-axis label
plt.ylabel("Y-axis") # add Y-axis label
plt.title("X vs Y") # add title
plt.show()
In [3]:
"""Question5: Draw a bargraph with x and y array elements"""

import matplotlib.pyplot as plt


import numpy as np
#arrays can able to store sequence of homogeneous/similar data
#drawing a bar chart with array of elements
x = np.array([10,20,30,40,50,60] )
y = np.array([10,20,35,48,25,60])
plt.bar(x,y ,color="red",width=5)
plt.grid(True)
plt.xlabel("X-axis") # add X-axis label
plt.ylabel("Y-axis") # add Y-axis label
plt.title("X vs Y") # add title
plt.show()

In [12]:
"""Question 6:creating a bargraph with multicolor bars and multisize"""

import matplotlib.pyplot as plt


Info = ['GOLD', 'SILVER', 'BRONZE', 'TOTAL']
Aus = [100, 120, 110, 330]
fig = plt.figure(figsize = (5, 5))
# plot stacked bar chart
plt.bar(Info, Aus, width=[0.5,0.3,0.2,0.8], color=['y','r','k','b'])
#yellow red black cyan
plt.xticks(rotation=180)
plt.yticks(rotation=-50)
plt.show()

In [11]:
""" Question 7. Draw a histogram for normal distribution curve"""

import matplotlib.pyplot as plt


import numpy as np
x = np.random.normal(170, 10, 250)
plt.hist(x)
plt.show()

In [12]:
""" Question 8. Draw a histogram for an array of 100 random numbers,
with a range of 5 intervals"""
import matplotlib.pyplot as plt
import numpy as np
y=np.random.randn(100)
#random is module and randn is a method to display random values
nbins=5; #bins are intervals/groups
plt.hist(y,nbins,edgecolor="yellow")
plt.show()
In [14]:
""" Question 9. Draw a pie chart for an array of cars and mileage"""

from matplotlib import pyplot as plt


import numpy as np

cars = np.array(['AUDI', 'BMW', 'FORD','TESLA', 'JAGUAR', 'MERCEDES'] )


mileage = np.array([23, 17, 35, 29, 12, 41] )
fig = plt.figure(figsize=(10, 7))
plt.pie(mileage, labels=cars)
plt.show()

In [1]:
""" Question 10. Draw a pie chart for an list of fruits and its calories"""

import matplotlib.pyplot as plt


import numpy as np

y = [35, 25, 25, 15]


x = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels = x)
plt.show()
In [7]:
""" Question 11. convert a picture into gray scale """
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('d:\\meridian.jpg',cv2.IMREAD_GRAYSCALE)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.show()

In [16]:
""" Question 12. display a picture by using imshow method and turn off axes """
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('d:\\meridian.jpg')
plt.imshow(img)
plt.axis('off')
plt.title("school logo")
plt.show()
In [27]:
""" Question 13. display a picture by using imshow method and turn off axes """
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('d:\\meridian.jpg' )
plt.imshow(img)
plt.axis('off')
plt.title("school logo")
plt.show()

In [33]:
""" Question 14. display a picture with normal colors and turn off axes """
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('d:\\meridian.jpg' )
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB) )
plt.axis('off')
plt.title("school logo")
plt.show()
In [25]:
# Question 15. Write a program to read an image and identify its shape using Python

import cv2
from matplotlib import pyplot as plt

# Using cv2.imread() method to read the image

img = cv2.imread('e:\\tomandjerry.jpg' )

# Check if the image was loaded successfully


if img is None:
print("Error: Could not read the image. Please check the path.")
else:
# Display the image using Matplotlib
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis('off') # Remove axes
plt.title("cartoon")
plt.show()

# Print the shape of the image


print("Image shape:", img.shape)

# Access a specific pixel color (x=40, y=25)


B, G, R = img[25, 40]
print("Blue:", B, "Green:", G, "Red:", R)

Image shape: (801, 800, 3)


Blue: 197 Green: 100 Red: 36

In [26]:
""" Question 16. write a python program to perform all arthmetic operations
on integer arrays """
import numpy as np
a=np.array([10,20,35,94,90,567])
b=np.array([1,2,3,4,90,87])
print(a+b)
print(a-b)
print(a*b)
print(a**b)
print(a%b)

[ 11 22 38 98 180 654]
[ 9 18 32 90 0 480]
[ 10 40 105 376 8100 49329]
[ 10 400 42875 78074896 0 110716871]
[ 0 0 2 2 0 45]

In [27]:
""" Question 17. write a python program to perform all mathematical
operations on integer arrays using functions """

import numpy as np

# create two arrays


array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([4, 9, 16, 25, 36])

# add the two arrays element-wise


arr_sum = np.add(array1, array2)

# subtract the array2 from array1 element-wise


arr_diff = np.subtract(array1, array2)

# compute square root of array2 element-wise


arr_sqrt = np.sqrt(array2)

print("\nSum of arrays:\n", arr_sum)


print("\nDifference of arrays:\n", arr_diff)
print("\nSquare root of first array:\n", arr_sqrt)

Sum of arrays:
[ 5 11 19 29 41]

Difference of arrays:
[ -3 -7 -13 -21 -31]

Square root of first array:


[2. 3. 4. 5. 6.]

In [30]:
""" Question 18.Python program to check a given number is odd or
even."""

# Input from the user


num = int(input("Enter a number: "))

# Check if the number is odd or even


if num % 2 == 0:
print(f"{num} is an Even number.")
else:
print(f"{num} is an Odd number.")

Enter a number: 58
58 is an Even number.
In [28]:
""" Question 19. Python program to find the largest number among three
different numbers:"""
# Input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Check which number is the largest


if num1 > num2 and num1 > num3:
print(f"The largest number is: {num1}")
elif num2 > num1 and num2 > num3:
print(f"The largest number is: {num2}")
else:
print(f"The largest number is: {num3}")

Enter the first number: 25


Enter the second number: 28
Enter the third number: 63
The largest number is: 63.0

In [29]:
""" Question 20. python program to find the sum of first 20
natural numbers """
# Initialize variables
num = 1
sum = 0

# While loop to iterate through the first 20 natural numbers


while num <= 20:
sum += num # Add the current number to the sum
num += 1 # Increment the number by 1

# Output the sum


print("The sum of the first 20 natural numbers is:", sum)

The sum of the first 20 natural numbers is: 210

In [2]:
"""Question 21.Python program to find the factorial of a number
provided by the user. """
factorial=1
# change the value for a different result
num = int(input("enter a number"))

for i in range(1,num + 1):


factorial = factorial * i
print("The factorial of",num,"is",factorial)

enter a number8
The factorial of 8 is 40320

In [3]:
"""Question 22.Python program to display Multiplication table
(from 1 to 10) in Python """

num = int(input("enter a number"))

# To take input from the user


# num = int(input("Display multiplication table of? "))

# Iterate 10 times from i = 1 to 10


for i in range(1, 11):
print(num, 'x', i, '=', num*i)
enter a number9
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90

In [ ]:

You might also like