0% found this document useful (0 votes)
7 views17 pages

Asup Unit1 Python Code Question Bank

The document provides a comprehensive question bank for Unit 1 focused on statistical computations using Python libraries such as NumPy, Pandas, and SciPy. It includes examples for calculating mean, median, mode, range, standard deviation, quartile deviation, and performing univariate and bivariate analyses with corresponding code snippets and expected outputs. Each section is categorized into basic, dataset-based, outlier-based, and combined examples for clarity.

Uploaded by

2shohaib2007
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)
7 views17 pages

Asup Unit1 Python Code Question Bank

The document provides a comprehensive question bank for Unit 1 focused on statistical computations using Python libraries such as NumPy, Pandas, and SciPy. It includes examples for calculating mean, median, mode, range, standard deviation, quartile deviation, and performing univariate and bivariate analyses with corresponding code snippets and expected outputs. Each section is categorized into basic, dataset-based, outlier-based, and combined examples for clarity.

Uploaded by

2shohaib2007
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

QUESTION BANK — UNIT 1 (PYTHON CODE ONLY)

1. MEAN (NumPy / Pandas)


Basic

1. Using NumPy, compute the mean of:


data = [12, 15, 18, 20, 25]
Code:

import numpy as np
data = [Link]([12, 15, 18, 20, 25])
mean = [Link](data)
print("Mean of the data is:",mean)

Output:

Mean of the data is: 18.0

2. Using Pandas, compute mean of:


marks = [55, 65, 75, 85, 95]
Code:

import pandas as pd
marks = [Link]([55, 65, 75, 85, 95])
print("Mean of the data is:",[Link]())

Output:

Mean of the data is: 75.0


Dataset-Based
3. Create a NumPy array:
data = [10, 20, 30, 40, 50, 60]
Compute mean.
Code:

import numpy as np
data = [Link]([10, 20, 30, 40, 50, 60])
mean = [Link](data)
print("Mean is:",mean)

Output:

Mean is: 35.0

4. Create a Pandas DataFrame:


Marks = [45, 50, 55, 60, 65]
Compute mean of column.

Code:

import pandas as pd
data = [45, 50, 55, 60, 65]
df = [Link](data,columns=["Marks"])
print(df)
mean = df["Marks"].mean()
print("Mean is: ",mean)

Output:

Marks
0 45
1 50
2 55
3 60
4 65
Mean is: 55.0
Outlier-Based
5. Compute mean of:
data = [10, 20, 30, 40, 1000]
Then remove outlier and recompute.
Code:

import pandas as pd
data = [10, 20, 30, 40, 1000]
df = [Link](data,columns=["Values"])
print(df)

mean_with_outlier = df["Values"].mean()
print("Mean with outlier: ",mean_with_outlier)

mean_without_outlier = df[df["Values"]<100]["Values"].mean()
print("Mean without outlier: ",mean_without_outlier)

Output:

Values
0 10
1 20
2 30
3 40
4 1000
Mean with outlier: 220.0
Mean without outlier: 25.0
Combined
6. Compute mean and plot histogram:
data = [5, 10, 15, 20, 25, 30]
Code:

import numpy as np
import [Link] as plt

data = [Link]([5, 10, 15, 20, 25, 30])


mean = [Link]()
print("Mean is: ",mean)

[Link](data)
[Link]("Histogram Of Data")
[Link]("X-Axis")
[Link]("Y-Axis")
[Link]()

Output:

Mean is: 17.5


2. MEDIAN (NumPy / Pandas)
Basic
7. Compute median using NumPy:
data = [11, 13, 15, 17, 19]
Code:

import numpy as np
data = [Link]([11, 13, 15, 17, 19])
median = [Link](data)
print("Median is: ",median)

Output:

Median is: 15.0

8. Compute median using Pandas:


data = [22, 24, 26, 28, 30]
Code:

import pandas as pd
data = [22, 24, 26, 28, 30]
df = [Link](data,columns=["Data"])
print("Median is: ",df["Data"].median())

Output:

Median is: 26.0


Combined
9. Compute median and display using boxplot:
data = [8, 12, 16, 20, 100]
Code:

import numpy as np
import [Link] as plt

data = [Link]([8, 12, 16, 20, 100])

median = [Link](data)
print("Median is:", median)

[Link](data)
[Link]("Boxplot of Data")
[Link]("Dataset")
[Link]("Values")
[Link]()

Output:

Median is: 16.0


3. MODE (SciPy / Pandas)
Basic
10. Compute mode using SciPy:
data = [10, 20, 20, 30, 40]
Code:

from scipy import stats


data = [10, 20, 20, 30, 40]
mode = [Link](data)
print("Mode is: ",mode[0])

Output:

Mode is: 20

11. Compute mode using Pandas:


data = [5, 5, 10, 15, 15]
Code:

import pandas as pd
data = [Link]([5, 5, 10, 15, 15])
mode = [Link]()
print("Mode is: ",mode[0])

Output:

Mode is: 5
Multiple Modes
12. Find mode(s):
data = [1, 2, 2, 3, 3, 4]
Code:

import pandas as pd
data = [Link]([1, 2, 2, 3, 3, 4])
mode = [Link]()
print("Mode is: ",mode[0],mode[1])
#or
print("Mode is: ",[Link]())

Output:

