0% found this document useful (0 votes)
6 views10 pages

B-56 Sanket Jambhulkar MLA-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

1/19/24, 2:39 PM Untitled8.

ipynb - Colaboratory

G. H. RAISONI COLLEGE OF ENGINEERING, NAGPUR


(An Autonomous Institute affiliated to RTM Nagpur University)
Department of Computer Science & Engineering
Session: Winter 2024

Date:19/01/2024
Practical Details: Practical No. 1
Student Details:
Roll Number 56
Name sanket jambhulkar
Semester 6th
Section B
Branch CSE
Subject Machine learning and algorithm

Introduction to Python programming and


Aim various libraries used for machine
learning.
Theory
Python is a versatile and powerful programming language that is
widely used in various fields, including web development, data
science, artificial intelligence, and machine learning. Its
readability and simplicity make it an excellent choice for
beginners, while its extensive libraries and frameworks make it
a favorite among experienced developers. In the context of
machine learning, Python has become the de facto language
due to its rich ecosystem of libraries and tools. Here's an
introduction to Python programming and some key libraries
used in machine learning:

Introduction to Python Programming:


1. Syntax:
● Python uses a clean and readable syntax, emphasizing code readability and simplicity.

● Indentation is used for block structures instead of braces.

● # Example of Python code

● if True:

● print("Hello, World!")

2. Data Types:
● Common data types include integers, floats, strings, lists, tuples, and dictionaries.

● # Example of basic data types

https://colab.research.google.com/drive/1XXnJqjEYBzg03yoTRUyHPPq9BkC2JTUQ#scrollTo=9tHj6NLy-M3M&printMode=true 1/10
1/19/24, 2:39 PM Untitled8.ipynb - Colaboratory

● num = 10

● pi = 3.14

● name = "John"

● my_list = [1, 2, 3]

● my_dict = {'key': 'value'}

3. Control Flow:
● Python supports conditional statements (if, else, elif) and loops (for, while)

● for i in range(5):

● print(i)

● if x > 0:

● print("Positive")

● elif x < 0:

● print("Negative")

● else:

● print("Zero")

4. Functions:
● Functions are defined using the def keyword.

● # Example of a function

● def greet(name):

● return f"Hello, {name}!"

● print(greet("Alice"))

5. Libraries and Modules:


● Python has a rich standard library and supports external libraries for various functionalities.

Python Libraries for Machine Learning:


1. NumPy:
● NumPy provides support for large, multi-dimensional arrays and matrices, along with mathematical functions
to operate on these arrays.

● import numpy as np

2. Pandas:
● Pandas is used for data manipulation and analysis. It provides data structures like DataFrames, which are
especially useful for handling structured data.

● import pandas as pd

3. Matplotlib and Seaborn:


● Matplotlib and Seaborn are used for data visualization, allowing you to create various types of plots and
charts.

https://colab.research.google.com/drive/1XXnJqjEYBzg03yoTRUyHPPq9BkC2JTUQ#scrollTo=9tHj6NLy-M3M&printMode=true 2/10
1/19/24, 2:39 PM Untitled8.ipynb - Colaboratory

● import matplotlib.pyplot as plt

● import seaborn as sns

4. Scikit-Learn:
● Scikit-Learn is a machine learning library that provides simple and efficient tools for data mining and data
analysis. It includes various algorithms for classification, regression, clustering, and more.

● from sklearn import model_selection, preprocessing, metrics

5. TensorFlow and PyTorch:


● TensorFlow and PyTorch are powerful deep learning frameworks widely used for building and training neural
networks.

● import tensorflow as tf

● import torch

6. Keras:
● Keras is a high-level neural networks API, often used in conjunction with
TensorFlow. It simplifies the process of building and training neural
networks.
python

from keras.models import Sequential


from keras.layers import Dense

from google.colab import drive


drive.mount('/content/drive')

import pandas as pd import numpy as numpy df =


pd.read_excel('/content/drive/MyDrive/sameer.xlsx')
print(df)

Roll No NAME SUB 1 SUB 2 SUB 3 Total


0 1.0 Deep 30 30 30 90
1 2.0 Jayesh 40 40 40 120
2 3.0 Yash 45 36 47 128
3 4.0 Sara 48 32 50 130
4 5.0 Gita 35 32 43 110
5 6.0 Jinal 32 31 37 100
6 7.0 Kavita 36 28 38 102
7 8.0 Minal 23 25 40 88
8 9.0 Naresh 43 27 50 120
9 10.0 Rima 37 44 46 127
10 NaN NaN 369 325 421 1115

import pandas as pd from sklearn.model_selection import train_test_split from


sklearn.linear_model import LinearRegression from sklearn.ensemble import
RandomForestClassifier from sklearn.metrics import mean_squared_error,
accuracy_score, classification_report

