# How I can measure a performance in term of time for Machine Learning In this article, I will introduce you to different possible approaches to machine learning projects in Python and give you some indications on their trade-offs in execution speed. Benchmark Machine Learning Execution Speed

Introduction

Thanks to recent advances in storage capacity and memory management, it has become much easier to create machine learning and deep learning projects from the comfort of your own home.

In this article, I will introduce you to different possible approaches to machine learning projects in Python and give you some indications on their trade-offs in execution speed. Some of the different approaches are:

• Using a personal computer/laptop CPU (Central processing unit)/GPU (Graphics processing unit).
• Using cloud services (Kaggle, Google Colab).

First of all, we need to import all the necessary dependencies:

``````import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from xgboost import XGBClassifier
import xgboost as xgb
from sklearn.metrics import accuracy_score
``````

For this example, I decided to fabricate a simple dataset using Gaussian Distributions consisting of four features and two labels (0/1):

``````# Creating a linearly separable dataset using Gaussian Distributions.
# The first half of the number in Y is 0 and the other half 1.
# Therefore I made the first half of the 4 features quite different from
# the second half of the features (setting the value of the means quite
# similar) so that make quite simple the classification between the
# classes (the data is linearly separable).
dataset_len = 40000000
dlen = int(dataset_len/2)
X_11 = pd.Series(np.random.normal(2,2,dlen))
X_12 = pd.Series(np.random.normal(9,2,dlen))
X_1 = pd.concat([X_11, X_12]).reset_index(drop=True)
X_21 = pd.Series(np.random.normal(1,3,dlen))
X_22 = pd.Series(np.random.normal(7,3,dlen))
X_2 = pd.concat([X_21, X_22]).reset_index(drop=True)
X_31 = pd.Series(np.random.normal(3,1,dlen))
X_32 = pd.Series(np.random.normal(3,4,dlen))
X_3 = pd.concat([X_31, X_32]).reset_index(drop=True)
X_41 = pd.Series(np.random.normal(1,1,dlen))
X_42 = pd.Series(np.random.normal(5,2,dlen))
X_4 = pd.concat([X_41, X_42]).reset_index(drop=True)
Y = pd.Series(np.repeat([0,1],dlen))
df = pd.concat([X_1, X_2, X_3, X_4, Y], axis=1)
df.columns = ['X1', 'X2', 'X3', 'X_4', 'Y']
``````
![](https://www.freecodecamp.org/news/content/images/2019/11/image-32.png) Figure 1: Example Dataset

Finally, now we just have to prepare our dataset to be fed into a machine learning model (dividing it into features and labels, and training and test sets):

``````train_size = 0.80
X = df.drop(['Y'], axis = 1).values
y = df['Y']

# label_encoder object knows how to understand word labels.
label_encoder = preprocessing.LabelEncoder()

# Encode labels
y = label_encoder.fit_transform(y)

# identify shape and indices
num_rows, num_columns = df.shape
delim_index = int(num_rows * train_size)

# Splitting the dataset in training and test sets
X_train, y_train = X[:delim_index, :], y[:delim_index]
X_test, y_test = X[delim_index:, :], y[delim_index:]

# Checking sets dimensions
print('X_train dimensions: ', X_train.shape, 'y_train: ', y_train.shape)
print('X_test dimensions:', X_test.shape, 'y_validation: ', y_test.shape)

# Checking dimensions in percentages
total = X_train.shape + X_test.shape
print('X_train Percentage:', (X_train.shape/total)*100, '%')
print('X_test Percentage:', (X_test.shape/total)*100, '%')
``````

The output train test split result is shown below:

``````X_train dimensions:  (32000000, 4) y_train:  (32000000,)
X_test dimensions: (8000000, 4) y_validation:  (8000000,)
X_train Percentage: 80.0 %
X_test Percentage: 20.0 %
``````

We are now ready to get started benchmarking the different approaches. In all the following examples, we will be using XGBoost (Gradient Boosted Decision Trees) as our classifier.

1) CPU

Training an XGBClassifier on my personal machine (without using a GPU), led to the following results:

``````%%time

