def graph5(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals data = df.iloc[:, 4:].drop('child_alone', axis=1) corr_list = [] correl = data.corr().values for row in correl: corr_list.append(list(row)) col_names = [col.replace('_', ' ').title() for col in data.columns] # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [{ 'data': [ Heatmap( z=corr_list, x=col_names, y=col_names, colorscale='Viridis', ) ], 'layout': { 'title': 'What Types of Messages Occur Together?', 'height': 750, 'width': 750, 'margin': dict(l=150, r=30, b=160, t=60, pad=4), } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('graph5.html', ids=ids, graphJSON=graphJSON)
def iheatmap(ds, data_name, x_coo, y_coo, colormap="Portland", go_dict={}, ly_dict={}, nb=True, return_fig=False, **kwargs): """ Automatic 2D-Heatmap plot using plotly. """ from plotly.graph_objs import Heatmap traces = [Heatmap({"z": (ds[data_name] .dropna(x_coo, how="all") .dropna(y_coo, how="all") .squeeze() .transpose(y_coo, x_coo) .data), "x": ds.coords[x_coo].data, "y": ds.coords[y_coo].data, "colorscale": colormap, "colorbar": {"title": data_name}, **go_dict})] layout = {"height": 600, "width": 650, "xaxis": {"showline": True, "mirror": "ticks", "ticks": "outside", "title": x_coo}, "yaxis": {"showline": True, "mirror": "ticks", "ticks": "outside", "title": y_coo}, **ly_dict} fig = {"data": traces, "layout": layout} if return_fig: return fig ishow(fig, nb=nb, **kwargs)
def index(): # extract data needed for visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) # extract categories counts voor visuals categories_counts = df.melt(id_vars='id', value_vars=list( df.columns)[4:]).groupby('variable').sum()['value'] categories_names = list(categories_counts.index) # extract categories correlation category_corr = df.iloc[:, 4:].corr().values category_corr_names = list(df.iloc[:, 4:].columns) # create visuals graphs = [ { 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { 'data': [Bar(x=categories_names, y=categories_counts)], 'layout': { 'title': 'Distribution of Categories', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Categories" } } }, { 'data': [ Heatmap( x=category_corr_names, y=category_corr_names, #[::-1], z=category_corr) ], 'layout': { 'title': 'Heatmap of correlation Categories' } } ] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def create_heatmap(self, param, gamma_dict): data = [] for key, value in gamma_dict.items(): value.sort() b0_set = [x[0] for x in value] data.append([math.log(x[1]) for x in value]) layout = Layout( title=f'Logarithm of average error for {self.param_name} = {param}', xaxis=dict(title='$b_0$'), yaxis=dict(title='prefix length'), height=700, font={'size': 20}) figure = Figure(layout=layout) figure.add_trace( Heatmap(z=data, x=b0_set, y=list(gamma_dict.keys()), reversescale=True, colorscale='Viridis', hovertemplate='b0: %{x}<br>prefix_len: %{y}<br>' 'log(error): %{z}<extra></extra>')) return figure
def matshow(matrix, title=None, x_labels=None, y_labels=None, fname=None): mat = np.copy(matrix) # idx = np.diag_indices_from(mat) # mat[idx] = 0 x_range = np.arange(0, mat.shape[0], 1) + 0.5 data = [ Heatmap(z=mat, y=x_labels, x=y_labels, zmin=-1, zmax=1, colorbar=ColorBar(title='Colorbar'), colorscale='RdBl') ] layout = dict( width=450, height=450, title=title, ) if fname is None: return {'data': data, 'layout': layout} else: raise NotImplementedError
def plotConfusionMatrix(self, dataFrame, name): """ @param data (pandas DF) The confusion matrix. @param name (str) """ labels = dataFrame.columns.values.tolist() values = map(list, dataFrame.values) data = Data( [Heatmap(z=values, x=labels, y=labels, colorscale='YIGnBu')]) layout = Layout( title='Confusion Matrix for ' + name, xaxis=XAxis(title='', side='top', titlefont=Font(family='Courier New, monospace', size=18, color='#7f7f7f')), yaxis=YAxis(title='', titlefont=Font(family='Courier New, monospace', size=18, color='#7f7f7f'), autorange='reversed'), barmode='overlay', autosize=True, width=1000, height=1000, margin=Margin(l=80, r=80, b=120, t=140)) fig = Figure(data=data, layout=layout) plot_url = py.plot(fig) print "Confusion matrix URL: ", plot_url
def plot_co_occurence_heatmap(x, y, z): """ Returns a dictionary that specifies the plotly graph object for plotting a heatmap that informs of the pair-wise co-occurrence of labels """ graph_dict = { "data": [Heatmap(x=x, y=y, z=z, hoverongaps=False, colorscale="Viridis")], "layout": { "title": "Co-occurrence of message labels", "autosize": False, "height": 800, "width": 1000, "yaxis": { "automargin": True, "autorange": "reversed", }, "xaxis": { "tickangle": 60, "automargin": True }, }, } return graph_dict
def create_heat_map(simulation_table: SimulationTable): data = [] for errors in simulation_table.errors.values(): errors.sort() b0_set = [b0 for b0, _ in errors] data.append([math.log(error) for _, error in errors]) layout = Layout( title=f"Logarithm of average error for {simulation_table.param_name} = " f"{simulation_table.param}", xaxis=dict(title="$b_0$"), yaxis=dict(title="prefix length"), height=700, font={"size": 20}, ) figure = Figure(layout=layout) figure.add_trace( Heatmap( z=data, x=b0_set, y=list(simulation_table.errors.keys()), reversescale=True, colorscale="Viridis", hovertemplate="b0: %{x}<br>prefix_len: %{y}<br>" "log(error): %{z}<extra></extra>", )) return figure
def _mk_annotation_bar(annotation: pd.DataFrame): colorscale, mapping = _define_colorscale(annotation) categories = set(map(str.strip, map(str, annotation.values[0]))) - {'nan'} num_categories = len(categories) if num_categories == 1: return None if not colorscale: colorscale = "Greys" values = annotation.values.copy() if mapping: for i in range(values.shape[0]): for j in range(values.shape[1]): values[i, j] = mapping[str( values[i, j])] if str(values[i, j]) != 'nan' else values[i, j] annotation_bar = Heatmap( y=list(annotation.index), x=list(annotation.columns), z=values, colorscale=colorscale, colorbar=dict( tickmode='array', tickvals=np.array(list(mapping.values())) if mapping else None, ticktext=np.array(list(mapping.keys())) if mapping else None), ) return annotation_bar
def index(): ''' Creates an array of plotly graphs in JSON to be rendered using the html in our templates folder OUTPUT: the rendered webpage with the plotly graphs ''' # extract data needed for visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) category_counts = df.drop( columns=['id', 'message', 'original', 'genre']).sum( axis=0, skipna=True) / df.shape[0] category_names = list(category_counts.index) cor = df.drop(columns=['id']).corr() # create visuals graphs = [ { 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { 'data': [Bar(x=category_names, y=category_counts)], 'layout': { 'title': 'Distribution of Categories', 'yaxis': { 'title': "Proportion" }, 'xaxis': { 'tickangle': 45, } } }, { 'data': [Heatmap(z=cor.values, x=cor.index, y=cor.columns)], 'layout': { 'title': 'Correlation of Categories', }, 'xaxis': { 'tickangle': 45, } }, ] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def heatMap(x, y, z, title=None): #colorscale = [[0, 'rgb(250,250,250)'], [1, 'rgb(255, 153, 0)']] colorscale = [[0, 'rgb(250,250,250)'], [1, 'rgb(0, 150, 255)']] data = [ Heatmap( x=x, y=y, z=z, colorscale=colorscale, colorbar=dict(outlinewidth=0, outlinecolor='#cfcfcf'), ) ] defaultAxis = dict( type='category', showticklabels=True, tickfont=dict(family='Old Standard TT, serif', size=8, color='black'), ) topAxis = defaultAxis.copy() topAxis.update(dict(side='top')) bottomAxis = defaultAxis.copy() bottomAxis.update(dict(side='bottom', overlaying='x')) layout = Layout(xaxis=topAxis, xaxis2=bottomAxis, yaxis=defaultAxis, title=title) return {'data': data, 'layout': layout}
def index(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) # message length by Character message_lengths = df.message.str.len() # 10 bins counts of message length within 4th quartile length_percents = (message_lengths[ message_lengths <= message_lengths.quantile(.99)].value_counts( normalize=True, bins=10).round(decimals=4).sort_index()) * 100 # generate x-ticks base on Serise.value_counts.index for plotly xticks = [] for idx in length_percents.index: xticks.append(str(int(idx.left)) + '-' + str(int(idx.right))) # correlation between labels labels = df.loc[:, 'related':] cor = labels.corr() # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [{ 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { 'data': [Bar(x=xticks, y=length_percents.values)], 'layout': { 'title': 'Message Length by Character', 'yaxis': { 'title': "Percentage" }, 'xaxis': { 'title': "Message Length" } } }, { 'data': [Heatmap(z=cor.values, x=cor.columns, y=cor.index)], 'layout': { 'title': 'Heatmap of Labels', 'height': 1100 } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) # distribution of message categories X = df["message"] Y = df.drop(["id", "message", "original", "genre"], axis=1) Y_sum = Y.sum(axis=0) column_names = list(Y.columns) # Correlation of message categories on heatmap corr = Y.corr() # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [{ 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': 'Count' }, 'xaxis': { 'title': 'Genre' } } }, { 'data': [Bar(x=column_names, y=Y_sum)], 'layout': { 'title': 'Distribution of Message Categories', 'height': 600, 'margin': dict(b=220), 'yaxis': { 'title': 'Count' }, 'xaxis': { 'title': 'Message Categories' } } }, { 'data': [Heatmap(x=column_names, y=column_names, z=corr)], 'layout': { 'title': 'Correlation of Message categories', 'height': 1000, 'margin': dict(b=220, l=150, r=150), 'xaxis': { 'title': 'Message Categories' } } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) categories_counts = df.loc[:, 'related':].sum().sort_values(ascending=False) categories_names = list(categories_counts.index) categories_counts_top5 = categories_counts[:5] categories_names_top5 = list(categories_counts_top5.index) df_Y = df.loc[:, 'related':] correlation_array = df_Y[categories_names_top5].corr().values # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [{ 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genre', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { 'data': [Bar(x=categories_names, y=categories_counts, orientation='v')], 'layout': { 'title': 'Distribution of Message Categories', 'yaxis': { 'title': "Categories Name" }, 'xaxis': { 'title': "Disaster Categories" } } }, { 'data': [Heatmap(z=correlation_array)], 'layout': { 'title': 'Correlation of Top 5 Categories', 'yaxis': { 'title': "Top 5 Categories Name" }, 'xaxis': { 'title': "Top 5 Categories Name" } } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # Extract 10 random message examples random_10_df = df.sample(n=5) random_10_messages = random_10_df['message'].tolist() # Extract data for genre counts genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) # Extract data for category counts categories_df = df.drop(["id", "message", "original", "genre"], axis=1) category_names = list(categories_df.columns.values) category_totals = categories_df.apply(pd.Series.value_counts) category_totals = category_totals.iloc[1] # Extract data for heatmap coocc_matrix = categories_df.T.dot(categories_df) coocc_matrix = coocc_matrix.values # create visuals graphs = [ { 'data': [Bar(x=category_names, y=category_totals)], 'layout': { 'title': 'Message category usage counts', #'yaxis': { # 'title': "Count" #}, #'xaxis': { # 'title': "Category name" #} } }, { 'data': [Heatmap(x=category_names, y=category_names, z=coocc_matrix)], 'layout': { 'title': 'Message category co-occurance heatmap', #'yaxis': { # 'title': "Category name" #}, #'xaxis': { # 'title': "Category name" #} } } ] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON, messages=random_10_messages)
def index(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) # create 2nd chart y_cats = ['related', 'request', 'offer', 'aid_related', 'medical_help', 'medical_products',\ 'search_and_rescue', 'security', 'military', 'child_alone', 'water',\ 'food', 'shelter', 'clothing', 'money', 'missing_people', 'refugees',\ 'death', 'other_aid', 'infrastructure_related', 'transport',\ 'buildings', 'electricity', 'tools', 'hospitals', 'shops',\ 'aid_centers', 'other_infrastructure', 'weather_related', 'floods',\ 'storm', 'fire', 'earthquake', 'cold', 'other_weather', 'direct_report'] list_words = list(pd.unique(df_plot["word"])) word_count_list = [] for cat in y_cats: df_plot[df_plot["category"] == cat][["word", "count"]] df_temp = pd.DataFrame(list_words, columns=["word"]) df_temp = df_temp.merge( df_plot[df_plot["category"] == cat][["word", "count"]], how="left") word_count_list.append(list(df_temp["count"])) # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [{ 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { 'data': [Heatmap(x=list_words, y=y_cats, z=word_count_list)], 'layout': { 'title': 'Top 10 cleaned words per category and their count', 'yaxis': { 'title': "Categories" }, 'xaxis': { 'title': "Cleanned, stemmed words" }, 'height': 800 } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # extract data needed for visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) # create visuals graphs = [ { 'data': [ Bar( x=genre_names, y=genre_counts ) ], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } } ] # heatmap of label pairwise correlations # drop some cols and replace 1's with np.nan heatmap_df = df.drop(['id'], axis=1).corr().replace(1, np.nan) cols = list(heatmap_df.columns) # grab the lower triangle of the values array - zeroes out the other values heatmap_df = np.tril(heatmap_df) # replace 0s with nan heatmap_df[heatmap_df == 0] = np.nan heatmap_data = Heatmap( z=heatmap_df, x=cols, y=cols ) heatmap_layout = Layout( title=dict(text='Message Label Pairwise Correlations', x=0.5), width=1000, height=1000 ) graphs.append(Figure(data=heatmap_data, layout=heatmap_layout)) # append the earlier created word cloud figure graphs.append(wordcloud_fig) # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs # print('here at the index page!') return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # extract data needed for visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) category_names = df.drop(['id', 'message', 'original', 'genre'], axis=1).columns category_melt = pd.melt(df, id_vars=['id'], value_vars=category_names, var_name='categories', value_name='flag') category_counts = category_melt.groupby( 'categories')['flag'].sum().sort_values(ascending=False) category_corrations = df[category_names].corr().values # create visuals graphs = [{ 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Message Count" }, 'xaxis': { 'title': "Message Genre" } } }, { 'data': [Bar(x=category_names, y=category_counts)], 'layout': { 'title': 'Histogram of Message Categories', 'yaxis': { 'title': "Category Count" }, 'xaxis': { 'title': "Category Names" } } }, { 'data': [Heatmap( x=category_names, y=category_names, z=category_corrations, )], 'layout': { 'title': 'Heatmap of Message Categories and Occurrences' } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def plot_categorical_conditional(spn, d, featureId, categoricalId, detail=1000, fname=None): joint = spn.marginalize([featureId, categoricalId]) marg = spn.marginalize([categoricalId]) def proba(query): return np.exp(joint.eval(query) - marg.eval(query)) x_range = spn.domains[featureId] x_is_cat = spn.featureTypes[featureId] == 'categorical' x_detail = detail if not x_is_cat else len(spn.domains[featureId]) x_names = None if not x_is_cat else d['features'][featureId][ 'encoder'].inverse_transform(np.array(x_range)) y_range = spn.domains[categoricalId] y_names = d['features'][categoricalId]['encoder'].inverse_transform( np.array(y_range)) grid = np.mgrid[x_range[0]:x_range[-1]:x_detail * 1j, y_range[0]:y_range[-1]:len(spn.domains[categoricalId]) * 1j] grid = grid.reshape(2, -1).T query = np.zeros((grid.shape[0], spn.numFeatures)) query[:, :] = np.nan query[:, featureId] = grid[:, 0] query[:, categoricalId] = grid[:, 1] result = proba(query) result = np.exp(result) result.shape = (x_detail, len(spn.domains[categoricalId])) # plot data = [ Heatmap(z=result, x=y_names, y=np.linspace(spn.domains[featureId][0], spn.domains[featureId][-1], detail) if not x_is_cat else x_names, colorbar=ColorBar(title='Colorbar'), colorscale='Hot') ] layout = dict( width=450, height=450, title='COnditional probability function of "{}" and "{}"'.format( spn.featureNames[featureId], spn.featureNames[categoricalId]), yaxis=dict(title=spn.featureNames[featureId], autotick=True), xaxis=dict(title=spn.featureNames[categoricalId], autotick=True)) if fname is None: return {'data': data, 'layout': layout} else: raise NotImplementedError
def index(): # extract genre counts genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) # extract categories category_map = df.iloc[:, 4:].corr().values category_names = list(df.iloc[:, 4:].columns) # extract length of texts length_direct = df.loc[df.genre == 'direct', 'text_length'] length_social = df.loc[df.genre == 'social', 'text_length'] length_news = df.loc[df.genre == 'news', 'text_length'] # create visuals graphs = [{ 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { 'data': [Heatmap(x=category_names, y=category_names[::-1], z=category_map)], 'layout': { 'title': 'Heatmap of Categories' } }, { 'data': [ Histogram(y=length_direct, name='Direct', opacity=0.5), Histogram(y=length_social, name='Social', opacity=0.5), Histogram(y=length_news, name='News', opacity=0.5) ], 'layout': { 'title': 'Distribution of Text Length', 'yaxis': { 'title': 'Count' }, 'xaxis': { 'title': 'Text Length' } } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # extract data needed for visuals related_counts = df.groupby('related').count()['message'] related_names = [str(item) for item in related_counts.index] issues_counts = df[df.related == 1][df.columns[2:]].sum() issues_names = df.columns[2:] cat_cols = df.columns[1:] cats = df[cat_cols] interrelations = pd.concat( [cats[cats[col] == 1].mean() for col in cat_cols], axis=1).fillna(0) # create visuals graphs = [{ 'data': [Bar(x=related_names, y=related_counts)], 'layout': { 'title': 'Distribution of Messages Related to Disasters', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Related (1 = True, 0 = False)" } } }, { 'data': [Bar(x=issues_names, y=issues_counts)], 'layout': { 'title': 'Distribution of Issues of Disaster Related Messages', 'xaxis': { 'title': "Issue Category" }, 'yaxis': { 'title': "Count, Given Message Is Disaster Related" } } }, { 'data': [Heatmap(z=interrelations, x=cat_cols, y=cat_cols)], 'layout': { 'title': 'Interrelations of Categories', 'xaxis': { 'title': "Category" }, 'yaxis': { 'title': "Category (only being 1=True)" } } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): ''' Method to display the index page, it will generate plots etc ''' # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) category_count = df.drop( columns=['id', 'message', 'original', 'genre']).sum() category_names = list(category_count.index) category_corr = df.drop( columns=['id', 'message', 'original', 'genre']).corr() # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [ { ## Genre bar plot 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { ## Category bar plot 'data': [Bar(x=category_names, y=category_count)], 'layout': { 'title': 'Distribution of Message Category', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Category" } } }, { ## Correlation heatmap 'data': [Heatmap(z=category_corr, x=category_names, y=category_names)], 'layout': { 'title': 'Correlation of Message Category', 'width': 800, 'height': 800 } } ] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) category_counts_by_genre = df.groupby('genre').sum().iloc[:,1:] # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [ { 'data': [ Bar( x=genre_names, y=genre_counts ) ], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } } ,{ 'data': [ Heatmap( y=list(category_counts_by_genre.index), x=category_counts_by_genre.columns, z=category_counts_by_genre ) ], 'layout': { 'title': 'Heatmap of Category Count by Message Genre', 'yaxis': { 'title': "Category" }, 'xaxis': { 'title': "Genre" } } } ] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) category_names = df.iloc[:, 4:].columns category_counts = (df.iloc[:, 4:] != 0).sum().values Y = df.iloc[:, 4:] category_correlation = Y.corr() # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [{ 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { 'data': [Bar(x=category_names, y=category_counts)], 'layout': { 'title': 'Number of Message per Category', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Category", 'tickangle': 35 } } }, { 'data': [ Heatmap(z=category_correlation.values, x=category_names, y=category_names) ], 'layout': { 'title': 'Categories Correlation', 'height': 1000 } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def imshow(self, ax, X, extent=None, label=None, vmin=None, vmax=None, **imshow_kwargs): if not 'showscale' in imshow_kwargs: imshow_kwargs['showscale'] = False return Heatmap(z=X, name=label, x0=extent[0], dx=float(extent[1]-extent[0])/(X.shape[0]-1), y0=extent[2], dy=float(extent[3]-extent[2])/(X.shape[1]-1), zmin=vmin, zmax=vmax, showlegend=label is not None, hoverinfo='z', **imshow_kwargs)
def index(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) top_messages_diasters = df[df == 1].count().sort_values( ascending=False).head(10) top_messages_disasters_names = list( df[df == 1].count().sort_values(ascending=False).head(10).index) y = df.drop(columns=['id', 'message', 'original', 'genre']) cat_counts = [sum(y[x]) for x in y.columns.values] correlation = y.corr() labels = y.columns.values # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [{ 'data': [Bar(x=top_messages_disasters_names, y=top_messages_diasters)], 'layout': { 'title': 'Top Categories Disasters Messages', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Category" } } }, { 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { 'data': [Heatmap(z=correlation.values, x=labels, y=labels)], 'layout': { 'title': 'Correlation between variables', 'height': 1200 } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals genre_counts = df.groupby('genre').count()['text'] genre_names = list(genre_counts.index) # extract data for the heatmap of categories categories_list = [ c for c in df.columns if c not in ['genre', 'text', 'related'] ] top_cat_df = df[categories_list].mean().sort_values( ascending=False).head(10) top_cat_names = top_cat_df.index cat_hm_val = [] for x in top_cat_names: curr_cat = df[x] == 1 cat_hm_val.append( df[curr_cat][top_cat_names].mean().round(2).values.tolist()) # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [{ 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Category" } } }, { 'data': [Heatmap({ 'x': top_cat_names, 'y': top_cat_names, 'z': cat_hm_val })], 'layout': { 'title': 'Correlation between messages Categories', 'xaxis': { 'title': "Most detected categories" } } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals #data for first graph genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) #data for second graph categories = df.drop(['id', 'message', 'original', 'genre'], axis=1).astype(float) categories_names = categories.columns.values categories_counts = [sum(categories[cat]) for cat in categories_names] #data for the third graph correlations = categories.corr().values # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [{ 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'How Were the Messages Recieved?', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { 'data': [Bar(x=categories_names, y=categories_counts)], 'layout': { 'title': 'Number of Messages in Each Category', 'yaxis': { 'title': "Count" }, 'height': 800, 'margin': dict(b=230, ), } }, { 'data': [Heatmap(z=correlations, x=categories_names, y=categories_names)], 'layout': { 'title': 'How Categories of Messages are Correlated?', 'height': 800, 'margin': dict(l=150, r=20, b=150, t=50), } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def index(): # extract data needed for visuals # TODO: Below is an example - modify to extract data for your own visuals genre_counts = df.groupby('genre').count()['message'] genre_names = list(genre_counts.index) lengths = df.message.str.split().str.len() length_counts, length_division = np.histogram( lengths, range=(0, lengths.quantile(0.99))) y = df.drop(['id', 'message', 'original', 'genre'], axis=1).astype(float) corr = y.corr() labels = y.columns.values # create visuals # TODO: Below is an example - modify to create your own visuals graphs = [{ 'data': [Bar(x=length_division, y=length_counts)], 'layout': { 'title': 'Message Length Distribution', }, 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Message Length" } }, { 'data': [Bar(x=genre_names, y=genre_counts)], 'layout': { 'title': 'Distribution of Message Genres', 'yaxis': { 'title': "Count" }, 'xaxis': { 'title': "Genre" } } }, { 'data': [Heatmap(z=corr.values, x=labels, y=labels)], 'layout': { 'title': 'Categories Correlation', 'height': 1000 } }] # encode plotly graphs in JSON ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)] graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder) # render web page with plotly graphs return render_template('master.html', ids=ids, graphJSON=graphJSON)
def print_lattice(lattice): """Print the lattice configuration.""" x = list(range(lattice.shape[0])) y = list(range(lattice.shape[1])) cs = [[0, 'rgba(255, 255, 255, 0'], [1, 'rgba(0, 0, 0, 1)']] h = Heatmap(x=x, y=y, z=lattice, colorscale=cs) lyt = Layout(xaxis=dict(showgrid=True, dtick=1, showticklabels=False), yaxis=dict(showgrid=True, dtick=1, showticklabels=False, scaleanchor="x")) py.plot(Figure(data=[h], layout=lyt), filename="_lattice.html")