< 1 />
YZV102E
How to Use Python for Machine Learning?
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 2 />
Week 11
How to Use Python for
Scientific Computation?
Machine Learning Concepts
Regression
Classification
Unsupervised Learning
Machine Learning Concepts
Reinforcement Learning
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 3 />
What is Machine
ARTIFICIAL INTELLIGENCE
Learning?
• Application of algorithms that learn from
examples (data)
• Representation and generalization
MACHINE LEARNING
• Useful in everyday life
• Especially useful in data analysis
DEEP LEARNING
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 4 />
Rules
Classical
Answers
Programming
Data
Answers
Machine
Rules
Learning
Data
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 5 />
Example Applications
• Computer Vision • Object Classification in
• Robotics Photos
• Virtual Assistants • Image Caption Generation
• Email Spam Filtering • Automatic Game Playing
• Product Recommendation
• Self-driving Cars
• Machine Translation
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
Machine Learning Styles
< 6 />
Unsupervised Supervised Reinforcement
Learning Learning Learning
The pattern must be found. The pattern is given. Policy optimization.
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
7
< />
How to Build Machine Learning
Systems
Labels
Training New
Dataset Learning
Final Model Data
Labels Algorithm
Validation Dataset
Raw Test Dataset
Data Labels
Preprocessing Learning Evaluation Prediction
• Feature Extraction & Scaling • Model Selection
• Feature Selection • Cross-Validation
• Dimensionality Reduction • Performance
• Sampling Metrics
• Hyperparameter
Optimization
Algorithms
< 8 />
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
Sample Dataset
< 9 />
Attributes
1. age
2. sex
3. chest pain type (4 values)
4. resting blood pressure
5. serum cholestoral in mg/dl
6. fasting blood sugar > 120 mg/dl
7. resting electrocardiographic results (values 0,1,2)
8. maximum heart rate achieved
9. exercise induced angina
10. oldpeak = ST depression induced by exercise relative to rest
11. the slope of the peak exercise ST segment
12. number of major vessels (0-3) colored by flourosopy
13. thal: 3 = normal; 6 = fixed defect; 7 = reversable defect
[Link]
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 10 />
•Simple and efficient tools for predictive data
analysis
•Accessible to everybody, and reusable in various
contexts
•Built on NumPy, SciPy, and matplotlib
•Open source, commercially usable
• BSD license
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 11 />
Week 11
How to Use Python for
Scientific Computation?
Machine Learning Concepts
Regression
Classification
Unsupervised Learning
Regression
Reinforcement Learning
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
Linear Regression
< 12 />
1 1 !
𝑀𝐴𝐸 = ' 𝑦 − 𝑦* 𝑀𝑆𝐸 = ' 𝑦 − 𝑦*
𝑛 𝑛
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
Linear Regression Sample Dataset Diabetes Data 13 < />
[Link]
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
Linear Regression Sample Dataset Diabetes Data 14 < />
import numpy as np
from sklearn import datasets, linear_model
from [Link] import mean_squared_error, r2_score
# Load the diabetes dataset
diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)
# Use only one feature
diabetes_X = diabetes_X[:, [Link], 2]
# Split the data into training/testing sets
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]
# Split the targets into training/testing sets
diabetes_y_train = diabetes_y[:-20]
diabetes_y_test = diabetes_y[-20:]
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
[Link](diabetes_X_train, diabetes_y_train)
# Make predictions using the testing set
diabetes_y_pred = [Link](diabetes_X_test)
[Link]
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
Feature Engineering
< 15 />
• Dimension Reduction
• Derived variables
• Date -> Year, Month, Day
• Create one or more variables
based on other variable(s).
• Dummy Variables
• One-hot-encoding
• Label Encoding
• Turning Integers to Categories
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 16 />
Week 11
How to Use Python for
Scientific Computation?
Machine Learning Concepts
Regression
Classification
Unsupervised Learning
Classification
Reinforcement Learning
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 17 />
Classification – The Famous IRIS Dataset
•150 samples
•3 labels: species of Iris (Iris setosa, Iris
virginica and Iris versicolor)
•4 features: Sepal length,Sepal width,Petal
length,Petal Width in cm
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
18
< />
SPAM Mail Classification
IRIS Dataset
Credit Card Fraud Detection
Face Recognition
Binary Classification Muliclass Classification
Number of target labels
Classification
Number of labels for each example
Single Label Multilabel
SPAM Mail Classification News category identification
Face Recognition
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 19 />
K-NN Classification
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 20 />
from sklearn import datasets
iris = datasets.load_iris()
x = [Link]
y = [Link]
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.5)
from sklearn import neighbors
classifier = [Link](n_neigbors=5)
[Link](x_train, y_train)
predictions = [Link](x_test)
from [Link] import accuracy_score
print(accuracy_score(y_test, predictions))
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 21 />
Decision Tree Classifier
From Alpaydin, 2010
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 22 />
from sklearn import datasets
iris = datasets.load_iris()
x = [Link]
y = [Link]
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.5)
from sklearn import tree
classifier = [Link]()
[Link](x_train, y_train)
predictions = [Link](x_test)
from [Link] import accuracy_score
print(accuracy_score(y_test, predictions))
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 23 />
Week 11
How to Use Python for
Scientific Computation?
Machine Learning Concepts
Regression
Classification
Unsupervised Learning
Unsupervised Learning
Reinforcement Learning
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
Unsupervised Learning
< 24 />
Explore structure of data to extract meaningful information without the
guidance of a known outcome variable or reward function.
x3
• Clustering organizes data into meaningful subgroups • Dimensionality Reduction
(clusters) – “unsupervised classification” compresses data onto smaller
dimensional subspace while
retaining most of the relevant
x1 information
x2
x1
z1
z2
x2
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 25 />
Week 11
How to Use Python for
Scientific Computation?
Machine Learning Concepts
Regression
Classification
Unsupervised Learning
Reinforcement Learning
Reinforcement Learning
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
< 26 />
Classification – The Famous IRIS Dataset
Building an agent that improves its performance based on
interactions with the environment by optimizing a policy.
An agent uses reinforcement state
learning, through interaction Agent
with the environment, to learn
reward
a series of actions (inputs) that action
maximizes the reward signal, Environment
measured by a reward function.
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ
27
< />
< 28 />
End of chapter
Please study yourself on what you have learned today!
YZV102E Introduction to Programming for Data Science (Python) by A. Cüneyd TANTUĞ