In [1]:
names = ['alice', 'jonathan', 'bobby']
ages = [24, 32, 45]
ranks = ['kinda cool', 'really cool', 'insanely cool']
In [3]:
for (name, age, rank) in zip(names, ages, ranks):
print name, age, rank
In [4]:
for index, (name, age, rank) in enumerate(zip(names, ages, ranks)):
print index, name, age, rank
In [5]:
# return, esc, shift+enter, ctrl+enter
# text keyboard shortcuts -- cmd > (right), < left,
# option delete (deletes words)
# type "h" for help
# tab
# shift-tab
# keyboard shortcuts
# - a, b, y, m, dd, h, ctrl+shift+-
In [14]:
%matplotlib inline
%config InlineBackend.figure_format='retina'
import matplotlib.pyplot as plt
# no pylab
import seaborn as sns
sns.set_context('talk')
sns.set_style('darkgrid')
plt.rcParams['figure.figsize'] = 12, 8 # plotsize
import numpy as np
# don't do `from numpy import *`
import pandas as pd
In [9]:
# If you have a specific function that you'd like to import
from numpy.random import randn
In [10]:
x = np.arange(100)
y = np.sin(x)
plt.plot(x, y)#;
Out[10]:
In [12]:
%matplotlib notebook
In [13]:
x = np.arange(10)
y = np.sin(x)
plt.plot(x, y)#;
Out[13]:
Check out http://matplotlib.org/gallery.html select your favorite.
In [15]:
%%bash
for num in {1..5}
do
for infile in *;
do
echo $num $infile
done
wc $infile
done
In [20]:
print "hi"
!pwd
In [17]:
!ping google.com
In [18]:
this_is_magic = "Can you believe you can pass variables and strings like this?"
In [22]:
hey = !echo $this_is_magic
In [23]:
hey
Out[23]:
In [24]:
x = np.arange(10000)
print x # smart printing
In [25]:
print x[0] # first element
print x[-1] # last element
print x[0:5] # first 5 elements (also x[:5])
print x[:] # "Everything"
In [26]:
print x[-5:] # last five elements
In [27]:
print x[-5:-2]
In [28]:
print x[-5:-1] # not final value -- not inclusive on right
In [ ]:
In [29]:
x = np.random.randint(5, 5000, (3, 5))
In [30]:
x
Out[30]:
In [31]:
np.sum(x)
Out[31]:
In [32]:
x.sum()
Out[32]:
In [42]:
np.sum(x)
Out[42]:
In [41]:
np.sum(x, axis=0)
Out[41]:
In [43]:
np.sum(x, axis=1)
Out[43]:
In [44]:
x.sum(axis=1)
Out[44]:
In [45]:
# Multi dimension array slice with a comma
x[:, 2]
Out[45]:
In [ ]:
In [46]:
y = np.linspace(10, 20, 11)
y
Out[46]:
In [47]:
np.linspace?
In [ ]:
np.linspace()
# shift-tab; shift-tab-tab
np.
In [48]:
def does_it(first=x, second=y):
"""This is my doc"""
pass
In [49]:
y[[3, 5, 7]]
Out[49]:
In [ ]:
does_it()
In [51]:
num = 3000
x = np.linspace(1.0, 300.0, num)
y = np.random.rand(num)
z = np.sin(x)
np.savetxt("example.txt", np.transpose((x, y, z)))
In [52]:
%less example.txt
In [53]:
!wc example.txt
In [54]:
!head example.txt
In [55]:
#Not a good idea
a = []
b = []
for line in open("example.txt", 'r'):
a.append(line[0])
b.append(line[2])
a[:10] # Whoops!
Out[55]:
In [56]:
a = []
b = []
for line in open("example.txt", 'r'):
line = line.split()
a.append(line[0])
b.append(line[2])
a[:10] # Strings!
Out[56]:
In [57]:
a = []
b = []
for line in open("example.txt", 'r'):
line = line.split()
a.append(float(line[0]))
b.append(float(line[2]))
a[:10] # Lists!
Out[57]:
In [58]:
# Do this!
a, b = np.loadtxt("example.txt", unpack=True, usecols=(0,2))
In [59]:
a
Out[59]:
In [60]:
from numpy.random import randn
In [61]:
num = 50
x = np.linspace(2.5, 300, num)
y = randn(num)
plt.scatter(x, y)
Out[61]:
In [64]:
y > 1
Out[64]:
In [65]:
y[y > 1]
Out[65]:
In [66]:
y[(y < 1) & (y > -1)]
Out[66]:
In [67]:
plt.scatter(x, y, c='b', s=50)
plt.scatter(x[(y < 1) & (y > -1)], y[(y < 1) & (y > -1)], c='r', s=50)
Out[67]:
In [68]:
y[~((y < 1) & (y > -1))] = 1.0
plt.scatter(x, y, c='b')
plt.scatter(x, np.clip(y, -0.5, 0.5), color='red')
Out[68]:
In [71]:
num = 350
slope = 0.3
x = randn(num) * 50. + 150.0
y = randn(num) * 5 + x * slope
plt.scatter(x, y, c='b')
Out[71]:
In [72]:
# plt.scatter(x[(y < 1) & (y > -1)], y[(y < 1) & (y > -1)], c='r')
# np.argsort, np.sort, complicated index slicing
dframe = pd.DataFrame({'x': x, 'y': y})
g = sns.jointplot('x', 'y', data=dframe, kind="reg")
In [73]:
from ggplot import ggplot, aes, geom_line, stat_smooth, geom_dotplot, geom_point
In [74]:
ggplot(aes(x='x', y='y'), data=dframe) + geom_point() + stat_smooth(colour='blue', span=0.2)
Out[74]:
In [ ]: