[go: up one dir, main page]

0% found this document useful (0 votes)
57 views41 pages

Intern Saro

It's a report for machine learning

Uploaded by

jayaprakash.mnm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views41 pages

Intern Saro

It's a report for machine learning

Uploaded by

jayaprakash.mnm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

GOVERNMENT COLLEGE OF ENGINEERING, BODINAYAKANUR

THENI-625 582
Affliated to Anna University,Chennai

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

TVS TRAINING AND SERVICE LIMITED


INTERNSHIP REPORT
Topic: MACHINE LEARNING USING PYTHON

Submitted by

Name: SARAVANAN S J

Register number:923321104043

1
ACKNOWLEDGEMENT

The Internship opportunity I had with “MACHINE LEARNING USING


PYTHON” was a wonderful experience for lessons of life and professional
development. Therefore I consider myself as a very obliged individual to be the
part of TVS Training And Service. I am thankful also for having a chance to meet
so many people and professionals who led me through this internship period.

We would like to express our deep sense of gratitude to our respected


Principal Dr.C.Vasanthanayaki., M.E., Ph.D., who has bestowed the kind grace
and affection of us in accomplishing this internship.

Sincere thanks to Dr. C. RANI, M.E., Ph.D., Head of the Department of


Computer Science and Engineering on providing facilities to attend the internship
in TVS Training and Service, Chennai and utilize all the facilities in this college.

We would like to thank our faculty adviser Mrs.R.VIMALA M.E., Assistant


Professor , Department of Computer Science and Engineering, who guided us
throughout the entire phase of this internship.

It is her motivation and guidance which made us explore this internship.

It is our opportunity to express gratitude and sincere thanks to our guide


Dr.B.Sivaranjani M.Tech., Ph.D., Assistant Professor , Department of Computer
Science and Engineering who gave us the motivation and guided us to do this
internship.

I express my deepest thanks to the TVS Training and Service members for
taking part in useful decision and giving necessary advices, guidance and arranged
all facilities to make things easier.

I choose this moment to acknowledge your contribution gratefully.

I perceive this opportunity as a big milestone in my professional


development. I will strive to use gained skills and knowledge in the best possible
way, and I will continue to work on their improvement, in order to attain the gist of
our profession.

2
CONTENTS

Acknowledgement

Chapter 1: Company Profile

Chapter 2: Basic Python Concepts


2.1: Introduction to Python
2.2: Data types
2.3: Control Flow, Function , Strings
2.4: List, Tuples , Dictionaries
2.5: OOPS concepts

Chapter 3: Machine Learning


3.1: Python Libraries (Numpy, Pandas, Matplotlib etc…)
3.2: Supervised Learning Algorithm
3.3: UnSupervised Learning Algorithm

Chapter 4: Final Project

Chapter 5: Conclusion

Chapter 6: Gallery

3
CHAPTER 1
COMPANY PROFILE

TVS Training and Services Ltd is a TVS group company that was
established in 2010, in Chennai. TVS TS offers training in various domains from
automotive and engineering to finance and retail. We train and facilitate
employment to unemployed youth across the country.
TVS TS offers more than 250 courses in various domains. Our courses are
generally designed at 3 levels viz. Basic, Intermediary and Advanced, to suit
different levels of audience such as entry, supervisory and managerial. The short
term courses range from 1 to 5 days of duration, while the longer duration
programs range from 6 to 90 days based on the customer needs.

VISION
 To be an exemplary training organization that is committed to developing
skills and competencies in individuals to meet global standards of excellence
across multiple domains.
 To impart multi-skill training to unemployed youth across India that will
open doors to a wide range of employment opportunities and ensure
professional advancement.

MISSION
 To provide high quality training in knowledge, skills and social
competencies across domains in a wide range of processes and systems to
diverse clients/ individuals and businesses.
 To develop custom training programs designed and continually improved, to
meet each client’s unique requirements and solve specific problems.

4
TVS TS offer various services through its 6 business divisions.
 B2B |Business to Business.
 B2E |Business to Education.
 B2G |Business to Government
 BFSI |Business to Banking, Financial Services & Insurance Companies.
 STAFFING |Contracted employment on TVS TS rolls.
 NAPS |National Apprenticeship Promotion Scheme.

