0% found this document useful (0 votes)
5 views37 pages

Aiml Lab

The document covers various statistical methods for machine learning using Jupyter Notebooks, including measures of central tendency (mean, median, mode) and variability (range, variance, standard deviation). It also demonstrates implementations of gradient descent, K-means clustering, linear regression, and logistic regression, both with and without using libraries like NumPy and scikit-learn. The examples include code snippets and outputs for better understanding of the concepts.

Uploaded by

kannahithesh
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)
5 views37 pages

Aiml Lab

The document covers various statistical methods for machine learning using Jupyter Notebooks, including measures of central tendency (mean, median, mode) and variability (range, variance, standard deviation). It also demonstrates implementations of gradient descent, K-means clustering, linear regression, and logistic regression, both with and without using libraries like NumPy and scikit-learn. The examples include code snippets and outputs for better understanding of the concepts.

Uploaded by

kannahithesh
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
You are on page 1/ 37

11/9/24, 1:54 PM Explore statistical methods for machine learning - Jupyter Notebook

Explore statistical methods for machine


learning

With formula
In [6]: from collections import Counter

In [1]: # Sample data


data = [1, 2, 2, 3, 4, 4, 4, 5, 6]

In [2]: # Measures of Central Tendency


# Mean
mean = sum(data) / len(data)

