This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Generates the simulated server log | |
# | |
import csv | |
import numpy as np | |
import random | |
base = 750 | |
amplitude = 500 | |
growth = 1e-6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Sagaceco.TimeSeries.Patterns.Models | |
{ | |
public class ExponentialMovingModel | |
{ | |
private double average = 0.0; | |
private double variance = 0.0; | |
public ExponentialMovingModel() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Sagaceco.ServerLog | |
{ | |
public class WeeklyLogModel | |
{ | |
private ExponentialMovingModel[] models = new ExponentialMovingModel[2016]; | |
private double radius = 3.0; | |
public WeeklyLogModel() | |
{ | |
for(int i = 0; i < models.Length; i++) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Update(double x, double y) | |
{ | |
count++; | |
sumX += x; | |
sumY += y; | |
sumXY += x * y; | |
sumXX += x * x; | |
sumYY += y * y; | |
double meanX = sumX / count; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool IsOutlier(double radius, double x, double y) | |
{ | |
double yHat = GetValue( x ); | |
double studentizedResidue = Math.Abs(y - yHat) / Math.Sqrt( GetResidualVariance() ); | |
return studentizedResidue > radius; | |
} | |
private double GetResidualVariance() | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Parameters | |
eps_in_meters = 100.0 | |
num_samples = 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cluster the data | |
from sklearn.cluster import DBSCAN | |
earth_perimeter = 40070000.0 # In meters | |
eps_in_radians = eps_in_meters / earth_perimeter * (2 * math.pi) | |
uk_acc['cluster'] = DBSCAN(eps=eps_in_radians, min_samples=num_samples, metric='haversine').fit_predict(uk_acc[['rad_lat', 'rad_lng']]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Group the observations by cluster identifier | |
groups = uk_acc.groupby('cluster') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create the list of cluster blobs | |
from shapely.ops import cascaded_union | |
clusters = list() | |
blobs = list() | |
counts = list() | |
for cluster_id, points in groups: | |
if cluster_id >= 0: | |
buffer_radius = eps_in_meters * 0.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create the GeoDataFrame from the cluster numbers and blobs | |
data = { 'cluster': clusters, 'polygon': blobs, 'count': counts } | |
cluster_gdf = gpd.GeoDataFrame(pd.DataFrame(data), geometry='polygon') | |
cluster_gdf.crs = {'init': 'epsg:4326'} | |
ax = cluster_gdf.geometry.plot(linewidth=2.0, color='red', edgecolor='red') |
OlderNewer