[Go to site: main page, start]

0% found this document useful (0 votes)
18 views33 pages

Unit 1 ML Python Programs

The document outlines a Machine Learning course at B.M.S College of Engineering, detailing the curriculum, including Python programming, data types, data structures, and object-oriented programming principles. It includes practical experiments on Python modules, probability and statistics, and data manipulation using various formats like CSV, HTML, XML, and JSON. The course emphasizes self-learning and practical application of machine learning concepts and Python programming.

Uploaded by

Shrinidhi Achar
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)
18 views33 pages

Unit 1 ML Python Programs

The document outlines a Machine Learning course at B.M.S College of Engineering, detailing the curriculum, including Python programming, data types, data structures, and object-oriented programming principles. It includes practical experiments on Python modules, probability and statistics, and data manipulation using various formats like CSV, HTML, XML, and JSON. The course emphasizes self-learning and practical application of machine learning concepts and Python programming.

Uploaded by

Shrinidhi Achar
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

Dr.

Latha H N
Dept of E & C
B.M.S College of Engineering
Bangalore

Course Machine Learning


Title:
Course 23EC6PE1ML
Code:
Lecture hours Practical Self-learning Total hours
AAT

39 hrs - (44+4+3 ) hrs 90hrs


SELF LEARNING AAT 1 : 44 hrs
CIE : 4 hrs
SEE : 3 hrs
Total: 51 hrs

Unit -1: Python for ML


Python for ML: Data types, Understanding and creation of: list, tuple, dictionary

Writing functions, conditional and looping statements

Python libraries for ML: Data Preparation using Numpy and Pandas functions

Numpy and Pandas exercise

parsing and importing data from a text file, data visualization with Matplotlib.

Introduction: Machine Learning, Why use ML, Types of ML systems.

Supervised, Unsupervised learning

Semi-supervised and Reinforcement learning,

Challenges of ML, Problems ML can solve, Classification and Regression Overview.


Experiment 1

AIM: Introduction to Python, Features of Python, Coding guidelines

Introduction to Python:
Python is a high-level, interpreted programming language known for its simplicity and
readability. It was created by Guido van Rossum and first released in 1991. Python emphasizes
code readability and has a clean syntax that allows programmers to express concepts in fewer
lines of code compared to other programming languages.

Python is widely used for web development, scientific computing, data analysis, artificial
intelligence, machine learning, and automation. It supports multiple programming paradigms,
including procedural, object-oriented, and functional programming.

Features of Python:
a) Easy to Learn and Read: Python has a clear and concise syntax that makes it easy to learn and
read. It emphasizes code readability, which reduces the cost of program maintenance.

b) Expressive Language: Python allows developers to express concepts in fewer lines of code
compared to other languages. It provides built-in data structures, high-level dynamic typing, and
support for dynamic memory management.

c) Cross-platform Compatibility: Python is available for various operating systems like


Windows, macOS, and Linux. This cross-platform compatibility allows developers to write code
once and run it on different platforms without any major modifications.

d) Extensive Libraries and Frameworks: Python has a rich ecosystem of libraries and
frameworks that provide ready-to-use functionalities for various tasks. For example, NumPy for
numerical computations, pandas for data analysis, Django for web development, and TensorFlow
for machine learning.

e) Integration Capabilities: Python can easily integrate with other programming languages like C,
C++, and Java. This allows developers to leverage existing codebases and libraries in their
Python projects.

f) Large Community and Support: Python has a large and active community of developers who
contribute to its growth and development. There are numerous online resources, forums, and
communities where developers can seek help and share knowledge

1
Coding Guidelines:
Python follows a set of coding guidelines known as "PEP 8" (Python Enhancement Proposal 8).
These guidelines promote code consistency and readability, making it easier for developers to
understand and maintain Python code. Here are some key points from PEP 8:

a) Indentation: Use four spaces for indentation. Avoid using tabs or a mix of spaces and tabs.

b) Line Length: Limit lines to a maximum of 79 characters. If a line exceeds this limit, it should
be broken into multiple lines using parentheses or backslashes.

c) Naming Conventions: Use lowercase letters and underscores for variable and function names
(e.g., my_variable, calculate_total). Use CamelCase for class names (e.g., MyClass).

d) Comments: Use comments to explain the code's logic and provide additional information.
Comments should be clear, concise, and placed on a separate line when necessary.

e) Blank Lines: Use blank lines to separate logical sections of code and improve readability.

f) Imports: Place imports at the top of the file, each on a separate line. Group imports by standard
library imports, third-party library imports, and local imports, leaving a blank line between each
group.

g) Function and Method Definitions: Use docstrings to provide documentation for functions and
methods. Docstrings should be enclosed in triple quotes and describe the function's purpose,
parameters, and return value.
.

2
Experiment 2

AIM: Discussion on IPython Shell, Coding Practice using Python Data


Types and Data Structures

IPython:

IPython (Interactive Python) is a command shell for interactive computing in multiple


programming languages, originally developed for the Python programming language, that offers
introspection, rich media, shell syntax, tab completion. IPython is based on an architecture that
provides parallel and distributed computing. IPython enables parallel applications to be developed,
executed, debugged and monitored interactively. This architecture abstracts out parallelism,
enabling IPython to support many different styles of parallelism.

Python Data Types

Data types are the classification or categorization of data items. It represents the kind of value that
tells what operations can be performed on a particular [Link] following are the standard or built-
in data types in Python:
· Numeric
· String
· Boolean

Numeric data type:

The numeric data type in Python represents the data that has a numeric value. A numeric value can
be an integer, a floating number, or even a complex number.
· Integers –It contains positive or negative whole numbers (without fractions or decimals).
In Python, there is no limit to how long an integer value can be.
· Float –It is a real number with a floating-point representation. It is specified by a decimal
point.
· Complex Numbers –It is specified as (real part) + (imaginary part)j. For example – 2+3j
type() function is used to determine the type of data type.
Example:
a=70
b = 2.75
c =1+5j
print(type(a))
print(type(b))
print(type(c))

3
Output:

<type ‘int’>

<type ‘float’>

<type ‘complex’>

String: Strings in Python are arrays of bytes representing Unicode characters. A string is a
collection of one or more characters put in a single quote, double-quote, or triple-quote. In python
there is no character data type, a character is a string of length one.
Example:
str="Electronic"
print(type(str))

Output:
<type ‘str’>

Boolean:
Boolean type provides two built-in values, True and False. These values are used to determine the
given statement true or false. It denotes by the class bool. True can be represented by any non-zero
value or 'T' whereas false can be represented by the 0 or 'F'.
Example:

print(type(True))

print(type(False))

print(type(true))

Output:
<class 'bool'>
<class 'bool'>

Python Data Structures

Data Structures are fundamentals of any programming language around which a program is built.
Data Structures are a way of organizing data so that it can be accessed more efficiently depending
upon the situation.

Data structures are:

4
· List

· Tuple

· Dictionary

· Set

List: A list is defined as an ordered collection of items, and it is one of the essential data structures
when using Python. Lists are mutable, Lists are defined by using parentheses to enclose the
elements, which are separated by commas.

Example:

List = [1, 2, 3, "GFG", 2.3]

print(List)

Output:
[1, 2, 3, 'GFG', 2.3]

Tuple: A tuple is a built-in data structure in Python that is an ordered collection of objects. Unlike
lists, tuples come with limited functionality. Tuples are immutable. Tuples cannot be modified,
added, or deleted once they’ve been created. The use of parentheses in creating tuples is optional,
but they are used to distinguish between the start and end of the tuple.

Example:

Tuple = ('Pink', 'Blue')

print(Tuple)

Output:
('Pink', 'Blue')

Dictionary: A dictionary is a collection of {key: value} pairs. Keys map values, and to access
the values, in the places of indexes, we need to use the keys. It is an unordered collection of data.

Example:

emptydict = {}

dict1 = {1: 'A', 2: 'B', 3: 'C'}

5
print(dict1)

Output:

dict1: {1: 'A', 2: 'B', 3: 'C'}

Set: Set is an unordered collection of data that is mutable and does not allow any duplicate
element. Sets are basically used to include membership testing and eliminating duplicate entries.

Example:

a= {10, 20, 30, 30, 40, 20}

b= {50, 60, 10}

x=a | b

Output :

{10, 20, 30, 40, 50, 60}

6
Experiment 3

AIM: Python Programming Modules

In Python, a module is a file containing Python definitions and statements. The file name is the
module name with the suffix .py appended. Modules in Python are simply Python files with a .py
extension that contain functions, classes, and other Python code that can be imported into your
own Python scripts.
Python programming modules provide a way to organize code and make it more reusable. They
allow you to break up your code into smaller, more manageable chunks, which makes it easier to
maintain and update your code over time.
There are several built-in modules in Python that provide functionality for specific tasks, such as:
● math: provides mathematical functions for complex numbers, trigonometry, logarithms,
etc.
● os: provides a way to interact with the operating system, such as accessing files and
directories, environment variables, etc.
● datetime: provides classes for working with dates and times.
● random: provides functions for generating random numbers and selecting random items
from lists.
● re: provides regular expression matching operations.
In addition to these built-in modules, there are also many third-party modules available that can
be installed using package managers like pip. These modules provide additional functionality for
tasks such as web development, data analysis, scientific computing, and more.

7
Experiment 4

AIM: OOPS Concept Object, Inheritance, Class

An object-oriented paradigm is to design the program using classes and objects. The object is
related to real-word entities such as book, house, pencil, etc. The oops concept focuses on writing
the reusable code. It is a widespread technique to solve the problem by creating objects.

Major principles of object-oriented programming system are given below.

Class:

The class can be defined as a collection of objects. It is a logical entity that has some specific
attributes and methods. For example: if you have an employee class, then it should contain an
attribute and method, i.e. an email id, name, age, salary, etc.

Syntax

class ClassName:

<statement-1>

<statement-N>

Object:

The object is an entity that has state and behavior. It may be any real-world object like the mouse,
keyboard, chair, table, pen, etc.

Everything in Python is an object, and almost everything has attributes and methods. All functions
have a built-in attribute doc , which returns the docstring defined in the function source code.

8
When we define a class, it needs to create an object to allocate the memory. Consider the following
example.

Example:

class car:

def init (self,modelname, year):

[Link] = modelname

[Link] = year

def display(self):

print([Link],[Link])

c1 = car("Toyota", 2016)

[Link]()

Output:

Toyota 2016

In the above example, we have created the class named car, and it has two attributes modelname
and year. We have created a c1 object to access the class attribute. The c1 object will allocate
memory for these values. We will learn more about class and object in the next tutorial.

Inheritance :

Inheritance is the most important aspect of object-oriented programming, which simulates the real-
world concept of inheritance. It specifies that the child object acquires all the properties and
behaviors of the parent object.

By using inheritance, we can create a class which uses all the properties and behavior of another
class. The new class is known as a derived class or child class, and the one whose properties are
acquired is known as a base class or parent class.

It provides the re-usability of the code.

9
Experiment 5

AIM: Python Programming for Probability and Statistics

The NumPy library contains multidimensional array and matrix data structures (you’ll find more
information about this in later sections). It provides ndarray, a homogeneous n-dimensional array
object, with methods to efficiently operate on it. NumPy can be used to perform a wide variety of
mathematical operations on arrays. It adds powerful data structures to Python that guarantee
efficient calculations with arrays and matrices and it supplies an enormous library of high-level
mathematical functions that operate on these arrays and matrices.

Code:
import numpy
from [Link] import mode

arr1 = [Link]([10,20,30,40,50,60,20,20])
print("SUM =",[Link]())
print("MEAN =",[Link]())
print("MEDIAN =",[Link](arr1))
print("MODE = ",mode(arr1))

Output:

SUM = 250
MEAN = 31.25
MEDIAN = 25.0
MODE = ModeResult(mode=array([20]), count=array([3]))

10
Experiment 6

AIM: Python Programming for Manipulating Structured Data


– CSV, HTML, XML, JSON

CSV: A CSV file (Comma Separated Values file) is a type of plain text file that uses specific
structuring to arrange tabular data. Normally, CSV files use a comma to separate each specific
data value.
HTML: HTML (Hyper Text Markup Language) is the code that is used to structure a web page
and its content.
XML: Extensible Markup Language (XML) is a markup language that provides rules to define any
data. XML cannot perform computing operations by itself. Instead, any programming language or
software can be implemented for structured data management
JSON: JSON stands for JavaScript Object Notation. JSON is a lightweight format for storing and
transporting data. JSON is often used when data is sent from a serverto a web page

CSV
import csv # core lib we are loading
f1 = open("c:\\users\\admin\\desktop\\[Link]", "r")
csvfile = [Link](f1)
header = next(csvfile)
print(header)
for elem in csvfile:
print(elem)
[Link]()

Output:
['empid', 'name', 'dept', 'salary', 'yearsofexp']
['1001', 'hari', 'sales', '25000', '5']
['1002', 'mani', 'accts', '26000', '']
['1003', 'guru', 'purch', '35000', '']
['1004', 'muragesh', 'sales', '26000', '']
['1005', 'mahesh', 'purch', '24000', '']
['1006', 'rajesh', 'purch', '', '']
['1007', 'amar', 'sales', '15000', '']
['1008', 'lokesh', 'purch', '', '3']
['1009', 'kunal', 'accts', '', '']
['1010', 'pavan', 'accts', '12000', '']

11
['1011', 'arun', 'purch', '', '']
['1012', 'umesh', 'purch', '', '']
['1013', 'suresh', 'sales', '15000', '']
['1014', 'somesh', 'sales', '18000', '']
['1015', 'basava', 'sales', '19000', '']
['1016', 'rajan', 'purch', '20000', '']
['1017', 'john', 'sales', '210000', '']
['1018', 'manju', 'sales', '', '']
['1019', 'peter', 'purch', '', '2']
['1020', 'mamatha', 'accts', '', '']

HTML
!pip install bs4
from bs4 import BeautifulSoup # load the library
f1 = open("c:\\users\\admin\\desktop\\[Link]", "r")
soup = BeautifulSoup([Link](), "[Link]")
print([Link].h1)
[Link]()

Output:
<h1> HELLO WORLD </h1>

from bs4 import BeautifulSoup # load the library


f1 = open("c:\\users\\admin\\desktop\\[Link]", "r")
soup = BeautifulSoup([Link](), "[Link]")
#print(soup)
for elem in soup.find_all("h1"):
print(elem)
[Link]()

Output:
<h1> HELLO WORLD </h1>
<h1> Bengaluru </h1>
<h1> BMSCE </h1>
<h1> Basavangudi </h1>

12
XML
from bs4 import BeautifulSoup # load the library
f1 = open("c:\\users\\admin\\desktop\\[Link]", "r")
soup = BeautifulSoup([Link](), "lxml")
#print(soup)
for elem in soup.find_all("names"):
print([Link])
[Link]()

Output:
arun
hari

JSON
import json
f1=open("[Link]","r")
res = [Link](f1) # converted json into a python dict
print("User = ",res["user"])
print("pwd =",res["pwd"])
print("port =",res["port"])
print("OS1 =",res["os"][0])
print("OS2 =",res["os"][1])
print("OS3 =",res["os"][2])
[Link]()

Output:
User = root
pwd = root@123
port = 22
OS1 = win
OS2 = linux
OS3 = mac

13
Experiment 7

AIM: Python Programming for Web Scraping Using Requests, Bs4

Python web scraping is an automated method used for collecting large amounts of data from
websites and storing it in a structured [Link] begin the web scraping process,firstly we have to
load the URLs into a web scraping tool,such as [Link] tool will then crawl and extract data
from the URL.

Code:
import requests #repo library
from bs4 import BeautifulSoup
url="[Link]

resp =[Link](url)
print(resp)
print(resp.status_code)
soup = BeautifulSoup([Link], "[Link]")
#print(soup)
for elem in soup.find_all("h1"):
print(elem)

Output:
<Response [200]>
200

14
Experiment 8

AIM: Linear Algebra with Numpy and Scipy

Linear algebra:
The NumPy linear algebra functions rely on BLAS and LAPACK to provide efficient low level
implementations of standard linear algebra algorithms. Those libraries may be provided by NumPy
itself using C versions of a subset of their reference implementations but, when possible, highly
optimized libraries that take advantage of specialized processor functionality are preferred.
Examples of such libraries are OpenBLAS, MKL (TM), and ATLAS. Because those libraries are
multithreaded and processor dependent, environmental variables and external packages such as
threadpoolctl may be needed to control the number of threads or specify the processor architecture.

Scipy:
SciPy is a scientific computation library that uses NumPy underneath. SciPy stands for Scientific
Python. It provides more utility functions for optimization, stats and signal processing. Like
NumPy, SciPy is open source so we can use it freely. SciPy was created by NumPy's creator Travis
Olliphant. If SciPy uses NumPy underneath, why can we not just use NumPy? SciPy has optimized
and added functions that are frequently used in NumPy and Data Science. SciPy is predominantly
written in Python, but a few segments are written in C.

Code:

import numpy as np
arr1 = [Link]([10,20,30,40,50])
arr2 = [Link]([1,2,3,4,5])
res = arr1 + arr2
print(‘res: ’, res)

import numpy as np
mat1 = [Link]([[1,2,3],[4,5,6],[7,8,9]])
mat2 = [Link]([[1,2,3],[4,5,6],[7,8,9]])
res = mat1 + mat2
print(‘res1: ’, res)
print(‘res2: ’, [Link]())

15
Output:

res: [11 22 33 44 55]

res1: [[ 2 4 6] [ 8 10 12] [14 16 18]]

res2: [ 2 10 18]

16
Experiment 9

AIM: Data Manipulation with Pandas

In Machine Learning, the model requires a dataset to operate, i.e. to train and test. But data
doesn’t come fully prepared and ready to use. There are discrepancies like “Nan”/ “Null” / “NA”
values in many rows and columns. Sometimes the data set also contains some of the row and
columns which are not even required in the operation of our model. In such conditions, it
requires proper cleaning and modification of the data set to make it an efficient input for our
model. We achieve that by practicing “Data Wrangling” before giving data input to the model.

Code:
import pandas as pd #importing the pandas library
#creating the new dataset
namelst = ["ravi","john","hari","elan", "manu"]
langlst = ["cpp", "java", "cpp", "java", "java"]
loclst = ["blr","mum","blr","blr","del"]

df = [Link]({"name" : namelst, "lang":langlst, "loc" : loclst})

#printing the names column from data set


res = [Link]
print(df["name"])
print(res)

Output:

17
#printing the first row of the dataset
print( [Link][0])
print([Link][0]["name"]) #printing the name of 0th index of dataset

Output:

#creating the new value column with present dataset


df["newcolumn"] = df["name"].[Link]()

#fetching row which has the loc “blr”


df[df["loc"]=="blr"]

Output:

#creating the new columns called val1 and val2


df["val1"] = [1,2,3,4,5]
df["val2"] = [1,2,3,4,5]

#adding the column and storing the sum in new column


df["total"] = df["val1"] + df["val2"]
df

18
Output:

#importing the [Link] from desktop and storing it in the filepath variable
filepath = r"C:\Users\admin\Desktop\[Link]"
df1 = pd.read_csv(filepath)
[Link]()
df1["sales"] = df1["sales"].replace("50a", "50") #replacing the value of 50a to 50 in sales column
df1["sales"].fillna("0",inplace=True) #replacing the null values with 0
df1["sales"] = df1["sales"].astype("int32") #changing the datatype of sales to int32
[Link]()
df1["sales"].sum()

19
Experiment 10

AIM: Data Visualization Using Matplotlib and Seaborn

Code:
[Link]: Barchart
import numpy as np
import [Link] as plt

data = [23,85, 72, 43, 52]


labels = ['A', 'B', 'C', 'D', 'E']

[Link](range(len(data)), labels)
[Link]('Sections')
[Link]('Fees')

[Link]('School ')

[Link](range(len(data)), data)
[Link]()

Output:

20
[Link]: Pie Chart
import [Link] as plt

# Define Data Coordinates


cost = [10, 15, 15, 15, 20, 25]

# Define Label
work = ['Timber', 'Supervision', 'Steel', 'Bricks', 'Cement',
'Labour']

# Plot with autopct


[Link](cost, labels=work, autopct='%.1f%%')

# Add legend
[Link](labels=work, fontsize=8, loc='upper center',
bbox_to_anchor=(0.5, -0.04), ncol=3)
Output:

1. Seaborn: Heatmap
import pandas as pd
Import seaborn as sns
age = [10,20,30,40,50,60,70,80]
wt = [25,40,42,45,50,51,52,53]
ht = [4,5,5,5.1,5.5,5.5,5.5,5.5]
code = [1,2,3,4,5,6,7,8]

21
df = [Link]({"age": age,
"wt" : wt,
"ht" : ht,
"code": code})

[Link]([Link]())

Output:

2. Seaborn: Boxplot
import numpy as np
age = [-150,50,10,80,40,60,100,30,20,70,90,250]

arr = [Link](age)

print("Mean ",[Link]())
print("Median ",[Link](arr))

df = [Link]({"age": age})
[Link](age)

22
Output:
Mean 54.166666666666664
Median 55.0

23
Experiment 11

AIM: Data Pre-processing with Scikit Learn

Data pre-processing

Data pre-processing is a process of preparing the raw data and making it suitable for a machine
learning model. It is the first and crucial step while creating a machine learning model.

Data pre-processing helps to impute the null values or missing values. Helps to replace or delete
certain values. New columns can be derived from the previous existing columns say if you have 2
columns consisting of weight and height of a person, then we can calculate BMI of the person.
One of most important part of data pre-processing is having a categorical column i.e converting
into numerical data. Data pre-processing also includes class balancing [0/1] and data normalization
which is required when there is different percentage of positive data and negative data, hence data
with more weightage will influence the output.
Data pre-processing is done with Scikit Learn.

Code:

import pandas as pd

filepath = r"c:\Users\admin\Desktop\[Link]"

df = pd.read_csv(filepath)

df
#to know the info of the data frame
[Link]()

#to find the number of missing values


[Link]().sum()

#to drop the "yearsofexp" column because 85% values are missing
[Link]("yearsofexp",axis=1,inplace=True)

#to replace missing salary with median salary

df["salary"].fillna(df["salary"].median(),inplace = True)

#converting categorical value to numerical value

24
newdf = pd.get_dummies(df["dept"])

Newdf

#to drop department column of previous data

[Link]("dept",axis =1, inplace =True)

#to join the previous dataframe with exsisting data


res = [Link](newdf)
res

Output:

25
Experiment 12

AIM: Model Building with Scikit Learn for Supervised Learning.

Supervised Learning:
Supervised learning, also known as supervised machine learning, is a subcategory of machine
learning and artificial intelligence. It is defined by its use of labeled datasets to train algorithms
that to classify data or predict outcomes accurately. As input data is fed into the model, it adjusts
its weights until the model has been fitted appropriately, which occurs as part of the cross
validation process. Supervised learning helps organizations solve a variety of real-world problems
at scale, such as classifying spam in a separate folder from your inbox.

Code:
import numpy as np
import [Link] as plt
import pandas as pd
import seaborn as sns

filename = r"C:\Users\akash\Desktop\DeskTop\Task\[Link]"
labels = r"C:\Users\akash\Desktop\DeskTop\Task\auto_lables.txt"
data = pd.read_csv(filename, header = None)
lab = pd.read_csv(labels, header=None, delimiter=" ")

[Link] = lab[1]

[Link]()

[Link]("?", [Link], inplace=True)


[Link]().sum()
[Link](inplace=True, axis=0)
[Link]().sum()

[Link]()

data["price"] = data["price"].astype(np.int64)
[Link]()

26
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

xvalues = [Link](data["length"]).reshape(-1,1)
yvalues = [Link](data["price"]).reshape(-1,1)

