示例#1
0
def main():
    """ PCA analysis on iris dataset.
    Reads in data from specified directory, runs analysis, and saves figure.
    """
    # Parse save directory from arguments
    parser = argparse.ArgumentParser(description='Input / Output directories.')
    parser.add_argument('inputdir', type=str, help='Input directory path.')
    parser.add_argument('outdir', type=str, help='Output directory path.')
    args = parser.parse_args()
    INPUT_DIR = args.inputdir
    OUTPUT_DIR = args.outdir

    # Read data
    cars = data.cars()

    chart = alt.Chart(cars).mark_point().encode(
        x='Horsepower',
        y='Miles_per_Gallon',
        color='Origin',
    ).interactive()

    chart.save(OUTPUT_DIR)

    print('PRINT Saved to: {}'.format(OUTPUT_DIR))
    logging.info('Saved to: {}'.format(OUTPUT_DIR))
示例#2
0
def make_interactive_chart():
    '''
    '''
    pts = alt.selection_interval(encodings=['x', 'y'])

    rect = alt.Chart(data.cars()).mark_point().encode(
        x='Miles_per_Gallon:Q',
        y='Horsepower:Q',
        color=alt.condition(pts, 'Origin',
                            alt.value('lightgray'))).properties(selection=pts)

    # scale=alt.Scale(scheme='greenblue'),
    #         legend=alt.Legend(title='Total Sample')
    #     )
    # circ = rect.mark_point().encode(
    #     alt.ColorValue('grey'),
    #     alt.Size('count()',
    #         legend=alt.Legend(title='Sample in Selection')
    #     )
    # ).transform_filter(
    #     pts
    # )

    return alt.vconcat(rect | rect.encode(x='Acceleration')).resolve_legend(
        color="independent", size="independent")
示例#3
0
    def get(self, request, *args, **kwargs):
        context = locals()


        data3 = pd.DataFrame({
            'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
            'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
        })

        context['chart'] = alt.Chart(data3).mark_bar().encode(
            x='a',
            y='b'
        ).interactive()

        source = data.cars()

        context['chart2'] = alt.Chart(source).mark_circle().encode(
            x='Horsepower',
            y='Miles_per_Gallon',
            color='Origin'
        ).interactive()



        return render(request, self.template_name, context)
示例#4
0
def make_static_chart1():
    '''
    '''
    return alt.Chart(data=data.cars()).mark_circle(size=60).encode(
        x='Horsepower:Q',
        y='Miles_per_Gallon:Q',
        color='Origin:N',
        tooltip='Origin:N',
    ).interactive()
def test_cars_column_names():
    cars = data.cars()
    assert type(cars) is pd.DataFrame
    assert tuple(cars.columns) == ('Acceleration', 'Cylinders', 'Displacement',
                                   'Horsepower', 'Miles_per_Gallon', 'Name',
                                   'Origin', 'Weight_in_lbs', 'Year')

    cars = data.cars.raw()
    assert type(cars) is bytes
示例#6
0
def app():

    st.title("Let's demo Streamlit basics")

    st.sidebar.markdown("**Some tools we'll use:**")
    st.sidebar.markdown("""
        * Markdown
        * Pandas
        * Altair""")

    st.markdown("""## Markdown and Pandas
I am writing Markdown but I can also show a pandas DataFrame.
  """)

    df_cars = data.cars()
    st.write(df_cars.head())

    st.markdown("""## Altair
And I can easily make interactive altair plots.
  """)

    brush = alt.selection_interval(encodings=['x', 'y'])
    repeat_chart = alt.Chart(df_cars).mark_point().encode(
        alt.X(alt.repeat('column'), type='quantitative'),
        alt.Y('Miles_per_Gallon:Q'),
        color=alt.condition(brush, 'Origin:N', alt.value('lightgray')),
        opacity=alt.condition(
            brush, alt.value(0.7), alt.value(0.1))).properties(
                width=150, height=150).add_selection(brush).repeat(
                    column=['Weight_in_lbs', 'Acceleration', 'Horsepower'])

    st.write(repeat_chart)

    st.markdown("""## User Input
I can create a text input field to get input from the user.
  """)

    n = int(st.text_input("How many points do you want plotted:", '100'))

    x = np.random.random(n) * 10
    y = np.random.random(n) * 10
    s = np.random.random(n)

    df = pd.DataFrame({'x': x, 'y': y, 'size': s})

    chart = alt.Chart(df, width=400,
                      height=400).mark_point().encode(
                          x='x',
                          y='y',
                          size=alt.Size('size', legend=None),
                          tooltip=['size']).interactive()

    st.write(chart)
