Docsity

Machine Learning for Networking, Appunti di Machine learning

Anteprima parziale del testo

Scarica Machine Learning for Networking e più Appunti in PDF di Machine learning solo su Docsity!

Machine Learning for Networking

Pietro Armenante

I Semester a.a. 2023/

Contents

1 Introduction

Machine Learning is described as ”design methodologies to extract patterns from data, ideally without much domain-specific expertise”.

1.1 Big Data

Big Data is described as ”data whose scale, diversity and complexity require new architectures, techniques, algorithms and analytics to manage it and extract value and hidden knowledge from it”. The 5Vs of Big Data:

  • volume, data volume increses exponentially over time;
  • velocity, fast data generation rate (streaming data);
  • variety, various formats, types and structures;
  • veracity, data quality compromised by misrepresented data, software bugs, human error, etc.;
  • value, translate data into business advantage, very useful in decision making.

Big data challenges:

  • Technology and infrastructure, new architectures, programming paradigms and techniques, transfer the processing power to the data (MapReduce), Apache Hadoop/Spark ecosystem
  • Data management and analysis, new emphasys on “data”.

1.2 Data Science

Data Science is described as “extracting meaning from large quantities of data”. The data science process begin in the real world, observing raw data, processing and cleaning it, then apply ML/stats to have a model that can interact with the real world. And so on with this circle. Data generation can be passive (structured data), active (semistructured or unstrunctured data) or automatic production (sensor-based devices).

1.3 Artificial Intelligence (AI)

“AI is machines mimicking human intelligence to perform tasks”. AI is like electricity, it fuels our planet. Intelligence involves learning, AI involves ML.

1.4 Machine Learning

“Algorithms that enables computers to learn and make decisions without being explicitly programmed”. ML is about fitting models to data to make predictions or forecasts. There are mainly 2 kind of learning:

  • supervised learning (classification/regressions), which predict classes or value. For example it can recognise that kind of object you are showing it (classification) or it can predict possible future values for something (regression).
  • Unsupervised learning (clustering), which detect groups of similar data object, identifying exceptions and outliers. For example, it can understand in which family the animal you are showing it comes from.

Anyway, there are still some open issues like social impact like interpretability and trasparency of the analysis process or bias in algorithms and data (algorithm can learn also prejudices and biases from data, leading to unfair outcomes) or privacy preservation. Then there are also many technical issues like scalability, data dimensionality, complex data structures, data quality, streaming data.

2 Data Exploration Visualization and Correlation Analisys

2.1 Data Exploration Visualization

A dataset is a collection of data, a tabular representation of data that includes rows (1, ..., m) and columns (1, ..., n), where rows correspond to objects, records, point, case, sample, entity, or instance, while columns are the attributes. The size of the dataset has an impact on the choice of the analyses. Some algorithms require considerable hardware resources when applied to large datasets, in some cases it is not possible to execute them at all. There are solutions to reduce the size of the dataset preserving the completeness of the data like the data sampling can reduce the dataset size in terms of number of rows and the feature selection can reduce the number of attributes. Each column of the dataset represents one attribute/feature, data exploration can be performed in a univariate (one single columns) or multivariate (more columns) fashions. For further analysis consider the following basic information for each attribute: unit of measurement, attribute type (categorical, not numerical or fixed number of possible values, or numerical, discrete or continuous) and attribute domain (it is a good practice to verify if the attribute values satisfy the domain-driven constraints). Let’s consider a random variable X, a variable that can take on different values, each with a certain probability, in a random or uncertain manner. This variable is a theoretical distribution that can be characterized by many numbers like the mean value, the standard deviation, and so on. Consider a Gaussian random variable X ∼ N(0, 1). This variable is characterized by:

  • the probability density function PDF, which is in general P(x ∈ S) = fX (x) =

R +∞

−∞ fX^ (x)dx, but for the normal Gaussian is fX (x) = √^12 π e−^