model = XGBClassifier(tree_method='hist')
model.fit(X_train, y_train)
``````
``````CPU times: user 8min 1s, sys: 5.94 s, total: 8min 7s
Wall time: 8min 6s
XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0,
learning_rate=0.1, max_delta_step=0, max_depth=3,
min_child_weight=1, missing=None, n_estimators=100, n_jobs=1,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
silent=None, subsample=1, tree_method='hist', verbosity=1)
``````

Once we've trained our model, we can now check it's prediction accuracy:

``````sk_pred = model.predict(X_test)
sk_pred = np.round(sk_pred)
sk_acc = round(accuracy_score(y_test, sk_pred), 2)
print("XGB accuracy using Sklearn:", sk_acc*100, '%')
``````
``````XGB accuracy using Sklearn: 99.0 %
``````

In summary, using a standard CPU machine, it took about 8 minutes to train our classifier to achieve 99% accuracy.

2) GPU

I will now instead make use of an NVIDIA TITAN RTX GPU on my personal machine to speed up the training. In this case, in order to activate the GPU mode of XGB, we need to specify the tree_method as gpu_hist instead of hist.

``````%%time

model = XGBClassifier(tree_method='gpu_hist')
model.fit(X_train, y_train)
``````

Using the TITAN RTX led in this example to just 8.85 seconds of execution time (about 50 times faster than using just the CPU!).

``````sk_pred = model.predict(X_test)
sk_pred = np.round(sk_pred)
sk_acc = round(accuracy_score(y_test, sk_pred), 2)
print("XGB accuracy using Sklearn:", sk_acc*100, '%')
``````
``````XGB accuracy using Sklearn: 99.0 %
``````

This considerable improvement in speed was possible thanks to the ability of the GPU to take the load off from the CPU, freeing up RAM memory and parallelizing the execution of multiple tasks.

3) GPU Cloud Services

I will now go over two examples of free GPU cloud services (Google Colab and Kaggle) and show you what benchmark score they are able to achieve. In both cases, we need to explicitly turn on the GPUs on the respective notebooks and specify the XGBoost tree_method as gpu_hist.

Using Google Colab NVIDIA TESLA T4 GPUs, the following scores have been registered:

``````CPU times: user 5.43 s, sys: 1.88 s, total: 7.31 s
Wall time: 7.59 s
``````

### Kaggle

Using Kaggle instead led to a slightly higher execution time:

``````CPU times: user 5.37 s, sys: 5.42 s, total: 10.8 s
Wall time: 11.2 s
XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0,
learning_rate=0.1, max_delta_step=0, max_depth=3,
min_child_weight=1, missing=None, n_estimators=100, n_jobs=1,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
silent=None, subsample=1, tree_method='gpu_hist', verbosity=1)
``````

Using either Google Colab or Kaggle both led to a remarkable decrease in execution time.

One downside of using these services is the limited amount of CPU and RAM available. In fact, slightly increasing the dimensions of the example dataset caused Google Colab to run out of RAM memory (which wasn't an issue when using the TITAN RTX).

One possible way to fix this type of problem when working with constrained memory devices is to optimize the code to consume the least amount of memory possible (using fixed point precision and more efficient data structures).

4) Bonus Point: RAPIDS

As an additional point, I will now introduce you to RAPIDS, an open-source collection of Python libraries by NVIDIA. In this example, we will make use of its integration with the XGBoost library to speed up our workflow in Google Colab. The full notebook for this example (with instructions on how to set up RAPIDS in Google Colab) is available here or on my GitHub Account.

RAPIDS is designed to be the next evolutionary step in data processing. Thanks to its Apache Arrow in-memory format, RAPIDS can lead to up to around 50x speed improvement compared to Spark in-memory processing. Additionally, it is also able to scale from one to multi-GPUs.

All RAPIDS libraries are based on Python and are designed to have Pandas and Sklearn-like interfaces to facilitate adoption.

The structure of RAPIDS is based on different libraries in order to accelerate data science from end to end. Its main components are:

• cuDF = used to perform data processing tasks (Pandas-like).
• cuML = used to create machine learning models (Sklearn-like).
• cuGraph = used to perform graph analytics (NetworkX).

In this example, we will make use of it's XGBoost integration:

``````dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)

%%time

params = {}
booster_params = {}
booster_params['tree_method'] = 'gpu_hist'
params.update(booster_params)

clf = xgb.train(params, dtrain)
``````
``````CPU times: user 1.42 s, sys: 719 ms, total: 2.14 s
Wall time: 2.51 s
``````

As we can see above, using RAPIDS it took just about 2.5 seconds to train our model (decreasing time execution by almost 200 times!).

Finally, we can now check that we obtained exactly the same prediction accuracy using RAPIDS that we registered in the other cases:

``````rapids_pred = clf.predict(dtest)

rapids_pred = np.round(rapids_pred)
rapids_acc = round(accuracy_score(y_test, rapids_pred), 2)
print("XGB accuracy using RAPIDS:", rapids_acc*100, '%')
``````
``````XGB accuracy using RAPIDS: 99.0 %
``````

Conclusion

Finally, we can now compare the execution time of the different methods used. As shown in Figure 2, using GPU optimization can substantially decrease execution time, especially if integrated with the use of RAPIDS libraries.

Figure 2: Execution Time comparison

Figure 3 shows how many times faster the GPUs models are compared to our baseline CPU results.

Figure 3: Focus on CPU Execution Time Comparison

## Machine Learning, Data Science and Deep Learning with Python Complete hands-on Machine Learning tutorial with Data Science, Tensorflow, Artificial Intelligence, and Neural Networks. Introducing Tensorflow, Using Tensorflow, Introducing Keras, Using Keras, Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), Learning Deep Learning, Machine Learning with Neural Networks, Deep Learning Tutorial with Python

Machine Learning, Data Science and Deep Learning with Python

### Complete hands-on Machine Learning tutorial with Data Science, Tensorflow, Artificial Intelligence, and Neural Networks

Explore the full course on Udemy (special discount included in the link): http://learnstartup.net/p/BkS5nEmZg

In less than 3 hours, you can understand the theory behind modern artificial intelligence, and apply it with several hands-on examples. This is machine learning on steroids! Find out why everyone’s so excited about it and how it really works – and what modern AI can and cannot really do.

In this course, we will cover:
• Deep Learning Pre-requistes (gradient descent, autodiff, softmax)
• The History of Artificial Neural Networks
• Deep Learning in the Tensorflow Playground
• Deep Learning Details
• Introducing Tensorflow
• Using Tensorflow
• Introducing Keras
• Using Keras to Predict Political Parties
• Convolutional Neural Networks (CNNs)
• Using CNNs for Handwriting Recognition
• Recurrent Neural Networks (RNNs)
• Using a RNN for Sentiment Analysis
• The Ethics of Deep Learning
• Learning More about Deep Learning

At the end, you will have a final challenge to create your own deep learning / machine learning system to predict whether real mammogram results are benign or malignant, using your own artificial neural network you have learned to code from scratch with Python.

Separate the reality of modern AI from the hype – by learning about deep learning, well, deeply. You will need some familiarity with Python and linear algebra to follow along, but if you have that experience, you will find that neural networks are not as complicated as they sound. And how they actually work is quite elegant!

This is hands-on tutorial with real code you can download, study, and run yourself.

## Python Tutorial - Learn Python for Machine Learning and Web Development Python tutorial for beginners - Learn Python for Machine Learning and Web Development. Can Python be used for machine learning? Python is widely considered as the preferred language for teaching and learning ML (Machine Learning). Can I use Python for web development? Python can be used to build server-side web applications. Why Python is suitable for machine learning? How Python is used in AI? What language is best for machine learning?

Python tutorial for beginners - Learn Python for Machine Learning and Web Development

TABLE OF CONTENT

