top of page

Exercícios de Machine Learning

pesquisa-basica-vida-real.jpg

Problema da Iris usando a técnica k-NN (k-Nearest-Neighbour)!!!

São 150 linhas. Usamos 105 amostras para treino e 45 para teste

Iris.csv

Material Introdutório sobre a Técnica k-NN

A Tarefa é enviar o código Python da página 74 do livro Aurelien Geron.

Esse código está na "Seção: 2.6.2 - Better Evaluation Using Cross-Validation".

 

É o código do "RandomForestRegressor"!!!

Aurélien Géron, “Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems”, Editora: O'Reilly Media; 2º edição (15 outubro 2019).

Arquivo zipado "housing.csv"

Base de Dados para o teste de 1 neurônio

import matplotlib.pyplot as plt

 

dataset = [[2.7810836,2.550537003,0],

[1.465489372,2.362125076,0],

[3.396561688,4.400293529,0],

[1.38807019,1.850220317,0],

[3.06407232,3.005305973,0],

[7.627531214,2.759262235,1],

[5.332441248,2.088626775,1],

[6.922596716,1.77106367,1],

[8.675418651,-0.242068655,1],

[7.673756466,3.508563011,1]]

 

var_x = []

var_y = []

label = []

listcolors = []

 

for i in range(10):

    var_x.append(dataset[i][0])

    var_y.append(dataset[i][1])

    label.append(dataset[i][2])

    if label[i] == 0:

        listcolors.append('red')

    else:

        listcolors.append('blue')

    #end if

#end for

 

plt.scatter(var_x, var_y, color=listcolors)

plt.show()

More Data Bases for Classifications Problems

sonar.all-data.csv

The label associated with each record contains the letter "R" if the object is a rock and "M" if it is a mine (metal cylinder).

base_dados_logical_XOR.csv

The labes are 0 and 1. Only two classes. But the Data is NOT Linearly Separable,

The cancer dataset, which is a very famous classification problem.

data 2.csv

What is M and B in breast cancer dataset?

The dataset used in this study for breast cancer detection has two classes, which are B ('benign') and M ('malignant'. )

Base de Dados para testar o MLPClassifier do scikit-learn !

Arquivo "X_pd.csv"

Arquivo "y_pd.csv"

Uma colaboração do colega Edmar da Silva Albuquerque (esa3@poli.br)

import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split


iris = load_iris()

X = iris.data

y = iris.target

 

# One-hot Encoding

 

y_onehotencoding = np.zeros((150,3))

for i in range(150):
    if y[i] == 0:
        y_onehotencoding[i,0] = 1

    #end if
    if y[i] == 1:
        y_onehotencoding[i,1] = 1

    #end if
    if y[i] == 2:
        y_onehotencoding[i,2] = 1

    #end if
#end for

X_train, X_test, y_train, y_test = train_test_split(X, y_onehotencoding,
                                                    test_size=0.3,
                                                    random_state=1)


#modelo1 = MLPClassifier(hidden_layer_sizes = (20, 4), # 8, 8, 4, 2
modelo1 = MLPClassifier(hidden_layer_sizes = (8, 8, 4, 3),
                        activation = 'logistic',
                        solver = 'adam',
                        learning_rate = 'constant',
                        learning_rate_init = 0.01,
                        tol = 1e-3,
                        max_iter = 2000)


modelo1.fit(X_train, y_train)

 

# Note: The default solver ‘adam’ works pretty well on relatively 
# large datasets (with thousands of training samples or more) in terms of 
# both training time and validation score. For small datasets, however, 
# ‘lbfgs’ can converge faster and perform better.

 

y_pred = modelo1.predict(X_test)

 

from sklearn.metrics import accuracy_score
score = accuracy_score(y_test, y_pred, normalize=True)
print('score = ', score)

# The best performance is 1 with normalize == True

#

import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# Load the Iris dataset and split it into training and testing sets
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=1)

# Create an MLP model with one hidden layer
model1 = Sequential()
model1.add(Dense(10, input_dim=4, activation='relu'))

#model1.add(Dense(10, input_dim=4, activation='sigmoid'))
model1.add(Dense(1, activation='sigmoid'))

 

# Compile the model and fit it to the training data
model1.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model1.fit(X_train, y_train, epochs=100, batch_size=16, verbose=0)

 

# Evaluate the model on the testing data
loss, accuracy = model1.evaluate(X_test, y_test, verbose=0)
print('Accuracy: {:.2f}%'.format(accuracy*100))