data = {
'Roll No': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, None],
'NAME': ['Deep', 'Jayesh', 'Yash', 'Sara', 'Gita', 'Jinal', 'Kavita', 'Minal', 'Naresh', 'Rima', None],
'SUB 1': [30, 40, 45, 48, 35, 32, 36, 23, 43, 37, 369],
'SUB 2': [30, 40, 36, 32, 32, 31, 28, 25, 27, 44, 325],
'SUB 3': [30, 40, 47, 50, 43, 37, 38, 40, 50, 46, 421],
'Total': [90, 120, 128, 130, 110, 100, 102, 88, 120, 127, 1115]
} df =

pd.DataFrame(data)

https://colab.research.google.com/drive/1XXnJqjEYBzg03yoTRUyHPPq9BkC2JTUQ#scrollTo=9tHj6NLy-M3M&printMode=true 3/10
1/19/24, 2:39 PM Untitled8.ipynb - Colaboratory
features = df.drop(['Roll No', 'NAME', 'Total'], axis=1)
labels = df['Total']

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=2)

regression_model = LinearRegression()
regression_model.fit(X_train, y_train)

y_pred_regression = regression_model.predict(X_test)

mse = mean_squared_error(y_test, y_pred_regression)


print(f"Mean Squared Error (Regression): {mse:.2f}")

Mean Squared Error (Regression): 0.00

import pandas as pd

data = {
'Roll No': [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, None],
'NAME': ['Deep', 'Jayesh', 'Yash', 'Sara', 'Gita', 'Jinal', 'Kavita', 'Minal', 'Naresh', 'Rima', None],
'SUB 1': [30, 40, 45, 48, 35, 32, 36, 23, 43, 37, 369],
'SUB 2': [30, 40, 36, 32, 32, 31, 28, 25, 27, 44, 325],
'SUB 3': [30, 40, 47, 50, 43, 37, 38, 40, 50, 46, 421],
'Total': [90, 120, 128, 130, 110, 100, 102, 88, 120, 127, 1115]
}

df = pd.DataFrame(data)

print(df)
Roll No NAME SUB 1 SUB 2 SUB 3 Total
0 1.0 Deep 30 30 30 90
1 2.0 Jayesh 40 40 40 120
2 3.0 Yash 45 36 47 128
3 4.0 Sara 48 32 50 130
4 5.0 Gita 35 32 43 110
5 6.0 Jinal 32 31 37 100
6 7.0 Kavita 36 28 38 102
7 8.0 Minal 23 25 40 88
8 9.0 Naresh 43 27 50 120
9 10.0 Rima 37 44 46 127
10 NaN None 369 325 421 1115

import pandas as pd df =
pd.read_excel('/content/drive/MyDrive/sameer.xlsx')
df.iloc[0:5,0:4]

Roll No NAME SUB 1 SUB 2

0 1.0 Dee 30 30
p
1 2.0 Jayes 40 40
h
2 3.0 Ya 45 36
sh
3 4.0 Sar 48 32
a
4 5.0 Gita 35 32
import pandas as pd df =
pd.read_excel('/content/drive/MyDrive/sameer.xlsx')
df.iloc[0:-1,0:-1]

Roll No NAME SUB 1 SUB 2 SUB 3

0 1.0 Deep 30 30 30

1 2.0 Jayesh 40 40 40

2 3.0 Yash 45 36 47

https://colab.research.google.com/drive/1XXnJqjEYBzg03yoTRUyHPPq9BkC2JTUQ#scrollTo=9tHj6NLy-M3M&printMode=true 4/10
1/19/24, 2:39 PM Untitled8.ipynb - Colaboratory
3 4.0 Sara 48 32 50

4 5.0 Gita 35 32 43

5 6.0 Jinal 32 31 37

6 7.0 Kavita 36 28 38

7 8.0 Minal 23 25 40

8 9.0 Naresh 43 27 50

9 10.0 Rima 37 44 46

len(df)
df.describe()

Roll No SUB 1 SUB 2 SUB 3 Total

cou 10.000 11.000 11.000000 11.0000 11.000


nt 00 000 00 000
mea 5.500 67.090 59.090909 76.5454 202.727
n 00 909 55 273
std 3.027 100.383 88.371324 114.4013 302.936
65 718 67 327
mi 1.000 23.000 25.000000 30.0000 88.000
n 00 000 00 000
25 3.250 33.500 29.000000 39.0000 101.000
% 00 000 00 000
50 5.500 37.000 32.000000 43.0000 120.000
% 00 000 00 000
75 7.750 44.000 38.000000 48.5000 127.500
% 00 000 00 000
ma 10.000 369.000 325.0000 421.0000 1115.000
x 00 000 00 00 000
df.head(3)
Roll No NAME SUB 1 SUB 2 SUB 3 Total

0 1. De 3 3 3 90
0 ep 0 0 0
1 2. Jaye 4 4 4 12
0 sh 0 0 0 0
2 3. Ya 4 3 4 12
0 sh 5 6 7 8
df.tail(4)

Roll No NAME SUB 1 SUB 2 SUB 3 Total

7 8 Mina 23 25 40 88
. l
0
8 9 Nar 43 27 50 120
. esh
0
9 1 Rim 37 44 46 127
0. a
0
10 N NaN 369 325 421 111
a 5
N
df.sample
()
Roll NA SUB SUB SUB Tot
No ME 1 2 3 al

6 7.0 Kavita 36 28 38 102

df.size

66

df1=sum(df.Total)
print(df1)

2230

df.drop

https://colab.research.google.com/drive/1XXnJqjEYBzg03yoTRUyHPPq9BkC2JTUQ#scrollTo=9tHj6NLy-M3M&printMode=true 5/10
1/19/24, 2:39 PM Untitled8.ipynb - Colaboratory
<bound method DataFrame.drop of Roll No NAME SUB 1 SUB 2 SUB 3 Total
0 1.0 Deep 30 30 30 90
1 2.0 Jayesh 40 40 40 120
2 3.0 Yash 45 36 47 128
3 4.0 Sara 48 32 50 130
4 5.0 Gita 35 32 43 110
5 6.0 Jinal 32 31 37 100
6 7.0 Kavita 36 28 38 102
7 8.0 Minal 23 25 40 88
8 9.0 Naresh 43 27 50 120
9 10.0 Rima 37 44 46 127
10 NaN NaN 369 325 421 1115>

import numpy as np

a = np.array([1, 2, 3, 4, 5]) b =
np.array([[10, 11, 12], [6, 7, 8]])

print(a.ndim)
print(b.ndim)

1
2

import numpy as np

a = np.array([1, 2, 3, 4, 5]) b =
np.array([[10, 11, 12], [6, 7, 8]],
ndmin=3)

print(a.ndim)
print(b.ndim)

1
3
import matplotlib.pyplot as plt
import pandas as pd

# Your data
data = {
'Roll No': [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
'NAME': ['Deep', 'Jayesh', 'Yash', 'Sara', 'Gita', 'Jinal', 'Kavita', 'Minal', 'Naresh', 'Rima'],
'SUB 1': [30, 40, 45, 48, 35, 32, 36, 23, 43, 37],
'SUB 2': [30, 40, 36, 32, 32, 31, 28, 25, 27, 44],
'SUB 3': [30, 40, 47, 50, 43, 37, 38, 40, 50, 46],
'Total': [90, 120, 128, 130, 110, 100, 102, 88, 120, 127]
}

# Creating a DataFrame
df =
pd.DataFrame(data)

# Plotting fig, ax =
plt.subplots(figsize=(10, 6))

# Plotting bars bar_width = 0.2 bar_positions = range(len(df['Roll No']))


ax.bar(bar_positions, df['SUB 1'], width=bar_width, label='SUB 1') ax.bar([pos + bar_width
for pos in bar_positions], df['SUB 2'], width=bar_width, label='SUB 2') ax.bar([pos + 2 *
bar_width for pos in bar_positions], df['SUB 3'], width=bar_width, label='SUB 3')
ax.bar([pos + 3 * bar_width for pos in bar_positions], df['Total'], width=bar_width,
label='Total')

# Adding labels and title ax.set_xticks([pos + 1.5 *


bar_width for pos in bar_positions])
ax.set_xticklabels(df['NAME']) ax.set_xlabel('Students')
ax.set_ylabel('Marks') ax.set_title('Subject-wise and
Total Marks for Students')

# Adding legend
ax.legend()

# Display the plot


plt.show()

https://colab.research.google.com/drive/1XXnJqjEYBzg03yoTRUyHPPq9BkC2JTUQ#scrollTo=9tHj6NLy-M3M&printMode=true 6/10
1/19/24, 2:39 PM Untitled8.ipynb - Colaboratory

import matplotlib.pyplot as plt


import pandas as pd

# Your data
data = {
'Roll No': [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
'NAME': ['Deep', 'Jayesh', 'Yash', 'Sara', 'Gita', 'Jinal', 'Kavita', 'Minal', 'Naresh', 'Rima'],
'SUB 1': [30, 40, 45, 48, 35, 32, 36, 23, 43, 37],
'SUB 2': [30, 40, 36, 32, 32, 31, 28, 25, 27, 44],
'SUB 3': [30, 40, 47, 50, 43, 37, 38, 40, 50, 46],
'Total': [90, 120, 128, 130, 110, 100, 102, 88, 120, 127]
}

# Creating a DataFrame
df =
pd.DataFrame(data)

# Plotting scatter plot fig, ax =


plt.subplots(figsize=(10, 6))

# Scatter plot for SUB 1 ax.scatter(df['NAME'],


df['SUB 1'], label='SUB 1', s=100)

# Scatter plot for SUB 2 ax.scatter(df['NAME'],


df['SUB 2'], label='SUB 2', s=100)

# Scatter plot for SUB 3 ax.scatter(df['NAME'],


df['SUB 3'], label='SUB 3', s=100)

# Scatter plot for Total ax.scatter(df['NAME'],


df['Total'], label='Total', s=100)

# Adding labels and title


ax.set_xlabel('Students')
ax.set_ylabel('Marks') ax.set_title('Scatter
Plot of Marks for Students')

# Adding legend
ax.legend()

# Rotate x-axis labels for better readability


plt.xticks(rotation=45, ha='right')

# Display the plot


plt.tight_layout()
plt.show()

https://colab.research.google.com/drive/1XXnJqjEYBzg03yoTRUyHPPq9BkC2JTUQ#scrollTo=9tHj6NLy-M3M&printMode=true 7/10
1/19/24, 2:39 PM Untitled8.ipynb - Colaboratory

import matplotlib.pyplot as plt


import pandas as pd
# Your data
data = {
'Roll No': [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
'NAME': ['Deep', 'Jayesh', 'Yash', 'Sara', 'Gita', 'Jinal', 'Kavita', 'Minal', 'Naresh', 'Rima'],
'SUB 1': [30, 40, 45, 48, 35, 32, 36, 23, 43, 37],
'SUB 2': [30, 40, 36, 32, 32, 31, 28, 25, 27, 44],
'SUB 3': [30, 40, 47, 50, 43, 37, 38, 40, 50, 46],
'Total': [90, 120, 128, 130, 110, 100, 102, 88, 120, 127]
}

# Creating a DataFrame
df =
pd.DataFrame(data)

# Choose a student (for example, 'Deep')


student_name = 'Deep'

# Filter data for the selected student


student_data = df[df['NAME'] == student_name]

# Data for the pie chart subject_marks = student_data[['SUB 1', 'SUB 2',
'SUB 3', 'Total']].values.flatten() subject_labels = ['SUB 1', 'SUB 2',
'SUB 3', 'Total']

# Plotting the pie chart fig, ax = plt.subplots() ax.pie(subject_marks, labels=subject_labels, autopct='%1.1f%%',


startangle=90, colors=['lightblue', 'lightgreen', 'lightcoral', 'lightsal ax.axis('equal') # Equal aspect ratio ensures that
pie is drawn as a circle.

# Adding title plt.title(f'Marks Distribution


for {student_name}')

# Display the plot


plt.show()

https://colab.research.google.com/drive/1XXnJqjEYBzg03yoTRUyHPPq9BkC2JTUQ#scrollTo=9tHj6NLy-M3M&printMode=true 8/10
1/19/24, 2:39 PM Untitled8.ipynb - Colaboratory

import matplotlib.pyplot as plt


import pandas as pd

# Your data
data = {
'Roll No': [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
'NAME': ['Deep', 'Jayesh', 'Yash', 'Sara', 'Gita', 'Jinal', 'Kavita', 'Minal', 'Naresh', 'Rima'],
'SUB 1': [30, 40, 45, 48, 35, 32, 36, 23, 43, 37],
'SUB 2': [30, 40, 36, 32, 32, 31, 28, 25, 27, 44],
'SUB 3': [30, 40, 47, 50, 43, 37, 38, 40, 50, 46],
'Total': [90, 120, 128, 130, 110, 100, 102, 88, 120, 127]
}

# Creating a DataFrame
df =
pd.DataFrame(data)

# Plotting the bar graph for total marks

fig, ax = plt.subplots(figsize=(10, 6))

ax.bar(df['NAME'], df['Total'],

color='skyblue')

# Adding labels and title


ax.set_xlabel('Students')
ax.set_ylabel('Total Marks') ax set
title('Total Marks for Each Student')
ax.set_title( Total Marks for Each
Student )

# Rotate x-axis labels for better readability


plt.xticks(rotation=45, ha='right')

# Display the plot


plt.tight_layout()
plt.show()

https://colab.research.google.com/drive/1XXnJqjEYBzg03yoTRUyHPPq9BkC2JTUQ#scrollTo=9tHj6NLy-M3M&printMode=true 9/10
1/19/24, 2:39 PM Untitled8.ipynb - Colaboratory

Conclusion :- Hence, I have successfully learn python


programming and various libraries used for machine learning.

https://colab.research.google.com/drive/1XXnJqjEYBzg03yoTRUyHPPq9BkC2JTUQ#scrollTo=9tHj6NLy-M3M&printMode=true 10/10

You might also like