SlideShare una empresa de Scribd logo
1 de 94
Descargar para leer sin conexión
LOREM
I P S U M
NUMBER
CRUNCHING
IN PYTHONEnrico Franchi (efranchi@ce.unipr.it) &
Valerio Maggio (valerio.maggio@unina.it)
DOLOR
S I T OUTLINE
• Scientific and Engineering Computing
• Common FP pitfalls
• Numpy NDArray (Memory and Indexing)
• Case Studies
DOLOR
S I T OUTLINE
• Scientific and Engineering Computing
• Common FP pitfalls
• Numpy NDArray (Memory and Indexing)
• Case Studies
DOLOR
S I T OUTLINE
• Scientific and Engineering Computing
• Common FP pitfalls
• Numpy NDArray (Memory and Indexing)
• Case Studies
number-crunching: n. [common] Computations of a
numerical nature, esp. those that make extensive
use of floating-point numbers. This term is in
widespread informal use outside hackerdom and even
in mainstream slang, but has additional hackish
connotations: namely, that the computations are
mindless and involve massive use of brute force.
This is not always evil, esp. if it involves ray tracing
or fractals or some other use that makes pretty
pictures, esp. if such pictures can be used as screen
backgrounds. See also crunch.
number-crunching: n. [common] Computations of a
numerical nature, esp. those that make extensive
use of floating-point numbers. This term is in
widespread informal use outside hackerdom and even
in mainstream slang, but has additional hackish
connotations: namely, that the computations are
mindless and involve massive use of brute force.
This is not always evil, esp. if it involves ray tracing
or fractals or some other use that makes pretty
pictures, esp. if such pictures can be used as screen
backgrounds. See also crunch.
We are not evil.
number-crunching: n. [common] Computations of a
numerical nature, esp. those that make extensive
use of floating-point numbers. This term is in
widespread informal use outside hackerdom and even
in mainstream slang, but has additional hackish
connotations: namely, that the computations are
mindless and involve massive use of brute force.
This is not always evil, esp. if it involves ray tracing
or fractals or some other use that makes pretty
pictures, esp. if such pictures can be used as screen
backgrounds. See also crunch.
We are not evil.
Just chaotic neutral.
AMET
M E N T I
T U M ALTERNATIVES
• Matlab (IDE, numeric computations oriented, high quality algorithms,
lots of packages, poor GP programming support, commercial)
• Octave (Matlab clone)
• R (stats oriented, poor general purpose programming support)
• Fortran/C++ (very low level, very fast, more complex to use)
• In general, these tools either are low level GP or high level DSLs
HIS EX,
T E M P O
R PYTHON
• Numpy (low-level numerical computations) +
Scipy (lots of additional packages)
• IPython (wonderfull command line interpreter) +
IPython Notebook (“Mathematica-like” interactive documents)
• HDF5 (PyTables, H5Py), Databases
• Specific libraries for machine learning, etc.
• General Purpose Object Oriented Programming
TOOLSCU
S E D
TOOLSCU
S E D
TOOLSCU
S E D
DENIQU
E
G U B E R
G R E N
Our Code
Numpy
Atlas/MKL
Improvements
Improvements
Algorithms are
fast because of
highly optimized
C/Fortran code
4 30 LOAD_GLOBAL 1 (dot)
33 LOAD_FAST 0 (a)
36 LOAD_FAST 1 (b)
39 CALL_FUNCTION 2
42 STORE_FAST 2 (c)
NUMPY STACK
c = a · b
ndarray
ndarray
Memory
behavior
shape, stride, flags
(i0, . . . , in 1) ! I
Shape: (d0, …, dn-1)
4x3
An n-dimensional array references some
(usually contiguous memory area)
An n-dimensional array has
property such as its shape or the
data-type of the elements containes
Is an object, so there is some
behavior, e.g., the def. of
__add__ and similar stuff
N-dimensional arrays are homogeneous
(i0, . . . , in 1) ! I
C-contiguousF-contiguous
Shape: (d0, …, dn)
IC =
n 1X
k=0
ik
n 1Y
j=k+1
dj
IF =
n 1X
k=0
ik
k 1Y
j=0
dj
Shape: (d0, …, dk ,…, dn-1)
Shape: (d0, …, dk ,…, dn-1)
IC = i0 · d0 + i14x3
IF = i0 + i1 · d1
ElementLayout
inMemory
Stride
C-contiguous F-contiguous
sF (k) =
k 1Y
j=0
dj
IF =
nX
k=0
ik · sF (k)
sC(k) =
n 1Y
j=k+1
dj
IC =
n 1X
k=0
ik · sC(k)
Stride
C-contiguousF-contiguous
C-contiguous
(s0 = d0, s1 = 1) (s0 = 1, s1 = d1)
IC =
n 1X
k=0
ik
n 1Y
j=k+1
dj IF =
n 1X
k=0
ik
k 1Y
j=0
dj
ndarray
Memory
behavior
shape, stride, flags
ndarray
behavior
shape, stride, flags
View View
View View
Views
C-contiguous
ndarray
behavior
(1,4)
Memory
C-contiguous
ndarray
behavior
(1,4)
Memory
ndarray
Memory
behavior
shape, stride, flags
matrix
Memory
behavior
shape, stride, flags
ndarray
matrix
BasicIndexing
AdvancedIndexing
Broadcasting!
AdvancedIndexing
Broadcasting!
AdvancedIndexing
Broadcasting!
AdvancedIndexing
Broadcasting!
AdvancedIndexing2
AdvancedIndexing2
AdvancedIndexing2
AdvancedIndexing2
AdvancedIndexing2
AdvancedIndexing2
Vectorize!
Don’t use explicit for loops unless you have to!
PART II:
NUMBER CRUNCHING
IN ACTION
PART II:
NUMBER CRUNCHING
IN ACTION
General Disclaimer:
All the Maths appearing in the next slides is only intended to better introduce the considered case studies. Speakers are not
responsible for any possible disease or “brain consumption” caused by too much formulas.
So BEWARE; use this information at your own risk!
It's intention is solely educational. We would strongly encourage you to use this information in cooperation with a medical or
health professional.
AwfulMaths
BEFORE STARTING
What do you need to get started:
• A handful Unix Command-line tool:
• Linux / Mac OSX Users: Your’re done.
• Windows Users: It should be the time to change your OS :-)
• [I]Python (You say?!)
• A DBMS:
• Relational: e.g., SQLite3, PostgreSQL
• No-SQL: e.g., MongoDB
MINIM
S C R I P
T O R E M
LOREM
I P S U M
BENCHMARKING
LOREM
I P S U M
• Vectorization (NumPy vs. “pure” Python
• Loops and Math functions (i.e., sin(x))
• Matrix-Vector Product
• Different implementations of Matrix-Vector Product
CASE STUDIES ON
NUMERICAL EFFICIENCY
HwInfo
Vectorization:sin(x)
Vectorization:sin(x)
Vectorization:sin(x)
Vectorization:sin(x)
Vectorization:sin(x)
Vectorization:sin(x)
NumPy, Winssin(x):Results
NumPy, Wins
fatality
sin(x):Results
NumPy, Wins
fatality
sin(x):Results
NumPy, Wins
fatality
sin(x):Results
Matrix-VectorProduct
dot
dot
dot
dot
dot
dot
NumPy, Winsdot:Results
NumPy, Wins
fatality
dot:Results
LOREM
I P S U M
NUMBER CRUNCHING
APPLICATIONS
MACHINE LEARNING
• Machine Learing = Learning by Machine(s)
• Algorithms and Techniques to gain insights from data or a dataset
• Supervised or Unsupervised Learning
• Machine Learning is actively being used today, perhaps in many more places than
you’d expected
• Mail Spam Filtering
• Search Engine Results Ranking
• Preference Selection
• e.g., Amazon “Customers Who Bought This Item Also Bought”
NAM IN,
S E A
N O
LOREM
I P S U M
CLUSTERING:
BRIEF INTRODUCTION
• Clustering is a type of unsupervised learning that automatically forms
clusters (groups) of similar things.
It’s like automatic classification.
You can cluster almost anything, and the more similar the items are
in the cluster, the better your clusters are.
• k-means is an algorithm that will find k clusters for a given dataset.
• The number of clusters k is user defined.
• Each cluster is described by a single point known as the centroid.
• Centroid means it’s at the center of all the points in the cluster.
from scipy.cluster.vq import kmeans, vq
K-means
from scipy.cluster.vq import kmeans, vq
K-means
from scipy.cluster.vq import kmeans, vq
K-means
from scipy.cluster.vq import kmeans, vq
K-means
from scipy.cluster.vq import kmeans, vq
K-means
K-meansplotfrom scipy.cluster.vq import kmeans, vq
K-meansplotfrom scipy.cluster.vq import kmeans, vq
LOREM
I P S U M
EXAMPLE:
CLUSTERING POINTS
ON A MAP
Here’s the situation:
your friend <NAME> wants you to take him out in the greater Portland, Oregon,
area (US) for his birthday.
A number of other friends are going to come also, so you need to provide a plan
that everyone can follow.
Your friend has given you a list of places he wants to go.
This list is long; it has 70 establishments in it.
YahooAPI:geoGrab
s s f f
Latitude and Longitude Coordinates of two
points (s and f)
Corresponding differences
ˆ = arccos(sin s sin f + cos s cos f cos )
Spherical Distance Measure
SphericalDistanceMeasure
kmeanswithdistLSC
• Problem: Given an input matrix A,
calculate if possible, its inverse matrix.
• Definition:
In linear algebra, a n-by-n (square) matrix A is
invertible (a.k.a. is nonsingular or
nondegenerate) if there exists a n-by-n matrix B
(A-1) such that: AB = BA = In
TRIVIAL EXAMPLE:INVERSE MATRIX
✓ Eigen Decomposition:
• If A is nonsingular, i.e., it can be eigendecomposed and none of its
eigenvalue is equal to zero
✓ Cholesky Decomposition:
• If A is positive definite, where is the Conjugate transpose matrix
of L (i.e., L is a lower triangular matrix)
✓ LU Factorization: (with L and U Lower (Upper) Triangular
Matrix)
✓ Analytic Solution: (writing the Matrix of Cofactors), a.k.a. Cramer Method
A 1
= Q⇤Q 1
A 1
= (L⇤
) 1
L 1
A 1
= 1
det(A) (CT
)i,j = 1
det(A) (Cji) = 1
det(A)
0
B
B
B
@
C1,1 C1,2 · · · C1,n
C2,1 C2,2 · · · C2,n
...
...
...
...
Cm,1 Cm,2 · · · Cm,n
1
C
C
C
A
L⇤
A = LU
Solution(s)
C =
0
@
C1,1 C1,2 C1,3
C2,1 C2,2 C2,3
C3,1 C3,2 C3,3
1
A
Example
C =
0
@
C1,1 C1,2 C1,3
C2,1 C2,2 C2,3
C3,1 C3,2 C3,3
1
A
Example
C 1
=
1
det(C)
⇤
⇤
0
@
(C2,2C3,3 C2,3C3,2) (C1,3C3,2 C1,2C3,3) (C1,2C2,3 C1,3C2,2)
(C2,3C3,1 C2,1C3,3) (C1,1C3,3 C1,3C3,1) (C1,3C2,1 C1,1C2,3)
(C2,1C3,2 C2,2C3,1) (C3,1C1,2 C1,1C3,2) (C1,1C2,2 C1,2C2,1)
1
A
C =
0
@
C1,1 C1,2 C1,3
C2,1 C2,2 C2,3
C3,1 C3,2 C3,3
1
A
Example det(C) = C1,1(C2,2C3,3 C2,3C3,2)
+C1,2(C1,3C3,2 C1,2C3,3)
+C1,3(C1,2C2,3 C1,3C2,2)
C 1
=
1
det(C)
⇤
⇤
0
@
(C2,2C3,3 C2,3C3,2) (C1,3C3,2 C1,2C3,3) (C1,2C2,3 C1,3C2,2)
(C2,3C3,1 C2,1C3,3) (C1,1C3,3 C1,3C3,1) (C1,3C2,1 C1,1C2,3)
(C2,1C3,2 C2,2C3,1) (C3,1C1,2 C1,1C3,2) (C1,1C2,2 C1,2C2,1)
1
A
HomeMade
Duplicated Code
HomeMade
Duplicated Code
Template Method Pattern
HomeMade
Duplicated Code
Template Method Pattern
However, we still have to implement
from scratch computational functions!!
Reinventing the wheel!
HomeMade
Numpyfrom numpy import linalg
Type: function
String Form:<function inv at 0x105f72b90>
File: /Library/Python/2.7/site-packages/numpy/linalg/linalg.py
Definition: linalg.inv(a)
Source:
def inv(a):
"""
Compute the (multiplicative) inverse of a matrix. [...]
Parameters
----------
a : array_like, shape (M, M)
Matrix to be inverted.
Returns
-------
ainv : ndarray or matrix, shape (M, M)
(Multiplicative) inverse of the matrix `a`.
Raises
------
LinAlgError
If `a` is singular or not square.
[...]
"""
a, wrap = _makearray(a)
return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
Underthehood
• Alternative built-in solutions to the same problem:
NumpyAlternatives
Thanks for your kind attention.
Vectorization:i+=2
Vectorization:i+=2
Vectorization:i+=2
Vectorization:i+=2
NumPy, Winsi+=2:Results
fatality
NumPy, Winsi+=2:Results
Create k points for starting centroids
(often randomly)
While any point has changed cluster assignment
for every point in dataset:
for every centroid:
d = distance(centroid,point)
assign(point, nearest(cluster))
for each cluster:
mean = average(cluster)
centroid[cluster] = mean
K-means

Más contenido relacionado

La actualidad más candente

Deep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGLDeep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGLOswald Campesato
 
D3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningD3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningOswald Campesato
 
Machine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowMachine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowAndrew Ferlitsch
 
NYAI - A Path To Unsupervised Learning Through Adversarial Networks by Soumit...
NYAI - A Path To Unsupervised Learning Through Adversarial Networks by Soumit...NYAI - A Path To Unsupervised Learning Through Adversarial Networks by Soumit...
NYAI - A Path To Unsupervised Learning Through Adversarial Networks by Soumit...Rizwan Habib
 
Neural networks with python
Neural networks with pythonNeural networks with python
Neural networks with pythonSimone Piunno
 
Convolutional neural network in practice
Convolutional neural network in practiceConvolutional neural network in practice
Convolutional neural network in practice남주 김
 
Introdution and designing a learning system
Introdution and designing a learning systemIntrodution and designing a learning system
Introdution and designing a learning systemswapnac12
 
MLIP - Chapter 2 - Preliminaries to deep learning
MLIP - Chapter 2 - Preliminaries to deep learningMLIP - Chapter 2 - Preliminaries to deep learning
MLIP - Chapter 2 - Preliminaries to deep learningCharles Deledalle
 
Tensor flow (1)
Tensor flow (1)Tensor flow (1)
Tensor flow (1)景逸 王
 
NIPS読み会2013: One-shot learning by inverting a compositional causal process
NIPS読み会2013: One-shot learning by inverting  a compositional causal processNIPS読み会2013: One-shot learning by inverting  a compositional causal process
NIPS読み会2013: One-shot learning by inverting a compositional causal processnozyh
 
Data-Driven Recommender Systems
Data-Driven Recommender SystemsData-Driven Recommender Systems
Data-Driven Recommender Systemsrecsysfr
 
MLIP - Chapter 3 - Introduction to deep learning
MLIP - Chapter 3 - Introduction to deep learningMLIP - Chapter 3 - Introduction to deep learning
MLIP - Chapter 3 - Introduction to deep learningCharles Deledalle
 
Dictionary Learning for Massive Matrix Factorization
Dictionary Learning for Massive Matrix FactorizationDictionary Learning for Massive Matrix Factorization
Dictionary Learning for Massive Matrix Factorizationrecsysfr
 
Interaction Networks for Learning about Objects, Relations and Physics
Interaction Networks for Learning about Objects, Relations and PhysicsInteraction Networks for Learning about Objects, Relations and Physics
Interaction Networks for Learning about Objects, Relations and PhysicsKen Kuroki
 
Reading group gan - 20170417
Reading group   gan - 20170417Reading group   gan - 20170417
Reading group gan - 20170417Shuai Zhang
 
MLIP - Chapter 5 - Detection, Segmentation, Captioning
MLIP - Chapter 5 - Detection, Segmentation, CaptioningMLIP - Chapter 5 - Detection, Segmentation, Captioning
MLIP - Chapter 5 - Detection, Segmentation, CaptioningCharles Deledalle
 
The world of loss function
The world of loss functionThe world of loss function
The world of loss function홍배 김
 

La actualidad más candente (20)

Deep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGLDeep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGL
 
D3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningD3, TypeScript, and Deep Learning
D3, TypeScript, and Deep Learning
 
Machine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowMachine Learning - Introduction to Tensorflow
Machine Learning - Introduction to Tensorflow
 
NYAI - A Path To Unsupervised Learning Through Adversarial Networks by Soumit...
NYAI - A Path To Unsupervised Learning Through Adversarial Networks by Soumit...NYAI - A Path To Unsupervised Learning Through Adversarial Networks by Soumit...
NYAI - A Path To Unsupervised Learning Through Adversarial Networks by Soumit...
 
Neural networks with python
Neural networks with pythonNeural networks with python
Neural networks with python
 
Convolutional neural network in practice
Convolutional neural network in practiceConvolutional neural network in practice
Convolutional neural network in practice
 
Introdution and designing a learning system
Introdution and designing a learning systemIntrodution and designing a learning system
Introdution and designing a learning system
 
MLIP - Chapter 2 - Preliminaries to deep learning
MLIP - Chapter 2 - Preliminaries to deep learningMLIP - Chapter 2 - Preliminaries to deep learning
MLIP - Chapter 2 - Preliminaries to deep learning
 
Sparse autoencoder
Sparse autoencoderSparse autoencoder
Sparse autoencoder
 
Tensor flow (1)
Tensor flow (1)Tensor flow (1)
Tensor flow (1)
 
ppt
pptppt
ppt
 
Chapter 1 - Introduction
Chapter 1 - IntroductionChapter 1 - Introduction
Chapter 1 - Introduction
 
NIPS読み会2013: One-shot learning by inverting a compositional causal process
NIPS読み会2013: One-shot learning by inverting  a compositional causal processNIPS読み会2013: One-shot learning by inverting  a compositional causal process
NIPS読み会2013: One-shot learning by inverting a compositional causal process
 
Data-Driven Recommender Systems
Data-Driven Recommender SystemsData-Driven Recommender Systems
Data-Driven Recommender Systems
 
MLIP - Chapter 3 - Introduction to deep learning
MLIP - Chapter 3 - Introduction to deep learningMLIP - Chapter 3 - Introduction to deep learning
MLIP - Chapter 3 - Introduction to deep learning
 
Dictionary Learning for Massive Matrix Factorization
Dictionary Learning for Massive Matrix FactorizationDictionary Learning for Massive Matrix Factorization
Dictionary Learning for Massive Matrix Factorization
 
Interaction Networks for Learning about Objects, Relations and Physics
Interaction Networks for Learning about Objects, Relations and PhysicsInteraction Networks for Learning about Objects, Relations and Physics
Interaction Networks for Learning about Objects, Relations and Physics
 
Reading group gan - 20170417
Reading group   gan - 20170417Reading group   gan - 20170417
Reading group gan - 20170417
 
MLIP - Chapter 5 - Detection, Segmentation, Captioning
MLIP - Chapter 5 - Detection, Segmentation, CaptioningMLIP - Chapter 5 - Detection, Segmentation, Captioning
MLIP - Chapter 5 - Detection, Segmentation, Captioning
 
The world of loss function
The world of loss functionThe world of loss function
The world of loss function
 

Destacado

A Tree Kernel based approach for clone detection
A Tree Kernel based approach for clone detectionA Tree Kernel based approach for clone detection
A Tree Kernel based approach for clone detectionValerio Maggio
 
Design patterns and Refactoring
Design patterns and RefactoringDesign patterns and Refactoring
Design patterns and RefactoringValerio Maggio
 
Machine Learning for Software Maintainability
Machine Learning for Software MaintainabilityMachine Learning for Software Maintainability
Machine Learning for Software MaintainabilityValerio Maggio
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentValerio Maggio
 
Unsupervised Machine Learning for clone detection
Unsupervised Machine Learning for clone detectionUnsupervised Machine Learning for clone detection
Unsupervised Machine Learning for clone detectionValerio Maggio
 
LINSEN an efficient approach to split identifiers and expand abbreviations
LINSEN an efficient approach to split identifiers and expand abbreviationsLINSEN an efficient approach to split identifiers and expand abbreviations
LINSEN an efficient approach to split identifiers and expand abbreviationsValerio Maggio
 
Scaffolding with JMock
Scaffolding with JMockScaffolding with JMock
Scaffolding with JMockValerio Maggio
 
Unit testing and scaffolding
Unit testing and scaffoldingUnit testing and scaffolding
Unit testing and scaffoldingValerio Maggio
 
Improving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniquesImproving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniquesValerio Maggio
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeValerio Maggio
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with JunitValerio Maggio
 

Destacado (13)

A Tree Kernel based approach for clone detection
A Tree Kernel based approach for clone detectionA Tree Kernel based approach for clone detection
A Tree Kernel based approach for clone detection
 
Web frameworks
Web frameworksWeb frameworks
Web frameworks
 
Junit in action
Junit in actionJunit in action
Junit in action
 
Design patterns and Refactoring
Design patterns and RefactoringDesign patterns and Refactoring
Design patterns and Refactoring
 
Machine Learning for Software Maintainability
Machine Learning for Software MaintainabilityMachine Learning for Software Maintainability
Machine Learning for Software Maintainability
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unsupervised Machine Learning for clone detection
Unsupervised Machine Learning for clone detectionUnsupervised Machine Learning for clone detection
Unsupervised Machine Learning for clone detection
 
LINSEN an efficient approach to split identifiers and expand abbreviations
LINSEN an efficient approach to split identifiers and expand abbreviationsLINSEN an efficient approach to split identifiers and expand abbreviations
LINSEN an efficient approach to split identifiers and expand abbreviations
 
Scaffolding with JMock
Scaffolding with JMockScaffolding with JMock
Scaffolding with JMock
 
Unit testing and scaffolding
Unit testing and scaffoldingUnit testing and scaffolding
Unit testing and scaffolding
 
Improving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniquesImproving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniques
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing code
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
 

Similar a Number Crunching in Python

DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfArumugam90
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using javaNarayan Sau
 
Machine Learning, Deep Learning and Data Analysis Introduction
Machine Learning, Deep Learning and Data Analysis IntroductionMachine Learning, Deep Learning and Data Analysis Introduction
Machine Learning, Deep Learning and Data Analysis IntroductionTe-Yen Liu
 
DeepLearningLecture.pptx
DeepLearningLecture.pptxDeepLearningLecture.pptx
DeepLearningLecture.pptxssuserf07225
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...DrkhanchanaR
 
Machine learning and linear regression programming
Machine learning and linear regression programmingMachine learning and linear regression programming
Machine learning and linear regression programmingSoumya Mukherjee
 
Programming in python
Programming in pythonProgramming in python
Programming in pythonIvan Rojas
 
Deep Learning Introduction - WeCloudData
Deep Learning Introduction - WeCloudDataDeep Learning Introduction - WeCloudData
Deep Learning Introduction - WeCloudDataWeCloudData
 
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)Universitat Politècnica de Catalunya
 
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...Universitat Politècnica de Catalunya
 
