In [12]:
'''
The following will produce a line graph representing
the order (represented by p-set) in which each learner's
parameters converged
'''


Out[12]:
"\nThe following will produce a line graph representing\nthe order (represented by p-set) in which each learner's\nparameters converged\n"

In [13]:
import pandas as pd
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go
import seaborn as sns
import random

In [14]:
matplotlib inline

In [15]:
# Import and view data from out.csv
df = pd.read_csv('out.csv')
df.head()


Out[15]:
p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13
0 0 0 0 0 0 0 0 1 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 3 1 0
2 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 1 0 0 0 3 0 0 3 0 0

In [22]:
# Get the number of rows in the dataframe
numLearners = df.shape[0]

# Create a list of lists, each inner list represents
# a parameter and the sentence on which it converged on
rowList = []
for i in range(0, numLearners):
    # Use iloc to grab a row
    # and convert it to a list
    l = list(df.iloc[[i]].values.flatten())
    dbList = []
    for j in range(0, 13):
        dbList.append([l[j], j+1])
    dbList.sort(key=lambda x: x[0])
    rowList.append(dbList)

In [1]:
# Returns one of five colors based on the current
# numbers's last digit. This will be assigned
# to a line in the graph representing a learner
def get_line_color(num):
    lastDigit = num % 10
    if lastDigit == 0 or lastDigit == 5:
        return (22, 96, 167)
    elif lastDigit == 1 or lastDigit == 6:
        return (0, 153, 51)
    elif lastDigit == 2 or lastDigit == 7:
        return (153, 51, 153)
    elif lastDigit == 3 or lastDigit == 8:
        return (255, 204, 0)
    else: # lastDigit == 4 or lastDigit == 9
        return (205, 12, 24)

In [2]:
# Stores every completed line that will appear
# on the graph
data = []
for i in range(0, numLearners):
    # Variables used to store the x-axis
    # and y axis data of each learner
    xData = []
    yData = []
    
    # The appropiate data is added,
    # xData stores the p-set of each parameter
    # yData stores the corresponding parameter number
    psetCounter = 1
    currentConvergenceTime = rowList[i][0][0]
    for j in range(0, 13):
        if rowList[i][j][0] != currentConvergenceTime:
            psetCounter += 1
            currentConvergenceTime = rowList[i][j][0]
        xData.append(psetCounter)
        yData.append(rowList[i][j][1])
    trace = go.Scatter(
        x = xData,
        y = yData,
        name = 'child{}'.format(i+1),
        line = dict(
            # Random color is chosen of the five possible
            color = ('rgb{}'.format(get_line_color(i))),
            width = 2)
    )
    data.append(trace)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-9910f02ad968> in <module>()
      2 # on the graph
      3 data = []
----> 4 for i in range(0, numLearners):
      5     # Variables used to store the x-axis
      6     # and y axis data of each learner

NameError: name 'numLearners' is not defined

In [19]:
# Format the graph
# xaxis and yaxis describe
# the layout specifications for each
layout = go.Layout(
    title = 'P-sets of Parameters',
    xaxis=dict(
        autotick=False,
        ticks='outside',
        tick0=0,
        dtick=1.00,
        ticklen=8,
        tickwidth=4,
        tickcolor='#000',
        range=[0, 13],
        title='P-set'
    ),
    yaxis=dict(
        autotick=False,
        ticks='outside',
        tick0=0,
        dtick=1.0,
        ticklen=8,
        tickwidth=4,
        tickcolor='#000',
        range=[1, 13],
        title='Parameters'
    )
)

In [20]:
# Plot and embed the graph in the ipython notebook
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='styled-line')


Out[20]:

In [ ]: