iPython 101 - using numpy, scipy, matplotlib, sqlite3 and Bokeh.

it takes a long time to load - be patient!


In [17]:
a = "hello world - BRUCE"
print(a)
import time

#print("date and time when this Notebook was run on http://localhost:8888/notebooks/ipython_101_notebook-Copy1.ipynb")
print("Date & Time : ",time.strftime("%Y-%m-%d  %H:%M:%S"))


hello world - BRUCE
Date & Ttime :  2016-08-20  15:36:25

In [1]:
# OneNote Python101 Notebook --> Bokeh simple line 1
from bokeh.plotting import figure,  show  
from bokeh.io import output_notebook 
output_notebook() 
p = figure(plot_width=400, plot_height=400)  
# add a line renderer  
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)  
show(p)


Loading BokehJS ...
Out[1]:

<Bokeh Notebook handle for In[1]>


In [ ]:


In [102]:
#from numpy import pi,linspace,sin
#from matplotlib.pyplot import plot
#x = linspace(0,3*pi,500)
#print(x)
#plot(x,sin(x**2))
#title('A simple chirp');
# OneNote Python 101 - Worked Exampes - bokeh multi line, multi axis 


import numpy as np 
from bokeh.plotting import  show, figure 
from bokeh.io import output_notebook

x = np.linspace(0, 4*np.pi, 100) 
#print(x)
y = np.sin(x) 

output_notebook() 

p = figure() 
p.circle(x, y, legend="sin(x)") 
p.line(x, y, legend="sin(x)") 
p.line(x, 2*y, legend="2*sin(x)", 
       line_dash=[4, 4], line_color="orange", line_width=2) 
p.square(x, 3*y, legend="3*sin(x)", fill_color=None, line_color="green") 
p.line(x, 3*y, legend="3*sin(x)", line_color="green") 

show(p)


Loading BokehJS ...
Out[102]:

<Bokeh Notebook handle for In[102]>


In [ ]:
#from bokeh.plotting import figure, output_file, show

# prepare some data
#x = [1, 2, 3, 4, 5]
#y = [6, 7, 2, 4, 5]

# output to static HTML file
#output_file("lines.html", title="line plot example")

# create a new plot with a title and axis labels
#p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

# add a line renderer with legend and line thickness
#p.line(x, y, legend="Temp.", line_width=2)

# show the results
#show(p)

In [116]:
import pandas as pd 

from bokeh.plotting import figure 
from bokeh.io import output_file, show
from bokeh.models import (
  GMapPlot, GMapOptions, ColumnDataSource, Circle, DataRange1d, PanTool, WheelZoomTool, BoxSelectTool
)

#Location = r'C:\Users\bruce\OneDrive\Udacity_Intro_to_Data_Analysis\airports.dat'
#AAPL = pd.read_csv(Location,

AAPL = pd.read_csv( 
    "https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat",
header=None,
sep=',',    
names = ['Airport_ID', 'Name', 'City', 'Country', 'IATA_FAA', 'ICAO', 'Latitude', 'Longitude', 'Altitude', 'Timezone','DST','Tz database_time_zone'],

)

#1,"Goroka","Goroka","Papua New Guinea","GKA","AYGA",-6.081689,145.391881,5282,10,"U","Pacific/Port_Moresby"
#507,"Heathrow","London","United Kingdom","LHR","EGLL",51.4775,-0.461389,83,0,"E","Europe/London"
#503,"City","London","United Kingdom","LCY","EGLC",51.505278,0.055278,19,0,"E","Europe/London"
print(type(AAPL))
print(AAPL.dtypes)
print(AAPL.describe())

print("\n")
print("display.max_columns = ", pd.get_option("display.max_columns"))

#print(AAPL.head(10))

# Method 1:
print("Using Method 1") 
print(AAPL[AAPL['Altitude'] == AAPL['Altitude'].max()])
print("\n")