Arquivo "melb_data.csv"

Arquivo "melbourne_data_house.py"

In a Regression problem, the aim is to predict the output of a continuous value, like a price or a probability. Contrast this with a Classification problem, where the aim is to select a class from a list of classes (for example, where a picture contains an apple or an orange, recognizing which fruit is in the picture).

This tutorial uses the classic “Auto MPG” dataset and demonstrates how to build models to predict the fuel efficiency of the late-1970s and early 1980s automobiles. To do this, you will provide the models with a description of many automobiles from that time period. This description includes attributes like cylinders, displacement, horsepower, and weight.

 

https://github.com/tensorflow/docs/blob/master/site/en/tutorials/keras/regression.ipynb

Arquivo "mpg.csv"

Arquivo "regressor.ipynb"

The Multi-armed Bandit Problem
(Reinforcement Learning Problem)

Imagine a gambler in a casino that offers a game where one of several different levers can be pulled. Each lever provides a random payoff. The payoff from the kth lever is a sample from a normal distribution with mean mk and standard deviation one. The mk are liable to be different and are not known. However, the casino guarantees that they will not change. (In this simple example, the environment does not therefore change). The game can be played many times. What strategy should the gambler follow to maximize the expected payoff over many trials?

 

Clearly the gambler should keep careful records so that the average payoff realized so far from each of the levers is known at all times. At each trial of the game, the gambler has to decide between two alternatives:

 

1 - Choose the lever that has given the best average payoff so far

 

2 - Try a new lever

 

This is known as the exploitation vs. exploration choice. The first alternative is exploitation (also known as the greedy action). If, after choosing each lever a few times, the gambler only followed the first strategy, she would not find the best lever unless she was lucky. Some exploration, where another lever is chosen at random, is therefore a good idea. Exploitation maximizes the immediate expected payoff, but a little exploration may improve the long-run payoff.

 

******************************************

 

Consider a situation where there are four levers and

 

𝑚1 = 1.2,   𝑚2 = 1.0,    𝑚3 = 0.8,    𝑚4 = 1.4

 

The gambler would of course choose the fourth lever every time if these values were known. However, the mk have to be inferred from the results of trials.

 

******************************************

E o jogador só tem 1000 tentativas !!!

******************************************

This is the original form of the k-armed bandit problem, so named by analogy to a slot machine, or “one-armed bandit,” except that it has k levers instead of one.

 

Each action selection is like a play of one of the slot machine’s levers, and the rewards are the payoffs for hitting the jackpot. Through repeated action selections you are to maximize your winnings by concentrating your actions on the best levers.

 

Another analogy is that of a doctor choosing between experimental treatments for a series of seriously ill patients. Each action is the selection of a treatment, and each reward is the survival or well-being of the patient. Today the term “bandit problem” is sometimes used for a generalization of the problem described above, but in this book we use it to refer just to this simple case.

 

Ref.: R.S. Sutton and A.G. Barto, "Reinforcement Learning: An Introduction", 2nd edition, 2018, The MIT Press.

 

******************************************

John C Hull, “Machine Learning in Business: An Introduction to the World of Data Science”, Publisher:‎ Independently published (2020). (https://www-2.rotman.utoronto.ca/~hull/MLThirdEditionFiles/index3rdEd.html)

Epsilon-Greedy Algorithm in Reinforcement Learning
The Multi-armed Bandit Problem

Deixo aqui como sugestão o link abaixo para ajudar na elaboração do código Python para a solução do Multi-armed Bandit Problem usando Reinforcement Learning.

Reinforcement Learning to Inventory Control Problem

Fazer um código em Python para reproduzir a Figura 2.6 (do pdf abaixo) e depois descobrir qual é a melhor "Política", considerando:

 

a) que existe uma taxa de aluguel para manter 2 unidades em estoque que é o dobro da taxa de aluguel para manter somente uma unidade em estoque ou nenhuma unidade;

b) p(sale) = 0.3; e

 

c) que existe uma taxa de entrega quando você compra uma unidade para colocar no estoque. E você pode pedir também duas unidades para colocar no estoque, então nesse caso a taxa de entrega é a mesma de que quando você pede uma unidade para colocar no estoque.

Phil Winder Ph.D., “Reinforcement Learning: Industrial Applications of Intelligent Agents”,

Publisher: O'Reilly Media; 1st edition (December 15, 2020)

image Leroy Merlin - completa.jpg

Noticia publicada em 27 de maio de 2024

bottom of page