You are on page 1of 5

1. Generate a list of Random numbers as per input from the user.

Code:
import random
import math
num=int(input("Enter the number of random values you want:"))
list1=list()
for i in range(num):
x=random.random()
y="{0:.3f}".format(x)
y=float(y)
y=y*10
z=math.ceil(y)
list1.append(z)
print(list1)
Output:
2. Find the mean, median, mode and midrange of a set of numbers provided by the user.
Code:
import statistics
num=int(input("Enter the number of values you want to enter:"))
list1=list()
for i in range(num):
list1.append(int(input("Enter a number: ")))
print(list1)
print("The mean of the given list is")
print(sum(list1)/len(list1))
print("The median of the given list is ")
print(statistics.median(list1))
print("The mode of the given list is ")
print(statistics.mode(list1))
print("The midrange of the given list is ")
print((max(list1)+min(list1))/2)

Output:
3. Find the five number summary and display the boxplot.
Code:
from numpy import percentile
from numpy.random import rand
import numpy as np
import matplotlib.pyplot as plt
num=int(input("Enter the number of values you want "))
list1=list()
for i in range(num):
list1.append(int(input("Enter a number: ")))
list1.sort()
data = np.array(list1
quartiles = percentile(data, [25, 50, 75])
data_min, data_max = data.min(), data.max()
print('Min: %.3f' % data_min)
print('Q1: %.3f' % quartiles[0])
print('Median: %.3f' % quartiles[1])
print('Q3: %.3f' % quartiles[2])
print('Max: %.3f' % data_max)
fig = plt.figure(figsize =(10, 7))
plt.boxplot(data)
plt.show()
Output:
4. Perform the basic exploratory data analysis on a sample dataset.
Here the sample dataset that is used is called titanic.
Code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
data = sns.load_dataset('titanic')
data.head()
data.describe()
data.info()
data.isnull().sum()
plt.hist(data['age'], bins = 20)
plt.xlabel('Age')
plt.ylabel('Count')
plt.show()
Output:
5. Display the statistical descriptions of a sample dataset.
Code:
import pandas as pd
import seaborn as sns
data = sns.load_dataset('titanic')
print(data.describe())
Output:

6. Find the similarity measure of two objects i and j where i=(12,4,6,5,1) and
j=(12,5,7,9,4) using Euclidean and Manhattan distance.

Code:
from math import sqrt
i = (12, 4, 6, 5, 1)
j = (12, 5, 7, 9, 4)
# Euclidean distance
euclidean_distance = sqrt(sum((a - b) ** 2 for a, b in zip(i, j)))
print("Euclidean Distance between i and j: ", euclidean_distance)
# Manhattan distance
manhattan_distance = sum(abs(a - b) for a, b in zip(i, j))
print("Manhattan Distance between i and j: ", manhattan_distance)
Output:

You might also like