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"))
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)
Out[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)
Out[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)
Out[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()
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()
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)