Deep learning from scratch
Deep learning from scratch Deep learning from scratch
Deep learning from scratch Eran Shlomo
 
Introduction to Big Data Science
Introduction to Big Data ScienceIntroduction to Big Data Science
Introduction to Big Data ScienceAlbert Bifet
 
机器学习Adaboost
机器学习Adaboost机器学习Adaboost
机器学习AdaboostShocky1
 
Understanding Basics of Machine Learning
Understanding Basics of Machine LearningUnderstanding Basics of Machine Learning
Understanding Basics of Machine LearningPranav Ainavolu
 
Data Science and Machine Learning with Tensorflow
 Data Science and Machine Learning with Tensorflow Data Science and Machine Learning with Tensorflow
Data Science and Machine Learning with TensorflowShubham Sharma
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsRajendran
 

Similar a Number Crunching in Python (20)

DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
 
supervised.pptx
supervised.pptxsupervised.pptx
supervised.pptx
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
 
Machine Learning, Deep Learning and Data Analysis Introduction
Machine Learning, Deep Learning and Data Analysis IntroductionMachine Learning, Deep Learning and Data Analysis Introduction
Machine Learning, Deep Learning and Data Analysis Introduction
 
DeepLearningLecture.pptx
DeepLearningLecture.pptxDeepLearningLecture.pptx
DeepLearningLecture.pptx
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
 
