How can I resolve all these errors from folium? - python

I am trying to create a map following this tutorial. I am using almost the exact same code as in the tutorial, but get a series I am unsure how to begin resolving, anyone have any thoughts on how I could start resolving?
Here is my code below. I think my table merge worked okay because it appears correctly when I print the table, but I think things start going downhill with the choropleth function.
'''
import geopandas
import pandas as pd
import folium
url = 'https://en.wikipedia.org/wiki/List_of_countries_by_meat_consumption'
tables = pd.read_html(url)
table = tables[0]
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
table = world.merge(table, how='left', left_on=['name'], right_on=['Country'])
pd.options.display.width = 4000
pd.set_option('display.max_rows', 188)
table = table.dropna(subset=['kg/person (2002)[9][note 1]'])
meat_map = folium.Map()
folium.Choropleth(
geo_data=table,
name='choropleth',
data=table,
columns=['Country', 'kg/person (2002)[9][note 1]'],
key_on ='feature.properties.name',
fill_color ='YlGnBu',
fill_opacity = 0.7,
line_opacity = 0.2,
legend_name = "Meat consumption in kg/person"
).add_to(meat_map)
here is my error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-b9be599a18fa> in <module>
10 meat_map = folium.Map()
11
---> 12 folium.Choropleth(
13 geo_data=table,
14 name='choropleth',
~\anaconda3\envs\geo_env\lib\site-packages\folium\features.py in __init__(self, geo_data, data, columns, key_on, bins, fill_color, nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, line_opacity, name, legend_name, overlay, control, show, topojson, smooth_factor, highlight, **kwargs)
1287 smooth_factor=smooth_factor)
1288 else:
-> 1289 self.geojson = GeoJson(
1290 geo_data,
1291 style_function=style_function,
~\anaconda3\envs\geo_env\lib\site-packages\folium\features.py in __init__(self, data, style_function, highlight_function, name, overlay, control, show, smooth_factor, tooltip, embed, popup, zoom_on_click, marker)
497 self.marker = marker
498
--> 499 self.data = self.process_data(data)
500
501 if self.style or self.highlight:
~\anaconda3\envs\geo_env\lib\site-packages\folium\features.py in process_data(self, data)
540 if hasattr(data, 'to_crs'):
541 data = data.to_crs('EPSG:4326')
--> 542 return json.loads(json.dumps(data.__geo_interface__))
543 else:
544 raise ValueError('Cannot render objects with any missing geometries'
~\anaconda3\envs\geo_env\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
229 cls is None and indent is None and separators is None and
230 default is None and not sort_keys and not kw):
--> 231 return _default_encoder.encode(obj)
232 if cls is None:
233 cls = JSONEncoder
~\anaconda3\envs\geo_env\lib\json\encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)
~\anaconda3\envs\geo_env\lib\json\encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
~\anaconda3\envs\geo_env\lib\json\encoder.py in default(self, o)
177
178 """
--> 179 raise TypeError(f'Object of type {o.__class__.__name__} '
180 f'is not JSON serializable')
181
TypeError: Object of type MultiPolygon is not JSON serializable
'''

Related

sklearn - AttributeError: 'CustomScaler' object has no attribute 'copy'

When i write my custom scaler to scale my data without scaling dummies i've already created, i get an error regarding custom scaler not having copy?
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import StandardScaler
class CustomScaler(BaseEstimator,TransformerMixin):
def __init__(self,columns,copy=True,with_mean=True,with_std=True):
self.scaler = StandardScaler(copy,with_mean,with_std)
self.columns = columns
self.mean_ = None
self.var_ = None
def fit(self, X, y=None):
self.scaler.fit(X[self.columns], y)
self.mean_ = np.mean(X[self.columns])
self.var_ = np.var(X[self.columns])
return self
def transform(self, X, y=None, copy=True):
init_col_order = X.columns
X_scaled = pd.DataFrame(self.scaler.transform(X[self.columns]),
columns=self.columns)
X_not_scaled = X.loc[:,~X.columns.isin(self.columns)]
return pd.concat([X_not_scaled, X_scaled], axis=1)[init_col_order]
-> These are my unscaled columns
unscaled_inputs.columns.values
array(['Reason_1', 'Reason_2', 'Reason_3', 'Reason_4', 'Month',
'Day of the week', 'Transportation Expense', 'Distance to Work',
'Age', 'Daily Work Load Average', 'Body Mass Index', 'Education',
'Children', 'Pets'], dtype=object)
-> dummy variables i don't want to scale
columns_to_omit = ['Reason_1', 'Reason_2', 'Reason_3', 'Reason_4','Education']
-> variables i want to scale
columns_to_scale = [x for x in unscaled_inputs.columns.values if x not in columns_to_omit]
-> Giving input data "columns_to_scale" to my "CustomScaler"
absenteeism_scaler = CustomScaler(columns_to_scale)
-> i get this warning
C:\Users\prati\Anaconda3\lib\site-packages\sklearn\utils\validation.py:70:
FutureWarning: Pass copy=True, with_mean=True, with_std=True as keyword args. From
version 1.0 (renaming of 0.25) passing these as positional arguments will result in an
error
warnings.warn(f"Pass {args_msg} as keyword args. From version "
-> When i fit my "absenteeism_scaler" to my "unscaled_inputs", i get an error, but the error doesn't stop my code from executing further
absenteeism_scaler.fit(unscaled_inputs)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj, include,
exclude)
968
969 if method is not None:
--> 970 return method(include=include, exclude=exclude)
971 return None
972 else:
~\Anaconda3\lib\site-packages\sklearn\base.py in _repr_mimebundle_(self, **kwargs)
462 def _repr_mimebundle_(self, **kwargs):
463 """Mime bundle used by jupyter kernels to display estimator"""
--> 464 output = {"text/plain": repr(self)}
465 if get_config()["display"] == 'diagram':
466 output["text/html"] = estimator_html_repr(self)
~\Anaconda3\lib\site-packages\sklearn\base.py in __repr__(self, N_CHAR_MAX)
258 n_max_elements_to_show=N_MAX_ELEMENTS_TO_SHOW)
259
--> 260 repr_ = pp.pformat(self)
261
262 # Use bruteforce ellipsis when there are a lot of non-blank characters
~\Anaconda3\lib\pprint.py in pformat(self, object)
151 def pformat(self, object):
152 sio = _StringIO()
--> 153 self._format(object, sio, 0, 0, {}, 0)
154 return sio.getvalue()
155
~\Anaconda3\lib\pprint.py in _format(self, object, stream, indent, allowance, context,
level)
168 self._readable = False
169 return
--> 170 rep = self._repr(object, context, level)
171 max_width = self._width - indent - allowance
172 if len(rep) > max_width:
~\Anaconda3\lib\pprint.py in _repr(self, object, context, level)
402
403 def _repr(self, object, context, level):
--> 404 repr, readable, recursive = self.format(object, context.copy(),
405 self._depth, level)
406 if not readable:
~\Anaconda3\lib\site-packages\sklearn\utils\_pprint.py in format(self, object, context,
maxlevels, level)
178
179 def format(self, object, context, maxlevels, level):
--> 180 return _safe_repr(object, context, maxlevels, level,
181 changed_only=self._changed_only)
182
~\Anaconda3\lib\site-packages\sklearn\utils\_pprint.py in _safe_repr(object, context,
maxlevels, level, changed_only)
423 recursive = False
424 if changed_only:
--> 425 params = _changed_params(object)
426 else:
427 params = object.get_params(deep=False)
~\Anaconda3\lib\site-packages\sklearn\utils\_pprint.py in _changed_params(estimator)
89 estimator with non-default values."""
90
---> 91 params = estimator.get_params(deep=False)
92 init_func = getattr(estimator.__init__, 'deprecated_original',
93 estimator.__init__)
~\Anaconda3\lib\site-packages\sklearn\base.py in get_params(self, deep)
193 out = dict()
194 for key in self._get_param_names():
--> 195 value = getattr(self, key)
196 if deep and hasattr(value, 'get_params'):
197 deep_items = value.get_params().items()
AttributeError: 'CustomScaler' object has no attribute 'copy'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
700 type_pprinters=self.type_printers,
701 deferred_pprinters=self.deferred_printers)
--> 702 printer.pretty(obj)
703 printer.flush()
704 return stream.getvalue()
~\Anaconda3\lib\site-packages\IPython\lib\pretty.py in pretty(self, obj)
392 if cls is not object \
393 and callable(cls.__dict__.get('__repr__')):
--> 394 return _repr_pprint(obj, self, cycle)
395
396 return _default_pprint(obj, self, cycle)
~\Anaconda3\lib\site-packages\IPython\lib\pretty.py in _repr_pprint(obj, p, cycle)
698 """A pprint that just redirects to the normal repr function."""
699 # Find newlines and replace them with p.break_()
--> 700 output = repr(obj)
701 lines = output.splitlines()
702 with p.group():
~\Anaconda3\lib\site-packages\sklearn\base.py in __repr__(self, N_CHAR_MAX)
258 n_max_elements_to_show=N_MAX_ELEMENTS_TO_SHOW)
259
--> 260 repr_ = pp.pformat(self)
261
262 # Use bruteforce ellipsis when there are a lot of non-blank characters
~\Anaconda3\lib\pprint.py in pformat(self, object)
151 def pformat(self, object):
152 sio = _StringIO()
--> 153 self._format(object, sio, 0, 0, {}, 0)
154 return sio.getvalue()
155
~\Anaconda3\lib\pprint.py in _format(self, object, stream, indent, allowance, context,
level)
168 self._readable = False
169 return
--> 170 rep = self._repr(object, context, level)
171 max_width = self._width - indent - allowance
172 if len(rep) > max_width:
~\Anaconda3\lib\pprint.py in _repr(self, object, context, level)
402
403 def _repr(self, object, context, level):
--> 404 repr, readable, recursive = self.format(object, context.copy(),
405 self._depth, level)
406 if not readable:
~\Anaconda3\lib\site-packages\sklearn\utils\_pprint.py in format(self, object, context,
maxlevels, level)
178
179 def format(self, object, context, maxlevels, level):
--> 180 return _safe_repr(object, context, maxlevels, level,
181 changed_only=self._changed_only)
182
~\Anaconda3\lib\site-packages\sklearn\utils\_pprint.py in _safe_repr(object, context,
maxlevels, level, changed_only)
423 recursive = False
424 if changed_only:
--> 425 params = _changed_params(object)
426 else:
427 params = object.get_params(deep=False)
~\Anaconda3\lib\site-packages\sklearn\utils\_pprint.py in _changed_params(estimator)
89 estimator with non-default values."""
90
---> 91 params = estimator.get_params(deep=False)
92 init_func = getattr(estimator.__init__, 'deprecated_original',
93 estimator.__init__)
~\Anaconda3\lib\site-packages\sklearn\base.py in get_params(self, deep)
193 out = dict()
194 for key in self._get_param_names():
--> 195 value = getattr(self, key)
196 if deep and hasattr(value, 'get_params'):
197 deep_items = value.get_params().items()
AttributeError: 'CustomScaler' object has no attribute 'copy'
-> it still gives me output, but i don't understand, that if i get an error the code should stops executing?, but it runs afterwards,
P.S - this is just the left half of the data, as i can't fit all the data here, sry for that....
scaled_inputs = absenteeism_scaler.transform(unscaled_inputs)
scaled_inputs
Reason_1 Reason_2 Reason_3 Reason_4 Month Day of the week Transportation
Expense
0 0 0 0 1 0.182726 -0.683704 1.005844
1 0 0 0 0 0.182726 -0.683704 -1.574681
2 0 0 0 1 0.182726 -0.007725 -0.654143
3 1 0 0 0 0.182726 0.668253 0.854936
4 0 0 0 1 0.182726 0.668253 1.005844
Update 1: as i removed
copy=True,with_mean=True,with_std=True from init, my error got resolved, but i think the scaled data will be an "inplace" change & the mean and S.D thing, and so if i don't want to make inplace changes to my data what can i do???
Update 2: is it because copy, with_mean, and with_std are "True" by default?? just check the StandardScaler library on sklearn
def _init__(self,columns):
self.scaler = StandardScaler()
self.columns = columns
self.mean = None
self.var_ = None
Positional/keyword warning/error
The first warning (an error for sklearn versions >1.0) is because you're setting keyword arguments of the StandardScaler as positional arguments here:
def __init__(...):
self.scaler = StandardScaler(copy,with_mean,with_std)
...
You should change it to use the keywords,
def __init__(...):
self.scaler = StandardScaler(copy=copy, with_mean=with_mean, with_std=with_std)
...
No attribute copy error
This is only being thrown when trying to display the html representation of the transformer, after fitting has already succeeded, which is why you're able to continue and transform successfully.
But the issue is more serious than that if you want to make use of the transformer in pipelines, grid searches, etc. In order to clone properly, you need to follow the specific guidance of the sklearn API, or else provide your own get_params and set_params. The __init__ method should set an attribute for every parameter, and only those attributes. So here it should be
def __init__(self, columns, copy=True, with_mean=True, with_std=True):
self.columns = columns
self.copy = copy
self.with_mean = with_mean
self.with_std = with_std
And then make the contained StandardScaler at fit time:
def fit(self, X, y=None):
self.scaler = StandardScaler(copy=self.copy, with_mean=self.with_mean, with_std=self.with_std)
self.scaler.fit(X[self.columns], y)
self.mean_ = np.mean(X[self.columns])
self.var_ = np.var(X[self.columns])
return self

