11/1/24, 1:31 PM Practical PRogram List 2.
ipynb - Colab
# Variation 1
# initializing lists
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
# printing original lists
print ("Original list 1 : " + str(test_list1))
print ("Original list 2 : " + str(test_list2))
# using naive method to
# add two list
res_list = []
for i in range(0, len(test_list1)):
res_list.append(test_list1[i] + test_list2[i])
# printing resultant list
print ("Resultant list is :" + str(res_list))
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is :[5, 8, 10, 8, 18]
# Variation 2
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
# printing original lists
print ("Original list 1 :" + str(test_list1))
print ("Original list 2 : " + str(test_list2))
# using list comprehension to
# add two list
res_list = [test_list1[i] + test_list2[i] for i in range(len(test_list1))]
# printing resultant list
print ("Resultant list is : " + str(res_list))
Original list 1 :[1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Write a program to add the elements of the two lists.
# Variation 3
import numpy as np
# initializing lists
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
# printing original lists
print("Original list 1 : " + str(test_list1))
print("Original list 2 : " + str(test_list2))
# using numpy.sum() to add two lists
res_array = np.array(test_list1) + np.array(test_list2)
res_list = res_array.tolist()
# printing resultant list
print("Resultant list is : " + str(res_list))
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Write a program to calculate mean, median and mode using Numpy
# Python program to print mean of elements
# Without numpy
# list of elements to calculate mean
n_num = [1, 2, 3, 4, 5]
n = len(n_num)
get_sum = sum(n_num)
https://colab.research.google.com/drive/12nDuWAlDY72rhJrBNxXqiO4cmTQxcb8I#scrollTo=bjHm50HqpeXd&printMode=true 1/6
11/1/24, 1:31 PM Practical PRogram List 2.ipynb - Colab
mean = get_sum / n
print("Mean / Average is: " + str(mean))
# Using Numpy
import numpy
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = numpy.mean(speed)
print(x)
# Python program to print median of elements
# Without Numpy
# list of elements to calculate median
n_num = [1, 2, 3, 4, 5]
n = len(n_num)
n_num.sort()
if n % 2 == 0:
median1 = n_num[n//2]
median2 = n_num[n//2 - 1]
median = (median1 + median2)/2
else:
median = n_num[n//2]
print("Median is: " + str(median))
# Using Numpy
import numpy
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = numpy.median(speed)
print(x)
# Python program to print mode of elements
# without numpy
from collections import Counter
# list of elements to calculate mode
n_num = [1, 2, 3, 4, 5, 5]
n = len(n_num)
data = Counter(n_num)
get_mode = dict(data)
mode = [k for k, v in get_mode.items() if v == max(list(data.values()))]
if len(mode) == n:
get_mode = "No mode found"
else:
get_mode = "Mode is / are: " + ', '.join(map(str, mode))
print(get_mode)
# Using Numpy
import numpy
speed = [99,86,87,88,86,103,87,94,78,77,85,86]
x = numpy.median(speed)
print(x)
Write a program to display line chart from (2,5) to (9,10).
#Line Chart is used to represent a relationship between two data X and Y on a different axis.
import matplotlib.pyplot as plt
# data to display on plots
x = [2, 9]
y = [5, 10]
https://colab.research.google.com/drive/12nDuWAlDY72rhJrBNxXqiO4cmTQxcb8I#scrollTo=bjHm50HqpeXd&printMode=true 2/6
11/1/24, 1:31 PM Practical PRogram List 2.ipynb - Colab
# This will plot a simple line chart
# with elements of x as x axis and y
# as y axis
plt.plot(x, y)
plt.title("Line Chart")
# Adding the legends
plt.legend(["Line"])
plt.show()
Write a program to display a scatter chart for the following points (2,5), (9,10),(8,3),(5,7),(6,18).
#Scatter plots are used to observe the relationship between variables and use dots to represent the relationship between them.
import matplotlib.pyplot as plt
# data to display on plots
x = [2, 9, 8, 15, 6]
y = [5, 10, 3, 7, 18]
# This will plot a simple scatter chart
plt.scatter(x, y)
# Adding legend to the plot
plt.legend("A")
# Title to the plot
plt.title("Scatter chart")
plt.show()
Read csv file saved in your system and display 10 rows.
https://colab.research.google.com/drive/12nDuWAlDY72rhJrBNxXqiO4cmTQxcb8I#scrollTo=bjHm50HqpeXd&printMode=true 3/6
11/1/24, 1:31 PM Practical PRogram List 2.ipynb - Colab
import pandas as pd
df = pd.read_csv('/content/sample_data/mnist_train_small.csv')# file name to be given in single quotes
df.head(n=10)
6 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 ... 0.581 0.582 0.583 0.584 0.585 0.586 0.587 0.588 0.589 0.590
0 5 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
1 7 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
2 9 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
3 5 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
4 2 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
6 6 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
7 5 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
8 5 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
9 2 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
10 rows × 785 columns
Read csv file saved in your system and display its information
import pandas as pd
df = pd.read_csv('/content/sample_data/mnist_train_small.csv')# file name to be given in single quotes
df.info()
df.describe
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 19999 entries, 0 to 19998
Columns: 785 entries, 6 to 0.590
dtypes: int64(785)
memory usage: 119.8 MB
<bound method NDFrame.describe of 6 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 ... 0.581 0.582 0.583 \
0 5 0 0 0 0 0 0 0 0 0 ... 0 0 0
1 7 0 0 0 0 0 0 0 0 0 ... 0 0 0
2 9 0 0 0 0 0 0 0 0 0 ... 0 0 0
3 5 0 0 0 0 0 0 0 0 0 ... 0 0 0
4 2 0 0 0 0 0 0 0 0 0 ... 0 0 0
... .. .. ... ... ... ... ... ... ... ... ... ... ... ...
19994 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
19995 1 0 0 0 0 0 0 0 0 0 ... 0 0 0
19996 2 0 0 0 0 0 0 0 0 0 ... 0 0 0
19997 9 0 0 0 0 0 0 0 0 0 ... 0 0 0
19998 5 0 0 0 0 0 0 0 0 0 ... 0 0 0
0.584 0.585 0.586 0.587 0.588 0.589 0.590
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0
... ... ... ... ... ... ... ...
19994 0 0 0 0 0 0 0
19995 0 0 0 0 0 0 0
19996 0 0 0 0 0 0 0
19997 0 0 0 0 0 0 0
19998 0 0 0 0 0 0 0
[19999 rows x 785 columns]>
Write a program to read an image and display using Python
import cv2 # import OpenCV
from matplotlib import pyplot as plt # import matplotlib
import numpy as np # import numpy
img = cv2.imread('Copy of man.jpg') #Load the image file into memory
plt.imshow(img)
plt.title('man')
plt.axis('off')
plt.show()
https://colab.research.google.com/drive/12nDuWAlDY72rhJrBNxXqiO4cmTQxcb8I#scrollTo=bjHm50HqpeXd&printMode=true 4/6
11/1/24, 1:31 PM Practical PRogram List 2.ipynb - Colab
img = cv2.imread('Images/man.jpg') #Load the image file into memory
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('man')
plt.axis('off')
plt.show()
img = cv2.imread('/content/Copy of man.jpg',0) # the number zero opens the image as a grayscale image
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic') #cmap specifies color mapping, gray in this case.
plt.title('man')
plt.axis('off')
plt.show()
https://colab.research.google.com/drive/12nDuWAlDY72rhJrBNxXqiO4cmTQxcb8I#scrollTo=bjHm50HqpeXd&printMode=true 5/6
11/1/24, 1:31 PM Practical PRogram List 2.ipynb - Colab
Write a program to read an image and identify its shape using Python
img = cv2.imread('/content/rgb.png',0)
print(img.shape)
(2000, 2000)
https://colab.research.google.com/drive/12nDuWAlDY72rhJrBNxXqiO4cmTQxcb8I#scrollTo=bjHm50HqpeXd&printMode=true 6/6