Seaborn Heatmap Error: " AttributeError: 'NoneType' object has no attribute 'reshape' "
I am doing an NLP project looking at the cosine similarities between the respective works of 10 different classic rock artists. I have done similarity queries and created a dataframe called similarities that looks like this.
I built the similarities dataframe by first building an empty one with the artist names as the indices and the column names and then ran the following code to run the similarity queries and populate the dataframe with the scores:
artist_words = data['lyrics'][artist]
artist_vec_bow = dictionary.doc2bow(artist_words.lower().split())
artist_vec_lsi = lsi[artist_vec_bow]
artist_sims = index[artist_vec_lsi]
artist_sims_sorted = sorted(enumerate(artist_sims), key=lambda item: -item[1])
for position, score in artist_sims_sorted:
similarities.at[artist, musicians[position][1]] = score
I would like to create a Seaborn heatmap to visualize the correlations indicated in the DataFrame.
But when I run the following code:
sns.heatmap(similarities)
I get a long error message that concludes with :
AttributeError: 'NoneType' object has no attribute 'reshape'
Can anyone help me figure out how to visualize this?
similarities.info() returns the following:
Index: 11 entries, bob_dylan to willie_nelson
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 bob_dylan 11 non-null object
1 david_bowie 11 non-null object
2 janis_joplin 11 non-null object
3 john_prine 11 non-null object
4 leonard_cohen 11 non-null object
5 linda_ronstadt 11 non-null object
6 mark_knopfler 11 non-null object
7 neil_young 11 non-null object
8 stevie_nicks 11 non-null object
9 the_band 11 non-null object
10 willie_nelson 11 non-null object
dtypes: object(11)
memory usage: 1.3+ KB
similarities.describe() returns the following:
In case the full error message is helpful, here it is:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/tb/j430qmhn16s6nsz08nf0y7y40000gn/T/ipykernel_40566/1345125298.py in <module>
----> 1 ax2 = sns.heatmap(
2 similarities,
3 vmin=-1, vmax=1, center=0,
4 cmap=sns.diverging_palette(20, 220, n=200),
5 square=True
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
44 )
45 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46 return f(**kwargs)
47 return inner_f
48
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/seaborn/matrix.py in heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, xticklabels, yticklabels, mask, ax, **kwargs)
546 if square:
547 ax.set_aspect("equal")
--> 548 plotter.plot(ax, cbar_ax, kwargs)
549 return ax
550
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/seaborn/matrix.py in plot(self, ax, cax, kws)
330 # Possibly rotate them if they overlap
331 if hasattr(ax.figure.canvas, "get_renderer"):
--> 332 ax.figure.draw(ax.figure.canvas.get_renderer())
333 if axis_ticklabels_overlap(xtl):
334 plt.setp(xtl, rotation="vertical")
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
72 @wraps(draw)
73 def draw_wrapper(artist, renderer, *args, **kwargs):
---> 74 result = draw(artist, renderer, *args, **kwargs)
75 if renderer._rasterizing:
76 renderer.stop_rasterizing()
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/figure.py in draw(self, renderer)
2778
2779 self.patch.draw(renderer)
-> 2780 mimage._draw_list_compositing_images(
2781 renderer, self, artists, self.suppressComposite)
2782
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
130 if not_composite or not has_images:
131 for a in artists:
--> 132 a.draw(renderer)
133 else:
134 # Composite any adjacent images together
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/_api/deprecation.py in wrapper(*inner_args, **inner_kwargs)
429 else deprecation_addendum,
430 **kwargs)
--> 431 return func(*inner_args, **inner_kwargs)
432
433 return wrapper
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
2919 renderer.stop_rasterizing()
2920
-> 2921 mimage._draw_list_compositing_images(renderer, self, artists)
2922
2923 renderer.close_group('axes')
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
130 if not_composite or not has_images:
131 for a in artists:
--> 132 a.draw(renderer)
133 else:
134 # Composite any adjacent images together
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/collections.py in draw(self, renderer)
2099 offsets = np.column_stack([xs, ys])
2100
-> 2101 self.update_scalarmappable()
2102
2103 if not transform.is_affine:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/collections.py in update_scalarmappable(self)
924 # pcolormesh, scatter, maybe others flatten their _A
925 self._alpha = self._alpha.reshape(self._A.shape)
--> 926 self._mapped_colors = self.to_rgba(self._A, self._alpha)
927
928 if self._face_is_mapped:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/cm.py in to_rgba(self, x, alpha, bytes, norm)
358 if norm:
359 x = self.norm(x)
--> 360 rgba = self.cmap(x, alpha=alpha, bytes=bytes)
361 return rgba
362
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/colors.py in __call__(self, X, alpha, bytes)
612 self._init()
613
--> 614 mask_bad = X.mask if np.ma.is_masked(X) else np.isnan(X) # Mask nan's.
615 xa = np.array(X, copy=True)
616 if not xa.dtype.isnative:
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/IPython/core/formatters.py in __call__(self, obj)
339 pass
340 else:
--> 341 return printer(obj)
342 # Finally look for special method names
343 method = get_real_method(obj, self.print_method)
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
248
249 if 'png' in formats:
--> 250 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
251 if 'retina' in formats or 'png2x' in formats:
252 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
132 FigureCanvasBase(fig)
133
--> 134 fig.canvas.print_figure(bytes_io, **kw)
135 data = bytes_io.getvalue()
136 if fmt == 'svg':
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
2228 else suppress())
2229 with ctx:
-> 2230 self.figure.draw(renderer)
2231
2232 if bbox_inches:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
72 @wraps(draw)
73 def draw_wrapper(artist, renderer, *args, **kwargs):
---> 74 result = draw(artist, renderer, *args, **kwargs)
75 if renderer._rasterizing:
76 renderer.stop_rasterizing()
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/figure.py in draw(self, renderer)
2778
2779 self.patch.draw(renderer)
-> 2780 mimage._draw_list_compositing_images(
2781 renderer, self, artists, self.suppressComposite)
2782
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
130 if not_composite or not has_images:
131 for a in artists:
--> 132 a.draw(renderer)
133 else:
134 # Composite any adjacent images together
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/_api/deprecation.py in wrapper(*inner_args, **inner_kwargs)
429 else deprecation_addendum,
430 **kwargs)
--> 431 return func(*inner_args, **inner_kwargs)
432
433 return wrapper
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
2919 renderer.stop_rasterizing()
2920
-> 2921 mimage._draw_list_compositing_images(renderer, self, artists)
2922
2923 renderer.close_group('axes')
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
130 if not_composite or not has_images:
131 for a in artists:
--> 132 a.draw(renderer)
133 else:
134 # Composite any adjacent images together
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
49 renderer.start_filter()
50
---> 51 return draw(artist, renderer, *args, **kwargs)
52 finally:
53 if artist.get_agg_filter() is not None:
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/collections.py in draw(self, renderer)
2128 coordinates, offsets, transOffset,
2129 # Backends expect flattened rgba arrays (n*m, 4) for fc and ec
-> 2130 self.get_facecolor().reshape((-1, 4)),
2131 self._antialiased, self.get_edgecolors().reshape((-1, 4)))
2132 gc.restore()
AttributeError: 'NoneType' object has no attribute 'reshape'
<Figure size 432x288 with 2 Axes>
from Recent Questions - Stack Overflow https://ift.tt/3xujhOy
https://ift.tt/3hTD6IN

Comments
Post a Comment