Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silhouette Plot #247

Merged
merged 8 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/36.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ dependencies:
- pywget
- proplot
- contextily
- scikit-plot
1 change: 1 addition & 0 deletions ci/37.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ dependencies:
- pywget
- proplot
- contextily
- scikit-plot
1 change: 1 addition & 0 deletions ci/38.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ dependencies:
- pywget
- proplot
- contextily
- scikit-plot
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ dependencies:
- tobler >=0.2.1
- proplot
- contextily

- scikit-plot
20 changes: 20 additions & 0 deletions geosnap/_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import geopandas as gpd
import pandas as pd
import scikitplot as skplt

from ._data import _Map, datasets
from .analyze import cluster as _cluster
Expand Down Expand Up @@ -321,6 +322,25 @@ def cluster_spatial(
comm.models[model_name] = model
return comm

def silplot(self, model_name, year, **kwargs):
""" Returns a silhouette plot of the model that is passed to it.

Parameters
----------
model_name : str , required
model to be silhouette plotted
year : dict key, optional
year of model if model has model for each year
kwargs : **kwargs, optional
pass through to plot_silhouette()
Returns
-------
silhouette plot of given model.

"""
plot = skplt.metrics.plot_silhouette(self.models[model_name][year].X, self.models[model_name][year].labels, **kwargs)
return plot

def transition(
self, cluster_col, time_var="year", id_var="geoid", w_type=None, permutations=0
):
Expand Down
45 changes: 40 additions & 5 deletions geosnap/analyze/analytics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Tools for the spatial analysis of neighborhood change."""

from collections import namedtuple

import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
Expand All @@ -25,9 +23,46 @@
ward_spatial,
)

ModelResults = namedtuple(
"model", ["X", "columns", "labels", "instance", "W"], rename=False
)

class ModelResults:
"""Stores data about cluster and cluster_spatial models.

Attributes
----------
X: array-like
data used to compute model
columns: list-like
columns used in model
W: 'queen', 'rook', or spatial weights matrix
spatial weights matrix used in model
labels: array-like
labels of each column
instance: AgglomerativeCluserting object, or other model specific object type
how many clusters model was computed with

"""
def __init__(self, X, columns, labels,instance,W,):
"""Initialize a new ModelResults instance.

Parameters
----------
X: array-like
data of the cluster
columns: list-like
columns used to compute model
W: 'queen', 'rook', or pyasl spatial weights matrix
spatial weights matrix used in model
labels: array-like
labels of each column
instance: AgglomerativeCluserting object, or other model specific object type
how many clusters model was computed with

"""
self.columns = columns
self.X = X
self.W = W
self.instance = instance
self.labels = labels


def cluster(
Expand Down