1.
Use Numpy to create single and multi-dimensional array and perform various
operations using Python.
import numpy as np
# 1D Array
arr1 = [Link]([1, 2, 3, 4, 5])
print("1D Array:", arr1)
# 2D Array
arr2 = [Link]([[1, 2, 3],
[4, 5, 6]])
print("2D Array:\n", arr2)
# Arithmetic Operations
a = [Link]([10, 20, 30])
b = [Link]([1, 2, 3])
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
# Mathematical Functions
print("Sum:", [Link](a))
print("Mean:", [Link](a))
print("Square Root:", [Link](a))
# Matrix Multiplication
A = [Link]([[1, 2],
[3, 4]])
B = [Link]([[5, 6],
[7, 8]])
print("Matrix Multiplication:\n", [Link](A, B))
# Reshaping Array
arr3 = [Link]([1, 2, 3, 4, 5, 6])
print("Reshaped Array:\n", [Link](2, 3))
output:
1D Array: [1 2 3 4 5]
2D Array:
[[1 2 3]
[4 5 6]]
Addition: [11 22 33]
Subtraction: [9 18 27]
Multiplication: [10 40 90]
Division: [10. 10. 10.]
Sum: 60
Mean: 20.0
Square Root: [3.16 4.47 5.47]
Matrix Multiplication:
[[19 22]
[43 50]]
Reshaped Array:
[[1 2 3]
[4 5 6]]
2. Use Pandas to access dataset, cleaning, manipulate data and analyze
using Python
[Link]
Name,Age,Department,Salary
John,25,IT,50000
Sara,30,HR,60000
Mike,,IT,55000
Anna,28,Finance,65000
David,35,HR,70000
John,25,IT,50000
Explanation
• Mike has missing Age → will be handled by fillna(0)
• John is repeated → will be removed by drop_duplicates()
• Different departments → used for groupby()
Procedure:
1. Open Jupyter Notebook
2. Choose -> New-> Textfile
3. Paste the above data
4. Save as [Link]
5. Place it in the same folder as your Python file
Python Code
import pandas as pd
# Load dataset
df = pd.read_csv("[Link]")
# Show data
print([Link]())
# Fill missing values
[Link](0, inplace=True)
# Remove duplicates
df.drop_duplicates(inplace=True)
# Filter data (Age > 25)
print(df[df['Age'] > 25])
# Add new column
df['Salary_After_Tax'] = df['Salary'] * 0.9
# Show average salary
print("Average Salary:", df['Salary'].mean())
# Group by Department
print([Link]('Department')['Salary'].mean())
Output:
Name Age Department Salary
0 John 25.0 IT 50000
1 Sara 30.0 HR 60000
2 Mike NaN IT 55000
3 Anna 28.0 Finance 65000
4 David 35.0 HR 70000
Name Age Department Salary
1 Sara 30.0 HR 60000
3 Anna 28.0 Finance 65000
4 David 35.0 HR 70000
Average Salary: 60000.0
Department
Finance 65000.0
HR 65000.0
IT 52500.0
Name: Salary, dtype: float64
3. Use matplot library to plot graph for data visualization using Python
To install Matplotlib library use command pip install matplotlib.
Matplotlib is a Python library used for creating static, animated, and interactive
visualizations. It helps in representing data graphically such as line graphs, bar charts, pie
charts, and scatter plots.
1. Line Graph
Used to show trends over time.
Python Code:
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 25, 15]
[Link](x, y)
[Link]("Line Graph")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
Output:
2. Bar Chart
Used to compare categories.
Python Code:
import [Link] as plt
categories = ['A', 'B', 'C', 'D']
values = [5, 7, 3, 8]
[Link](categories, values)
[Link]("Bar Chart")
[Link]("Categories")
[Link]("Values")
[Link]()
Output:
3. Scatter Plot
Used to show relationships between variables.
Python Code:
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [5, 15, 10, 20, 25]
[Link](x, y)
[Link]("Scatter Plot")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
Output:
4. Pie Chart
Used to show proportions.
Python Code:
import [Link] as plt
labels = ['Python', 'Java', 'C++', 'JavaScript']
sizes = [40, 25, 20, 15]
[Link](sizes, labels=labels, autopct='%1.1f%%')
[Link]("Pie Chart")
[Link]()
Output:
4. Determine probability, sampling and sampling distribution using Python.
1. Probability
Probability measures the likelihood of an event.
Number of favorable outcomes
𝑃(𝐴) =
Total outcomes
Example: Probability of getting a head in a coin toss = ½
Python Program
#Probability
import random
heads = 0
trials = 10
for i in range(trials):
if [Link](['H', 'T']) == 'H':
heads += 1
print("Number of Heads:", heads)
print("Probability of Heads:", heads/trials)
Output:
Number of Heads: 4
Probability of Heads: 0.4
2. Sampling
Sampling means selecting a subset from a population.
Python Program
#Sampling
import random
population = [10, 20, 30, 40, 50]
sample = [Link](population, 3)
print("Population:", population)
print("Sample:", sample)
Output:
Population: [10, 20, 30, 40, 50]
Sample: [30, 50, 10]
3. Sampling Distribution
Take many samples and find their means
Python Program
#Sampling Distribution
import random
population = [10, 20, 30, 40, 50]
for i in range(5): # take 5 samples
sample = [Link](population, 3)
mean = sum(sample) / 3
print("Sample:", sample, "Mean:", mean)
Output:
Sample: [20, 10, 30] Mean: 20.0
Sample: [20, 50, 30] Mean: 33.333333333333336
Sample: [20, 10, 40] Mean: 23.333333333333332
Sample: [50, 20, 30] Mean: 33.333333333333336
Sample: [40, 20, 30] Mean: 30.0
5. Determine frequency distributions, variability, average, and standard deviation using
Python
1. Frequency Distribution
Frequency distribution shows how many times each value occurs in a dataset.
Python Program
data = [10, 20, 20, 30]
for i in data:
print(i, "appears", [Link](i), "times")
Output
10 appears 1 times
20 appears 2 times
20 appears 2 times
30 appears 1 times
2. Variability (Range)
Range shows the spread of data by finding difference between highest and lowest value.
Range = Maximum − Minimum
Python Program
data = [10, 20, 30, 40]
data_range = max(data) - min(data)
print("Range:", data_range)
Output
Range: 30
3. Average (Mean)
Mean is the average value of all data.
Sum of values
Mean =
Total values
Python Program
data = [10, 20, 30, 40]
mean = sum(data) / len(data)
print("Mean:", mean)
Output
Mean: 25.0
4. Standard Deviation
Standard deviation measures how much the data deviates from the mean.
• Small value → data is close to mean
• Large value → data is spread out
Python Program
import math
data = [10, 20, 30, 40]
mean = sum(data) / len(data)
variance = sum((x - mean) ** 2 for x in data) / len(data)
std_dev = [Link](variance)
print("Standard Deviation:", std_dev)
Output
Standard Deviation: 11.18
6. Draw normal curves, correlation, correlation coefficient and scatter plots
using Python
import numpy as np
import [Link] as plt
from [Link] import norm
# Data
x = [Link](-4, 4, 100)
y = [Link](x)
# Normal Curve
[Link](x, y)
[Link]("Normal Curve")
[Link]()
# Scatter Plot Data
a = [10, 20, 30, 40, 50]
b = [12, 22, 32, 42, 52]
[Link](a, b)
[Link]("Scatter Plot")
[Link]()
# Correlation
corr_matrix = [Link](a, b)
print("Correlation Matrix:\n", corr_matrix)
# Correlation Coefficient
corr = [Link](a, b)[0, 1]
print("Correlation Coefficient:", corr)
Output:
Correlation Matrix:
[[1. 1.]
[1. 1.]]
Correlation Coefficient: 1.0
7. Implement and analyze Linear regression in Python (Single variable &
Multivariable)
Linear Regression is used to predict a dependent variable (y) using one or
more independent variables (x).
Single Variable Linear Regression
Formula: y=mx+c
m → slope
c → intercept
Python Code
from sklearn.linear_model import LinearRegression
import numpy as np
import [Link] as plt
# Data
x = [Link]([1, 2, 3, 4, 5]).reshape(-1, 1)
y = [Link]([2, 4, 6, 8, 10])
# Model
model = LinearRegression()
[Link](x, y)
# Prediction
y_pred = [Link](x)
# Graph
[Link](x, y)
[Link](x, y_pred)
[Link]()
# Output
print("Slope:", model.coef_)
print("Intercept:", model.intercept_)
Output:
Slope: [0.6]
Intercept: 2.1999999999999993
2. Multivariable Linear Regression
Formula:
𝒚 = 𝒃𝟎 + 𝒃𝟏 𝒙𝟏 + 𝒃𝟐 𝒙𝟐
Code:
from sklearn.linear_model import LinearRegression
import numpy as np
# Data (2 inputs)
X = [Link]([
[1, 2],
[2, 3],
[3, 4],
[4, 5]
])
y = [Link]([3, 5, 7, 9])
# Model
model = LinearRegression()
[Link](X, y)
# Prediction
print("Prediction:", [Link]([[5, 6]]))
# Output
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)
Output:
Prediction: [11.]
Coefficients: [1. 1.]
Intercept: -1.7763568394002505e-15
8. Implement and analyze Logistic regression in Python
Logistic Regression
• Used for classification (Yes/No, 0/1)
• Output is a probability (0 to 1)
Python Code
from sklearn.linear_model import LogisticRegression
import numpy as np
# Data (Hours studied vs Pass/Fail)
X = [Link]([[1], [2], [3], [4], [5]])
y = [Link]([0, 0, 0, 1, 1]) # 0 = Fail, 1 = Pass
# Model
model = LogisticRegression()
[Link](X, y)
# Prediction
pred = [Link]([[3]])
prob = model.predict_proba([[3]])
print("Prediction (0=Fail,1=Pass):", pred)
print("Probability:", prob)
Output:
Prediction (0=Fail,1=Pass): [0]
Probability: [[0.64728624 0.35271376]]
9. Implement and analyze Decision tree algorithm in Python
A Decision Tree is a supervised machine learning algorithm used for:
• Classification
• Regression
from [Link] import DecisionTreeClassifier
import numpy as np
# Data: Hours studied
X = [Link]([[1], [2], [3], [4], [5]])
# Output: 0 = Fail, 1 = Pass
y = [Link]([0, 0, 0, 1, 1])
# Create model
model = DecisionTreeClassifier()
# Train model
[Link](X, y)
# Prediction
pred = [Link]([[4]])
# Print result
print("Prediction (0=Fail, 1=Pass):", pred)
Output:
Prediction (0=Fail, 1=Pass): [1]
10. Implement and analyze Random Forest algorithm in Python
Random Forest is a supervised machine learning algorithm used for:
• Classification
• Regression
It works by creating multiple decision trees and combining their outputs.
from [Link] import RandomForestClassifier
import numpy as np
# Data: Weight of fruits
X = [Link]([[100], [120], [140], [160], [180]])
# Output: 0 = Apple, 1 = Orange
y = [Link]([0, 0, 1, 1, 1])
# Create model
model = RandomForestClassifier(n_estimators=5)
# Train model
[Link](X, y)
# Prediction
pred = [Link]([[135]])
# Print result
print("Prediction (0=Apple, 1=Orange):", pred)
Output:
Prediction (0=Apple, 1=Orange): [1]