Training Centers
TVS TS is one of the largest training partner of,
 NSDC (National Skill Development Corporation) &
 TNSDC (Tamil Nadu Skill Development Corporation)
NSDC & TNSDC creates opportunity to train students from rural areas and
facilitate with employment opportunity through TVS TS

5
CHAPTER 2

BASIC PYTHON CONCEPTS

2.1: INTRODUCTION TO PYTHON

Python:
Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.

It is used for:

 web development (server-side),


 software development,
 mathematics,
 system scripting.

Uses of Python:

 Python can be used on a server to create web applications.


 Python can be used alongside software to create workflows.
 Python can connect to database systems. It can also read and modify files.
 Python can be used to handle big data and perform complex mathematics.
 Python can be used for rapid prototyping, or for production-ready software
development.

Python Indentation

 Indentation refers to the spaces at the beginning of a code line.


 Where in other programming languages the indentation in code is for
readability only, the indentation in Python is very important.
 Python uses indentation to indicate a block of code.

6
Example:

if 5 > 2:

print("Five is greater than two!")

Python Comments
 Comments can be used to explain Python code.

 Comments can be used to make the code more readable.


 Comments can be used to prevent execution when testing code.

Creating a Comment:

Comments starts with a #, and Python will ignore them:

Example:

#This is a comment
print("Hello, World!")
Variables
Variables are containers for storing data values.
Creating Variables
 Python has no command for declaring a variable.
 A variable is created the moment you first assign a value to it.
Example:
x=5
y = "John"
print(x)
print(y)

7
2.2: DATA TYPES

Built-in Data Types


In programming, data type is an important concept.
Variables can store data of different types, and different types can do
different things.
Python has the following data types built-in by default, in these
categories:
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType

Getting the Data Type

You can get the data type of any object by using the type() function:
Example:
x=5
print(type(x))

Specify a Variable Type

There may be times when you want to specify a type on to a variable. This
can be done with casting. Python is an object-orientated language, and as such it
uses classes to define data types, including its primitive types.

Casting in python is therefore done using constructor functions:

8
 int() - constructs an integer number from an integer literal, a float literal (by
removing all decimals), or a string literal (providing the string represents a
whole number)
 float() - constructs a float number from an integer literal, a float literal or a
string literal (providing the string represents a float or an integer)
 str() - constructs a string from a wide variety of data types, including strings,
integer literals and float literals

Example:

x = int(1) # x will be 1

y = float("3") # z will be 3.0

z = str(2) # y will be '2'

2.3: CONTROL FLOW, FUNCTION , STRINGS

Control Flow
Python Conditions and If statements:
Python supports the usual logical conditions from mathematics:

 Equals: a == b
 Not Equals: a != b
 Less than: a < b
 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in "if statements"
and loops.

An "if statement" is written by using the if keyword.

Example:
a = 33
b = 200
if b > a:
print("b is greater than a")

9
In this example we use two variables, a and b, which are used as part of the if
statement to test whether b is greater than a. As a is 33, and b is 200, we know that
200 is greater than 33, and so we print to screen that "b is greater than a".
Elif:

The elif keyword is Python's way of saying "if the previous conditions were not
true, then try this condition".

Example:

a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Else:

The else keyword catches anything which isn't caught by the preceding conditions.

Example:

a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Nested If:

You can have if statements inside if statements, this is called nested if statements.

Example:
x = 41

if x > 10:
print("Above ten,")

10
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")

Python Loops

Python has two primitive loop commands:

 while loops
 for loops

The while Loop

With the while loop we can execute a set of statements as long as a condition
is true.
Example:
i=1
while i < 6:
print(i)
i += 1
The break Statement

With the break statement we can stop the loop even if the while condition is
true:
Example:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1

11
The continue Statement

With the continue statement we can stop the current iteration, and continue
with the next:

Example:

i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Python For Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and works
more like an iterator method as found in other object-orientated programming
languages.

With the for loop we can execute a set of statements, once for each item in a
list, tuple, set etc.
Example:
Print a fruits in a list
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Looping through the string
It will return a sequence of characters
for x in "banana":
print(x)

12
Functions

 A function is a block of code which only runs when it is called.


 You can pass data, known as parameters, into a function.
 A function can return data as a result.
Creating a Function

In Python a function is defined using the def keyword:

Example:
def my_function():
print("Hello from a function")
Calling a Function