# Method 2:
print("Using Method 2") 
print("\n")
Sorted = AAPL.sort_values(['Altitude'], ascending=False)
print(Sorted.head(1))
print("\n")
print(AAPL['Altitude'].max())
print("\n")

    
#print(AAPL[AAPL['City'] == "Leicester"]) 
#print(AAPL[AAPL['City'] == "London"])
BAPL=AAPL[AAPL['City'] == "London"]
print("type(BAPL) ", type(BAPL))
print("\n")
print(BAPL[['City','Country','Latitude','Longitude']])
print("\n")

CAPL=BAPL[BAPL['Country'] == "United Kingdom"]
print("type(CAPL) ", type(CAPL))
print("\n")
print(CAPL[['Name','City','Country','Latitude','Longitude']])
print("\n")




#map_options = GMapOptions(lat=30.29, lng=-97.73, map_type="roadmap", zoom=11)
map_options = GMapOptions(lat=51.1, lng=-0.40,map_type="roadmap", zoom=9)

plot = GMapPlot(
    x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options, title="London"
)


source = ColumnDataSource(
    data=dict(
#        lat=CAPL['Latitude'],
#        lon=CAPL['Latitude'],
        lat=[51.477500, 51.148056],
        lon=[-0.461389, -0.190278],
#        lat=[30.29, 30.20, 30.29],
#        lon=[-97.70, -97.74, -97.78],
    )
)
# London Heathrow 
#lat=51.477500
#lon=-0.461389

#London Gatwick
#Lat=51.148056
#Lon=-0.190278


#circle = Circle(x="lon", y="lat", size=5, fill_color="blue", fill_alpha=0.8, line_color=None)
plot.add_glyph(source, circle)

plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool())
output_file("gmap_plot.html")
show(plot)


<class 'pandas.core.frame.DataFrame'>
Airport_ID                 int64
Name                      object
City                      object
Country                   object
IATA_FAA                  object
ICAO                      object
Latitude                 float64
Longitude                float64
Altitude                   int64
Timezone                 float64
DST                       object
Tz database_time_zone     object
dtype: object
        Airport_ID     Latitude    Longitude      Altitude     Timezone
count  8107.000000  8107.000000  8107.000000   8107.000000  8107.000000
mean   4766.361046    26.817720    -3.921969    933.449365     0.169236
std    2943.205193    27.866953    85.900873   1624.740899     5.737326
min       1.000000   -89.999997  -179.877000  -1266.000000   -12.000000
25%    2091.500000     8.824928   -79.022498     38.000000    -5.000000
50%    4257.000000    34.987800     5.292028    272.000000     1.000000
75%    7508.500000    47.957599    49.785821   1020.000000     4.000000
max    9541.000000    82.517778   179.951000  14472.000000    13.000000


display.max_columns =  20
Using Method 1
      Airport_ID             Name      City Country IATA_FAA  ICAO  Latitude  \
7875        9310  Yading Daocheng  Daocheng   China      DCY  ZUDC   29.3231   

      Longitude  Altitude  Timezone DST Tz database_time_zone  
7875   100.0533     14472       8.0   N        Asia/Chongqing  


Using Method 2


      Airport_ID             Name      City Country IATA_FAA  ICAO  Latitude  \
7875        9310  Yading Daocheng  Daocheng   China      DCY  ZUDC   29.3231   

      Longitude  Altitude  Timezone DST Tz database_time_zone  
7875   100.0533     14472       8.0   N        Asia/Chongqing  


14472


type(BAPL)  <class 'pandas.core.frame.DataFrame'>


        City         Country   Latitude  Longitude
172   London          Canada  43.033056 -81.151111
488   London  United Kingdom  51.874722  -0.368333
498   London  United Kingdom  51.148056  -0.190278
499   London  United Kingdom  51.505278   0.055278
503   London  United Kingdom  51.477500  -0.461389
543   London  United Kingdom  51.885000   0.235000
5968  London  United Kingdom  51.528400  -0.133100
6213  London  United Kingdom  51.515833  -0.176111
6241  London  United Kingdom  51.494999  -0.144643
6270  London  United Kingdom  51.530000  -0.125000
6296  London  United Kingdom  51.470000  -0.177833
6597  London  United Kingdom  51.532519  -0.126300
6981  London   United States  37.086889 -84.077389
7158  London  United Kingdom  51.508056  -0.127778
7300  London  United Kingdom  51.532600   0.123300
7478  London  United Kingdom  51.528400  -0.133100
7543  London  United Kingdom  51.496400  -0.143910
7906  London  United Kingdom  51.503100  -0.114700
7908  London          Canada  42.981900 -81.246400
7921  London  United Kingdom  51.528400  -0.133100
7922  London  United Kingdom  51.503100  -0.114700