• 00:00:00 Introduction
• 00:01:49 Installing Python 3
• 00:06:10 Your First Python Program
• 00:08:11 How Python Code Gets Executed
• 00:11:24 How Long It Takes To Learn Python
• 00:13:03 Variables
• 00:18:21 Receiving Input
• 00:22:16 Python Cheat Sheet
• 00:22:46 Type Conversion
• 00:29:31 Strings
• 00:37:36 Formatted Strings
• 00:40:50 String Methods
• 00:48:33 Arithmetic Operations
• 00:51:33 Operator Precedence
• 00:55:04 Math Functions
• 00:58:17 If Statements
• 01:06:32 Logical Operators
• 01:11:25 Comparison Operators
• 01:16:17 Weight Converter Program
• 01:20:43 While Loops
• 01:24:07 Building a Guessing Game
• 01:30:51 Building the Car Game
• 01:41:48 For Loops
• 01:47:46 Nested Loops
• 01:55:50 Lists
• 02:01:45 2D Lists
• 02:05:11 My Complete Python Course
• 02:06:00 List Methods
• 02:13:25 Tuples
• 02:15:34 Unpacking
• 02:18:21 Dictionaries
• 02:26:21 Emoji Converter
• 02:30:31 Functions
• 02:35:21 Parameters
• 02:39:24 Keyword Arguments
• 02:44:45 Return Statement
• 02:48:55 Creating a Reusable Function
• 02:53:42 Exceptions
• 03:01:46 Classes
• 03:07:46 Constructors
• 03:14:41 Inheritance
• 03:19:33 Modules
• 03:30:12 Packages
• 03:36:22 Generating Random Values
• 03:44:37 Working with Directories
• 03:50:47 Pypi and Pip
• 03:55:34 Project 1: Automation with Python
• 04:10:22 Project 2: Machine Learning with Python
• 04:58:37 Project 3: Building a Website with Django

If you liked this post, share it with all of your programming buddies!

## Machine Learning Full Course - Learn Machine Learning This complete Machine Learning full course video covers all the topics that you need to know to become a master in the field of Machine Learning.

Machine Learning Full Course | Learn Machine Learning | Machine Learning Tutorial

It covers all the basics of Machine Learning (01:46), the different types of Machine Learning (18:32), and the various applications of Machine Learning used in different industries (04:54:48).This video will help you learn different Machine Learning algorithms in Python. Linear Regression, Logistic Regression (23:38), K Means Clustering (01:26:20), Decision Tree (02:15:15), and Support Vector Machines (03:48:31) are some of the important algorithms you will understand with a hands-on demo. Finally, you will see the essential skills required to become a Machine Learning Engineer (04:59:46) and come across a few important Machine Learning interview questions (05:09:03). Now, let's get started with Machine Learning.

Below topics are explained in this Machine Learning course for beginners:

1. Basics of Machine Learning - 01:46

2. Why Machine Learning - 09:18

3. What is Machine Learning - 13:25

4. Types of Machine Learning - 18:32

5. Supervised Learning - 18:44

6. Reinforcement Learning - 21:06

7. Supervised VS Unsupervised - 22:26

8. Linear Regression - 23:38

9. Introduction to Machine Learning - 25:08

10. Application of Linear Regression - 26:40

11. Understanding Linear Regression - 27:19

12. Regression Equation - 28:00

13. Multiple Linear Regression - 35:57

14. Logistic Regression - 55:45

15. What is Logistic Regression - 56:04

16. What is Linear Regression - 59:35

17. Comparing Linear & Logistic Regression - 01:05:28

18. What is K-Means Clustering - 01:26:20

19. How does K-Means Clustering work - 01:38:00

20. What is Decision Tree - 02:15:15

21. How does Decision Tree work - 02:25:15

22. Random Forest Tutorial - 02:39:56

23. Why Random Forest - 02:41:52

24. What is Random Forest - 02:43:21

25. How does Decision Tree work- 02:52:02

26. K-Nearest Neighbors Algorithm Tutorial - 03:22:02

27. Why KNN - 03:24:11

28. What is KNN - 03:24:24

29. How do we choose 'K' - 03:25:38

30. When do we use KNN - 03:27:37

31. Applications of Support Vector Machine - 03:48:31

32. Why Support Vector Machine - 03:48:55

33. What Support Vector Machine - 03:50:34

34. Advantages of Support Vector Machine - 03:54:54

35. What is Naive Bayes - 04:13:06

36. Where is Naive Bayes used - 04:17:45

37. Top 10 Application of Machine Learning - 04:54:48

38. How to become a Machine Learning Engineer - 04:59:46

39. Machine Learning Interview Questions - 05:09:03