x_train,x_test,y_train,y_test = train_test_split(xvalues, yvalues, test_size = .20)


LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)

reg = LinearRegression()
[Link](x_train,y_train)

y_pred = [Link](x_test)

[Link](x_test, y_test, color = "b")


[Link](x_test, y_pred, color="r")
[Link]()

Output:

from [Link] import r2_score

r2 = r2_score(y_test, y_pred)

print(r2)

Output:

0.6560977003252608
27
Experiment 13
AIM: Model Building with Scikit Learn for Unsupervised
Learning
Unsupervised Learning:
Unsupervised machine learning is a type of machine learning where the algorithm learns patterns
and relationships in data without being explicitly told what to look for. Unlike supervised learning,
where labeled examples are provided to the algorithm, unsupervised learning algorithms work with
unlabeled data.
In unsupervised learning, the goal is to identify patterns or structure within the data that can
provide insights or help with prediction tasks. This can involve clustering similar data points
together, dimensionality reduction to visualize high-dimensional data, or generative modeling to
create new data samples that are similar to the input data.

Code:

import numpy as np
import [Link] as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import KNeighborsClassifier
from [Link] import classification_report, confusion_matrix

url = "[Link]

names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']

dataset = pd.read_csv(url, names=names)


X = [Link][:, :-1].values
y = [Link][:, 4].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)

scaler = StandardScaler()
[Link](X_train)

X_train = [Link](X_train)

28
X_test = [Link](X_test)

classifier = KNeighborsClassifier(n_neighbors=5)
[Link](X_train, y_train)

y_pred = [Link](X_test)

print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))

error = []

for i in range(1, 40):


knn = KNeighborsClassifier(n_neighbors=i)
[Link](X_train, y_train)
pred_i = [Link](X_test)
[Link]([Link](pred_i != y_test))

[Link](figsize=(12, 6))
[Link](range(1, 40), error, color='red', linestyle='dashed', marker='o',
markerfacecolor='blue', markersize=10)
[Link]('Error Rate K Value')
[Link]('K Value')
[Link]('Mean Error')
[Link]()

Output:

29
30
Experiment -14

AIM: Model Evaluation with Scikit Learn

Scikit-learn is one of the most popular Python libraries for Machine Learning. It provides
models, datasets, and other useful functions. In this article, I will describe the most popular
techniques provided by scikit-learn for Model Evaluation.

Model Evaluation permits us to evaluate the performance of a model, and compare different
models, to choose the best one to send into production. There are different techniques for Model
Evaluation, which depend on the specific task we want to solve. In this article, we focus on the
following tasks:

● Regression
● Classification

To evaluate a regression model, the most popular metrics are:


● Mean Absolute Error — the average of the difference between the actual value and
the predicted one. It measures how far the predictions are from the actual output.
The lower the MAE, the better the model.
● Root Mean Squared Error — the square root of Mean Squared Error (MSE). MSE
calculates the average of the square of the difference between the actual values and
the predicted ones.
● R2 score — the proportion of variance in Y that can be explained by X.

Code:

from pandas import DataFrame


import [Link] as plt
from [Link] import KMeans

Data = {'x':
[25,34,22,27,33,33,31,22,35,34,67,54,57,43,50,57,59,52,65,47,49,48,35,33,44,45,38,43,51,46],

'y’: [79,51,53,78,59,74,73,57,69,75,51,32,40,47,53,36,35,58,59,50,25,20,14,12,20,5,29,27,8,7]
}

31
df = DataFrame(Data,columns=['x','y'])

kmeans = KMeans(n_clusters=5).fit(df)
centroids = kmeans.cluster_centers_
print(centroids)

[Link](df['x'], df['y'], c= kmeans.labels_.astype(float), s=50, alpha=0.5)


[Link](centroids[:, 0], centroids[:, 1], c='red', s=50)
[Link]()

Output:

[[42.55555556 15.77777778]
[54. 53. ]
[30.83333333 74.66666667]
[55.2 33.6 ]
[27.75 55. ]]

32

You might also like