type(CAPL)  <class 'pandas.core.frame.DataFrame'>


                               Name    City         Country   Latitude  \
488                           Luton  London  United Kingdom  51.874722   
498                         Gatwick  London  United Kingdom  51.148056   
499                            City  London  United Kingdom  51.505278   
503                        Heathrow  London  United Kingdom  51.477500   
543                        Stansted  London  United Kingdom  51.885000   
5968  London Euston Railway Station  London  United Kingdom  51.528400   
6213             Paddington Station  London  United Kingdom  51.515833   
6241    London Victoria Bus Station  London  United Kingdom  51.494999   
6270              London St Pancras  London  United Kingdom  51.530000   
6296                London Heliport  London  United Kingdom  51.470000   
6597     St Pancras Railway Station  London  United Kingdom  51.532519   
7158                   All Airports  London  United Kingdom  51.508056   
7300           London - Kings Cross  London  United Kingdom  51.532600   
7478                 Euston Station  London  United Kingdom  51.528400   
7543               Victoria Station  London  United Kingdom  51.496400   
7906         Waterloo International  London  United Kingdom  51.503100   
7921                 Euston Station  London  United Kingdom  51.528400   
7922         Waterloo International  London  United Kingdom  51.503100   

      Longitude  
488   -0.368333  
498   -0.190278  
499    0.055278  
503   -0.461389  
543    0.235000  
5968  -0.133100  
6213  -0.176111  
6241  -0.144643  
6270  -0.125000  
6296  -0.177833  
6597  -0.126300  
7158  -0.127778  
7300   0.123300  
7478  -0.133100  
7543  -0.143910  
7906  -0.114700  
7921  -0.133100  
7922  -0.114700  


Out[116]:

<Bokeh Notebook handle for In[116]>


In [1]:
__author__ = 'Bruce.Woodley'
# version 3
#http://sqlite.org/
#https://addons.mozilla.org/en-us/firefox/addon/sqlite-manager/
#For Ankur Pandey's  "sql learnings"
def main():
    import sqlite3
    import time

    conn = sqlite3.connect('music.sqlite3')
    cur = conn.cursor()
    cur.execute('DROP TABLE IF EXISTS Tracks')
    cur.execute('CREATE TABLE Tracks (title TEXT , plays INTEGER)')
    conn.close()

    conn = sqlite3.connect('music.sqlite3')
    cur = conn.cursor()
    cur.execute('INSERT INTO Tracks (title, plays) VALUES (?, ?)', ('Thunderstruck',20) )
    cur.execute('INSERT INTO Tracks (title, plays) VALUES (?, ?)',('My Way', 15) )
    cur.execute('INSERT INTO Tracks (title, plays) VALUES (?, ?)',('Lean On (by Major Lazer & DJ Snake)', 100) )
    conn.commit()

    print("Tracks:")
    cur.execute('SELECT title, plays FROM Tracks')
    for row in cur :
       print(row)
    cur.execute('DELETE FROM Tracks WHERE plays < 100')
    conn.commit()
    cur.close()
    
    print("\n")
    print("Ankur's SQL Learnings executed on date and time: ",time.strftime("%Y-%m-%d  %H:%M:%S"))
    return

main()


Tracks:
('Thunderstruck', 20)
('My Way', 15)
('Lean On (by Major Lazer & DJ Snake)', 100)


Ankur's SQL Learnings executed on date and time:  2016-08-20  16:25:15

In [ ]:
import pandas as pd 
from bokeh.plotting import figure 

AAPL = pd.read_csv( 
    "http://ichart.yahoo.com/table.csv?s=AAPL&a=0&b=1&c=2000&d=0&e=1&f=2015", 
    parse_dates=['Date']) 
#print(AAPL)

MSFT = pd.read_csv( 
    "http://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000&d=0&e=1&f=2015", 
    parse_dates=['Date']) 