What is causing a circular reference when plotting a map using ArcGIS API for Python?

Following the documentation for the ArcGIS API for Python, I'm attempting to plot coordinates from a spatially enabled dataframe. Perhaps my understanding of a circular reference is off, but I can't see how the object would be referencing itself.
import pandas as pd
from arcgis.gis import GIS
## importing geocoded data from pickle
prop_sdf = pd.read_pickle("./recent_geocoded.pkl")
prop_sdf = pd.DataFrame.spatial.from_xy(prop_sdf, 'lat','long')
prop_sdf.head()
This is what my resulting dataframe looks like:
unique_id lat long
0 43432884 41.701011 -70.019244
2 43400770 41.641784 -70.366659
3 43425636 41.701954 -70.146602
4 43427274 41.720506 -70.021849
5 43427818 41.649288 -70.490767
I'm able to render the interactive map
m1 = GIS().map("United States")
m1.zoom = 4
m1.center = [39,-98]
But hit an error when plotting:
prop_sdf.spatial.plot(map_widget=m1)
m1
Trace:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-30-d48a9f223ff4> in <module>
----> 1 prop_sdf.spatial.plot(map_widget=m1)
2 m1
~\anaconda3\lib\site-packages\arcgis\features\geo\_accessor.py in plot(self, map_widget, **kwargs)
2002 self._data.columns = [c.replace(" ", "_") for c in self._data.columns]
2003 # plot and be merry
-> 2004 _plot_map_widget(map_widget)
2005 self._data.columns = orig_col
2006 return True
~\anaconda3\lib\site-packages\arcgis\features\geo\_accessor.py in _plot_map_widget(mp_wdgt)
1984 colors=kwargs.pop('cmap', None) or kwargs.pop('colors', None) or kwargs.pop('pallette', 'jet'),
1985 alpha=kwargs.pop('alpha', 1),
-> 1986 **kwargs)
1987
1988 # small helper to address zoom level
~\anaconda3\lib\site-packages\arcgis\features\geo\_viz\mapping.py in plot(df, map_widget, name, renderer_type, symbol_type, symbol_style, col, colors, alpha, **kwargs)
191 fc.layer['layerDefinition']['drawingInfo']['renderer'] = renderer
192 if map_exists:
--> 193 map_widget.add_layer(fc, options={'title':name})
194 else:
195 map_widget.add_layer(fc, options={'title':name})
~\anaconda3\lib\site-packages\arcgis\widgets\_mapview\_mapview.py in add_layer(self, item, options)
1051 _is_geoenabled(item):
1052 item = item.spatial.to_feature_collection()
-> 1053 self._add_layer_to_widget(item, options)
1054
1055 def _add_layer_to_webmap(self, item, options):
~\anaconda3\lib\site-packages\arcgis\widgets\_mapview\_mapview.py in _add_layer_to_widget(self, item, options)
1091 # (i.e., do what was done for ImageryLayer for all major Layers)
1092 # 'No type' layer just means that we'll figure it out at JS time
-> 1093 _lyr = _make_jsonable_dict(item._lyr_json)
1094 if ('type' in _lyr and \
1095 _lyr['type'] == 'MapImageLayer') and \
~\anaconda3\lib\site-packages\arcgis\widgets\_mapview\_mapview.py in _make_jsonable_dict(obj)
60 will later be used to delete all values with this value"""
61 return flag
---> 62 dict_ = json.loads(json.dumps(obj, default=default_func))
63 return { k:v for k, v in dict_.items() if v != flag }
64
~\anaconda3\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
236 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
237 separators=separators, default=default, sort_keys=sort_keys,
--> 238 **kw).encode(obj)
239
240
~\anaconda3\lib\json\encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)
~\anaconda3\lib\json\encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
ValueError: Circular reference detected

Making choropleth map from dataframe using folium

I'm trying to make a choropleth map using pandas to read csv data. I followed a tutorial online, but didn't get my code to work.
Tutorial link: https://towardsdatascience.com/choropleth-maps-with-folium-1a5b8bcdd392. Here is the code i'm trying to get to work:
# CHOLOPLETH MAP
import json
kunnat_geo = r'kunnat.geojson'
coords = pd.read_csv('taulu2.csv')
map_cholo = folium.Map(location=[65,26], zoom_start=4, tiles='stamenwatercolor')
map_cholo.choropleth(
geo_data=kunnat_geo,
data=coords,
columns=['ALUE', 'MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018'],
key_on='features.properties.Kunta',
fill_color='YlGnBu',
fill_opacity=1,
line_opacity=1,
legend_name='MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018',
smooth_factor=0)
map_cholo
However it gives me an attribute error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-74-026c8c7bfd66> in <module>
14 line_opacity=1,
15 legend_name='MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018',
---> 16 smooth_factor=0)
17
18 map_cholo
D:\Anaconda3\lib\site-packages\folium\folium.py in choropleth(self, *args, **kwargs)
416 )
417 from folium.features import Choropleth
--> 418 self.add_child(Choropleth(*args, **kwargs))
419
420 def keep_in_front(self, *args):
D:\Anaconda3\lib\site-packages\folium\features.py in __init__(self, geo_data, data, columns, key_on, bins, fill_color, nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, line_opacity, name, legend_name, overlay, control, show, topojson, smooth_factor, highlight, **kwargs)
1249 style_function=style_function,
1250 smooth_factor=smooth_factor,
-> 1251 highlight_function=highlight_function if highlight else None)
1252
1253 self.add_child(self.geojson)
D:\Anaconda3\lib\site-packages\folium\features.py in __init__(self, data, style_function, highlight_function, name, overlay, control, show, smooth_factor, tooltip, embed, popup)
456 self.convert_to_feature_collection()
457 if self.style:
--> 458 self._validate_function(style_function, 'style_function')
459 self.style_function = style_function
460 self.style_map = {}
D:\Anaconda3\lib\site-packages\folium\features.py in _validate_function(self, func, name)
521 """
522 test_feature = self.data['features'][0]
--> 523 if not callable(func) or not isinstance(func(test_feature), dict):
524 raise ValueError('{} should be a function that accepts items from '
525 'data[\'features\'] and returns a dictionary.'
D:\Anaconda3\lib\site-packages\folium\features.py in style_function(x)
1223
1224 def style_function(x):
-> 1225 color, opacity = color_scale_fun(x)
1226 return {
1227 'weight': line_weight,
D:\Anaconda3\lib\site-packages\folium\features.py in color_scale_fun(x)
1204
1205 def color_scale_fun(x):
-> 1206 key_of_x = get_by_key(x, key_on)
1207 if key_of_x is None:
1208 raise ValueError("key_on `{!r}` not found in GeoJSON.".format(key_on))
D:\Anaconda3\lib\site-packages\folium\features.py in get_by_key(obj, key)
1201 return (obj.get(key, None) if len(key.split('.')) <= 1 else
1202 get_by_key(obj.get(key.split('.')[0], None),
-> 1203 '.'.join(key.split('.')[1:])))
1204
1205 def color_scale_fun(x):
D:\Anaconda3\lib\site-packages\folium\features.py in get_by_key(obj, key)
1200 def get_by_key(obj, key):
1201 return (obj.get(key, None) if len(key.split('.')) <= 1 else
-> 1202 get_by_key(obj.get(key.split('.')[0], None),
1203 '.'.join(key.split('.')[1:])))
1204
AttributeError: 'NoneType' object has no attribute 'get'
Here is the geojson file im using: https://raw.githubusercontent.com/varmais/maakunnat/master/kunnat.geojson
And the data: https://pastebin.com/SdEXDM89
The geojson you are using contains multiple flaws. Just an example, look at
{"id":49,"type":"Feature","geometry":{"coordinates":[[[[25.134789950981,65.0613390595066],[24.9509796014637,65.1411214235741],[24.1149334366251,65.1739425977789],[24.1374948169974,65.2416484128856],[24.6071512739822,65.211492673253],[25.2249108920834,65.2366422852267],[25.319454962569,65.2626286706296],[25.5524286749097,65.2529182323504],[25.6623616596278,65.260748036272],[25.7723806903971,65.2919894627541],[25.9132907485861,65.2931966157892],[26.0266782370841,65.2703735338041],[25.993210256336,65.1914324603082],[25.736352838218,65.2201254797071],[25.5637124399186,65.1919505955019],[25.635197316715,65.1390540421563],[25.6086182968484,65.0816350751541],[25.3775958184099,65.1070832092577],[25.2892903033333,65.0817828240486],[25.134789950981,65.0613390595066]]]],"type":"MultiPolygon"},"properties":{"nationalCode":84,"Country":"FI","name_fi":"Haukipudas","name_se":"Haukipudas"}}
It does not have Kunta in properties. This makes your geojson bad-formed. I suggest you to replace that geojson, or edit it as the very last resort.

Can someone explain this: TypeError: MinMaxScaler(copy=True, feature_range=(0, 1)) is not JSON serializable

I'm getting the error
TypeError: MinMaxScaler(copy=True, feature_range=(0, 1)) is not JSON
serializable
I've googled it but no results relating to the error. I must be the first in history to get it 🤣
I'm trying to scale the data from an ad campaign results and plot it on a plotly scatter graph.
I can plot before I scale but not after
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
#from IPython.display import display
from mpl_toolkits.mplot3d import Axes3D
%matplotlib inline
import mpld3
mpld3.enable_notebook()
pd.get_option("display.max_columns")
data = pd.read_csv("12776828-Ad-Sets-Lifetime.csv", header=0)
data = data[["Reporting starts","Results","Reach","Frequency","Cost per results","Amount spent (GBP)","Clicks (all)","CTR (all)","CPC (all) (GBP)","CPM (cost per 1,000 impressions) (GBP)","Link clicks","CPC (cost per link click) (GBP)","CTR (link click-through rate)","Website purchases","Button clicks"]]
filldate = pd.date_range('04-21-2017', '06-05-2017')
data = data.fillna(value=0)
pur0 = pur.reshape(1,-1)
lc0 = lc.reshape(1,-1)
spent0 = spent.reshape(1,-1)
from sklearn import preprocessing, svm
pur1 = preprocessing.StandardScaler().fit_transform(pur0)
lc1 = preprocessing.StandardScaler().fit_transform(lc0)
spent1 = preprocessing.StandardScaler().fit_transform(spent0)
import plotly #load plotly for plotting
import plotly.plotly as py
from plotly import tools
from plotly.graph_objs import * #all the types of plots that we will plot here
plotly.offline.init_notebook_mode() # run at the start of every ipython notebook
# Create a trace
Purch = Scatter(
y = pur1,
name="Purchases",
mode='markers',
marker=dict(
size=12,
color="green"
)
)
Clicks = Scatter(
#x = pur,
x = lc1,
name="Links Clicked",
mode='markers',
marker=dict(
size=12,
color="red"
)
)
Spend = Scatter(
#x = pur,
x = spent1,
name="Spent",
mode='markers',
marker=dict(
size=12,
color="blue"
)
)
data = [Spend, Purch]
# Plot and embed in ipython notebook!
plotly.offline.iplot(data, filename='basic-scatter')
Returns the error
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-41-c34375d51cd8> in <module>()
42
43 # Plot and embed in ipython notebook!
---> 44 plotly.offline.iplot(data, filename='basic-scatter')
C:\Users\nickd\Anaconda3\lib\site-packages\plotly\offline\offline.py in iplot(figure_or_data, show_link, link_text, validate, image, filename, image_width, image_height, config)
340
341 plot_html, plotdivid, width, height = _plot_html(
--> 342 figure_or_data, config, validate, '100%', 525, True
343 )
344
C:\Users\nickd\Anaconda3\lib\site-packages\plotly\offline\offline.py in _plot_html(figure_or_data, config, validate, default_width, default_height, global_requirejs)
180
181 plotdivid = uuid.uuid4()
--> 182 jdata = _json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
183 jlayout = _json.dumps(figure.get('layout', {}),
184 cls=utils.PlotlyJSONEncoder)
C:\Users\nickd\Anaconda3\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
235 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
236 separators=separators, default=default, sort_keys=sort_keys,
--> 237 **kw).encode(obj)
238
239
C:\Users\nickd\Anaconda3\lib\site-packages\plotly\utils.py in encode(self, o)
134
135 # this will raise errors in a normal-expected way
--> 136 encoded_o = super(PlotlyJSONEncoder, self).encode(o)
137
138 # now:
C:\Users\nickd\Anaconda3\lib\json\encoder.py in encode(self, o)
196 # exceptions aren't as detailed. The list call should be roughly
197 # equivalent to the PySequence_Fast that ''.join() would do.
--> 198 chunks = self.iterencode(o, _one_shot=True)
199 if not isinstance(chunks, (list, tuple)):
200 chunks = list(chunks)
C:\Users\nickd\Anaconda3\lib\json\encoder.py in iterencode(self, o,
_one_shot)
254 self.key_separator, self.item_separator, self.sort_keys,
255 self.skipkeys, _one_shot)
--> 256 return _iterencode(o, 0)
257
258 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
C:\Users\nickd\Anaconda3\lib\site-packages\plotly\utils.py in default(self, obj)
202 except NotEncodable:
203 pass
--> 204 return _json.JSONEncoder.default(self, obj)
205
206 #staticmethod
C:\Users\nickd\Anaconda3\lib\json\encoder.py in default(self, o)
177
178 """
--> 179 raise TypeError(repr(o) + " is not JSON serializable")
180
181 def encode(self, o):
TypeError: MinMaxScaler(copy=True, feature_range=(0, 1)) is not JSON serializable

Bokeh Geoviews use Lat/Long or UTM?

I am trying to plot the Zillow dataset with Bokeh using Geoviews and Datashader but I am having the damnedest time getting it to work. I am able to plot the data on a Cartesian plane fine but when I attempt to overlay the data with a map I run into errors.
I have used code adapted from the census-hv example on the datashader github. I believe my problem is that it is looking for the coordinates to be in UTM not Lat/Long. Because the code works when I have my coordinates multiplied by a few thousand. The points are then put above the map in white space. If i attempt to plot the proper lat/long coordinates I get the following errors.
Can someone please point me in the direction of a map that uses Lat/Long
>>>props.head()
longitude latitude
0 -118.654084 34.144442
1 -118.625364 34.140430
2 -118.394633 33.989359
3 -118.437206 34.148863
4 -118.385816 34.194168
import pandas as pd
import holoviews as hv
import geoviews as gv
import datashader as ds
from bokeh.models import WMTSTileSource
from holoviews.operation.datashader import datashade, dynspread
hv.notebook_ex
tension('bokeh')
%%opts Overlay [width=900 height=525 xaxis=None yaxis=None]
geomap = gv.WMTS(WMTSTileSource(url=\
'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{Z}/{Y}/{X}.jpg'))
points = hv.Points(gv.Dataset(props, kdims=['longitude', 'latitude']))
# color_key = {'w':'aqua', 'b':'lime', 'a':'red', 'h':'fuchsia', 'o':'yellow' }
race = datashade(points, x_sampling=50, y_sampling=50,
element_type=gv.Image)
geomap * race
RETURNS ERROR:
WARNING:root:dynamic_operation: Exception raised in callable
'dynamic_operation' of type 'function'.
Invoked as dynamic_operation(height=400, scale=1.0, width=400, x_range=None, y_range=None)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
305 pass
306 else:
--> 307 return printer(obj)
308 # Finally look for special method names
309 method = get_real_method(obj, self.print_method)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in pprint_display(obj)
255 if not ip.display_formatter.formatters['text/plain'].pprint:
256 return None
--> 257 return display(obj, raw=True)
258
259
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in display(obj, raw, **kwargs)
241 elif isinstance(obj, (HoloMap, DynamicMap)):
242 with option_state(obj):
--> 243 html = map_display(obj)
244 else:
245 return repr(obj) if raw else IPython.display.display(obj, **kwargs)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in wrapped(element)
127 try:
128 html = fn(element,
--> 129 max_frames=OutputMagic.options['max_frames'])
130
131 # Only want to add to the archive for one display hook...
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in map_display(vmap, max_frames)
196 return None
197
--> 198 return render(vmap)
199
200
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in render(obj, **kwargs)
57 if renderer.fig == 'pdf':
58 renderer = renderer.instance(fig='png')
---> 59 return renderer.html(obj, **kwargs)
60
61
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/plotting/renderer.py in html(self, obj, fmt, css, comm, **kwargs)
253 code to initialize a Comm, if the plot supplies one.
254 """
--> 255 plot, fmt = self._validate(obj, fmt)
256 figdata, _ = self(plot, fmt, **kwargs)
257 if css is None: css = self.css
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/plotting/renderer.py in _validate(self, obj, fmt)
189 if isinstance(obj, tuple(self.widgets.values())):
190 return obj, 'html'
--> 191 plot = self.get_plot(obj, renderer=self)
192
193 fig_formats = self.mode_formats['fig'][self.mode]
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/plotting/renderer.py in get_plot(self_or_cls, obj, renderer)
164 """
165 # Initialize DynamicMaps with first data item
--> 166 initialize_dynamic(obj)
167
168 if not isinstance(obj, Plot) and not displayable(obj):
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/plotting/util.py in initialize_dynamic(obj)
173 continue
174 if not len(dmap):
--> 175 dmap[dmap._initial_key()]
176
177
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in __getitem__(self, key)
942 # Not a cross product and nothing cached so compute element.
943 if cache is not None: return cache
--> 944 val = self._execute_callback(*tuple_key)
945 if data_slice:
946 val = self._dataslice(val, data_slice)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in _execute_callback(self, *args)
791
792 with dynamicmap_memoization(self.callback, self.streams):
--> 793 retval = self.callback(*args, **kwargs)
794 return self._style(retval)
795
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in __call__(self, *args, **kwargs)
489 # Nothing to do for callbacks that accept no arguments
490 (inargs, inkwargs) = (args, kwargs)
--> 491 if not args and not kwargs: return self.callable()
492 inputs = [i for i in self.inputs if isinstance(i, DynamicMap)]
493 streams = []
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/overlay.py in dynamic_mul(*args, **kwargs)
27 from .spaces import Callable
28 def dynamic_mul(*args, **kwargs):
---> 29 element = other[args]
30 return self * element
31 callback = Callable(dynamic_mul, inputs=[self, other])
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in __getitem__(self, key)
942 # Not a cross product and nothing cached so compute element.
943 if cache is not None: return cache
--> 944 val = self._execute_callback(*tuple_key)
945 if data_slice:
946 val = self._dataslice(val, data_slice)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in _execute_callback(self, *args)
791
792 with dynamicmap_memoization(self.callback, self.streams):
--> 793 retval = self.callback(*args, **kwargs)
794 return self._style(retval)
795
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in __call__(self, *args, **kwargs)
519
520 try:
--> 521 ret = self.callable(*args, **kwargs)
522 except:
523 posstr = ', '.join(['%r' % el for el in inargs]) if inargs else ''
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/util.py in dynamic_operation(*key, **kwargs)
101 self.p.kwargs.update(kwargs)
102 obj = map_obj[key] if isinstance(map_obj, HoloMap) else map_obj
--> 103 return self._process(obj, key)
104 else:
105 def dynamic_operation(*key, **kwargs):
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/util.py in _process(self, element, key)
87 kwargs = {k: v for k, v in self.p.kwargs.items()
88 if k in self.p.operation.params()}
---> 89 return self.p.operation.process_element(element, key, **kwargs)
90 else:
91 return self.p.operation(element, **self.p.kwargs)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/operation.py in process_element(self, element, key, **params)
133 """
134 self.p = param.ParamOverrides(self, params)
--> 135 return self._process(element, key)
136
137
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/operation/datashader.py in _process(self, element, key)
357
358 def _process(self, element, key=None):
--> 359 agg = aggregate._process(self, element, key)
360 shaded = shade._process(self, agg, key)
361 return shaded
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/operation/datashader.py in _process(self, element, key)
226 agg = getattr(cvs, glyph)(data, x, y, self.p.aggregator)
227 if agg.ndim == 2:
--> 228 return self.p.element_type(agg, **params)
229 else:
230 return NdOverlay({c: self.p.element_type(agg.sel(**{column: c}),
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/geoviews/element/geo.py in __init__(self, data, **kwargs)
81 elif crs:
82 kwargs['crs'] = crs
---> 83 super(_Element, self).__init__(data, **kwargs)
84
85
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/element/raster.py in __init__(self, data, bounds, extents, xdensity, ydensity, **params)
242 if bounds is None:
243 xvals = self.dimension_values(0, False)
--> 244 l, r, xdensity, _ = util.bound_range(xvals, xdensity)
245 yvals = self.dimension_values(1, False)
246 b, t, ydensity, _ = util.bound_range(yvals, ydensity)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/util.py in bound_range(vals, density)
1373 using significant digits reported by sys.float_info.dig.
1374 """
-> 1375 low, high = vals.min(), vals.max()
1376 invert = False
1377 if vals[0] > vals[1]:
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/numpy/core/_methods.py in _amin(a, axis, out, keepdims)
27
28 def _amin(a, axis=None, out=None, keepdims=False):
---> 29 return umr_minimum(a, axis, None, out, keepdims)
30
31 def _sum(a, axis=None, dtype=None, out=None, keepdims=False):
ValueError: zero-size array to reduction operation minimum which has no identity
Out[54]:
b':DynamicMap []'
I think the problem here is two-fold, first of all since the coordinates are latitudes and longitudes and you specify xsampling/ysampling values of 50 the datashaded image ends up with a tiny or zero shape, which causes this error. My suggestion would be to cast the coordinates to Google Mercator first. In future this PR will let you do so very simply by calling this:
import cartopy.crs as ccrs
projected = gv.operation.project(points, projection=ccrs.GOOGLE_MERCATOR)
...
To do this manually for now you can use the cartopy projection directly:
coords = ccrs.GOOGLE_MERCATOR.transform_points(ccrs.PlateCarree(), lons, lats)
projected = gv.Points(coords, crs=ccrs.GOOGLE_MERCATOR)
...

Categories