0% found this document useful (0 votes)
10 views11 pages

ML File Final

The document describes experiments performed using scikit-learn machine learning library in Jupyter notebooks. It loads diabetes dataset and performs linear regression on it using scikit-learn APIs. It splits the data into train and test sets, trains a linear regression model and evaluates it by measuring error metrics.

Uploaded by

Gautam Sharma
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)
10 views11 pages

ML File Final

The document describes experiments performed using scikit-learn machine learning library in Jupyter notebooks. It loads diabetes dataset and performs linear regression on it using scikit-learn APIs. It splits the data into train and test sets, trains a linear regression model and evaluates it by measuring error metrics.

Uploaded by

Gautam Sharma
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/ 11

9/20/23, 11:50 AM Lab1 - Jupyter Notebook Gautam Sharma RA2111042030011

Lab1: Basic Python Programs


In [1]:

print("Hello, World!")

Hello, World!

In [2]:

a = 5
b = 3
sum_result = a + b
difference_result = a - b
product_result = a * b
quotient_result = a / b

print("Sum:", sum_result)
print("Difference:", difference_result)
print("Product:", product_result)
print("Quotient:", quotient_result)

Sum: 8
Difference: 2
Product: 15
Quotient: 1.6666666666666667

In [3]:

name = input("Enter your name: ")


print("Hello, " + name + "!")

Enter your name: Gautam


Hello, Gautam!

In [4]:

age = int(input("Enter your age: "))

if age >= 18:


print("You are an adult.")
else:
print("You are a minor.")

Enter your age: 21


You are an adult.

localhost:8888/notebooks/Lab1.ipynb 1/3
9/20/23, 11:50 AM Lab1 - Jupyter Notebook Gautam Sharma RA2111042030011

In [5]:

for i in range(1, 11):


print(i)

total = 0
for i in range(1, 101):
total += i
print("Sum of numbers from 1 to 100:", total)

1
2
3
4
5
6
7
8
9
10
Sum of numbers from 1 to 100: 5050

In [6]:

def add(a, b):


return a + b

result = add(7, 4)
print("Result of addition:", result)

Result of addition: 11

In [7]:

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


print("Fruits:", fruits)
print("First fruit:", fruits[0])

fruits.append("orange")
print("Updated fruits:", fruits)

for fruit in fruits:


print(fruit)

Fruits: ['apple', 'banana', 'cherry']


First fruit: apple
Updated fruits: ['apple', 'banana', 'cherry', 'orange']
apple
banana
cherry
orange

localhost:8888/notebooks/Lab1.ipynb 2/3
9/20/23, 11:50 AM Lab1 - Jupyter Notebook Gautam Sharma RA2111042030011

In [8]:

student = {"name": "Alice", "age": 20, "grade": "A"}


print("Student:", student)
print("Student name:", student["name"])

Student: {'name': 'Alice', 'age': 20, 'grade': 'A'}


Student name: Alice

In [9]:

with open("sample.txt", "w") as file:


file.write("Hello, this is a Gautam's file.")

with open("sample.txt", "r") as file:


content = file.read()
print("File content:", content)

File content: Hello, this is a Gautam's file.

localhost:8888/notebooks/Lab1.ipynb 3/3
9/20/23, 11:50 AM Lab2 - Jupyter Notebook Gautam Sharma RA2111042030011

Lab 2: Introduction to Scikit-Learn Library


In [12]:

import sklearn

In [13]:

from sklearn.datasets import load_iris

iris = load_iris()
X = iris.data
y = iris.target

In [14]:

from sklearn.model_selection import train_test_split

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

Model
In [15]:

from sklearn.tree import DecisionTreeClassifier

clf = DecisionTreeClassifier()

In [16]:

clf.fit(X_train, y_train)

Out[16]:

DecisionTreeClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or
trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page
with nbviewer.org.

In [17]:

y_pred = clf.predict(X_test)

localhost:8888/notebooks/Lab2.ipynb 1/2
9/20/23, 11:50 AM Lab2 - Jupyter Notebook Gautam Sharma RA2111042030011

In [18]:

from sklearn.metrics import accuracy_score

accuracy = accuracy_score(y_test, y_pred)


print("Accuracy:", accuracy)

Accuracy: 1.0

In [19]:

from sklearn.model_selection import cross_val_score

scores = cross_val_score(clf, X, y, cv=5)


print("Cross-Validation Scores:", scores)

Cross-Validation Scores: [0.96666667 0.96666667 0.9 0.93333333 1.


]

In [20]:

from sklearn.model_selection import GridSearchCV

param_grid = {'max_depth': [2, 3, 4, 5]}


grid_search = GridSearchCV(clf, param_grid, cv=5)
grid_search.fit(X_train, y_train)
best_params = grid_search.best_params_

In [21]:

from joblib import dump, load

dump(clf, 'model.joblib')
loaded_model = load('model.joblib')

In [ ]:

localhost:8888/notebooks/Lab2.ipynb 2/2
9/20/23, 11:51 AM Lab3 - Jupyter Notebook Gautam Sharma RA2111042030011

Lab3: Basic Data preprocessing


In [1]:

import pandas as pd
import numpy as np

In [2]:

df = pd.read_csv(r'C:\Users\Gautam Sharma\Desktop\Accelerator.csv')

In [3]:

print(df.head())

Segment Category Sub-Category City Order Date


0 Home Office Office Furniture Storage Cabinets Holyoke 06-Jul-19
\
1 Corporate Office Supplies Folders Leominster 26-Nov-19
2 Corporate Office Supplies Folders Leominster 26-Nov-19
3 Consumer Office Supplies Label Maker Tape Leominster 25-Feb-19
4 Consumer Office Supplies Printing Paper Leominster 25-Feb-19

Postal Code Region Ship Status State Profit Quantity


0 1040.0 Eastern Early Delivery Massachusetts 36 3
\
1 1453.0 Eastern Early Delivery Massachusetts 17 5
2 1453.0 Eastern Early Delivery Massachusetts 12 5
3 1453.0 Eastern Early Delivery Massachusetts 3 6
4 1453.0 Eastern Early Delivery Massachusetts 55 3

Sales
0 262
1 732
2 15
3 958
4 22

localhost:8888/notebooks/Lab3.ipynb 1/3
9/20/23, 11:51 AM Lab3 - Jupyter Notebook Gautam Sharma RA2111042030011

In [4]:

print(df.describe())

Postal Code Profit Quantity Sales


count 9983.000000 9994.000000 9994.000000 9994.000000
mean 55245.233297 28.651891 3.789574 229.873324
std 32038.715955 234.255712 2.225110 623.248946
min 1040.000000 -6600.000000 1.000000 0.000000
25% 23223.000000 2.000000 2.000000 17.000000
50% 57103.000000 9.000000 3.000000 54.500000
75% 90008.000000 29.000000 5.000000 210.000000
max 99301.000000 8400.000000 14.000000 22638.000000

In [5]:

print(df.isnull().sum())

Segment 0
Category 0
Sub-Category 0
City 0
Order Date 0
Postal Code 11
Region 0
Ship Status 0
State 0
Profit 0
Quantity 0
Sales 0
dtype: int64

In [6]:

df = df.dropna()

mean_value = df['Postal Code'].mean()


df['Postal Code'].fillna(mean_value, inplace=True)

localhost:8888/notebooks/Lab3.ipynb 2/3
9/20/23, 11:51 AM Lab3 - Jupyter Notebook Gautam Sharma RA2111042030011

In [7]:

print(df.isnull().sum())

Segment 0
Category 0
Sub-Category 0
City 0
Order Date 0
Postal Code 0
Region 0
Ship Status 0
State 0
Profit 0
Quantity 0
Sales 0
dtype: int64

localhost:8888/notebooks/Lab3.ipynb 3/3
9/20/23, 11:51 AM Lab4 - Jupyter Notebook Gautam Sharma RA2111042030011

Lab 4: Implementation of linear regresion using Scikit


learn
In [1]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, linear_model
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score

In [2]:

diabetes = datasets.load_diabetes()
X = diabetes.data[:, np.newaxis, 2]
y = diabetes.target

In [3]:

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

In [4]:

regr = linear_model.LinearRegression()
regr.fit(X_train, y_train)

Out[4]:

LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or
trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page
with nbviewer.org.

In [5]:

y_pred = regr.predict(X_test)

localhost:8888/notebooks/Lab4.ipynb 1/3
9/20/23, 11:51 AM Lab4 - Jupyter Notebook Gautam Sharma RA2111042030011

In [6]:

print('Coefficients:', regr.coef_)

mse = mean_squared_error(y_test, y_pred)


print('Mean Squared Error:', mse)

r2 = r2_score(y_test, y_pred)
print('R-squared:', r2)

Coefficients: [998.57768914]
Mean Squared Error: 4061.8259284949268
R-squared: 0.23335039815872138

In [7]:

plt.scatter(X_test, y_test, color='black')


plt.plot(X_test, y_pred, color='blue', linewidth=3)
plt.xlabel('Feature')
plt.ylabel('Target')
plt.title('Linear Regression')
plt.show()

localhost:8888/notebooks/Lab4.ipynb 2/3
9/20/23, 11:51 AM Lab4 - Jupyter Notebook Gautam Sharma RA2111042030011

localhost:8888/notebooks/Lab4.ipynb 3/3

You might also like