IBM = pd.read_csv( 
    "http://ichart.yahoo.com/table.csv?s=IBM&a=0&b=1&c=2000&d=0&e=1&f=2015", 
    parse_dates=['Date']) 

def make_figure(): 
    p = figure(x_axis_type="datetime", width=700, height=300) 
    p.line(AAPL['Date'], AAPL['Adj Close'], color='#A6CEE3', legend='AAPL') 
    p.line(IBM['Date'], IBM['Adj Close'], color='#33A02C', legend='IBM') 
    p.line(MSFT['Date'], MSFT['Adj Close'], color='#FB9A99', legend='MSFT') 
    p.title = "Stock Closing Prices" 
    p.grid.grid_line_alpha=0.3 
    p.xaxis.axis_label = 'Date' 
    p.yaxis.axis_label = 'Price' 
    p.legend.orientation = "top_left" 
    return p 

from bokeh.io import output_notebook, show 

output_notebook() 

p = make_figure() 

show(p)

In [ ]:
import numpy as np 
from bokeh.plotting import  show, figure 
from bokeh.io import output_notebook

x = np.linspace(-1.0*np.pi, 2*np.pi, 400) 
#print(x)
y1 = np.sin(x) 
#y2 =np.exp(x)

output_notebook() 

p = figure() 
p.circle(x, y1, legend="sin(x)",line_color="orange") 
#p.circle(x, y2, legend="exp(x)",line_color="orange") 




show(p)

In [ ]:


In [ ]:


In [ ]:
from bokeh.sampledata.autompg import autompg as df 
#from bokeh.charts import Scatter, output_file, show 
from bokeh.charts import Scatter  
from bokeh.io import output_notebook, show 



scatter = Scatter(df, x='mpg', y='hp', color='cyl', marker='origin', 
                  title="mpg", xlabel="Miles Per Gallon", ylabel="Horsepower") 

#output_file('scatter.html') 
output_notebook() 


show(scatter)

In [ ]:
#from bokeh.sampledata.autompg import autompg as df
from bokeh.sampledata.autompg import autompg
from bokeh.sampledata.iris import flowers
#from bokeh.sampledata.stocks  import AAPL

#print(type(df))
print(str(flowers))
#print(df.index)
#print(df.info)

In [ ]:
a=0xfe
print(hex(a), int(a), oct(a), bin(a))

print(int('010', 2)  ) 
print(int('010', 8)  ) 
print(int('010', 10)  ) 
print(int('010', 16)  ) 

print(int('ff', 16)  )

In [ ]:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [1, 2, 6]
fig = plt.figure()
ax = plt.axes()

plt.plot(x, y)
plt.show()

In [ ]:
'''
  Example of selecting a transformation from two equally probable
  transformations
  '''
  import matplotlib.pyplot as plt
  import random

  def transformation_1(p):
      x = p[0]
      y = p[1]
      return x + 1, y - 1

  def transformation_2(p):
      x = p[0]
      y = p[1]
      return x + 1, y + 1

  def transform(p):
      # List of transformation functions
      transformations = [transformation_1, transformation_2]
      # Pick a random transformation function and call it
      t = random.choice(transformations)
      x, y = t(p)
      return x, y

  def build_trajectory(p, n):
      x = [p[0]]
      y = [p[1]]
      for i in range(n):
          p = transform(p)
          x.append(p[0])
          y.append(p[1])
      return x, y
  if __name__ == '__main__':
      # Initial point
      p = (1, 1)
      n = int(input('Enter the number of iterations: '))
      x, y = build_trajectory(p, n)
      # Plot
      plt.plot(x, y)
      plt.xlabel('X')
      plt.ylabel('Y')
      plt.show()

In [ ]:
'''
Draw a Barnsley Fern
'''
import random
import matplotlib.pyplot as plt

def transformation_1(p):
    x = p[0]
    y = p[1]
    x1 = 0.85*x + 0.04*y
    y1 = -0.04*x + 0.85*y + 1.6
    return x1, y1

def transformation_2(p):
    x = p[0]
    y = p[1]
    x1 = 0.2*x - 0.26*y
    y1 = 0.23*x + 0.22*y + 1.6
    return x1, y1

