Add Chicago robbery clusters

This commit is contained in:
Arkadiusz Kondas committed 2019-03-10 08:53:07 +01:00
1 parent daf43626b3
commit 7d5c98e53f
4 files changed
+80 -2

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace PhpmlExamples;
use Phpml\Clustering\KMeans;
require 'vendor/autoload.php';
$lines = file(__DIR__.'/../data/crimes-robbery.csv');
foreach ($lines as &$line) {
$row = explode(';', $line);
$line = [(float) $row[0], (float) $row[1]];
}
$clusterer = new KMeans(14);
$clusters = $clusterer->cluster($lines);
$lines = [];
foreach ($clusters as $key => $cluster) {
foreach ($cluster as $sample) {
$lines[] = sprintf('%s;%s;%s', $key, $sample[0], $sample[1]);
}
}
file_put_contents(__DIR__ . '/../data/crimes-robbery-clusters.csv', implode(PHP_EOL, $lines));
+2 -2
View File
@@ -5,7 +5,7 @@ $header = fgetcsv($handle);
$lines = [];
$index = 0;
$filter = ['ARSON'];
$filter = ['ROBBERY'];
while (($row = fgetcsv($handle)) !== false) {
if(!in_array($row[6], $filter)) {
continue;
@@ -23,5 +23,5 @@ while (($row = fgetcsv($handle)) !== false) {
}
fclose($handle);
file_put_contents(__DIR__ . '/../data/crimes-arson.csv', implode(PHP_EOL, $lines));
file_put_contents(__DIR__ . '/../data/crimes-robbery.csv', implode(PHP_EOL, $lines));
+15
View File
@@ -0,0 +1,15 @@
import csv
import random
from staticmap import StaticMap, CircleMarker
m = StaticMap(1000, 900, url_template='http://a.tile.stamen.com/toner/{z}/{x}/{y}.png')
with open('data/crimes-robbery.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=';')
for row in reader:
marker = CircleMarker((float(row[1]), float(row[0])), (232, 152, 16, 180), 6)
m.add_marker(marker)
image = m.render(center=[-87.684871189, 41.8348221989])
image.save('robbery.png')
+36
View File
@@ -0,0 +1,36 @@
import csv
import random
from staticmap import StaticMap, CircleMarker
m = StaticMap(1000, 900, url_template='http://a.tile.stamen.com/toner/{z}/{x}/{y}.png')
def cluster_to_color(cluster):
alpha = 180
return {
'0': (246, 81, 29,alpha),
'1': (255, 180, 0,alpha),
'2': (0, 166, 237,alpha),
'3': (127, 184, 0,alpha),
'4': (67, 188, 20, alpha),
'5': (102, 46, 155, alpha),
'6': (175, 127, 242, alpha),
'7': (146, 181, 29, alpha),
'8': (155, 100, 0, alpha),
'9': (100, 106, 237, alpha),
'10': (27, 84, 0, alpha),
'11': (167, 228, 20, alpha),
'12': (202, 146, 155, alpha),
'13': (75, 187, 42, alpha)
}[cluster]
with open('data/crimes-robbery-clusters.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=';')
for row in reader:
if row[0] == 'C':
marker = CircleMarker((float(row[2]), float(row[1])), (0,0,0), 15)
else:
marker = CircleMarker((float(row[2]), float(row[1])), cluster_to_color(row[0]), 6)
m.add_marker(marker)
image = m.render(center=[-87.684871189, 41.8348221989])
image.save('robbery-clusters.png')