Machine learning and linear regression programming
Machine learning and linear regression programmingMachine learning and linear regression programming
Machine learning and linear regression programming
 
Programming in python
Programming in pythonProgramming in python
Programming in python
 
Deep Learning Introduction - WeCloudData
Deep Learning Introduction - WeCloudDataDeep Learning Introduction - WeCloudData
Deep Learning Introduction - WeCloudData
 
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
 
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
 
Ml ppt at
Ml ppt atMl ppt at
Ml ppt at
 
Deep learning from scratch
Deep learning from scratch Deep learning from scratch
Deep learning from scratch
 
Introduction to Big Data Science
Introduction to Big Data ScienceIntroduction to Big Data Science
Introduction to Big Data Science
 
Paris Data Geeks
Paris Data GeeksParis Data Geeks
Paris Data Geeks
 
Lecture 1 (bce-7)
Lecture   1 (bce-7)Lecture   1 (bce-7)
Lecture 1 (bce-7)
 
机器学习Adaboost
机器学习Adaboost机器学习Adaboost
机器学习Adaboost
 
Understanding Basics of Machine Learning
Understanding Basics of Machine LearningUnderstanding Basics of Machine Learning
Understanding Basics of Machine Learning
 
Data Science and Machine Learning with Tensorflow
 Data Science and Machine Learning with Tensorflow Data Science and Machine Learning with Tensorflow