def transformation_3(p):
    x = p[0]
    y = p[1]
    x1 = -0.15*x + 0.28*y
    y1 = 0.26*x + 0.24*y + 0.44
    return x1, y1

def transformation_4(p):
    x = p[0]
    y = p[1]
    x1 = 0
    y1 = 0.16*y
    return x1, y1

def get_index(probability):
      r = random.random()
      c_probability = 0
      sum_probability = []
      for p in probability:
          c_probability += p
          sum_probability.append(c_probability)
      for item, sp in enumerate(sum_probability):
          if r <= sp:
              return item
      return len(probability)-1

def transform(p):
      # List of transformation functions
      transformations = [transformation_1, transformation_2,
                             transformation_3, transformation_4]
      probability = [0.85, 0.07, 0.07, 0.01]
      # Pick a random transformation function and call it
      tindex = get_index(probability)
      t = transformations[tindex]
      x, y = t(p)
      return x, y

def draw_fern(n):
      # We start with (0, 0)
      x = [0]
      y = [0]

      x1, y1 = 0, 0
      for i in range(n):
         x1, y1 = transform((x1, y1))
         x.append(x1)
         y.append(y1)
      return x, y

if __name__ == '__main__':
      n = int(input('Enter the number of points in the Fern: '))
      x, y = draw_fern(n)
      # Plot the points
      plt.plot(x, y, 'o')
      plt.title('Fern with {0} points'.format(n))
      plt.show()


Enter the number of points in the Fern: 34

In [ ]:
import math
math.sin(math.pi/2)

In [ ]:
import inspect 
import sympy
print(sympy.sin(math.pi/2))



#print(dir(sympy))
#print(inspect.getclasstree(sympy))
#print(inspect.getmembers(sympy.tan,isclass))


from sympy import sin, sine_transform, solve
print(sin(math.pi/2))


print("\n")
print ("inspect.ismodule(sympy.sin):", inspect.ismodule(sympy.sin))
print ("inspect.ismethod(sympy.sin):", inspect.ismethod(sympy.sin))
print ("inspect.isclass(sympy.sin):", inspect.isclass(sympy.sin))
print ("inspect.isfunction(sympy.sin)", inspect.isfunction(sympy.sin))
#print(dir(solve))

from sympy import Symbol
theta = Symbol('theta')
print( sympy.sin(theta) + sympy.sin(theta) ) 

from sympy import sin, solve, Symbol
u = Symbol('u')
t = Symbol('t')
g = Symbol('g')
theta1 = Symbol('theta1')
solve(u*sin(theta1)-g*t, t)



x = Symbol('x', positive=True)
if (x+5) > 0:
    print('Do Something where positive=True')
else:
    print('Do Something else where not(positive=True)')


from sympy import Limit, Symbol, S
x = Symbol('x')
y = Limit(1/x, x, S.Infinity)
print(type(y), y)

In [ ]:
import inspect 
import re




print(dir(re))
print("\n")
print ("inspect.ismodule(re.findall):", inspect.ismodule(re.findall))
print ("inspect.ismethod(re.findall):", inspect.ismethod(re.findall))
print ("inspect.isclass(re.findall):", inspect.isclass(re.findall))
print ("inspect.isfunction(re.findall)", inspect.isfunction(re.findall))


#print(inspect.getclasstree(re))
print(inspect.getmembers(re))




#print ("inspect.ismethod(findall):", inspect.ismethod(finall))
#print(dir(solve))

In [ ]:
#from numpy import pi,linspace,sin
#from matplotlib.pyplot import plot
#x = linspace(0,3*pi,500)
#print(x)
#plot(x,sin(x**2))
#title('A simple chirp');
# OneNote Python 101 - Worked Exampes - bokeh multi line, multi axis 


import numpy as np 
from bokeh.plotting import  show, figure 
from bokeh.io import output_notebook

x = np.linspace(0, 2*np.pi, 100) 
#print(x)
y = np.sin(1/x) 

output_notebook() 

p = figure() 
 
p.line(x, y, legend="sin(1/x)") 



show(p)