In [4]: # Median
sorted_data = sorted(data)
n = len(data)
if n % 2 == 0:
median = (sorted_data[n // 2 - 1] + sorted_data[n // 2]) / 2
else:
median = sorted_data[n // 2]

In [7]: # Mode
data_counts = Counter(data)
mode = data_counts.most_common(1)[0][0]

In [8]: # Display the results


print(f"Data: {data}")
print("\nMeasures of Central Tendency:")
print(f"Mean: {mean}")
print(f"Median: {median}")
print(f"Mode: {mode}")

Data: [1, 2, 2, 3, 4, 4, 4, 5, 6]

Measures of Central Tendency:


Mean: 3.4444444444444446
Median: 4
Mode: 4

In [9]: # Measures of Variability


# Range
data_range = max(data) - min(data)

localhost:8890/notebooks/Untitled Folder/Explore statistical methods for machine learning .ipynb 1/3


11/9/24, 1:54 PM Explore statistical methods for machine learning - Jupyter Notebook

In [10]: # Variance
mean_diff_squared = []
for x in data:
mean_diff_squared.append((x - mean) ** 2)
variance = sum(mean_diff_squared) / len(data)

In [11]: # Standard Deviation


std_deviation = variance ** 0.5

In [12]: print("\nMeasures of Variability:")


print(f"Range: {data_range}")
print(f"Variance: {variance}")
print(f"Standard Deviation: {std_deviation}")

Measures of Variability:
Range: 5
Variance: 2.246913580246914
Standard Deviation: 1.4989708403591158

Without Formula
In [15]: import numpy as np
from scipy import stats

In [16]: # Sample data


data = [1, 2, 2, 3, 4, 4, 4, 5, 6]

In [17]: # Measures of Central Tendency


# Mean
mean = np.mean(data)

In [18]: # Median
median = np.median(data)

In [24]: # Mode
mode = stats.mode(data)

localhost:8890/notebooks/Untitled Folder/Explore statistical methods for machine learning .ipynb 2/3


11/9/24, 1:54 PM Explore statistical methods for machine learning - Jupyter Notebook

In [25]: # Display the results


print(f"Data: {data}")
print("\nMeasures of Central Tendency:")
print(f"Mean: {mean}")
print(f"Median: {median}")
print(f"Mode: {mode}")

Data: [1, 2, 2, 3, 4, 4, 4, 5, 6]

Measures of Central Tendency:


Mean: 3.4444444444444446
Median: 4.0
Mode: ModeResult(mode=4, count=3)

In [26]: # Measures of Variability


# Range
data_range = np.ptp(data)

In [27]: # Variance
variance = np.var(data)

In [28]: # Standard Deviation


std_deviation = np.std(data)

In [29]: print("\nMeasures of Variability:")


print(f"Range: {data_range}")
print(f"Variance: {variance}")
print(f"Standard Deviation: {std_deviation}")

Measures of Variability:
Range: 5
Variance: 2.2469135802469133
Standard Deviation: 1.4989708403591155

In [ ]: ​

localhost:8890/notebooks/Untitled Folder/Explore statistical methods for machine learning .ipynb 3/3


9/13/24, 1:25 PM Gradient Descent - Jupyter Notebook

In [3]: # Step 1: Generate some data points


x = [1, 2, 3, 4, 5] # Independent variable
y = [2, 4, 5, 4, 5] # Dependent variable

# Step 2: Initialize parameters
m, b = 0, 0 # Start with some values
learning_rate = 0.01
num_iterations = 1000

# Step 3: Gradient Descent function
for _ in range(num_iterations):
# Calculate the gradients
m_grad = -(2 / len(x)) * sum([x[i] * (y[i] - (m * x[i] + b)) for i in range(l
b_grad = -(2 / len(x)) * sum([(y[i] - (m * x[i] + b)) for i in range(len(x))]

# Update the parameters


m -= learning_rate * m_grad
b -= learning_rate * b_grad

# Step 4: Predict y values using updated m and b
y_pred = [m * xi + b for xi in x]

print("Optimized Slope (m):", m)
print("Optimized Intercept (b):", b)
print("Predicted y values after Gradient Descent:", y_pred)

Optimized Slope (m): 0.6176946148762643


Optimized Intercept (b): 2.136116825825789
Predicted y values after Gradient Descent: [2.7538114407020533, 3.3715060555783
176, 3.989200670454582, 4.606895285330847, 5.224589900207111]

In [ ]: ​

localhost:8889/notebooks/Untitled Folder/Gradient Descent.ipynb# 1/1


11/16/24, 12:08 PM K-Means - Jupyter Notebook

Without Using Sklearn Library

localhost:8888/notebooks/Untitled Folder/K-Means.ipynb 1/5


11/16/24, 12:08 PM K-Means - Jupyter Notebook

In [1]: # Without Using Sklearn Library


import numpy as np
import matplotlib.pyplot as plt

# Simple K-Means implementation
def k_means(X, k, iterations=100):
# Step 1: Initialize centroids randomly from data points
np.random.seed(42) # For reproducibility
centroids = X[np.random.choice(len(X), k, replace=False)]

for _ in range(iterations):
# Step 2: Assign points to the nearest centroid
distances = np.linalg.norm(X[:, np.newaxis] - centroids, axis=2)
labels = np.argmin(distances, axis=1)

# Step 3: Update centroids as the mean of assigned points
new_centroids = np.array([X[labels == i].mean(axis=0) for i in range(k)

# Break if centroids do not change
if np.all(centroids == new_centroids):
break
centroids = new_centroids

return centroids, labels

# Example usage
if __name__ == "__main__":
# Sample dataset
X = np.array([
[1, 2], [1, 4], [1, 0],
[10, 2], [10, 4], [10, 0]
])

# Apply K-Means
k = 2
centroids, labels = k_means(X, k)

# Plotting the clusters
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', s=100, label='Data
plt.scatter(centroids[:, 0], centroids[:, 1], color='red', marker='x', s=20
plt.title('K-Means Clustering')
plt.xlabel('X1')
plt.ylabel('X2')
plt.legend()
plt.grid(True)
plt.show()

print("Centroids:\n", centroids)
print("Labels:\n", labels)

localhost:8888/notebooks/Untitled Folder/K-Means.ipynb 2/5


11/16/24, 12:08 PM K-Means - Jupyter Notebook

Centroids:
[[5.5 1. ]
[5.5 4. ]]
Labels:
[0 1 0 0 1 0]

With Using Sklearn Library

localhost:8888/notebooks/Untitled Folder/K-Means.ipynb 3/5


11/16/24, 12:08 PM K-Means - Jupyter Notebook

In [2]: # Without Using Sklearn Library


from sklearn.cluster import KMeans
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')

# Sample dataset
X = np.array([
[1, 2], [1, 4], [1, 0],
[10, 2], [10, 4], [10, 0]
])

# Apply K-Means
k = 2 # Number of clusters
kmeans = KMeans(n_clusters=k, random_state=42)
kmeans.fit(X)

# Get centroids and labels
centroids = kmeans.cluster_centers_
labels = kmeans.labels_

# Plotting the clusters
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', s=100, label='Data Poin
plt.scatter(centroids[:, 0], centroids[:, 1], color='red', marker='x', s=200, l
plt.title('K-Means Clustering (scikit-learn)')
plt.xlabel('X1')
plt.ylabel('X2')
plt.legend()
plt.grid(True)
plt.show()

print("Centroids:\n", centroids)
print("Labels:\n", labels)

localhost:8888/notebooks/Untitled Folder/K-Means.ipynb 4/5


11/16/24, 12:08 PM K-Means - Jupyter Notebook

Centroids:
[[ 1. 2.]
[10. 2.]]
Labels:
[0 0 0 1 1 1]

In [ ]: ​

localhost:8888/notebooks/Untitled Folder/K-Means.ipynb 5/5


9/13/24, 1:25 PM Linear Regression - Jupyter Notebook

In [3]: # Step 1: Generate some data points


x = [1, 2, 3, 4, 5] # Independent variable
y = [2, 4, 5, 4, 5] # Dependent variable

# Step 2: Calculate means of x and y
mean_x = sum(x) / len(x)
mean_y = sum(y) / len(y)

# Step 3: Calculate slope (m) and intercept (b)
numerator = sum([(x[i] - mean_x) * (y[i] - mean_y) for i in range(len(x))])
denominator = sum([(x[i] - mean_x) ** 2 for i in range(len(x))])
m = numerator / denominator
b = mean_y - m * mean_x

# Step 4: Predict y values using the model
y_pred = [m * xi + b for xi in x]

print("Slope (m):", m)
print("Intercept (b):", b)
print("Predicted y values:", y_pred)

Slope (m): 0.6


Intercept (b): 2.2
Predicted y values: [2.8000000000000003, 3.4000000000000004, 4.0, 4.6, 5.2]

In [ ]: ​

localhost:8889/notebooks/Untitled Folder/Linear Regression.ipynb 1/1


9/13/24, 1:23 PM Logistic Regression - Jupyter Notebook

In [1]: import math



# Step 1: Generate binary classification data
x = [1, 2, 3, 4, 5] # Independent variable
y = [0, 0, 0, 1, 1] # Dependent variable (binary classification)

# Step 2: Sigmoid function
def sigmoid(z):
return 1 / (1 + math.exp(-z))

# Step 3: Initialize parameters
m, b = 0, 0 # Start with initial values
learning_rate = 0.01
num_iterations = 1000

# Step 4: Gradient Descent for Logistic Regression
for _ in range(num_iterations):
m_grad = sum([(sigmoid(m * x[i] + b) - y[i]) * x[i] for i in range(len(x))])
b_grad = sum([(sigmoid(m * x[i] + b) - y[i]) for i in range(len(x))])

# Update the parameters


m -= learning_rate * m_grad
b -= learning_rate * b_grad

# Step 5: Predict probabilities using the sigmoid function
y_pred_prob = [sigmoid(m * xi + b) for xi in x]

# Convert probabilities to class predictions (0 or 1)
y_pred = [1 if prob > 0.5 else 0 for prob in y_pred_prob]

print("Optimized Slope (m):", m)
print("Optimized Intercept (b):", b)
print("Predicted class probabilities:", y_pred_prob)
print("Predicted classes:", y_pred)

Optimized Slope (m): 1.2693567720287338


Optimized Intercept (b): -4.177149454816769
Predicted class probabilities: [0.05176968460638721, 0.16267800083744785, 0.408
76355284120997, 0.7110065530114915, 0.8974893359190727]
Predicted classes: [0, 0, 0, 1, 1]

In [ ]: ​

localhost:8889/notebooks/Untitled Folder/Logistic Regression.ipynb# 1/1


8/7/24, 1:43 PM Over fitting - Jupyter Notebook

In [1]: import numpy as np


import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Generate synthetic data
np.random.seed(0)
X = 2 * np.pi * np.random.rand(50, 1) # Random values between 0 and 2*pi
y = np.sin(X).ravel() + np.random.randn(50) * 0.5 # Adding random noise to sin

# Split the data into training and testing sets
X_train, X_test = X[:40], X[40:]
y_train, y_test = y[:40], y[40:]

# Fit linear regression model
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

# Calculate mean squared error
train_error = mean_squared_error(y_train, y_train_pred)
test_error = mean_squared_error(y_test, y_test_pred)

# Plot the data points and regression line
plt.figure(figsize=(10, 6))
plt.scatter(X_train, y_train, color='blue', label='Training Data')
plt.scatter(X_test, y_test, color='red', label='Testing Data')
plt.plot(X_train, y_train_pred, color='green', label='Linear Regression')
plt.title('Linear Regression Example')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()

print(f'Training Error (MSE): {train_error:.3f}')
print(f'Testing Error (MSE): {test_error:.3f}')

localhost:8889/notebooks/Desktop/ML Lab/MD Python Lab/Over fitting.ipynb 2/5


8/7/24, 1:43 PM Over fitting - Jupyter Notebook

Training Error (MSE): 0.306


Testing Error (MSE): 0.619

localhost:8889/notebooks/Desktop/ML Lab/MD Python Lab/Over fitting.ipynb 3/5


8/7/24, 1:43 PM Over fitting - Jupyter Notebook

In [6]: import numpy as np


import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Generate synthetic data
np.random.seed(0)
X = np.random.rand(10, 1) * 10 # Random values between 0 and 10
y = 2 * X.ravel() + np.random.randn(10) * 2 # Linear relationship with some no

# Split the data into training and testing sets
X_train, X_test = X[:6], X[6:]
y_train, y_test = y[:6], y[6:]

# Fit linear regression model
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

# Calculate mean squared error
train_error = mean_squared_error(y_train, y_train_pred)
test_error = mean_squared_error(y_test, y_test_pred)

# Plot the data points and regression line
plt.figure(figsize=(10, 6))
plt.scatter(X_train, y_train, color='blue', label='Training Data')
plt.scatter(X_test, y_test, color='red', label='Testing Data')
plt.plot(X_train, y_train_pred, color='green', label='Linear Regression')
plt.title('Linear Regression Example')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()

# print training and testing values
print('Training Predictions:')
for pred in y_train_pred:
print(f'{pred:.3f}')

print('\nTesting Predictions:')
for pred in y_test_pred:
print(f'{pred:.3f}')

print(f'Training Error (MSE): {train_error:.3f}')


print(f'Testing Error (MSE): {test_error:.3f}')

localhost:8889/notebooks/Desktop/ML Lab/MD Python Lab/Over fitting.ipynb 4/5


8/7/24, 1:43 PM Over fitting - Jupyter Notebook

Training Predictions:
11.864
16.242
13.284
11.761
8.571
14.419

Testing Predictions:
8.938
20.888
22.780
7.513
Training Error (MSE): 0.515
Testing Error (MSE): 7.637

In [ ]: ​

localhost:8889/notebooks/Desktop/ML Lab/MD Python Lab/Over fitting.ipynb 5/5


9/22/23, 2:37 PM Program 2 - Jupyter Notebook

1. Logical Operator
In [1]:

# Logical operators are used on conditional statements (either True or False).


# They perform Logical AND, Logical OR and Logical NOT operations.

In [2]:

# Logical AND: True if both the operands are true


x = 5
y = 5
z = -5

if x > 0 and y > 0:


print("The numbers are greater than 0")

if x > 0 and y > 0 and z > 0:


print("The numbers are greater than 0")
else:
print("Atleast one number is not greater than 0")

The numbers are greater than 0


Atleast one number is not greater than 0

In [3]:

# Logical or operator returns True if either of the operands is True.


x = 5
y = 5
z = -5

if x > 0 or y > 0:
print("The numbers are greater than 0")

if x > 0 or y > 0 or z > 0:


print("Either of the number is greater than 0")
else:
print("No number is greater than 0")

The numbers are greater than 0


Either of the number is greater than 0

localhost:8888/notebooks/Documents/Program 2.ipynb 1/6


9/22/23, 2:37 PM Program 2 - Jupyter Notebook

In [4]:

# Logical not operator work with the single boolean value. If the boolean value is True i
x = 10

if not x:
print("Boolean value of a is True")

if not (x%3 == 0 or x%5 == 0):


print("10 is not divisible by either 3 or 5")
else:
print("10 is divisible by either 3 or 5")

10 is divisible by either 3 or 5

2. Bulding a Logic Rules in Python


In [5]:

# factorial of given number


num = int(input("Enter Your Number : "))
fact = 1
i=1
while i<=num:
fact=fact*i
i+=1
print("Factorial of",num,"is",fact)

Enter Your Number : 10


Factorial of 10 is 3628800

localhost:8888/notebooks/Documents/Program 2.ipynb 2/6


9/22/23, 2:37 PM Program 2 - Jupyter Notebook

In [6]:

# Python3 program to find simple interest


def SI(p,t,r):
print('The principal amount is : ', p)
print('The time period is : ', t)
print('The rate of interest is : ',r)
print('\n')

si = (p * t * r)/100

print('The Simple Interest is', si)


print('\n')
return si

# Driver code
P=int(input("Enter Principal Amount : "))
T=int(input("Enter Time period : "))
R=int(input("Enter Interest rate : "))
print('\n')
SI(P,T,R)

Enter Principal Amount : 100000


Enter Time period : 2
Enter Interest rate : 8

The principal amount is : 100000


The time period is : 2
The rate of interest is : 8

The Simple Interest is 16000.0

Out[6]:

16000.0

In [7]:

# python 3 programnto check whether the given number is armstrong or not

n=int(input('Enter Your Number : '))


s = n
b = len(str(n))
sum1 = 0
while n != 0:
r = n % 10
sum1 = sum1+(r**b)
n = n//10 #floor division
if s == sum1:
print("The given number", s, "is armstrong number")
else:
print("The given number", s, "is not armstrong number")

Enter Your Number : 153


The given number 153 is armstrong number

localhost:8888/notebooks/Documents/Program 2.ipynb 3/6


9/22/23, 2:37 PM Program 2 - Jupyter Notebook

In [8]:

# Python3 program to find maximum in arr[] of size n


arr = [1,10, 324, 9945, 90, 9808]
ans = max(arr)
print ("Largest in given array number is : ", ans)

Largest in given array number is : 9945

In [9]:

# Python program to print odd Numbers in a List


lst = [1, 2, 3, 4, 5, 6]
print('Odd Numbers are : ')
# iterating each number in list
for num in lst:

# checking condition
if num % 2 != 0:
print(num, end=" ")

Odd Numbers are :


1 3 5

In [10]:

s = "malayalam"
x = s[::-1] # Reverse of string (Spliting a string)
if s==x:
print("Yes ",s,' is Palindrom')
else:
print("No",s,' is not Palindrom')

Yes malayalam is Palindrom

In [11]:

s = "Python is a programming language"


x = s[::-1] # Reverse of string (Spliting a string)
print(x)
y = s.split()[::-1]
print(y)

egaugnal gnimmargorp a si nohtyP


['language', 'programming', 'a', 'is', 'Python']

localhost:8888/notebooks/Documents/Program 2.ipynb 4/6


9/22/23, 2:37 PM Program 2 - Jupyter Notebook

In [12]:

# Swapping Two numbers


num1=10
num2=20
num3=num1
num1=num2
num2=num3
print('num1 : ',num1)
print('num2 : ',num2)

# OR

x=10
y=20
x=x+y
y=x-y
x=x-y

print('x : ',x)
print('y : ',y)

num1 : 20
num2 : 10
x : 20
y : 10

localhost:8888/notebooks/Documents/Program 2.ipynb 5/6


9/22/23, 2:37 PM Program 2 - Jupyter Notebook

In [13]:

# Fibonacci Series
n = int(input('Enter number : '))
num1 = 0
num2 = 1
print(num1)
print(num2)
next_number = num2
count = 1

while count <= n:


print(next_number)
count += 1
#num1, num2 = num2, next_number
num1=num2
num2=next_number
next_number = num1 + num2
print()

Enter number : 10
0
1
1
2
3
5
8
13
21
34
55
89

In [ ]:

localhost:8888/notebooks/Documents/Program 2.ipynb 6/6


02/12/2023, 13:59 Program 5 - Jupyter Notebook

In [1]: # Import necessary modules


from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')

In [2]: # Loading data


irisData = load_iris()

In [3]: # Create feature and target arrays


X = irisData.data
y = irisData.target

In [4]: # Split into training and test set


X_train, X_test, y_train, y_test = train_test_split( X, y, test_size = 0.2,

In [5]: knn = KNeighborsClassifier(n_neighbors=7)


knn.fit(X_train, y_train)

Out[5]: KNeighborsClassifier(n_neighbors=7)

In [6]: # Predict on dataset which model has not seen before


print(knn.predict(X_test))

[1 0 2 1 1 0 1 2 2 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0]

In [7]: # Calculate the accuracy of the model


print(knn.score(X_test, y_test))

0.9666666666666667

In [8]: neighbors = np.arange(1, 40)


train_accuracy = np.empty(len(neighbors))
test_accuracy = np.empty(len(neighbors))

localhost:8888/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 5.ipynb 1/3


02/12/2023, 13:59 Program 5 - Jupyter Notebook

In [9]: # Loop over K values


for i, k in enumerate(neighbors):
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)

# Compute training and test data accuracy


train_accuracy[i] = knn.score(X_train, y_train)
test_accuracy[i] = knn.score(X_test, y_test)
print('Training Accuracy : ', train_accuracy[i] )
print('Testing Accuracy : ', test_accuracy[i])
print(' ')

Testing Accuracy : 1.0

Training Accuracy : 0.9583333333333334


Testing Accuracy : 1.0

Training Accuracy : 0.95


Testing Accuracy : 1.0

Training Accuracy : 0.95


Testing Accuracy : 0.9666666666666667

Training Accuracy : 0.9416666666666667


Testing Accuracy : 1.0

Training Accuracy : 0.9416666666666667


Testing Accuracy : 0.9666666666666667

Training Accuracy : 0.95


Testing Accuracy : 1.0

localhost:8888/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 5.ipynb 2/3


02/12/2023, 13:59 Program 5 - Jupyter Notebook

In [10]: # Generate plot


plt.scatter(neighbors, test_accuracy, label = 'Testing dataset Accuracy')
plt.scatter(neighbors, train_accuracy, label = 'Training dataset Accuracy')

plt.legend()
plt.xlabel('n_neighbors')
plt.ylabel('Accuracy')
plt.show()

In [ ]: ​

localhost:8888/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 5.ipynb 3/3


Week - 4, 5, 6 & 7: Using any data apply the concept of:
a. Linear regression
b. Gradient decent
c. Logistic regression

Linear Regression: Linear Regression models the relationship between the


dependent variable and one or more independent variables by fitting a linear equation
to the observed data.

y=mx+b, where y is the dependent variable, x is the independent variable, m is the


slope, and b is the intercept. Example: Predicting house prices based on the square
foot of the house.

The linear regression equation is given by:


Y=mx+b

Code :
# Step 1: Generate some data points
x = [1, 2, 3, 4, 5] # Independent variable
y = [2, 4, 5, 4, 5] # Dependent variable

# Step 2: Calculate means of x and y


mean_x = sum(x) / len(x)
mean_y = sum(y) / len(y)

# Step 3: Calculate slope (m) and intercept (b)


numerator = sum([(x[i] - mean_x) * (y[i] - mean_y) for i in range(len(x))])
denominator = sum([(x[i] - mean_x) ** 2 for i in range(len(x))])
m = numerator / denominator
b = mean_y - m * mean_x

# Step 4: Predict y values using the model


y_pred = [m * xi + b for xi in x]
print("Slope (m):", m)
print("Intercept (b):", b)
print("Predicted y values:", y_pred)
Output :
Slope (m): 0.6
Intercept (b): 2.2
Predicted y values: [2.8000000000000003, 3.4000000000000004, 4.0, 4.6, 5.2]

2. Gradient Descent

Gradient Descent is an optimization algorithm used to minimize the cost function. In


the case of linear regression, we minimize the Mean Squared Error (MSE).

Steps:

1. Initialize random values for m (slope) and b (intercept).


2. Compute the gradient (derivative) of the cost function with respect to m and b.
3. Update the values of mmm and bbb iteratively to minimize the error.

Formula:

X* = X – learning-rate f’(x)

θt​ =θt-1​ − α ∇J(θt)


where θ are the model parameters, α is the learning rate, and J(θ) is the loss function.

Gradient descent updates the parameters m and b by moving in the direction of the
negative gradient of the cost function. The updates for each iteration are given by:

 J(m,b) is the cost function (e.g., MSE).


 α is the learning rate (a small constant that controls the size of the update
steps).
 ∂J(m,b)/∂m is the partial derivative of the cost function with respect to m (the
slope).

Code :

import numpy as np

# Step 1: Generate some data points


x = [1, 2, 3, 4, 5] # Independent variable
y = [2, 4, 5, 4, 5] # Dependent variable

# Step 2: Initialize parameters


b = np.random.randn()
m = 0 # Start with some values
learning_rate = 0.01
num_iterations = 1000

# Step 3: Gradient Descent function


for _ in range(num_iterations):
# Calculate the gradients
m_grad = -(2 / len(x)) * sum([x[i] * (y[i] - (m * x[i] + b)) for i in range(len(x))])

# Update the parameters


m -= learning_rate * m_grad

# Step 4: Predict y values using updated m and b


y_pred = [m * xi + b for xi in x]

print("Optimized Slope (m):", m)


print("Optimized Intercept (b):", b)
print("Predicted y values after Gradient Descent:", y_pred)

Optimized Slope (m): 1.1447356618327063


Optimized Intercept (b): 0.20263590661340863
Predicted y values after Gradient Descent: [1.347371568446115, 2.492107230278821,
3.6368428921115274, 4.781578553944233, 5.92631421577694]

3. Logistic Regression

Logistic regression is used for binary classification problems where the output is
categorical (usually 0 or 1). Instead of modeling the output directly, logistic
regression models the probability that the output belongs to a particular class using
the logistic function:
Code :

import math

# Step 1: Generate binary classification data

x = [1, 2, 3, 4, 5] # Independent variable

y = [0, 0, 0, 1, 1] # Dependent variable (binary classification)

# Step 2: Sigmoid function

def sigmoid(z):

return 1 / (1 + math.exp(-z))

# Step 3: Initialize parameters

m, b = 0, 0 # Start with initial values

learning_rate = 0.01

num_iterations = 1000

# Step 4: Gradient Descent for Logistic Regression

for _ in range(num_iterations):

m_grad = sum([(sigmoid(m * x[i] + b) - y[i]) * x[i] for i in range(len(x))])

b_grad = sum([(sigmoid(m * x[i] + b) - y[i]) for i in range(len(x))])

# Update the parameters


m -= learning_rate * m_grad

b -= learning_rate * b_grad

# Step 5: Predict probabilities using the sigmoid function

y_pred_prob = [sigmoid(m * xi + b) for xi in x]

# Convert probabilities to class predictions (0 or 1)

y_pred = [1 if prob > 0.5 else 0 for prob in y_pred_prob]

print("Optimized Slope (m):", m)

print("Optimized Intercept (b):", b)

print("Predicted class probabilities:", y_pred_prob)

print("Predicted classes:", y_pred)

Output :
Optimized Slope (m): 1.2693567720287338
Optimized Intercept (b): -4.177149454816769
Predicted class probabilities: [0.05176968460638721, 0.16267800083744785, 0.40
876355284120997, 0.7110065530114915, 0.8974893359190727]
Predicted classes: [0, 0, 0, 1, 1]
08/09/2023, 15:54 Week 1 & 2 - Jupyter Notebook

Program 1 : Python program to display text


statement(Hello World)
In [1]:

print("Hello World!")

Hello World!

In [2]:

x='Hello World!'
print(x)

Hello World!

In [3]:

print(str(input("Enter String : ")))

Enter String : Hello World!


Hello World!

Program 2 : Python program to display today's date


and time
In [4]:

# importing datetime module for now()


import datetime

# using now() to get current time


current_time = datetime.datetime.now()

# Printing value of now.


print("Time now at greenwich meridian is:", current_time)

Time now at greenwich meridian is: 2023-09-08 15:53:17.070465

localhost:8808/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 1/Week 1 %26 2.ipynb 1/8


08/09/2023, 15:54 Week 1 & 2 - Jupyter Notebook

In [5]:

import datetime

# using now() to get current time


current_time = datetime.datetime.now()

# Printing attributes of now().


print("The attributes of now() are :")

print("Year :", current_time.year)

print("Month : ", current_time.month)

print("Day : ", current_time.day)

print("Hour : ", current_time.hour)

print("Minute : ", current_time.minute)

print("Second :", current_time.second)

print("Microsecond :", current_time.microsecond)

The attributes of now() are :


Year : 2023
Month : 9
Day : 8
Hour : 15
Minute : 53
Second : 17
Microsecond : 273058

In [6]:

import datetime

# for timezone()
import pytz

# using now() to get current time. To get the date and time for a particular timezone now
# takes timezone as input to give timezone-oriented output time.
current_time = datetime.datetime.now(pytz.timezone('Asia/Kolkata'))

# printing current time in india


print("The current time in india is :", current_time)

The current time in india is : 2023-09-08 15:53:18.050001+05:30

In [7]:

from datetime import datetime

print("UTC Time: ", datetime.utcnow()) # UTC Stands for Coordinated Universal Time,
# These times are useful when you are dealing with Applications that have a global user f

UTC Time: 2023-09-08 10:23:18.070812

localhost:8808/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 1/Week 1 %26 2.ipynb 2/8


08/09/2023, 15:54 Week 1 & 2 - Jupyter Notebook

In [8]:

from datetime import datetime as dt

x = dt.now().isoformat() #isoformat() method is used to get the current date and time in
#It starts with the year, followed by the month, the day, the hour, the minutes, seco
print('Current ISO:', x)

Current ISO: 2023-09-08T15:53:18.250710

In [9]:

# Here, we are getting the current time using the time module.
import time

curr_time = time.strftime("%H:%M:%S", time.localtime())

print("Current Time is :", curr_time)

Current Time is : 15:53:18

In [10]:

import time

millisec = int(round(time.time() * 1000))

print("Time in Milli seconds: ", millisec)

nano_seconds = time.time_ns()

print("Current time in Nano seconds is : ", nano_seconds)

Time in Milli seconds: 1694168598535


Current time in Nano seconds is : 1694168598534612400

In [11]:

import time

# current GMT Time Green Mean Time, which is also known as GMT can be used by using time.
# just need to pass the time in seconds to this method to get the GMT
gmt_time = time.gmtime(time.time())

print('Current GMT Time:\n', gmt_time)

Current GMT Time:


time.struct_time(tm_year=2023, tm_mon=9, tm_mday=8, tm_hour=10, tm_min=2
3, tm_sec=18, tm_wday=4, tm_yday=251, tm_isdst=0)

In [12]:

import time

print("Epoch Time is : ", int(time.time())) #We can get the Epoch current time by convert

Epoch Time is : 1694168598

localhost:8808/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 1/Week 1 %26 2.ipynb 3/8


08/09/2023, 15:54 Week 1 & 2 - Jupyter Notebook

Program 3 : Python program to find the sum of given


numbers
In [13]:

num1=100
num2=50
sum=num1+num2
print("Addition of two no. : ",sum)

Addition of two no. : 150

In [14]:

num1=int(input('Enter First Number : '))


num2=int(input('Enter Second Number : '))
sum=num1+num2
print("Addition of two Number : ",sum)

Enter First Number : 5


Enter Second Number : 34
Addition of two Number : 39

In [15]:

sum=0
num=int(input("Enter the NUmber : "))
for i in range(num+1):
sum+=i
print("Addition of given Number : ", sum)

Enter the NUmber : 100


Addition of given Number : 5050

Program 4 : Python program to display the matrix

localhost:8808/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 1/Week 1 %26 2.ipynb 4/8


08/09/2023, 15:54 Week 1 & 2 - Jupyter Notebook

In [16]:

# Python program to create a matrix using numpy array

# Importing numpy
import numpy as np

# Creating the matrix


record = np.array( [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("The matrix is : \n", record)
print(record.dtype)

The matrix is :
[[1 2 3]
[4 5 6]
[7 8 9]]
int32

In [17]:

record = np.array( [[100,'Itika', 89],


['Aditi', 96, 82],
['Harry', 91, 81],
['Andrew', 87, 91],
['Peter', 72, 79]])
print("The matrix is : \n", record)
print(record.dtype)

The matrix is :
[['100' 'Itika' '89']
['Aditi' '96' '82']
['Harry' '91' '81']
['Andrew' '87' '91']
['Peter' '72' '79']]
<U11

In [18]:

# Python program to show how to create a matrix using the matrix method

# importing numpy
import numpy as np

# Creating a matrix
matrix = np.matrix('3,4,5;5,6,7;8,9,10')
print(matrix)

[[ 3 4 5]
[ 5 6 7]
[ 8 9 10]]

Program 5 : Python program to perform matrix


operation
localhost:8808/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 1/Week 1 %26 2.ipynb 5/8
08/09/2023, 15:54 Week 1 & 2 - Jupyter Notebook

In [19]:

# Python code to demonstrate matrix operations


# add(), subtract() and divide()

# importing numpy for matrix operations


import numpy

# initializing matrices
x = numpy.array([[1, 2], [4, 5]])
y = numpy.array([[7, 8], [9, 10]])

# using add() to add matrices


print ("The element wise addition of matrix is : ")
print (numpy.add(x,y))

# using subtract() to subtract matrices


print ("The element wise subtraction of matrix is : ")
print (numpy.subtract(x,y))

# using divide() to divide matrices


print ("The element wise division of matrix is : ")
print (numpy.divide(x,y))

# using multiply() to multiply matrices element wise


print ("The element wise multiplication of matrix is : ")
print (numpy.multiply(x,y))

# using dot() to multiply matrices


print ("The product of matrices is : ")
print (numpy.dot(x,y))

# using "T" to transpose the matrix


print ("The transpose of given matrix is : ")
print (x.T)

The element wise addition of matrix is :


[[ 8 10]
[13 15]]
The element wise subtraction of matrix is :
[[-6 -6]
[-5 -5]]
The element wise division of matrix is :
[[0.14285714 0.25 ]
[0.44444444 0.5 ]]
The element wise multiplication of matrix is :
[[ 7 16]
[36 50]]
The product of matrices is :
[[25 28]
[73 82]]
The transpose of given matrix is :
[[1 4]
[2 5]]

Program 6 : Python program to display Dictionory

localhost:8808/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 1/Week 1 %26 2.ipynb 6/8


08/09/2023, 15:54 Week 1 & 2 - Jupyter Notebook

In [21]:

student = {
"RollNo" : 1232,
"Name" : 'XYZ',
"maths Mark" : 65,
"physics Mark" : 55,
"chemistry Mark" : 87
}
print("Print Student Details : ",student)
print("Student Details are : ")
for x,y in student.items():
print(x,' : ',y)

Print Student Details : {'RollNo': 1232, 'Name': 'XYZ', 'maths Mark': 65,
'physics Mark': 55, 'chemistry Mark': 87}
Student Details are :
RollNo : 1232
Name : XYZ
maths Mark : 65
physics Mark : 55
chemistry Mark : 87

Program 7 : Python Program to access Tuple Value

In [22]:

# Tuples are used to store multiple items in a single variable. A tuple is a collection w

In [23]:

thistuple = ("apple", "banana", "cherry", "apple", "cherry")


print(thistuple)

for i in thistuple:
print(i)

('apple', 'banana', 'cherry', 'apple', 'cherry')


apple
banana
cherry
apple
cherry

Program 8 : Python Program to access Tuple Value

In [24]:

# Lists are used to store multiple items in a single variable. List items are ordered, ch

localhost:8808/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 1/Week 1 %26 2.ipynb 7/8


08/09/2023, 15:54 Week 1 & 2 - Jupyter Notebook

In [25]:

thislist = ["apple", "banana", "cherry", "apple", "cherry"]


print(thislist)
for i in thislist:
print(i)

['apple', 'banana', 'cherry', 'apple', 'cherry']


apple
banana
cherry
apple
cherry

localhost:8808/notebooks/Desktop/Sana Afreen/MD Python Lab/Program 1/Week 1 %26 2.ipynb 8/8

You might also like