def test_cars_column_names():
    cars = data.cars()
    assert type(cars) is pd.DataFrame
    assert sorted(cars.columns) == [
        "Acceleration",
        "Cylinders",
        "Displacement",
        "Horsepower",
        "Miles_per_Gallon",
        "Name",
        "Origin",
        "Weight_in_lbs",
        "Year",
    ]

    cars = data.cars.raw()
    assert type(cars) is bytes
示例#8
0
def app():

    st.title("Streamlit demo for 202101")

    st.markdown(""" ## Some section
    This is great!
    * bullet 1
    * bullet 2
    """)

    st.sidebar.markdown('''This is the sidebar''')

    df_cars = data.cars()
    st.write(df_cars.head())

    brush = alt.selection_interval(encodings=['x', 'y'])
    repeat_chart = alt.Chart(df_cars).mark_point().encode(
        alt.X(alt.repeat('column'), type='quantitative'),
        alt.Y('Miles_per_Gallon:Q'),
        color=alt.condition(brush, 'Origin:N', alt.value('lightgray')),
        opacity=alt.condition(
            brush, alt.value(0.7), alt.value(0.1))).properties(
                width=150, height=150).add_selection(brush).repeat(
                    column=['Weight_in_lbs', 'Acceleration', 'Horsepower'])

    st.write(repeat_chart)

    st.markdown(""" ## User Input Demo
    """)

    n = int(st.text_input('How many point do you want', '100'))

    x = np.random.random(n) * 10
    y = np.random.random(n) * 10
    s = np.random.random(n)

    df = pd.DataFrame({'x': x, 'y': y, 'size': s})

    chart = alt.Chart(df, width=400,
                      height=400).mark_point().encode(
                          x='x',
                          y='y',
                          size=alt.Size('size', legend=None),
                          tooltip=['size']).interactive()

    st.write(chart)
示例#9
0
def log_altair(step):
    source = data.cars()

    brush = alt.selection(type='interval')

    points = alt.Chart(source).mark_point().encode(
        x='Horsepower:Q',
        y='Miles_per_Gallon:Q',
        color=alt.condition(brush, 'Origin:N',
                            alt.value('lightgray'))).add_selection(brush)

    bars = alt.Chart(source).mark_bar().encode(
        y='Origin:N', color='Origin:N',
        x='count(Origin):Q').transform_filter(brush)

    chart = points & bars

    tracking.log_altair_chart(name='altair_chart', figure=chart, step=step)
示例#10
0
def _log_altair_figure():
    source = data.cars()

    brush = alt.selection(type='interval')

    points = alt.Chart(source).mark_point().encode(
        x='Horsepower:Q',
        y='Miles_per_Gallon:Q',
        color=alt.condition(brush, 'Origin:N',
                            alt.value('lightgray'))).add_selection(brush)

    bars = alt.Chart(source).mark_bar().encode(
        y='Origin:N', color='Origin:N',
        x='count(Origin):Q').transform_filter(brush)

    chart = points & bars

    # Log altair figure as interactive chart in the experiment' artifacts tab.
    log_chart(name='altair_chart', chart=chart)
示例#11
0
    def test_get_html_from_altair(self):
        # given
        source = data.cars()

        chart = (
            alt.Chart(source)
            .mark_circle(size=60)
            .encode(
                x="Horsepower",
                y="Miles_per_Gallon",
                color="Origin",
                tooltip=["Name", "Origin", "Horsepower", "Miles_per_Gallon"],
            )
            .interactive()
        )

        # when
        result = get_html_content(chart)

        # then
        self.assertTrue(result.startswith("<!DOCTYPE html>\n<html>\n<head>\n  <style>"))
示例#12
0
def get_cars():
    time.sleep(3) #Simulamos tiempo de carga
    return data.cars()
示例#13
0
	if sub_topic == "Voice Activity Detection":
		SG_activity.add_activity()

	elif sub_topic == "Voice Activity Detection":
		SG_gender.add_gender()

# IV. Data Visualization

elif topic == "Data Visualization":

    sub_topic = st.sidebar.radio("Project", ["Dataset Explorer", "New-York Uber"])
    page = st.sidebar.selectbox("Choose a page", ["Table", "Exploration"])

    if sub_topic == "Dataset Explorer":

    	df = data.cars()

    	def visualize_data(df, x_axis, y_axis):
    		graph = alt.Chart(df).mark_circle(size=60).encode(
				x=x_axis,
				y=y_axis,
				color='Origin',
				tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']).interactive()
    		st.write(graph)

    	if page == "Table":
    		st.header("Explore the raw table.")
    		st.write("Please select a page on the left.")
    		st.write(df)
    	elif page == "Exploration":
	    	st.title("Data Exploration")
示例#14
0
 

# streamlit run python_streamlit_dashboard.py

# *** IMPORTS ***
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression
from vega_datasets import data as vds

# *** DATAFRAME ***
st.title('Cars DataFrame')
cars = vds.cars().dropna()
st.write(cars)

measures = ['Miles_per_Gallon', 'Cylinders', 'Displacement', 'Horsepower', 'Weight_in_lbs', 'Acceleration']

# *** SCATTER PLOT WITH DROPDOWN SELECTBOXES ***
st.title('Scatter Plot')
scatter_x = st.selectbox('x', measures)
scatter_y = st.selectbox('y', measures)
sns.regplot(x=scatter_x, y=scatter_y, data=cars)
st.pyplot()

# prediction model (i.e.-mpg at 7000 pounds)
st.title('Predict MPG')
weight = st.number_input('enter vehicle weight', min_value=1500, max_value=6000)
X = np.array(cars.Weight_in_lbs).reshape(-1,1)
示例#15
0
"""
Selection Histogram
===================
This chart shows an example of using an interval selection to filter the
contents of an attached histogram, allowing the user to see the proportion
of items in each category within the selection.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data

source = data.cars()

brush = alt.selection(type='interval')

points = alt.Chart().mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=alt.condition(brush, 'Origin:N', alt.value('lightgray'))
).add_selection(
    brush
)

bars = alt.Chart().mark_bar().encode(
    y='Origin:N',
    color='Origin:N',
    x='count(Origin):Q'
).transform_filter(
    brush
)
示例#16
0
plt.plot(epochs, loss, 'r', label='Training Loss')
plt.plot(epochs, val_loss, 'b', label='Validation Loss')
plt.title('Training and validation loss')
plt.legend(loc=0)
plt.savefig('LossPlot.png',dpi=200)
plt.show()

!scp DeepLearn* *.png drive/My\ Drive/DataScience/PlantVillage

!scp *.csv drive/My\ Drive/DataScience/PlantVillage

# Commented out IPython magic to ensure Python compatibility.
# %load_ext google.colab.data_table

from vega_datasets import data
data.cars()

from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode

def take_photo(filename='photo.jpg', quality=0.8):
  js = Javascript('''
    async function takePhoto(quality) {
      const div = document.createElement('div');
      const capture = document.createElement('button');
      capture.textContent = 'Capture';
      div.appendChild(capture);

      const video = document.createElement('video');
      video.style.display = 'block';
                                'y2': [1, 4, 2, 2, 3]})

stacked_area_plot = figure(plot_width=600, plot_height=300)

stacked_area_plot.varea_stack(['y1', 'y2'],
                              x='x',
                              color=('green', 'lightgreen'),
                              source=stacked_area_df)

show(stacked_area_plot)
In [68]:
# stacked_area_plot.varea_stack?
Scatter Plots
In [69]:
# vega data sets cars data
cars = vds.cars()
cars.tail()
Out[69]:
Acceleration	Cylinders	Displacement	Horsepower	Miles_per_Gallon	Name	Origin	Weight_in_lbs	Year
401	15.6	4	140.0	86.0	27.0	ford mustang gl	USA	2790	1982-01-01
402	24.6	4	97.0	52.0	44.0	vw pickup	Europe	2130	1982-01-01
403	11.6	4	135.0	84.0	32.0	dodge rampage	USA	2295	1982-01-01
404	18.6	4	120.0	79.0	28.0	ford ranger	USA	2625	1982-01-01
405	19.4	4	119.0	82.0	31.0	chevy s-10	USA	2720	1982-01-01
In [70]:
# scatter plot

# data
x_scatter = cars.Weight_in_lbs
y_scatter = cars.Miles_per_Gallon
示例#18
0
    'score': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})

src2_x, src2_y = np.meshgrid(range(-5, 5), range(-5, 5))
src2_z = src2_x**2 + src2_y**2

# Convert this grid to columnar data expected by Altair
src2 = pd.DataFrame({
    'a': src2_x.ravel(),
    'b': src2_y.ravel(),
    'c': src2_z.ravel()
})

tests = {
    'test1-1': [
        data.cars(), ('Q', 'Welcome to Jarvis!'),
        ('G', alt.Chart(data.cars()).mark_circle(opacity=0.9).encode(
            x='Horsepower', y='Miles_per_Gallon').to_json()),
        ('Q', 'Can you draw the above graph with natural language?'), 2
    ],
    'test1-2': [
        data.cars(), ('Q', 'Welcome to Jarvis!'),
        ('G', alt.Chart(data.cars()).mark_circle(opacity=0.9).encode(
            x='Horsepower', y='Miles_per_Gallon').to_json()),
        ('G',
         alt.Chart(data.cars()).mark_circle(opacity=0.9 - 0.2 - 0.2).encode(
             x='Horsepower', y='Miles_per_Gallon').to_json()),
        ('Q', 'Can you convert from the upper graph to the lower graph?'),
        ('P', 'scatterplot miles per gallon by horsepower'), 3
    ],
    'test1-3': [
示例#19
0
def SwitchExample(argument):
    from vega_datasets import data
    switcher = {"Airports": data.airports(), "Cars": data.cars()}
    return switcher.get(argument, "Not found!")
示例#20
0
from __future__ import annotations
import typing as t
from vega_datasets import data

if t.TYPE_CHECKING:
    from pandas.core.frame import DataFrame

df: DataFrame = data.cars()
print(df.head())
print(df.describe())
print(df.columns)
grouped_df = df.groupby(by=["Year"]).agg(
    {"Horsepower": ["max", "min", "mean", "std", "count"]})
print(grouped_df)

print("")
print("----------------------------------------")
print("")
df: DataFrame = data.iris()
print(df.head())
print(df.describe())
print(df.columns)
grouped_df = df.groupby(by=["species"]).agg({
    "sepalLength": ["max", "min", "mean", "std", "count"],
    "sepalWidth": ["max", "min", "mean", "std", "count"],
})
print(grouped_df)
示例#21
0
def load_data():
    df = data.cars()
    return df
示例#22
0
"""
Text over a Heatmap
-------------------

An example of a layered chart of text over a heatmap using the cars dataset.
"""
# category: other charts
import altair as alt
from vega_datasets import data

source = data.cars()

# Configure common options
base = alt.Chart(source)
scale = alt.Scale(paddingInner=0)

# Configure heatmap
heatmap = base.mark_rect().encode(alt.X('Cylinders:O', scale=scale),
                                  alt.Y('Origin:O', scale=scale),
                                  color='count()')

# Configure text
text = base.mark_text(baseline='middle').encode(x='Cylinders:O',
                                                y='Origin:O',
                                                text='count()',
                                                color=alt.condition(
                                                    alt.datum['count_*'] > 100,
                                                    alt.value('black'),
                                                    alt.value('white')))

# Draw the chart
示例#23
0
 def __init__(self, path):
     self.cars = data.cars()
     self.path = path
示例#24
0
def test_df():
    test_data = data.cars()
    return test_data
示例#25
0
import altair as alt

from vega_datasets import data

print(data.cars())

cars = data.cars()

chart = alt.Chart(cars).mark_point().encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
)

chart.save('chart.png')
示例#26
0
def get_cars():
    time.sleep(3) #estamos simulando la carga del dataset
    return data.cars()
# Plot / Altair
# Use Altair to create plot specifications for the Vega card.
# ---
import altair
from vega_datasets import data
from h2o_wave import site, ui

spec = altair.Chart(data.cars()).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
    tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon'
             ]).properties(width='container',
                           height='container').interactive().to_json()

page = site['/demo']

page['example'] = ui.vega_card(
    box='1 1 4 5',
    title='Altair Example',
    specification=spec,
)

page.save()
示例#28
0
def get_data_source():
    return data.cars()
示例#29
0
"""
REST API Resource Routing
http://flask-restplus.readthedocs.io
"""

from datetime import datetime
from flask import request
from flask_restplus import Resource, fields

from .security import require_auth
from . import api_rest

import altair
from vega_datasets import data
cars = data.cars()


class SecureResource(Resource):
    """ Calls require_auth decorator on all requests """
    method_decorators = [require_auth]


@api_rest.route('/resource/<string:resource_id>')
class ResourceOne(Resource):
    """ Unsecure Resource Class: Inherit from Resource """
    def get(self, resource_id):
        timestamp = datetime.utcnow().isoformat()
        return {'timestamp': timestamp}

    def post(self, resource_id):
        json_payload = request.json
"""
Violin Plot
-----------
This example shows how to make a Violin Plot using Altair's density transform.
"""
# category: other charts
import altair as alt
from vega_datasets import data

alt.Chart(data.cars()).transform_density(
    'Miles_per_Gallon',
    as_=['Miles_per_Gallon', 'density'],
    extent=[5, 50],
    groupby=['Origin']
).mark_area(orient='horizontal').encode(
    y='Miles_per_Gallon:Q',
    color='Origin:N',
    x=alt.X(
        'density:Q',
        stack='center',
        impute=None,
        title=None,
        axis=alt.Axis(labels=False, values=[0],grid=False, ticks=True),
    ),
    column=alt.Column(
        'Origin:N',
        header=alt.Header(
            titleOrient='bottom',
            labelOrient='bottom',
            labelPadding=0,
        ),