Mode is: 2 3
Mode is: [2, 3]
4. RANGE (NumPy / Pandas)
Basic
13. Compute range:
data = [5, 10, 15, 20, 25]
Code:

import numpy as np
data = [Link]([5, 10, 15, 20, 25])
rangeval = [Link](data) - [Link](data)
print("Range is:",rangeval)

Output:

Range is: 20

Dataset-Based
14. Compute range using Pandas:
Marks = [40, 50, 60, 70, 80]
Code:

import pandas as pd
series = [Link]([40, 50, 60, 70, 80])
rangeval = [Link]() - [Link]()
print("Range is: ",rangeval)

Output:

Range is: 40
Outlier-Based
15. Compare range, Remove outliers:
data = [10, 20, 30, 40]
data2 = [10, 20, 30, 1000]
Code:

import pandas as pd
data1 = [Link]([10, 20, 30, 40])
data2 = [Link]([10, 20, 30, 1000])

data2 = data2[data2<1000]

range1 = [Link]() - [Link]()


range2 = [Link]() - [Link]()

print("Range of data1: ",range1)


print("Range of data2: ",range2)

Output:

Range of data1: 30
Range of data2: 20
5. STANDARD DEVIATION (NumPy / Pandas)
Basic
16. Compute standard deviation:
data = [10, 20, 30, 40, 50]
Code:

import numpy as np
data = [Link]([10, 20, 30, 40, 50])
print("Standard Deviation is: ",[Link](data))

Output:

Standard Deviation is: 14.142135623730951

Dataset-Based
17. Compute std using Pandas:
Marks = [60, 65, 70, 75, 80]
Code:

import numpy as np
import pandas as pd

data = [60, 65, 70, 75, 80]

print("Standard Deviation using numpy is: ",[Link](data))

series = [Link](data)
print("Standard Deviation using pandas is:",[Link](ddof=0))

Output:

Standard Deviation using numpy is: 7.0710678118654755


Standard Deviation using panas is: 7.0710678118654755
6. QUARTILE DEVIATION (NumPy / Pandas)
Basic
18. Compute Q1, Q3 and quartile deviation:
data = [10, 20, 30, 40, 50]
Code:

import numpy as np

data = [10, 20, 30, 40, 50]

Q1 = [Link](data,25)
Q3 = [Link](data,75)
QD = (Q3-Q1)/2

print("Q1 is: ",Q1)


print("Q3 is: ",Q3)
print("Quartile Deviation is: ",QD)

Output:

Q1 is: 20.0
Q3 is: 40.0
Quartile Deviation is: 10.0
Dataset-Based
19. Compute quartiles using Pandas:
Marks = [55, 60, 65, 70, 75]
Code:

import pandas as pd
Marks = [Link]([55, 60, 65, 70, 75])
Q1 = [Link](0.25)
Q3 = [Link](0.75)
QD = (Q3-Q1)/2

print("Q1 is: ",Q1)


print("Q3 is: ",Q3)
print("Quartile Deviation is: ",QD)

Output:

Q1 is: 60.0
Q3 is: 70.0
Quartile Deviation is: 5.0
7. UNIVARIATE ANALYSIS (Pandas + Matplotlib)
Basic
20. Create DataFrame and summarize by using describe():
Marks = [50, 60, 70, 80, 90]
Code:

import pandas as pd
Marks = [50, 60, 70, 80, 90]
df = [Link](Marks,columns=["Marks"])
summary = [Link]()
print(summary)

Output:

Marks
count 5.000000
mean 70.000000
std 15.811388
min 50.000000
25% 60.000000
50% 70.000000
75% 80.000000
max 90.000000
Visualization
21. Plot histogram:
data = [12, 15, 18, 21, 24, 27]
Code:

import [Link] as plt


data = [12, 15, 18, 21, 24, 27]
[Link](data)
[Link]("X axis")
[Link]("Y axis")
[Link]("Data Histogram")
[Link]()

Output:
8. BIVARIATE ANALYSIS (Pandas + Matplotlib)
Basic
22. Create DataFrame and Compute correlation:
Hours = [1, 2, 3, 4, 5]
Marks = [50, 55, 65, 70, 80]
Code:

import pandas as pd
Hours = [1, 2, 3, 4, 5]
Marks = [50, 55, 65, 70, 80]
df = [Link]({"Hours":Hours, "Marks":Marks})
print("Correlation is: ", df["Hours"].corr(df["Marks"]))

Output:

Correlation is: 0.9933992677987827


Combined
24. Perform full analysis:
Study = [2, 4, 6, 8, 10]
Marks = [40, 50, 65, 75, 90]
(Find Correlation, Scatter, Graph)
Code:

import pandas as pd
import [Link] as plt
Study = [2, 4, 6, 8, 10]
Marks = [40, 50, 65, 75, 90]

df = [Link]({"Study":Study, "Marks":Marks})

print("Correlation is: ",df["Study"].corr(df["Marks"]))

[Link](df["Study"],df["Marks"])
[Link]("Study vs Marks Scatter Plot")
[Link]("Study")
[Link]("Marks")
[Link]()

[Link](df["Study"],df["Marks"], marker='x')
[Link]("Study vs Marks Graph")
[Link]("Study")
[Link]("Marks")
[Link]()

Output:

Correlation is: 0.9976086055845276

You might also like