Skip to content

Instantly share code, notes, and snippets.

View joaofig's full-sized avatar

João Paulo Figueira joaofig

View GitHub Profile
@joaofig
joaofig / GenerateServerLog.py
Last active August 26, 2016 08:49
"Server Log" simulation generator
#
# Generates the simulated server log
#
import csv
import numpy as np
import random
base = 750
amplitude = 500
growth = 1e-6
@joaofig
joaofig / ExponentialMovingModel.cs
Created August 26, 2016 09:47
ExponentialMovingModel
using System;
namespace Sagaceco.TimeSeries.Patterns.Models
{
public class ExponentialMovingModel
{
private double average = 0.0;
private double variance = 0.0;
public ExponentialMovingModel()
@joaofig
joaofig / WeeklyLogModel.cs
Created August 26, 2016 09:49
WeeklyLogModel
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++)
@joaofig
joaofig / LinearRegressionModel.Update.cs
Created September 1, 2016 08:44
Linear regression model update method
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;
@joaofig
joaofig / LinearRegressionModel.IsOutlier.cs
Created September 1, 2016 08:49
Linear regression model outlier detector
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()
{
@joaofig
joaofig / DBSCAN_parameters.py
Created August 13, 2018 13:27
Setting DBSCAN's parameters
# Parameters
eps_in_meters = 100.0
num_samples = 10
@joaofig
joaofig / run_DBSCAN_sklearn.py
Created August 19, 2018 14:41
Running DBSCAN
# 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']])
@joaofig
joaofig / goup_DBSCAN_clusters.py
Created August 19, 2018 14:46
Group DBSCAN clusters
# Group the observations by cluster identifier
groups = uk_acc.groupby('cluster')
@joaofig
joaofig / create_DBSCAN_bubbles.py
Created August 19, 2018 14:47
Create DBSCAN bubbles
# 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
@joaofig
joaofig / create_geodataframe.py
Created August 19, 2018 14:51
Create GeoDataFrame
# 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')