def mds(m, axis=0, dim=2, method='spearman', **kwargs): """ Multidimensional scaling plot from a preference matrix. See: https://en.wikipedia.org/wiki/Multidimensional_scaling Args: m: preference matrix. dim: number of dimensions to plot (2 or 3). method: any metric method. **kwargs: arguments for rk.scatterplot function (e.g. fontsize). """ names = None if axis == 0: if rk.is_dataframe(m): names = m.columns m = m.T # transpose elif axis == 1: if rk.is_dataframe(m): names = m.index else: raise Excpetion('axis must be 0 or 1.') # Compute pairwise distances dist_matrix = rk.distance_matrix(m, method=method) # Call the plot functions mds_from_dist_matrix(dist_matrix, dim=dim, names=names, **kwargs)
def tsne(m, axis=0, dim=2, **kwargs): """ Use T-SNE algorithm to show the matrix m in a 2 or 3 dimensions space. Args: axis: axis of dimensionality reduction. dim: number of dimensions. 2 for 2D plot, 3 for 3D plot. **kwargs: arguments for rk.scatterplot function (e.g. fontsize, pointsize). """ names = None if axis == 0: if rk.is_dataframe(m): names = m.columns m = m.T # transpose elif axis == 1: if rk.is_dataframe(m): names = m.index else: raise Excpetion('axis must be 0 or 1.') m_transformed = TSNE(n_components=dim).fit_transform(m) # Display scatterplot(m_transformed, dim=dim, names=names, **kwargs)
def show(m, rotation=90, title=None, size=2, annot=False, round=2, color='royalblue', cmap=None): """ Display a ranking or a prefrence matrix. If m is 1D: show ballot (bar plot). If m is 2D: show preferences (heatmap). TODO: annot argument adding the values in the plot. Args: rotation: x labels rotation. title: string - title of the figure. size: integer - higher value for a smaller figure. annot: If True, write the values. round: Number of decimals to display if annot is True. color: Color for 1D bar plot. cmap: Color map for 2D heatmap. """ dim = len(m.shape) if dim == 1: # 1D x = np.arange(len(m)) bar_plot = plt.bar(x, m, align='center', color=color) if annot: autolabel(bar_plot, m, round=round) if rk.is_series(m): plt.xticks(x, m.index, rotation=rotation) elif dim == 2: # 2D fig, ax = plt.subplots(figsize=(m.shape[1] / size, m.shape[0] / size)) sns.heatmap(m, ax=ax, annot=annot, linewidths=.2, fmt='0.' + str(round), cmap=cmap) x = np.arange(m.shape[1]) if rk.is_dataframe(m): plt.xticks(x, m.columns, rotation=rotation) else: raise (Exception( 'Passed array must have only 1 or 2 dimension, not {}.'.format( dim))) if title is not None: plt.title(title) plt.show()
def mds_from_dist_matrix(distance_matrix, dim=2, names=None, **kwargs): """ Multidimensional scaling plot from a symmetric distance matrix (pairwise distances). See: https://en.wikipedia.org/wiki/Multidimensional_scaling Args: m: distance matrix. dim: number of dimensions to plot (2 or 3). names: names of objects. Will be overwritten if distance_matrix is a pd.DataFrame. **kwargs: arguments for rk.scatterplot function (e.g. fontsize). """ if rk.is_dataframe(distance_matrix): names = distance_matrix.columns transformer = MDS(n_components=dim, dissimilarity='precomputed') m_transformed = transformer.fit_transform(distance_matrix) # Display scatterplot(m_transformed, dim=dim, names=names, **kwargs)