To call a function, use the function name followed by parenthesis:

Example:
def my_function():
print("Hello from a function")

my_function()
Arguments

It can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses. You
can add as many arguments as you want, just separate them with a comma.

The following example has a function with one argument (fname). When the
function is called, we pass along a first name, which is used inside the function to
print the full name:

Example:
def my_function(fname):
print(fname + " Refsnes")

my_function("Emil")

13
my_function("Tobias")
my_function("Linus")
Number of Arguments

By default, a function must be called with the correct number of arguments.


Meaning that if your function expects 2 arguments, you have to call the function
with 2 arguments, not more, and not less.

Example:

def my_function(fname, lname):


print(fname + " " + lname)

my_function("Emil", "Refsnes")

If you try to call the function with 1 or 3 arguments, you will get an error.
Keyword Arguments

You can also send arguments with the key = value syntax.

This way the order of the arguments does not matter.

Example:

def my_function(child3, child2, child1):


print("The youngest child is " + child3)

my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")


Default Parameter Value

The following example shows how to use a default parameter value.

If we call the function without argument, it uses the default value:

Example:

def my_function(country = "Norway"):

print("I am from " + country)

14
my_function("Sweden")
my_function("India")
my_function()

Passing a List as an Argument

You can send any data types of argument to a function (string, number, list,
dictionary etc.), and it will be treated as the same data type inside the function.

E.g. if you send a List as an argument, it will still be a List when it reaches
the function:

Example:

def my_function(food):
for x in food:
print(x)

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

my_function(fruits)
Return Values

To let a function return a value, use the return statement:

Example:

def my_function(x):
return 5 * x

print(my_function(3))

15
Strings

Strings in python are surrounded by either single quotation marks, or double


quotation marks.

'hello' is the same as "hello".

You can display a string literal with the print() function:

Example:

a = "Hello"
print(a)

Multiline Strings

You can assign a multiline string to a variable by using three quotes:

Example:

a = """Lorem ipsum dolor sit amet,


consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
Strings are Arrays

Like many other popular programming languages, strings in Python are


arrays of bytes representing unicode characters.

However, Python does not have a character data type, a single character is
simply a string with a length of 1.

Square brackets can be used to access elements of the string.

Example:
a = "Hello, World!"
print(a[1])

16
String Length

To get the length of a string, use the len() function.

Example:

a = "Hello, World!"
print(len(a))
Check String

To check if a certain phrase or character is present in a string, we can use the


keyword in.

Example:

txt = "The best things in life are free!"


print("free" in txt)

String Methods:

capitalize() Converts the first character to upper case

casefold() Converts string into lower case

center() Returns a centered string

count() Returns the number of times a specified value occurs in a


string

index() Searches the string for a specified value and returns the position
of where it was found

isalnum() Returns True if all characters in the string are alphanumeric

isalpha() Returns True if all characters in the string are in the alphabet

isascii() Returns True if all characters in the string are ascii characters

isdecimal() Returns True if all characters in the string are decimals

17
2.4: LIST, TUPLES , DICTIONARIES:

List:

Lists are used to store multiple items in a single variable.

Lists are one of 4 built-in data types in Python used to store collections of
data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and
usage.

Lists are created using square brackets:


Example:
#Create a list
thislist = ["apple", "banana", "cherry"]
print(thislist)

List Items

List items are ordered, changeable, and allow duplicate values.

List items are indexed, the first item has index [0], the second item has
index [1] etc.

Example:

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


print(thislist)

List Methods

Python has a set of built-in methods that you can use on lists.

Method Description

append() Adds an element at the end of the list

18
clear() Removes all the elements from the list

copy() Returns a copy of the list

count() Returns the number of elements with the specified value

index() Returns the index of the first element with the value

insert() Adds an element at the specified position

pop() Removes the element at the specified position

remove() Removes the item with the specified value

reverse() Reverses the order of the list

sort() Sorts the list

Tuple:

Tuples are used to store multiple items in a single variable.

Tuple is one of 4 built-in data types in Python used to store collections of


data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.

A tuple is a collection which is ordered and unchangeable.

Tuples are written with round brackets.

Example:

#Creating a tuple

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


print(thistuple)
Tuple Items

Tuple items are ordered, unchangeable, and allow duplicate values.

19
Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.

Example:

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


print(thistuple)

Tuple Methods

Python has two built-in methods that you can use on tuples.

Method Description

count() Returns the number of times a specified value occurs in a tuple

index() Searches the tuple for a specified value and returns the position
of where it was found

Set:

Sets are used to store multiple items in a single variable.

Set is one of 4 built-in data types in Python used to store collections of data,
the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.

A set is a collection which is unordered, unchangeable , and unindexed.

Note: Set items are unchangeable, but you can remove items and add new
items.

True and 1, False and 0 are considered as a same values

Sets are written with curly brackets.

Example:

20
thisset = {"apple", "banana", "cherry"}
print(thisset)
Set Items

Set items are unordered, unchangeable, and do not allow duplicate values.

Duplicate will be removed

Example:

thisset = {"apple", "banana", "cherry", "apple"}

print(thisset)

Set Methods

Python has a set of built-in methods that you can use on sets.

Method Description

add() Adds an element to the set

clear() Removes all the elements from the set

copy() Returns a copy of the set

difference()- Returns a set containing the difference between two or

discard() Remove the specified item

intersection()& Returns a set, that is the intersection of two other sets

isdisjoint() Returns whether two sets have a intersection or not

pop() Removes an element from the set

remove() Removes the specified element

union()| Return a set containing the union of sets

21
Dictionary

Dictionaries are used to store data values in key:value pairs.

A dictionary is a collection which is ordered*, changeable and do not allow


duplicates.

Dictionaries are written with curly brackets, and have keys and values:

Example:

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)

Dictionary Items

Dictionary items are ordered, changeable, and do not allow duplicates.

Dictionary items are presented in key:value pairs, and can be referred to by


using the key name.

Example:

#Duplicate values will overwrite existing values

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)

22
Dictionary Methods

Python has a set of built-in methods that you can use on dictionaries.

Method Description

clear() Removes all the elements from the dictionary

copy() Returns a copy of the dictionary

fromkeys() Returns a dictionary with the specified keys and value

get() Returns the value of the specified key

items() Returns a list containing a tuple for each key value pair

keys() Returns a list containing the dictionary's keys

pop() Removes the element with the specified key

popitem() Removes the last inserted key-value pair

2.5: OOPS CONCEPTS

Python Class

A class is a collection of objects. A class contains the blueprints or the


prototype from which the objects are being created. It is a logical entity that
contains some attributes and methods.

Class syntax

class ClassName:

# Statement-1

# Statement-N

23
Python Objects

In object oriented programming Python, The object is an entity that has a


state and behavior associated with it. It may be any real-world object like a mouse,
keyboard, chair, table, pen, etc. Integers, strings, floating-point numbers, even
arrays, and dictionaries, are all objects.

The Python __init__ Method

The __init__ method is similar to constructors in C++ and Java. It is run as


soon as an object of a class is instantiated. The method is useful to do any
initialization you want to do with your object. Now let us define a class and create
some objects using the self and __init__ method.

Example:

class Dog:

attr1 = "mammal"

def __init__(self, name):

self.name = name

Rodger = Dog("Rodger")

Tommy = Dog("Tommy")

print("Rodger is a {}".format(Rodger.__class__.attr1))

print("My name is {}".format(Rodger.name))

Output

Rodger is a mammal

My name is Rodger

24
CHAPTER 3

MACHINE LEARNING

Machine Learning:

Machine learning is a branch of artificial intelligence (AI) that focuses on


the development of algorithms that enable computers to learn from and make
predictions or decisions based on data.

Instead of being explicitly programmed for specific tasks, machine learning


systems improve their performance as they are exposed to more data.

3.1: PYTHON LIBRARIES

Numpy:

NumPy is a Python library used for working with arrays.

It also has functions for working in domain of linear algebra, fourier


transform, and matrices.

25
NumPy was created in 2005 by Travis Oliphant. It is an open source project
and you can use it freely.

NumPy stands for Numerical Python.

Creating an array using numpy

NumPy is used to work with arrays. The array object in NumPy is


called ndarray.

We can create a NumPy ndarray object by using the array() function.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

print(type(arr))

Pandas:

Pandas is a Python library used for working with data sets.

It has functions for analyzing, cleaning, exploring, and manipulating data.

The name "Pandas" has a reference to both "Panel Data", and "Python Data
Analysis" and was created by Wes McKinney in 2008.

Series:

A Pandas Series is like a column in a table.

It is a one-dimensional array holding data of any type.

Import pandas as pd

a = [1, 7, 2]

myvar = pd.Series(a)

26
print(myvar)

Dataframe:

A Pandas DataFrame is a 2 dimensional data structure, like a 2 dimensional


array, or a table with rows and columns.

import pandas as pd

data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

#load data into a DataFrame object:


df = pd.DataFrame(data)

print(df)
Read CSV Files

A simple way to store big data sets is to use CSV files (comma separated
files).

CSV files contains plain text and is a well know format that can be read by
everyone including Pandas.

In our examples we will be using a CSV file called 'data.csv'.

import pandas as pd

df = pd.read_csv('data.csv')

print(df.head())

Seaborn:
Seaborn is a powerful Python visualization library built on top of
Matplotlib.

27
It provides a high-level interface for drawing attractive and informative
statistical graphics.

Seaborn simplifies the process of creating complex visualizations, making it


easier to explore and understand data.

Example:

import matplotlib.pyplot as plt

import seaborn as sns

sns.distplot([0, 1, 2, 3, 4, 5])

plt.show()

3.2: SUPERVISED LEARNING ALGORITHM

Supervised learning is a type of machine learning where a model is trained


using labeled data. In this approach, the algorithm learns to map input data
(features) to the correct output (labels) based on examples provided during
training. The goal is to make accurate predictions on new, unseen data.

Key Concepts:
Labeled Data: The training dataset consists of input-output pairs, where each
input is associated with a correct output label. For example, in a housing price
prediction task, features might include the size and location of a house, and the
label would be its price.

Training Phase: During training, the model learns to identify patterns and
relationships between the features and the labels. The algorithm adjusts its
parameters to minimize the difference between its predictions and the actual labels.

Types of Supervised Learning Tasks:

Classification: Predicting a categorical label. For example, determining if an


email is "spam" or "not spam."

28
Regression: Predicting a continuous value. For instance, estimating the price
of a car based on its features.

Common Algorithms:

 Linear Regression: Used for predicting continuous outcomes.


 Logistic Regression: Used for binary classification tasks.
 Decision Trees: Used for both classification and regression tasks, making
decisions based on feature thresholds.
 Random Forests: An ensemble method that combines multiple decision trees
to improve accuracy.
 Support Vector Machines (SVM): Used for classification tasks, especially in
high-dimensional spaces.
 Neural Networks: Used for complex tasks like image and speech
recognition.
Evaluation: After training, the model is evaluated on a separate testing dataset to
assess its performance. Common evaluation metrics include accuracy, precision,
recall, F1 score, and mean squared error.

Applications:
Supervised learning is widely used in various applications, including:

 Email Filtering: Classifying emails as spam or not spam.


 Image Recognition: Identifying objects or faces in images.
 Medical Diagnosis: Predicting diseases based on patient data.
 Credit Scoring: Determining the likelihood of a borrower defaulting
on a loan.

3.3: UnSupervised Learning Algorithm

Unsupervised learning is a type of machine learning where models are


trained using data that does not have labeled outputs. Instead of predicting specific
outcomes, the goal is to uncover hidden patterns or intrinsic structures within the
data.

29
Key Concepts:

1. Unlabeled Data: The training dataset consists solely of input data without
corresponding output labels. The model must learn from the data itself.

2. Goal: The main objectives of unsupervised learning are to explore the data,
identify groupings or patterns, and reduce dimensionality.

3. Common Techniques:
o Clustering: Grouping similar data points together based on their
features. For example, customer segmentation in marketing.
 Algorithms: K-Means, Hierarchical Clustering, DBSCAN.
o Dimensionality Reduction: Reducing the number of features in the
dataset while preserving important information. This helps visualize
high-dimensional data and reduce noise.
 Algorithms: Principal Component Analysis (PCA), t-
Distributed Stochastic Neighbor Embedding (t-SNE),
Autoencoders.

4. Evaluation: Since there are no labels, evaluating the performance of


unsupervised learning models can be challenging. Metrics like silhouette
score or elbow method for clustering can help assess the quality of the
clusters.
Applications:

Unsupervised learning has numerous applications, including:

 Customer Segmentation: Identifying distinct groups of customers based on


purchasing behavior.
 Anomaly Detection: Detecting unusual data points, such as fraud detection
in financial transactions.
 Market Basket Analysis: Finding associations between products frequently
purchased together (e.g., "customers who bought bread also bought butter").
 Image Compression: Reducing the size of image files without losing
significant quality.

30
CHAPTER 4

FINAL PROJECT

Provide better treatment using Reinforcement Learning

Our project demonstrates a machine learning approach for providing better


treatment over diabetes.It implements reinforcement learning algorithms, Q-
Learning and SARSA, to optimize diabetes treatment decisions. It models a simple
environment with two states (Controlled and Uncontrolled) and four actions
(Insulin, Oral Medication, Diet Adjustment, Exercise). The Q-table, representing
the expected cumulative rewards for state-action pairs, is updated through episodes
based on the chosen algorithm. The reward structure reflects the effectiveness of
treatments. The program trains the agent to learn optimal treatment strategies and
suggests actions based on the learned Q-values. Users can select the algorithm,
view the Q-table, and get treatment recommendations.It results in provide the
better treatment for diabetes.

PROGRAM:

import numpy as np

# Define possible states (health status for diabetes)

states = ['Controlled', 'Uncontrolled']

# Define possible actions (treatment options for diabetes)

actions = ['Insulin', 'Oral Medication', 'Diet Adjustment', 'Exercise']

# Initialize Q-table with zeros

q_table = np.zeros((len(states), len(actions)))

31
print(q_table)

# Define hyperparameters

learning_rate = 0.1

discount_factor = 0.9

episodes = 1000

epsilon = 0.1 # Exploration rate

# Reward structure (specific to diabetes treatment)

rewards = {

('Controlled', 'Insulin'): 10,

('Controlled', 'Oral Medication'): 8,

('Controlled', 'Diet Adjustment'): 6,

('Controlled', 'Exercise'): 7,

('Uncontrolled', 'Insulin'): -5,

('Uncontrolled', 'Oral Medication'): -2,

('Uncontrolled', 'Diet Adjustment'): 0,

('Uncontrolled', 'Exercise'): 1

# Simulated environment transition function

def transition(state, action):

32
if state == 'Controlled':

if action in ['Insulin', 'Exercise']:

return 'Controlled'

else:

return 'Uncontrolled'

else: # state == 'Uncontrolled'

if action in ['Insulin', 'Exercise']:

return 'Controlled'

else:

return 'Uncontrolled'

# Function to choose an action based on an epsilon-greedy policy

def choose_action(state, epsilon):

if np.random.uniform(0, 1) < epsilon:

return np.random.choice(actions) # Exploration

else:

state_index = states.index(state)

return actions[np.argmax(q_table[state_index])] # Exploitation

# Train using a specific algorithm

def train_algorithm(algorithm):

33
global q_table # Use the global q_table variable

if algorithm == 'q_learning':

q_table = np.zeros((len(states), len(actions))) # Reset Q-table for Q-learning

elif algorithm == 'sarsa':

q_table = np.zeros((len(states), len(actions))) # Reset Q-table for SARSA

for episode in range(episodes):

state = np.random.choice(states) # Start with a random state

action = choose_action(state, epsilon) # Choose initial action

done = False

steps = 0 # Count the number of steps to prevent infinite loops

while not done:

next_state = transition(state, action)

# Get the reward

reward = rewards.get((state, action), 0)

if algorithm == 'q_learning':

# Q-learning update rule

next_state_index = states.index(next_state)

best_next_action = np.argmax(q_table[next_state_index])

q_table[states.index(state), actions.index(action)] += learning_rate * (

reward + discount_factor * q_table[next_state_index,


best_next_action] - q_table[states.index(state), actions.index(action)]

34
)

elif algorithm == 'sarsa':

# SARSA update rule

next_action = choose_action(next_state, epsilon)

next_state_index = states.index(next_state)

next_action_index = actions.index(next_action)

q_table[states.index(state), actions.index(action)] += learning_rate * (

reward + discount_factor * q_table[next_state_index,


next_action_index] - q_table[states.index(state), actions.index(action)]

action = next_action # Update action to the next action

# Transition to the next state

state = next_state

# Debugging print statements to track progress

# print(f"Episode: {episode}, Step: {steps}, State: {state}, Action:


{action}, Reward: {reward}")

# Stopping condition (to avoid infinite loops)

steps += 1

if steps >= 100:

35
# print(f"Breaking out of loop to avoid infinite loop after {steps} steps.")

done = True

elif state == 'Controlled' and action == 'Insulin':

done = True

# Main loop to handle user input and algorithm selection

while True:

# Get algorithm type from user input with validation

algorithm = input("Enter the algorithm you want to use ('q_learning' or 'sarsa'):


").strip().lower()

while algorithm not in ['q_learning', 'sarsa']:

print("Invalid input. Please enter 'q_learning' or 'sarsa'.")

algorithm = input("Enter the algorithm you want to use ('q_learning' or


'sarsa'): ").strip().lower()

# Train with the selected algorithm

print(f"Training with {algorithm}...")

train_algorithm(algorithm)

print(f"Completed training with {algorithm}.")

# Display the learned Q-table

print("Learned Q-table:")

36
print(q_table)

# Making treatment decisions based on the trained Q-table

def suggest_treatment(current_state):

state_index = states.index(current_state)

action_index = np.argmax(q_table[state_index])

return actions[action_index]

# Example: Suggest treatment for a patient in the 'Uncontrolled' state

patient_state = input('Enter a Diabetes patient state (Controlled or


Uncontrolled): ')

suggested_treatment = suggest_treatment(patient_state)

print(f"Suggested treatment for a '{patient_state}' patient:


{suggested_treatment}")

# Ask if the user wants to continue with another algorithm

continue_choice = input("Do you want to continue with another algorithm?


(yes/no): ").strip().lower()

if continue_choice != 'yes':

break

OUTPUT:

[[0. 0. 0. 0.]

[0. 0. 0. 0.]]

37
Enter the algorithm you want to use ('q_learning' or
'sarsa'): q_learning

Training with q_learning...

Completed training with q_learning.

Learned Q-table:

[[50.24019982 54.47950972 50.50175878 70. ]

[41.47784024 55.6 57.6 64. ]]

Enter a Diabetes patient state (Controlled or


Uncontrolled): Controlled

Suggested treatment for a 'Controlled' patient:


Exercise

Do you want to continue with another algorithm?


(yes/no): yes

Enter the algorithm you want to use ('q_learning' or


'sarsa'): sarsa

Training with sarsa...

Completed training with sarsa.

Learned Q-table:

[[81.68886822 67.68728661 63.63397435 73.86777091]

[52.56274818 44.89618222 38.98034409 73.92669499]]

Enter a Diabetes patient state (Controlled or


Uncontrolled): Controlled

Suggested treatment for a 'Controlled' patient: Insulin

Do you want to continue with another algorithm?


(yes/no): yes

38
Enter the algorithm you want to use ('q_learning' or
'sarsa'): q_learning

Training with q_learning...

Completed training with q_learning.

Learned Q-table:

[[28.97413263 48.69859583 47.33736546 70. ]

[30.01963135 55.6 57.6 64. ]]

Enter a Diabetes patient state (Controlled or


Uncontrolled): Uncontrolled

Suggested treatment for a 'Uncontrolled' patient:


Exercise

Do you want to continue with another algorithm?


(yes/no): yes

Enter the algorithm you want to use ('q_learning' or


'sarsa'): sarsa

Training with sarsa...

Completed training with sarsa.

Learned Q-table:

[[57.33640018 60.66297885 58.64412848 65.51887554]

[50.70140011 49.01108137 52.19059882 59.12007082]]

Enter a Diabetes patient state (Controlled or


Uncontrolled): Uncontrolled

Suggested treatment for a 'Uncontrolled' patient:


Exercise

39
Do you want to continue with another algorithm?
(yes/no): no

CHAPTER 5

CONCLUSION

The internship at TTC1 focused on MACHINE LEARNING USING


PYTHON, starting with foundational concepts and progressing to hands-on
programming. Participants learned core algorithms for supervised and
unsupervised learning, explored evaluation metrics, and utilized essential tools and
libraries like Pandas, NumPy, Matplotlib, Seaborn, and Scikit-learn. Through real-
world projects, they applied machine learning techniques to various datasets,
overcoming challenges and showcasing their work by presenting project outcomes.
This comprehensive experience equipped interns with valuable skills and practical
knowledge in machine learning and data analysis.

40
CHAPTER 6

GALLERY

41

You might also like