x 22 , being an integer, the probability is the area between the curve and the limits of the integer (for a non-normal Gaussian (X ∼ N (μ, σ) the PDF is fX (x) = (^) σ√^12 π e−^

(x−μ)^2 2 σ^2 );

  • the minimum variable, which in general is the expected value E[X] = μ =

R +∞

−∞ xfX^ (x)dx, but for the Gaussian is −∞;

  • the maximum variable, which is in general σ = E[(X − E[X])^2 ], but for the Gaussian is +∞;
  • the mean value, which is 0;
  • the standard deviation, which is 1;
  • or also other mathematical functions like the cumulative density distribution, the Skewness, the Kurtosis and so on.

But in the reality of data, we don’t have these kind of distributions and formula ready to describe the data we are working on. We just have data. This data represent an empirical distribution, the distribution function associated with the empirical measure of a sample. Obviously, also in this case we can characterize the distribution with the value we talked about before, where:

μ =

m

X^ m

i=

xi (1)

σ =

v u u t 1 m − 1

X^ m

i=

(xi − μ)^2 (2)

2.1.2 Cumulative Distribution Function

A cumulative distribution function (CDF), often denoted as F(x), is used to describe the probability distribution of a random variable and provides information about the probability that the random variable takes on values less than or equal to a specific value x.

FX (x) = P (X ≤ x) (4)

P (a ≤ X ≤ b) = FX (b) − FX (a) (5)

The CDF also provides a visual representation of the shape of the distribution, probable values/intervals appear as steep or vertical sections of the CDF. Remember that from the fundamental theorem of Cal- culus, PDF is the derivate of CDF:

fX (x) =

dFX (x) dx

2.1.3 Empirical Cumulative Distribution Function

The ECDF is an estimate of the CDF that generated the points in the sample. To evaluate ECDF(x) for a particular value of x, we compute the fraction of values in the distribution less than or equal to x. The ECDF is a step function that jumps up by 1/m at each of the m data points. E.g. about 10% of pregnancies are shorter than 36 weeks, and about 90% are shorter than 41 weeks. The CDF also provides a visual representation of the shape of the distribution. Common values appear as steep or vertical sections of the CDF; in this example, the mode at 39 weeks is apparent. There are few values below 30 weeks, so the CDF in this range is flat.

2.1.4 Percentiles

Percentile indicates the value below which a given percentage of observations in a group of observations falls. In essence, percentiles divide a dataset into equal or specified intervals, making it easier to analyze and compare data. For example, the 25th percentile (also known as the first quartile) represents the value below which 25% of the data points fall, the 50th percentile (also known as the median) represents the value below which 50% of the data points fall, and the 75th percentile (the third quartile) represents the value below which 75% of the data points fall. Quartiles are a specific set of percentiles commonly used to understand the spread and central tendency of data. The first quartile (Q1) is the 25th percentile, the second quartile (Q2) is the 50th percentile

(median), and the third quartile (Q3) is the 75th percentile. These quartiles are useful for dividing the data into four equal parts, providing insight into the spread and symmetry of the distribution. Expressing data in terms of percentiles allows for a comprehensive representation of the entire distri- bution. You can see how data is distributed across different segments of the dataset. Identifying outliers is one of the valuable applications of percentiles. Outliers are data points that fall in the extreme lower or upper ends of the distribution. For instance, values in the first percentile (1st percentile) and the last percentile (100th percentile) are extreme outliers. Detecting and dealing with outliers is crucial for data cleaning and ensuring statistical robustness.

2.1.5 Boxplot

A boxplot, also known as a box-and-whisker plot, is a standardized way to visualize and summarize the distribution of a dataset. It provides a graphical representation of key statistical values, making it easy to understand the spread and central tendency of the data. Here are the key components of a boxplot and their meanings:

  • Median (Q2/50th Percentile): the median is represented by a horizontal line inside the box. It’s the middle value of the attribute, dividing the dataset into two equal halves. Half of the data points are below the median, and half are above it. If data are symmetric, this value will be in the middle of the box; if data are asymmetric, median will be closer to the upper or lower part of the box.
  • First Quartile (Q1/25th Percentile): the lower boundary of the box represents the first quartile (Q1), which is the 25th percentile.
  • Third Quartile (Q3/75th Percentile): the upper boundary of the box represents the third quartile (Q3), which is the 75th percentile.
  • Interquartile Range (IQR): the height of the box, from the first quartile (Q1) to the third quartile (Q3), represents the interquartile range (IQR) (the difference between Q1 and Q3). It provides a measure of data spread. The IQR is used to identify potential outliers.

2.2.1 Correlation

Correlation is a statistical measure that quantifies the strength and direction of the relationship be- tween two data objects or variables. It is a fundamental concept in data analysis and plays a crucial role in various aspects of data exploration, understanding data properties, and optimizing data-driven algorithms. Correlated features could be removed simplifying the next analytics steps. A correlation is a statistic intended to quantify the strength of the relationship between two variables. Possible way to compute correlations are the covariance (in order to compare two variables, they must have the same unit of measurement, alternatively, they must be normalised), Pearson (solves the problem of normalization, only detects linear correlation) and Spearman.

where x and y are columns, they are vectors. In the covariance formula we need to remove the sample

mean to understand, for example, if x is large, y is large as well (if the trend is the same).

sxy sxsy

is

the Pearson correlation coefficient. If it is -1 means that the two variables are not correlated (the first is getting bigger, the second one is getting smaller); while if it is 1 means that the two variables are correlated (the first is getting bigger, the second one is getting bigger as well). Of course, correlation can be also different from the linear one: it can be, for example, nonlinear (y = x^2 ). Anyway there are some types of nonlinear correlations that Pearson’s correlation can’t detect. The Spearman index or Spearman’s rank uses the variables rank instead of Pearson that uses the variables themselves. As rank, we define the order of the variables (the position) in a vector in order to list the sorted vector (e.g x = [3, 0, 1], the rank is R(x) = [3, 1, 2]). It assesses how well the relationship between two variables can be described using a monotonic function. In some cases the Spearman index allows to find a correlation when the Pearson index returns a value close to 0. It is defined as:

fs = ρR(x),R(y) =

cov(R(x), R(y)) σR(x)σR(y)

if all m ranks are distinct integers, it can be computed using the formula:

rs = 1 −

P

(R(xi) − R(yi))^2 m(m^2 − 1)

This is an example:

  • Given two vectors x = [3, 0, 1], y = [-1, 50, 2]
  • Their ranks are R(x) = [3, 1, 2], R(y) = [1, 3, 2]
  • R(x) - R(y) = [2, -2, 0]^2 = [4, 4, 0]
  • The sum is equal to 8
  • The coefficient will be 1 − 63 −−^88 = -

So, the trend is nonlinear. If you plot the 2 coordinates, you will see that the curve is nonlinear. Spearman’s rank is 1 when two variables are correlated by an increasing monotonic function, -1 if the function is decreasing monotonic, 0 if there isn’t a monotonic function correlation.

2.2.2 Plots

The simplest way to visually check for a relationship between two variables is a scatter plot. This plot is ok for the first visualization of data, but it is not so good for deep study: you can’t see how many points are in an area with many many points. Moreover, some scatter plot may be affected by rounding and conversion (1): a mathematical process of approximating a numerical value to a specified level of precision or a specific number of decimal places. A possible solution is to jitter the data (2), which means adding random noise to reverse the effect of rounding off. Then adding alpha parameter (transparency) to each point (3) in order to retrieve density information (darker zones correspond to higher density zones. Scatter plot can be, then, converted into hexbin plot. The hexbin plot uses hexagonal bins that are colored according to how many data points fall in it. This is similar to a 2D histogram (density representation).

Scatter Plot Percentiles, sometimes called a percentile plot or a percentile scatter plot, is a tech- nique used to visualize the relationship between two variables when one of the variables is categorized or

  • Plot the ECDF Once you have sorted the elements of the dataset. It starts calculating the proba- bility to have x ≤ −1 = 1/12, then P (x ≤ 0) = 3/12 + 1/12 = 4/12 and so on. Finally, you plot the steps.
  • Plot the boxplot In order to plot the boxplot, I need to calculate some values that are the Q1, Q2, Q3, IQR, the minimum and the maximum value. First remember to sort the dataset. - Calculate the median (Q2): If the number of data points (n) is odd, the median is the middle value in the sorted dataset. If n is even, the median is the average of the two middle values. In our case: (1 + 4.5)/2 = 2.75. - Calculate the lower quartile (Q1): For an odd number of data points (n), Q1 is the median of the lower half of the dataset (excluding Q2). For an even number of data points (n), Q1 is the median of the lower (first) n/2 data points. In our case: (0 + 0)/2 = 0. - Calculate the upper quartile (Q3): For an odd number of data points (n), Q3 is the median of the upper half of the dataset (excluding Q2). For an even number of data points (n), Q3 is the median of the upper (last) n/2 data points. In our case: (5 + 6)/2 = 5.5. - Calculate the interquartile range (IQR): IQR is the difference between Q3 and Q1 (IQR = Q - Q1). In our case: 5.5 + 0 = 5. 5 - Calculate the lower bound for potential outliers as Q1 - 1.5 * IQR (-8.25). Calculate the upper bound for potential outliers as Q3 + 1.5 * IQR (13.75).
  • Consider now y = [5, 1 , 7 , 4 , 9 , 8 , 7 , 15 , 5 , 9 , 9 , 5]. Compute Pearson and Spearman correlation co- efficient. The final results are Pearson Correlation Coefficient: 0.837 while Spearman Correlation Coefficient: 0.788. In order to do it manually, you need to follow the formula in this chapter using the mean and the deviation standard of the two dataset.

missing record can exist. Reasons for missing values are information is not collected (e.g., people decline to give their age and weight), attributes may not be applicable to all cases (e.g., annual income is not applicable to children). How can I handle missing values? Eliminate data objects or variables, estimate them (time series of temperature) or ignore them during analysis. Data set may include data objects that are duplicates, or almost duplicates of one another. Major issue happen when merging data from heterogeneous sources (different words/abbreviations for the same concept e.g., Street, St.). Data cleaning is needed.

3.4 Similarity and dissimilarity

Similarity is a numerical measure of how alike two data objects are, it is higher when objects are more alike, it often falls in the range [0,1]. Dissimilarity is a numerical measure of how different are two data objects, it is lower when objects are more alike. Often minimum is 0. Often called distance metric: but non every dissimilarity is a distant metric, it depends on its properties. Similarity and dissimilarity between two objects, x and y, with respect to a single, simple attribute.

3.4.1 Euclidean distance

d(x, y) =

v u u t

Xn

k=

(xk − yk)^2 (10)

where n is the number of dimensions (attributes) and and are, respectively, the k th attributes (compo- nents) of data objects x and y. Standardization might be applied, if scales differ.

3.4.2 Minkowski distance

d(x, y) = (

X^ n

k=

|xk − yk|r^ )

(^1) r (11)

where r is a parameter.

  • if r = 1, it is also called city block, Manhattan or L1 norm distance

d(x, y) =

X^ n

k=

|xk − yk| (12)

  • if r = 2, it is the euclidean distance
  • if r = ∞, it is also called Chebyshev, Supremum, Lmax norm, L∞ norm distance and it is defined as the maximum difference between any component of the vectors

d(x, y) = max k |xk − yk| (13)

3.4.3 Properties of a distance metric

A distance d is called metrics if satisfy the following properties for all points on the metric space:

  • the distance a point to itself is zero d(x, x) = 0 (14)
  • Positivity, the distance between two distinct points (x ̸= y) is always positive

d(x, y) > 0 (15)

  • Symmetry, the distance from x to y is always the same as the distance from y to x

d(x, y) = d(y, x) (16)

  • Triangle inequality, given a distance, it has to be less or at least equal to the sum of the other twos

d(x, z) ≤ d(x, y) + d(y, z) (17)

3.4.4 Mahalanobis distance

It measures the distance between two points with respect to a probability distribution with covariance ma- trix S. The Mahalanobis distance is thus unitless, scale invariant, and takes into account the correlations of the data set.

d(x, y; S) =

q (x − y)T^ S−^1 (x − y) (18)

where S is the sample covariance matrix, which can be calculated putting on the main diagonal the covariance calculated on the same point (the variance), and in the other parts all the possible covariance couple. That’s an example, on the first row cov(x, x)cov(x, y), on the second row cov(y, x)cov(y, y). Than, invert this matrix. If each or these axes is re-scaled to have an identity covariance matrix, then the Mahalanobis ditance corresponds to standard Euclidean distance in the transformed space. That’s the way to calculate the inverse of a matrix: Looking at the picture we see that the distance AB is smaller because their axe goes in a direction that is not the one where the majority of the points are.

  • Simple Random Sampling, there is an equal probability of selecting any particular item. It can be done without replacement, as each item is selected, it is removed from the population, or with replacement, objects are not removed from the population as they are selected for the sample, same object can be picked up more than once.
  • Stratified sampling, split the data into several partitions; then draw random samples from each partition. Taking sample from different partition helps the sampling gettinf faster.

When (feature) dimensionality increases, data becomes increasingly sparse in the space that it oc- cupies. Definitions of density and distance between points, which is critical for clustering and outlier detection, become less meaningful. This problem is called curse of dimensionality. Adding dimen- sions to a fixed number of elements, get the distance bigger between the elements. Feature subnet selection is a way to reduce dimensionality of data in two different possibilities: aggregating redundant features in one or more other attributes, or even eliminating irrelevant features useless fort the data mining task. Recursive feature elimination: assign weights to features, selects features by recursively considering smaller and smaller sets of features. First, the estimator is trained on the initial set of features and the importance of each feature is obtained, the least important features are pruned from current set of features. That procedure is recursively repeated on the pruned set until the desired number of features to select is eventually reached. It can be important to exploit feature importance of interpretable models or select automatically features detecting the most important ones (PCA, SVD).

3.5.1 Feature engineering

A feature is a numeric representation of an aspect of raw data. Feature engineering is the act of extracting features from raw data and transforming them. Formats that are suitable for the machine learning model. Feature learning: automate the choice of finding good features. According to the type of data under analysis different feature engineering techniques are needed, structured (numerical data, categorical data), unstructured (text, images, signals), mixed. Basic types of feature engineering techniques include Data transformation, Normalization, Discretization, Binarization.

3.5.2 Data transformation

Data transformation is the process of converting data from one format to another. Why do we transform data? Non numerical data is difficult to analyze if not transformed into numerical, Capture the important information in a data set much more efficiently than the original attributes, To better visualize the data (e.g. transform linear scale to logarithmic scale in audio context).

3.5.3 Discretization

Discretization is the process of converting a continuous attribute into an ordinal attribute. A potentially infinite number of values are mapped into a small number of categories. Examples of unsupervised discretization techniques:

  • N intervals with the same width, Easy to implement, as in histograms, It can be badly affected by outliers and sparse data, Incremental approach
  • N intervals with (approximately) the same cardinality (first interval small, then a little bit bigger, then bigger and so on), It better fits sparse data and outliers, Non incremental approach
  • Clustering, It fits well sparse data and outlier

3.5.4 Binarization

Binarization maps an attribute into one or more binary variables. Continuous attribute: first map the attribute to a categorical one. Categorical attribute can be mapping to a set of binary attributes or one-hot encoding. One-Hot Encoding use a group of bits, each bit represents a possible category, if the variable cannot belong to multiple categories at once, then only one bit in the group can be 1.

3.5.5 Normalization

An attribute transformation is a function that maps the entire set of values of a given attribute to a new set of replacement values such that each old value can be identified with one of the new values (some simple functions: xk, log(x), ex, |x|. Normalization or feature scaling refers to various techniques to adjust to differences among attributes in terms of mean, variance, range, etc. Min-Max Normalization bring all values into the range [0,1], is sensitive to outliers and retains the shape of the distribution, but change the intervals.

x′^ =

x − xmin xmax − xmin

Standardization, another kind of normalization, is not bounded to a certain range, it is used when we want to ensure zero mean and unit standard deviation. Gaussian is also standardized but standardization does not create a Gaussian, data remain of the same distribution but with zero mean and unit standard deviation.

x′^ =

x − μ σ