Docsity

Machine Learning Formulario - PoliMi | prof. Vercellis, Schemi e mappe concettuali di Machine learning

Toggle sidebar
bg1
Machine Learning
Outlier: observation which lies at a distance greater than d from at least p percentage of the other
observations in the dataset, can be identified with:
z-index method
or
Clustering
Scaling
1. Decimal scaling (highlights outliers)
2. Min Max (brings data to [min, max] interval)
3. Z-index scaling (brings data to [-3,3])
Feature extraction: creation of new variables that summarize within themselves the relevant information
contained in a subset of the original attributes
1. Sampling (extract observations)
2. Attribute selection eliminate features not relevant to the algorithm
i. Filter methods: select before training, independent on the algorithm
ii. Wrapper methods: selection in groups, algorithm run with different selections and best one chosen
iii. Embedded methods: feature selection embedded in the algorithm (e.g. trees), can be backward-
forward-back/forward
3. PCA - principal component analysis
PCA - principal components analysis
-can improve model’s accuracy
-no loss of information, but usually a subset of q < n
components has information content almost equivalent
to the original dataset
-Std required before applying
-Unsupervised method
Explanatory capability of the first q PCs
First component:
the solution is the eigenvector associated
w/ the largest eigenvalue of the covariance
matrix
max
w1
{w′
1Vw1:w′
1w1= 1} →L(w1,λ1) = w′
1Vw1−λ1(w′
1w1−1)
KT T
∂L(w1,λ1)
∂w1= 2Vw1−2λ1w1= 0
∂L(w1,λ1)
∂λ1= 1 −w′
1w1= 0
u1
λ1
Other components
{w′
2u1= 0
maxw1{w′
2Vw1:w′
2w1= 1}
Iq=λ1+ .. . + λq
λ1+ . .. + λn
x′
ij =xi j
10h
x′
ij =xij −xmin , j
xmax , j−xmin , j(x′
max , j−x′
min , j)+x′
min , j
x′
ij =xi j −¯μj
¯σj
: ,
X
⏟
original
dataset
PCA P
P= {p1,...!,pn}
better!at!explaining
data!variance
∈IRn
pi⊥pj!,!∀i≠j
new!basis
!and!pi=Xwi
zij =xi j −¯μj
¯σj
⇒{|zij |> 3!suspicious!
|zij |≫3outlier
Data discretization: to make continuous variables categorical or to reduce the number of distinct values
takes by categorical variables
1. Subjective division : with expert’s information
2. Subdivision into classes : or equal size or width
3. Hierarchical subdivision : relationships between attributes considered to divide them into classes
Measures of heterogeneity - categorical
-Gini and Entropy min when for one class and
for other classes : minimum heterogeneity
-Entropy : measure of impurity/randomness of the data
points
aj
fh= 1
fj≠h= 0
Categorical graphical analysis - categorical
Horizontal or vertical bar charts from the frequencies
aj
empirical density
pr=er
m lr
def. quantile : given any value , the p-order quantile is the value : observations will fall
on the left of [and the remaining on its right]
p∈[0; 1]
qp
p m
qp
(1 −p)m
Gini index
Entropy index
G= 1 −
H
∑
h=1
f2
h
E=−
H
∑
h=1
fhlog2fh
Measures of central tendency - numerical
-Mean: strongly affected by outliers (not robust)
-Median (central value): not as affected by outliers,
suitable for symmetric and open classes distributions
-Mode : value for which there’s a peak in the empirical
density curve
multimodal density curves have more peaks, mode has
no statistical relevance
aj
Univariate data analysis
study the behavior of as an independent attribute
aj
z - index
zin d
i=xi−¯μ
¯σ
signed distance between any value and , normalized on
¯μ
¯σ
Numerical graphical analysis - numerical
Plotted in a histogram
aj
values taken by
relative frequencies
relative empirical frequency
H= {1,2,...,H}
aj
V= {v1,...,vH}
fh=eh
m
mean
median
mode
¯μ=x1+x2+ . .. + xm
m=1
m
m
∑
i=1
xi
xmed =x(m+1) /2!if!m!odd!
xmed =
xm/2 +x(m+2)/ 2
2!if!m!even
xmid range =xm a x +xmin
2
Measures of dispersion - numerical
aj
variance
¯σ2=1
m−1
m
∑
i=1
s2
i=1
m−1
m
∑
i=1
(xi−¯μ)2
Given
𝒟!dataset
aj!generic!attribute!being!analyzed
aj= (x1,x2, . . . , xm)
m!no.!of!observations!
1
pf3
pf4
pf5

Anteprima parziale del testo

Scarica Machine Learning Formulario - PoliMi | prof. Vercellis e più Schemi e mappe concettuali in PDF di Machine learning solo su Docsity!

Machine Learning

Outlier : observation which lies at a distance greater than d from at least p percentage of the other observations in the dataset, can be identified with:

z-index method or Clustering

Scaling

  1. Decimal scaling (highlights outliers)
  2. Min Max (brings data to [min, max] interval)
  3. Z-index scaling (brings data to [-3,3])

Feature extraction: creation of new variables that summarize within themselves the relevant information contained in a subset of the original attributes

  1. Sampling (extract observations)
  2. Attribute selection eliminate features not relevant to the algorithm i. Filter methods : select before training, independent on the algorithm ii. Wrapper methods : selection in groups, algorithm run with different selections and best one chosen iii. Embedded methods : feature selection embedded in the algorithm (e.g. trees), can be backward- forward-back/forward
  3. PCA - principal component analysis

PCA - principal components analysis

- can improve model’s accuracy

- no loss of information, but usually a subset of q < n

components has information content almost equivalent to the original dataset

- Std required before applying

- Unsupervised method

Explanatory capability of the first q PCs

First component:

the solution is the eigenvector associated w/ the largest eigenvalue of the covariance matrix

max w 1

{ w ′ 1 Vw 1 : w ′ 1 w 1 = 1 } → L ( w 1 , λ 1 ) = w ′ 1 Vw 1 − λ 1 ( w ′ 1 w 1 − 1 )

KT T

∂ L ( w 1 , λ 1 )

∂ w 1 =^2 Vw^1 −^2 λ^1 w^1 =^0

∂ L ( w 1 , λ 1 )

∂ λ 1 =^1 −^ w ′^1 w^1 =^0

u 1 λ 1 V

Other components

w ′ 2 u 1 = 0

max w 1 { w ′ 2 Vw 1 : w ′ 2 w 1 = 1 }

Iq =

λ 1 +... + λq λ 1 +... + λn

xi ′ j =

xij

10 h x ′ ij =

xij − x min , j x max , j − x min , j (

x ′max , j − x ′min , j ) + x min′ , j

xi ′ j =

xij − μ ¯ j σ ¯ j

X : ,

⏟ original dataset

PCA P P = { p 1 , ... , p n }

better at explaining data variance

∈ IRn

p i ⊥ p j , ∀ i ≠ j

new basis

and p i = Xw i

zij =

xij − μ ¯ j σ ¯ j

| zij | > 3 suspicious | zij | ≫ 3 outlier

Data discretization: to make continuous variables categorical or to reduce the number of distinct values takes by categorical variables

  1. Subjective division : with expert’s information
  2. Subdivision into classes : or equal size or width
  3. Hierarchical subdivision : relationships between attributes considered to divide them into classes

Measures of heterogeneity - categorical

- Gini and Entropy min when for one class and

for other classes : minimum heterogeneity

- Entropy : measure of impurity/randomness of the data

points

aj fh = 1 fj ≠ h = 0

Categorical graphical analysis - categorical Horizontal or vertical bar charts from the frequencies

aj

pr = empirical density

er m lr

def. quantile : given any value , the p-order quantile is the value : observations will fall on the left of [and the remaining on its right]

p ∈ [ 0 ; 1 ] qp p m qp ( 1 − p ) m

Gini index

Entropy index

G = 1 −

H ∑ h = 1

f (^) h^2

E = −

H ∑ h = 1

fh log 2 fh

Measures of central tendency - numerical

- Mean: strongly affected by outliers (not robust)

- Median ( central value ): not as affected by outliers,

suitable for symmetric and open classes distributions

- Mode : value for which there’s a peak in the empirical

density curve multimodal density curves have more peaks, mode has no statistical relevance

aj

Univariate data analysis study the behavior of a j as an independent attribute

z (^) ii nd = z - index

xi − μ ¯ σ ¯

signed distance between any value and μ ¯, normalized on σ ¯

Numerical graphical analysis - numerical Plotted in a histogram

aj

values taken by relative frequencies

relative empirical frequency

H = {1,2,..., H } a j V = { v 1 ,... , vH }

fh =

eh m

mean

median

mode

μ ¯ =

x 1 + x 2 +... + xm m

=

1 m

m ∑ i = 1

xi

xmed^ = x ( m + 1 )/ 2 if m odd

xmed^ =

xm / 2 + x ( m + 2 )/ 2 2 if^ m^ even xmidrange^ =

xma x + xmin 2

Measures of dispersion - aj numerical σ ¯^2 = (^) m^1 − 1 variance

m ∑ i = 1

s i^2 = (^) m^1 − 1

m ∑ i = 1

( xi − μ ¯ )^2

Given

𝒟 dataset a j generic attribute being analyzed a j = ( x 1 , x 2 ,... , xm ) m no. of observations

Scatter plots Loess plots (scatter + local regression) Level curves QQ plots Comparing box plots Time series

Multivariate data analysis Scatter plots matrix Star plots Spider webchart

Bivariate data analysis study the behavior of the pair as a pair Usually explanatory variable, target variable

{ aj , ak } a j a k

fr = marginal frequencies

K ∑ s = 1

trs

gs =

J ∑ r = 1

trs

Analysis of empirical density

With

μ ¯ k =

m

m ∑ i = 1

( xi − μ ¯ ) k

Ias = 0 simmetric :

Ias > 0 skewed to the right Ias < 0 skewed to the left Icurt = 0 ⇒ 𝒩 :

Icurt > 0 emp. density hypernormal Icurt < 0 emp. density hyponormal

vjk = cov( a j , a k ) =

1 m − 2

m ∑ i = 1

( xij − μ ¯ j )( xik − μ ¯ k )

rij = corr( a k , a j ) =

vij σ ¯ j σ ¯ k

∈ [−1,1]

Graphical analysis - numerical

- loess plot: depends on (i) degree of the polynomial

local regression and (ii) size of the neighborhood used for the regression [ is a regularizer]

- Qq plots : if series of data extracted from the same

probability distribution, the points will fall on x=y

{ aj , ak } λ α α

  1. Normal probability plot: x-axis sample quantiles, y-axis theoretical quantiles normal distribution falls on a straight line, uniform distribution falls on an s shaped curve

𝒩

Measures of heterogeneity - categorical

  • (^) contingency table is defined as a matrix whose generic element indicates the frequency with which pair of values appears in the records of the dataset

- the two attributes are independent if the analysis

of in relation to is equivalent to the univariate analysis of

{ aj , ak } T trs { xij = vr }, { xij = us } 𝒟 a j , a k a j a k a j

tr ( V ) = trace of

n ∑ j = 1

vij =

n ∑ j = 1

σ ¯^2 ij V

asymmetry index

kurtosis

Ias =

μ ¯ 3 σ ¯^3

Icurt =

μ ¯ 2 σ ¯^4

Measures of correlation - numerical Linear correlation = covariance std (significant if )

{ aj , ak }

| rij | > 0.

𝒟 dataset a j , a k generic attributes being analyzed a j = ( xj 1 , xj 2 ,... , xjm ); a k = ( xk 1 , xk 2 ,... , xkm ) m no. of observations

Classification - target variable class given , denotes a set of functions

the classification problem is the definition of the hypothesis space and a function which optimally describes the relationship between and

Sm = {( xi , yi ), i ∈ ℳ} : training set , xi ∈ IRnand yi ∈ 𝒟 ℋ f ( x ) : IRn^ → 𝒟 ⇒ ℋ f * ∈ ℋ xi yi

  1. One against all : k classes = k binary problem to isolate each class
  2. One against one (round robin) : k classes = K^2 binary problems and majority voting

Multicategory classification Every multi category problem can be reduced to a binary problem

Evaluation metrics

1. Accuracy

- is highly sensitive to outliers and imbalanced

datasets (not appropriate)

- Data used in the computation extracted with :

repeated random sampling (obs may appear more than once) holdout method (highly depends on the subsets) k-fold cross validation (more robust) leave one out (k fold, k=m)

  1. Confusion matrix accurate also for unbalanced datasets, weights can also be associated to improve balance true positive rate: predicted positives among all the real positives false positive rate: predicted false among all the real positives
  2. ROC curve visual expression of a sequence of confusion matrices, x :fpr and y:tpr 4. Cumulative gain Overfitting : production of an analysis that corresponds too closely or exactly to a particular set of data, and may therefore fail to fit additional data or predict future observations reliably

KNN - k nearest neighbors - heuristic model knn40=KNeighborsClassifier(n_neighbors=40)

accuracy

Recall (true positive rate)

Precision

F-index

L ( y , f ( x )) = {

0 if y = f ( x ) 1 if y ≠ f ( x )

Acc = 1 −

m

m ∑ i = 1

L ( f ( x i ))

T P T P + F N

% T P T P + F P

%

F =

( β + 1 )^2 tp × prc β^2 prc + tp

The new record is attributed to the class to which the majority of records out of K closest ones belongs ⇒ majority voting

Classification trees - heuristic model

- top - down induction (heuristic greedy procedure) greedy algorithm (finds local minima)

- Rules : generated in order to maximize the information gain (min variance/heterogeneity within one

node and max variance between different nodes) between nodes Chosen rule that maximizes the difference in impurity between father and descendant nodes

binary : each node has 2 branches multi split : each node has n>2 branches

Generalized regression

Validation of regression models

Ridge and Lasso regressions relaxes the computation of such that doesn’t have to be invertible

- regularization term (L1 or L2) added to the cost

function minimized in the optimization, to penalize too broad regression functions that overfit the problem

- When we go back to LR

But if we add weight to the cost function, and the weights are chosen when the cost function is min

- Lasso : better performance but non differentiable

w ̂ X ′ X

λ = 0 λ ≫ 1

X ′ X is not useful if , we have to resort to {

not enough data points > ill posed problem not invertible by nature

covariance of estimators confidence interval

hypothesis test : rejected and model ok

V = cov( w ̂) w ̂ j ±^ tα / 2 vij

H 0 : wi = 0 t ≫ 2, P < 0.05 H 0

Y = ∑

h

whgh ( Xi , X 2 ,... , Xn ) + b + ϵ

Significance of the coefficients estimated with SSE minimization are not deterministic, have an associated confidence interval has no significance if its interval contains 0, hypothesis test is conducted such that we can reject the hypothesis of and hence accept the model

w ̂ w ̂

⇒ wi

wi = 0

Extension of classification tech. to regression

  1. KNN : average of the k closets neighbors target value
  2. Regression trees : rules based on the variance within nodes, prediction value as average of target values of obs in a leaf
  3. SVM : fitting hyperplane instead of separating hyperplane (points in the margin not misclassifications and not in the error)
  4. Neural networks : SSE to minimize the coefficients wi of the activation functions g ( w ′ x − ξ ) = w 1 x 1 +... + wn xn − ξ = w ′ x − ξ

Normality and independence of residuals Residuals should not contain any deterministic information about the data, they should be completely random

  1. Heteroschedasticity of residuals (residuals depend on the predicted value): scatterplot of residuals
  2. Goodness of fit test to evaluate : chi square test, Kolmogorov - Smirnov test
  3. QQ plot of residuals to evaluate
  4. Cook’s distance for LS regression evaluates the influence each attribute has on the prediction

ϵ ∼ 𝒩 ϵ ∼ 𝒩

L1 - Lasso

L2 - Ridge

min w

R R ( w , 𝒟) = min w

L reg.^ ⏟ term

  • ( y − Xw )′( y − Xw )

λ

p

∑ j = 1

| βj |

λ

p

∑ j = 1

β j^2

Confidence interval of predictions (only LS prediction)

F-value

- If the p-value ≥ 0.05, F-statistic: then there

is no relationship between ANY of the independent variables and Y

- If the p-value < 0.05 F-statistic: then, AT

LEAST 1 independent variable is related to Y

F =

R SSreg / n SS E / m − n − 1

Analysis of variance The model should explain all the target’s variance and not leave any explanatory capability to the residuals SSE : sum squared of residuals RSSreg : sum squared of (variance of prediction) RSStot : sum squared of (variance of real target) F - value : proportion between variance explained by model and variance of residuals

y ̂− μ ¯ y y − μ ¯ y

y ̂±^ tα / 2 σ ¯

m

x − μ ¯ x ∑ m i = 1 ( xi − μ ¯ x )^2

linear correlation of variable j with all other variables

variance inflation factor

R (^) j^2

V I Fj =

1 − R j^2

Multicollinearity In multiple regression, the variables should not be linearly correlated VIF identifies multiple linear relationships among independent variables ( identifies only binary) VIF > 5 : multi collinearity

r

Clustering - unsupervised learning

- can be used in outliers detection

Classification I

  1. Partition methods
  2. Hierarchical methods
  3. Density based methods
  4. Grid methods Classification II
  5. Exclusive : cluster (every obs to one cluster)
  6. Fuzzy : weight [0,1] for each cluster
  7. Complete : every obs to one or more clusters
  8. Partial : some obs to no cluster

∀ xi ∈! ∀ xi , wij

how much of the data variance has been explained by the model

adjusted R (penalizes addition of variables not correlated to target)

R^2 =

R SSreg R SStot R a^2 dj = 1 − ( 1 − R^2 )

m − 1 m − n − 1

Identification of homogenous groups of records

- as similar as possible within cluster

- as different as possible between clusters

most methods are heuristic in nature: generate a subdivision into clusters of good quality, although not necessarily optimal,

Standardization strongly affect all clustering algorithms

∀ K ≥ 3

Affinity measures Clustering is based on the similarity/distance between observations a. Binary attributes a contingency table is built : Jaccard coefficient for non symmetric attributes, distance for symmetric b. Nominal attributes create dummies and treat as binary c. Numerical attributes use one of the distances standardization strongly affects the algorithm Mahalanobis depends on the relationship between variables (covariance matrix) d. Ordinal attributes non numerical attributes that can be placed on a natural ordinal scale then standardized and distance as for numerical attributes

Partition methods

- k predetermined

- Biased towards globular clusters

- Usually exhaustive and mutually exclusive

distance matrix

similarity

Jaccard coeff.

Euclidian

Manhattan

Minkowski

D

dij = dist ( x i , x k ) = dist ( x k , x i ) with i , k ∈ ℳ

sij = ( dij )−^1

dist( x i , x j ) =

q + u n

dist( x i , x j ) =

q + u q + u + v

dist( x i , x j ) =

n ∑ i = 1

( xij − xik )^2

dist( x i , x j ) =

n ∑ i = 1

| xij − xik |

dist( x i , x j ) = p^

n ∑ i = 1

| xij − xik | p

dist( x i , x j ) = ( x i − x k ) V − ik^1 ( x i − x k ) T

δijk = 0 if , :

  1. xij , xk j is missing
  2. a j is binary, symmetric, and xij = xk j = 0 1 all other cases

Δ ijk =

a j binary or nominal :

0 if xij = xik 1 otherwise

a j numerical →

| xij − xjk | max l xlk

dist =

∑ N j = 1 δjik Δ jik

∑ N j = 1 δjik

K-medoids variant of the k-means method, with purpose of mitigating the sensitivity of partitions containing extreme values of , since the mean is sensitive to outliers (i) random obs chosen as medoids, (ii) each obs assigned to the most similar medoid, (iii) replacement of each medoid with a non medoid ( replacement evaluation ) To assess the exchange of each medoid: overall contribution

Tih = (^) ∑ x k ∉ U

Rihk

Tih < 0 pair exchanged Tih > pair not exchanged ⇒ stop

K-means chosen k centroids (ii) each obs to the most similar centroid, (iii) barycenter of clusters computed = new centroid (iv) each obs reassigned to the closest centroid COG of the centroid changes at each iteration, when it doesn’t change anymore it means it has reached a stable point stop

- very sensitive to outliers

- Often terminates at a local minimum

- Does not work well when data is (i)

unbalanced, (ii) varies in density, (iii) non convex shapes

min w 1 ,…, w k

EQ =

K ∑ u = 1

∑ (dist( xi ,^ w h ))

2

Given a dataset of m observations, each representer by a vector in n- dimensional space, partition methods construct a subdivision of into a collection of non- empty subsets

𝒞 = { C 1 , C 2 , …, CK }, K ≤ m

Hierarchical methods

- based on tree structure

- Uses distance between clusters as merging/splitting

criteria

- Does not require k as input

The chosen distance affect the clustering Minimum distance = single linkage Maximum distance = complete linkage Mean distance Ward distance : minimum variance method computes all distances and merges clusters which lead to the cluster with minimum variance among the possible ones

Agglomerative methods - bottom up Each obs is a cluster in the beginning Two clusters with minimum distance are merged at each iteration (level) Everything ends up in a cluster Cut dendrogram the required level

Divisive methods - top down All obs in the same cluster Cannot compute all possible splits, so the two furthest away are identified, and each obs assigned to the closest

strongly affected by outliers (need to be removed before)

Evaluation of clustering How many clusters? Those that explain a sufficient variance of the data Cohesion : how similar obs in the same clusters are Separation : how different obs in different clusters are Silhouette : combination of how well an attribute is cohesive with other obs in its cluster and separated from obs of other clusters

Identification of recurrences and patterns within a large set of transactions

Empirical frequency

Association rules Rule: implication of the type ( if Y is true, then Z is true ) Probabilistic rule : implication of the type ( if Y is true, then Z is true with probability p)

Y ⇒ Z

Y

p X

based on distance between clusters , not single points

dist( Ch , Cf ) = min x i ∈ Ch , x k ∈ Cf

dist( x i , x k )

dist( Ch , Cf ) = max x i ∈ Ch , x k ∈ Cf

dist( x i , x k )

dist( Ch , Cf ) =

∑ x i ∈ Ch

∑ x k ∈ Cf

dist( x i , x k )

card{ Ch }card{ Cf }

coes( Ch ) = (^) ∑ x k ∈ Ch , x i ∈ Ch

dist( x i , x k )

sep( Ch ) = (^) ∑ xk ∈ ch , xi ∈ Cf

dist( x i , x k )

silh( x i ) =

vi − ui max( vi , ui )

1 ideal 0 overlapping clusters − 1 worst case

f ( L ) = card{ Ti : L ⊆ Ti , i = 1,2,…, m }

p = conf{ L ⇒ H } =

f ( L ∪ H ) f ( L )

s = supp{ L ⇒ H } =

f ( L ∪ H ) m l = lift{ L ⇒ H } =

conf{ L ⇒ H } f ( H )

=

f ( L ∪ H ) f ( L ) f ( H )

Association rule: probabilistic implication with the meaning ‘ if L is contained in T, then also H is contained in T with probability p ’ strong association rule

only strong rules are considered when analyzing a dataset to only extract strong rules, we use

L ⇒ H :

s > s min p > p min