%%bash ls -lh ~/ | head -n 3 !uname -a print("This is Python!") def fact(n): if n <= 0: return 1 return n*fact(n-1) fact(20) %%ruby puts 'This is Ruby playing with Python!!!' from IPython.display import IFrame IFrame('http://nbviewer.ipython.org/', width='100%', height=350) %install_ext http://raw.github.com/jrjohansson/version_information/master/version_information.py %load_ext version_information %version_information numpy, scipy, matplotlib, sympy, scikit_learn, nltk, pandas import numpy as np from __future__ import print_function # a vector: the argument to the array function is a Python list v = np.array([11, 12, 13, 14]) print('v =\n{}'.format(v)) # a matrix: the argument to the array function is a nested Python list M = np.array([[2, 1], [3, 4]]) print('M =\n{}'.format(M)) print (type(v), type(M)) print("v shape is {}".format(v.shape)) print("M shape is {}".format(M.shape)) print("Data type of v is {}".format(v.dtype)) print() print("M transpose =\n{}".format(M.T)) print() M.sort(axis=1) print("M sorted by row =\n{}".format(np.asarray(M))) print() print("v stats are mean = {}, standard deviation = {:.4}, max = {}, min ={}".format(v.mean(), v.std(), v.max(), v.min())) print() print("Converting matrix M to a vector {}".format(M.flatten())) print("Converting vector v to a matrix=\n{}".format(v.reshape(2,2))) print() print("M matrix size is {} and number of dimensions is {}".format(M.size, M.ndim)) x = np.arange(0, 10, 1) # arguments: start, stop, step print("Create a range\n{}".format(x)) print() # using linspace, both end points ARE included x = np.linspace(0, 10, 41) print("Create a spaced range\n{}".format(x)) print() # uniform random numbers in [0,1] x = np.random.rand(4,4) print("Create a uniform random matrix (4,4)\n{}".format(x)) print() # a diagonal matrix x = np.diag([1,2,3]) print("Create a digonal matrix\n{}".format(x)) print() x = np.zeros((3,3)) print("Create a zero matrix (3,3) \n{}".format(x)) print("v[0] = {}\n".format(v[0])) print("M =\n{}\n".format(M)) print("M[1, 1] = {}\n".format(M[1,1])) print("M[1] = {}\n".format(M[1])) print("M[1, :] = {}\n".format(M[1, :])) print("M[:, 1] = {}\n".format(M[:, 1])) print("M[1, :] = 0") M[1, :] = 0 print("M =\n{}\n".format(M)) A = np.array([[n+m*10 for n in range(5)] for m in range(5)]) print("A =\n{}\n".format(A)) print("A[1:4, 1:4]=\n{}\n".format(A[1:4, 1:4])) print("A[::2, ::2]=\n{}\n".format(A[::2, ::2])) print("A[ [1,4] ]=\n{}\n".format(A[[1,4]])) print("A[ [1,4], [2,-1] ]=\n{}\n".format(A[[1,4],[2,-1]])) A = np.array([[n+m*10 for n in range(5)] for m in range(5)]) print("A =\n{}\n".format(A)) print("A > 20 =\n{}\n".format(A > 20)) print("np.where(A > 20) =\n{}\n".format(np.where(A > 20))) print("np.argwhere(A > 20) =\n{}\n".format(np.argwhere(A > 20))) print("A - 10 =\n{}\n".format(A - 10)) print("A * 10 =\n{}\n".format(A * 10)) print("A * A =\n{}\n".format(A * A)) print("np.linalg.det(A) = {}\n".format(np.linalg.det(A))) try: print("np.linalg.inv(A) = {}\n".format(np.linalg.inv(A))) except np.linalg.LinAlgError as e: print("Matrix is singular") v = np.arange(5) print("v = {}\n".format(v)) print("||v|| = np.linalg.norm(v) = {}\n".format(np.linalg.norm(v))) print("np.dot(v.T, v) = {}\n".format(np.dot(v.T, v))) print("np.dot(v.T, v) ** 0.5 = {}".format(np.dot(v.T, v) ** 0.5)) print("v.shape = {}".format(v.shape)) u = v[:, np.newaxis] print("u = v[np.newaxis,:] =\n{}\n".format(u)) print("u.shape = {}".format(u.shape)) print("np.dot(u, u.T) =\n{}\n".format(np.dot(u, u.T))) #print("np.linalg.inv(") A = np.random.randint(0, 100, (4, 5)) v = np.arange(5) + 1. u = np.arange(4) + 2. print("A =\n{}\n".format(A)) print("A.max() = {}".format(A.max())) print("A.max(axis=0) = {}".format(A.max(axis=0))) print("A.min(axis=1) = {}".format(A.min(axis=1))) print() print("v = {}".format(v)) print("A / v =\n{}\n".format(A/v)) print() print("u = {}".format(u)) print("(A.T - u).T =\n{}\n".format((A.T-u).T)) print("np.diff(A, axis=0) =\n{}\n".format(np.diff(A, axis=0))) print("np.cumsum(A, axis=1) =\n{}\n".format(np.cumsum(A, axis=1))) %matplotlib inline import matplotlib.pyplot as plt x = np.linspace(0, 5, 10) y = x ** 2 fig, ax = plt.subplots() ax.plot(x, x**2, label="$y = x^2$") ax.plot(x, x**3, label="y = x**3") ax.legend(loc=2); # upper left corner ax.set_xlabel('x') ax.set_ylabel('y', fontsize=38) ax.set_title('Advertise Here'); xx = np.linspace(-0.75, 1., 100) n = np.array([0,1,2,3,4,5]) fig, axes = plt.subplots(1, 4, figsize=(12,3)) axes[0].scatter(xx, xx + 0.25*np.random.randn(len(xx))) axes[0].set_title("scatter") axes[1].step(n, n**2, lw=2) axes[1].set_title("step") axes[2].bar(n, n**2, align="center", width=0.5, alpha=0.5) axes[2].set_title("bar") axes[3].fill_between(x, x**2, x**3, color="green", alpha=0.5); axes[3].set_title("fill_between"); # A histogram n = np.random.randn(100000) fig, axes = plt.subplots(1, 2, figsize=(12,4)) axes[0].hist(n) axes[0].set_title("Default histogram") axes[0].set_xlim((min(n), max(n))) axes[1].hist(n, cumulative=True, bins=50) axes[1].set_title("Cumulative detailed histogram") axes[1].set_xlim((min(n), max(n))); from mpl_toolkits.mplot3d.axes3d import Axes3D alpha = 0.7 phi_ext = 2 * np.pi * 0.5 def flux_qubit_potential(phi_m, phi_p): return 2 + alpha - 2 * np.cos(phi_p)*np.cos(phi_m) - alpha * np.cos(phi_ext - 2*phi_p) phi_m = np.linspace(0, 2*np.pi, 100) phi_p = np.linspace(0, 2*np.pi, 100) X,Y = np.meshgrid(phi_p, phi_m) Z = flux_qubit_potential(X, Y).T fig = plt.figure(figsize=(8,6)) ax = fig.add_subplot(1,1,1, projection='3d') ax.plot_surface(X, Y, Z, rstride=4, cstride=4, alpha=0.25) cset = ax.contour(X, Y, Z, zdir='z', offset=-np.pi, cmap=plt.cm.coolwarm) cset = ax.contour(X, Y, Z, zdir='x', offset=-np.pi, cmap=plt.cm.coolwarm) cset = ax.contour(X, Y, Z, zdir='y', offset=3*np.pi, cmap=plt.cm.coolwarm) ax.set_xlim3d(-np.pi, 2*np.pi); ax.set_ylim3d(0, 3*np.pi); ax.set_zlim3d(-np.pi, 2*np.pi); import prettyplotlib as ppl import matplotlib as mpl np.random.seed(12) fig, ax = plt.subplots(1) # Show the whole color range for i in range(8): x = np.random.normal(loc=i, size=1000) y = np.random.normal(loc=i, size=1000) ppl.scatter(ax, x, y, label=str(i)) ppl.legend(ax) _ = ax.set_title('prettyplotlib `scatter` example\nshowing default color cycle and scatter params') from IPython.display import IFrame IFrame('http://matplotlib.org/gallery.html#lines_bars_and_markers', width='100%', height=550) import mpld3 mpld3.enable_notebook() np.random.seed(0) P = np.random.random(size=10) A = np.random.random(size=10) x = np.linspace(0, 10, 100) data = np.array([[x, Ai * np.sin(x / Pi)] for (Ai, Pi) in zip(A, P)]) fig, ax = plt.subplots(2) points = ax[1].scatter(P, A, c=P + A, s=200, alpha=0.5) ax[1].set_xlabel('Period') ax[1].set_ylabel('Amplitude') colors = plt.cm.ScalarMappable().to_rgba(P + A) for (x, l), c in zip(data, colors): ax[0].plot(x, l, c=c, alpha=0.5, lw=3) mpld3.disable_notebook() import bokeh try: from bokeh.sampledata import us_counties, unemployment except: bokeh.sampledata.download() from bokeh.sampledata import us_counties, unemployment from bokeh.plotting import * colors = ["#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043"] county_xs=[ us_counties.data[code]['lons'] for code in us_counties.data if us_counties.data[code]['state'] == 'tx' ] county_ys=[ us_counties.data[code]['lats'] for code in us_counties.data if us_counties.data[code]['state'] == 'tx' ] county_colors = [] for county_id in us_counties.data: if us_counties.data[county_id]['state'] != 'tx': continue try: rate = unemployment.data[county_id] idx = min(int(rate/2), 5) county_colors.append(colors[idx]) except KeyError: county_colors.append("black") output_notebook() patches(county_xs, county_ys, fill_color=county_colors, fill_alpha=0.7, line_color="white", line_width=0.5, title="Texas Unemployment 2009") show() from IPython.display import IFrame IFrame('http://bokeh.pydata.org/docs/gallery.html', width='100%', height=550) from IPython.html.widgets import interact, RadioButtonsWidget, IntSliderWidget, TextWidget def plot_sine(freq): x = np.linspace(-np.pi, np.pi, num=1000) plt.plot(x, np.sin(2*np.pi*freq*x)) interact(plot_sine, freq=(1, 10, 0.5)) def plot_sine2(amplitude, color, title): fig, ax = plt.subplots(figsize=(4, 3), subplot_kw={'axisbg':'#EEEEEE', 'axisbelow':True}) ax.grid(color='w', linewidth=2, linestyle='solid') x = np.linspace(0, 10, 1000) ax.plot(x, amplitude * np.sin(x), color=color, lw=5, alpha=0.4) ax.set_xlim(0, 10) ax.set_ylim(-10.1, 10.1) ax.set_title(title) return fig interact(plot_sine2, amplitude=IntSliderWidget(min=0, max=10, step=1,value=1), color=RadioButtonsWidget(values=['blue', 'green', 'red']), title=TextWidget(value="Advertise here")) from IPython.display import IFrame IFrame('https://plot.ly/feed', width='100%', height=550) import pandas as pd from pandas import Series, DataFrame labels = ['a', 'b', 'c', 'd', 'e'] s = Series([1, 2, 3, 4, 5], index=labels) s print("'b' in s = {}".format('b' in s)) print(" s['b'] = {}".format(s['b'])) mapping = s.to_dict() mapping Series(mapping) import pandas.io.data import datetime aapl = pd.io.data.get_data_yahoo('AAPL', start=datetime.datetime(2006, 10, 1), end=datetime.datetime(2012, 1, 1)) aapl.head() aapl.to_csv('aapl_ohlc.csv') !head aapl_ohlc.csv df = pd.read_csv('aapl_ohlc.csv', index_col='Date', parse_dates=True) df.head() df.index df[['Open', 'Close']].head() print(type(df['Open'])) print(type(df[['Open', 'Close']])) df['diff'] = df.Open - df.Close df.head() close_px = df['Adj Close'] mavg = pd.rolling_mean(close_px, 40) close_px.plot(label='AAPL') mavg.plot(label='mavg') plt.legend(loc='best') df = pd.io.data.get_data_yahoo(['AAPL', 'Googl', 'GE', 'IBM', 'KO', 'MSFT', 'PEP'], start=datetime.datetime(2010, 1, 1), end=datetime.datetime(2013, 1, 1))['Adj Close'] rets = df.pct_change() df.head() _ = pd.scatter_matrix(rets, diagonal='kde', figsize=(10, 10)) corr = rets.corr() plt.imshow(corr, cmap='hot', interpolation='none') plt.colorbar() plt.xticks(range(len(corr)), corr.columns) plt.yticks(range(len(corr)), corr.columns); import nltk nltk.download("punkt") sentences = """This is Rami. At eight o'clock on Thursday morning James Arthur didn't feel very good.""" sents = nltk.sent_tokenize(sentences) sents words = nltk.word_tokenize(sents[1]) words nltk.download("maxent_treebank_pos_tagger") tagged = nltk.pos_tag(words) tagged nltk.download("maxent_ne_chunker") nltk.download("words") entities = nltk.chunk.ne_chunk(tagged) list(entities.subtrees(filter=lambda x: x.node == 'PERSON')) stemmer = nltk.stem.LancasterStemmer() words = u"Stemming is funnier than a bummer says the sushi loving computer scientist".split() [stemmer.stem(w) for w in words] from lxml import html import requests from IPython.display import IFrame IFrame('http://econpy.pythonanywhere.com/ex/001.html', width='100%', height=250) page = requests.get('http://econpy.pythonanywhere.com/ex/001.html') tree = html.fromstring(page.text) #This will create a list of buyers: buyers = tree.xpath('//div[@title="buyer-name"]/text()') #This will create a list of prices prices = tree.xpath('//span[@class="item-price"]/text()') print('Buyers: ', buyers) print() print('Prices: ', prices) from bs4 import BeautifulSoup r = requests.get("http://www.google.com") data = r.text soup = BeautifulSoup(data) for link in soup.find_all('a'): print(link.get('href')) import networkx as nx G = nx.karate_club_graph() nx.draw_spring(G) plt.show()