Data Science and Machine Learning with Tensorflow
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 

Último

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 

Último (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 

Number Crunching in Python

  • 1. LOREM I P S U M NUMBER CRUNCHING IN PYTHONEnrico Franchi (efranchi@ce.unipr.it) & Valerio Maggio (valerio.maggio@unina.it)
  • 2. DOLOR S I T OUTLINE • Scientific and Engineering Computing • Common FP pitfalls • Numpy NDArray (Memory and Indexing) • Case Studies
  • 3. DOLOR S I T OUTLINE • Scientific and Engineering Computing • Common FP pitfalls • Numpy NDArray (Memory and Indexing) • Case Studies
  • 4. DOLOR S I T OUTLINE • Scientific and Engineering Computing • Common FP pitfalls • Numpy NDArray (Memory and Indexing) • Case Studies
  • 5. number-crunching: n. [common] Computations of a numerical nature, esp. those that make extensive use of floating-point numbers. This term is in widespread informal use outside hackerdom and even in mainstream slang, but has additional hackish connotations: namely, that the computations are mindless and involve massive use of brute force. This is not always evil, esp. if it involves ray tracing or fractals or some other use that makes pretty pictures, esp. if such pictures can be used as screen backgrounds. See also crunch.
  • 6. number-crunching: n. [common] Computations of a numerical nature, esp. those that make extensive use of floating-point numbers. This term is in widespread informal use outside hackerdom and even in mainstream slang, but has additional hackish connotations: namely, that the computations are mindless and involve massive use of brute force. This is not always evil, esp. if it involves ray tracing or fractals or some other use that makes pretty pictures, esp. if such pictures can be used as screen backgrounds. See also crunch. We are not evil.
  • 7. number-crunching: n. [common] Computations of a numerical nature, esp. those that make extensive use of floating-point numbers. This term is in widespread informal use outside hackerdom and even in mainstream slang, but has additional hackish connotations: namely, that the computations are mindless and involve massive use of brute force. This is not always evil, esp. if it involves ray tracing or fractals or some other use that makes pretty pictures, esp. if such pictures can be used as screen backgrounds. See also crunch. We are not evil. Just chaotic neutral.
  • 8. AMET M E N T I T U M ALTERNATIVES • Matlab (IDE, numeric computations oriented, high quality algorithms, lots of packages, poor GP programming support, commercial) • Octave (Matlab clone) • R (stats oriented, poor general purpose programming support) • Fortran/C++ (very low level, very fast, more complex to use) • In general, these tools either are low level GP or high level DSLs
  • 9. HIS EX, T E M P O R PYTHON • Numpy (low-level numerical computations) + Scipy (lots of additional packages) • IPython (wonderfull command line interpreter) + IPython Notebook (“Mathematica-like” interactive documents) • HDF5 (PyTables, H5Py), Databases • Specific libraries for machine learning, etc. • General Purpose Object Oriented Programming
  • 13. DENIQU E G U B E R G R E N Our Code Numpy Atlas/MKL Improvements Improvements Algorithms are fast because of highly optimized C/Fortran code 4 30 LOAD_GLOBAL 1 (dot) 33 LOAD_FAST 0 (a) 36 LOAD_FAST 1 (b) 39 CALL_FUNCTION 2 42 STORE_FAST 2 (c) NUMPY STACK c = a · b
  • 14. ndarray ndarray Memory behavior shape, stride, flags (i0, . . . , in 1) ! I Shape: (d0, …, dn-1) 4x3 An n-dimensional array references some (usually contiguous memory area) An n-dimensional array has property such as its shape or the data-type of the elements containes Is an object, so there is some behavior, e.g., the def. of __add__ and similar stuff N-dimensional arrays are homogeneous
  • 15. (i0, . . . , in 1) ! I C-contiguousF-contiguous Shape: (d0, …, dn) IC = n 1X k=0 ik n 1Y j=k+1 dj IF = n 1X k=0 ik k 1Y j=0 dj Shape: (d0, …, dk ,…, dn-1) Shape: (d0, …, dk ,…, dn-1) IC = i0 · d0 + i14x3 IF = i0 + i1 · d1 ElementLayout inMemory
  • 16. Stride C-contiguous F-contiguous sF (k) = k 1Y j=0 dj IF = nX k=0 ik · sF (k) sC(k) = n 1Y j=k+1 dj IC = n 1X k=0 ik · sC(k) Stride C-contiguousF-contiguous C-contiguous (s0 = d0, s1 = 1) (s0 = 1, s1 = d1) IC = n 1X k=0 ik n 1Y j=k+1 dj IF = n 1X k=0 ik k 1Y j=0 dj
  • 18.
  • 19.
  • 34. Vectorize! Don’t use explicit for loops unless you have to!
  • 37. General Disclaimer: All the Maths appearing in the next slides is only intended to better introduce the considered case studies. Speakers are not responsible for any possible disease or “brain consumption” caused by too much formulas. So BEWARE; use this information at your own risk! It's intention is solely educational. We would strongly encourage you to use this information in cooperation with a medical or health professional. AwfulMaths
  • 38. BEFORE STARTING What do you need to get started: • A handful Unix Command-line tool: • Linux / Mac OSX Users: Your’re done. • Windows Users: It should be the time to change your OS :-) • [I]Python (You say?!) • A DBMS: • Relational: e.g., SQLite3, PostgreSQL • No-SQL: e.g., MongoDB MINIM S C R I P T O R E M
  • 39. LOREM I P S U M BENCHMARKING
  • 40. LOREM I P S U M • Vectorization (NumPy vs. “pure” Python • Loops and Math functions (i.e., sin(x)) • Matrix-Vector Product • Different implementations of Matrix-Vector Product CASE STUDIES ON NUMERICAL EFFICIENCY
  • 53. dot
  • 54. dot
  • 55. dot
  • 56. dot
  • 57. dot
  • 58. dot
  • 61. LOREM I P S U M NUMBER CRUNCHING APPLICATIONS
  • 62. MACHINE LEARNING • Machine Learing = Learning by Machine(s) • Algorithms and Techniques to gain insights from data or a dataset • Supervised or Unsupervised Learning • Machine Learning is actively being used today, perhaps in many more places than you’d expected • Mail Spam Filtering • Search Engine Results Ranking • Preference Selection • e.g., Amazon “Customers Who Bought This Item Also Bought” NAM IN, S E A N O
  • 63. LOREM I P S U M CLUSTERING: BRIEF INTRODUCTION • Clustering is a type of unsupervised learning that automatically forms clusters (groups) of similar things. It’s like automatic classification. You can cluster almost anything, and the more similar the items are in the cluster, the better your clusters are. • k-means is an algorithm that will find k clusters for a given dataset. • The number of clusters k is user defined. • Each cluster is described by a single point known as the centroid. • Centroid means it’s at the center of all the points in the cluster.
  • 64. from scipy.cluster.vq import kmeans, vq K-means
  • 65. from scipy.cluster.vq import kmeans, vq K-means
  • 66. from scipy.cluster.vq import kmeans, vq K-means
  • 67. from scipy.cluster.vq import kmeans, vq K-means
  • 68. from scipy.cluster.vq import kmeans, vq K-means
  • 71. LOREM I P S U M EXAMPLE: CLUSTERING POINTS ON A MAP Here’s the situation: your friend <NAME> wants you to take him out in the greater Portland, Oregon, area (US) for his birthday. A number of other friends are going to come also, so you need to provide a plan that everyone can follow. Your friend has given you a list of places he wants to go. This list is long; it has 70 establishments in it.
  • 73. s s f f Latitude and Longitude Coordinates of two points (s and f) Corresponding differences ˆ = arccos(sin s sin f + cos s cos f cos ) Spherical Distance Measure SphericalDistanceMeasure
  • 75. • Problem: Given an input matrix A, calculate if possible, its inverse matrix. • Definition: In linear algebra, a n-by-n (square) matrix A is invertible (a.k.a. is nonsingular or nondegenerate) if there exists a n-by-n matrix B (A-1) such that: AB = BA = In TRIVIAL EXAMPLE:INVERSE MATRIX
  • 76. ✓ Eigen Decomposition: • If A is nonsingular, i.e., it can be eigendecomposed and none of its eigenvalue is equal to zero ✓ Cholesky Decomposition: • If A is positive definite, where is the Conjugate transpose matrix of L (i.e., L is a lower triangular matrix) ✓ LU Factorization: (with L and U Lower (Upper) Triangular Matrix) ✓ Analytic Solution: (writing the Matrix of Cofactors), a.k.a. Cramer Method A 1 = Q⇤Q 1 A 1 = (L⇤ ) 1 L 1 A 1 = 1 det(A) (CT )i,j = 1 det(A) (Cji) = 1 det(A) 0 B B B @ C1,1 C1,2 · · · C1,n C2,1 C2,2 · · · C2,n ... ... ... ... Cm,1 Cm,2 · · · Cm,n 1 C C C A L⇤ A = LU Solution(s)
  • 77. C = 0 @ C1,1 C1,2 C1,3 C2,1 C2,2 C2,3 C3,1 C3,2 C3,3 1 A Example
  • 78. C = 0 @ C1,1 C1,2 C1,3 C2,1 C2,2 C2,3 C3,1 C3,2 C3,3 1 A Example C 1 = 1 det(C) ⇤ ⇤ 0 @ (C2,2C3,3 C2,3C3,2) (C1,3C3,2 C1,2C3,3) (C1,2C2,3 C1,3C2,2) (C2,3C3,1 C2,1C3,3) (C1,1C3,3 C1,3C3,1) (C1,3C2,1 C1,1C2,3) (C2,1C3,2 C2,2C3,1) (C3,1C1,2 C1,1C3,2) (C1,1C2,2 C1,2C2,1) 1 A
  • 79. C = 0 @ C1,1 C1,2 C1,3 C2,1 C2,2 C2,3 C3,1 C3,2 C3,3 1 A Example det(C) = C1,1(C2,2C3,3 C2,3C3,2) +C1,2(C1,3C3,2 C1,2C3,3) +C1,3(C1,2C2,3 C1,3C2,2) C 1 = 1 det(C) ⇤ ⇤ 0 @ (C2,2C3,3 C2,3C3,2) (C1,3C3,2 C1,2C3,3) (C1,2C2,3 C1,3C2,2) (C2,3C3,1 C2,1C3,3) (C1,1C3,3 C1,3C3,1) (C1,3C2,1 C1,1C2,3) (C2,1C3,2 C2,2C3,1) (C3,1C1,2 C1,1C3,2) (C1,1C2,2 C1,2C2,1) 1 A
  • 83. Duplicated Code Template Method Pattern However, we still have to implement from scratch computational functions!! Reinventing the wheel! HomeMade
  • 85. Type: function String Form:<function inv at 0x105f72b90> File: /Library/Python/2.7/site-packages/numpy/linalg/linalg.py Definition: linalg.inv(a) Source: def inv(a): """ Compute the (multiplicative) inverse of a matrix. [...] Parameters ---------- a : array_like, shape (M, M) Matrix to be inverted. Returns ------- ainv : ndarray or matrix, shape (M, M) (Multiplicative) inverse of the matrix `a`. Raises ------ LinAlgError If `a` is singular or not square. [...] """ a, wrap = _makearray(a) return wrap(solve(a, identity(a.shape[0], dtype=a.dtype))) Underthehood
  • 86. • Alternative built-in solutions to the same problem: NumpyAlternatives
  • 87. Thanks for your kind attention.
  • 94. Create k points for starting centroids (often randomly) While any point has changed cluster assignment for every point in dataset: for every centroid: d = distance(centroid,point) assign(point, nearest(cluster)) for each cluster: mean = average(cluster) centroid[cluster] = mean K-means