More strings
In [30]:
l = 'once upon a time'.split(' ')
In [31]:
'-'.join(l)
Out[31]:
In [35]:
s = 'Brainstorming has lost'
print s
s = s.replace('lost', 'won')
print s
l = s.split(' ')
l.insert(2, 'almost')
print ' '.join(l)
List comprehension
In [8]:
animals_list = ['dog', 'cat', 'bird']
a = ['%ss'%each for each in animals_list]
print a
In keyword and more for loops
In [37]:
print 'dog' in animals_list
print 'storm' in 'brainstorming'
print 'zebra' not in animals_list
for each in animals_list:
if 'a' in each:
print 'a is in %s'%each
else:
print 'a is not in %s'%each
In [15]:
for i, each in enumerate(animals_list):
print each, 'is in %s position'%i
In [17]:
how_many_legs = [4, 4, 2]
for animal, n_legs in zip(animals_list, how_many_legs):
print '%s has %s legs'%(animal, n_legs)
In [19]:
# converting it to a dictionary using list comprehension
d = dict([(animal, n_legs) for animal, n_legs in zip(animals_list, how_many_legs)])
print d
Json format
In [20]:
import json
json.dump(d, open('/tmp/test.json','w'))
d2 = json.load(open('/tmp/test.json', 'r'))
d2
Out[20]:
In [1]:
import os
os.getcwd()
Out[1]:
In [2]:
os.listdir(os.curdir)
Out[2]:
In [3]:
os.mkdir('/tmp/junkdir')
In [8]:
'junkdir' in os.listdir('/tmp')
Out[8]:
In [9]:
os.rmdir('/tmp/junkdir')
In [10]:
'junkdir' in os.listdir('/tmp')
Out[10]:
In [11]:
fp = open('/tmp/junk.txt', 'w')
fp.write('hello world\n')
fp.close()
In [12]:
fp = open('/tmp/junk.txt')
print fp.read()
In [13]:
os.remove('/tmp/junk.txt')
In [15]:
os.chdir('/tmp')
os.getcwd()
Out[15]:
In [16]:
os.path.join(os.curdir, 'junk.txt')
Out[16]:
In [17]:
os.path.abspath(os.path.join(os.curdir, 'junk.txt'))
Out[17]:
In [18]:
os.path.dirname(os.path.abspath(os.path.join(os.curdir, 'junk.txt')))
Out[18]:
In [20]:
os.path.basename(os.path.abspath(os.path.join(os.curdir, 'junk.txt')))
Out[20]:
In [21]:
os.path.splitext(os.path.abspath(os.path.join(os.curdir, 'junk.txt')))
Out[21]:
In [22]:
os.path.exists('junk.txt')
Out[22]:
In [23]:
os.path.isfile('junk.txt')
Out[23]:
In [24]:
os.path.isdir('junk.txt')
Out[24]:
In [25]:
os.system('ls')
Out[25]:
In [28]:
for dirpath, dirnames, filenames in os.walk(os.curdir):
for fp in filenames:
print os.path.abspath(fp)
In [29]:
import shutil
shutil.move
shutil.rmtree
shutil.copy
In [93]:
import glob
txtfiles = glob.glob('*.txt')
print txtfiles
In [56]:
import numpy as np
c = np.cos(0)
print 'cos(0) = %s'%c
d = np.log(1)
print 'log(1) = %s'%d
e = np.exp(0)
print 'exp(0) = %s'%e
a = np.array([0, 1, 2, 3])
print a.ndim
print a.shape
print a.mean()
print a.std()
b = np.array([[0, 1, 2], [3, 4, 5]])
print b.ndim
print b.shape
print np.sum(a)
print np.sum(b, axis=1)
print b.T
In [38]:
a = np.arange(10)
a
Out[38]:
In [39]:
b = np.array([[0, 1, 2], [3, 4, 5]])
b
Out[39]:
In [45]:
c = np.linspace(0, 1, 6)
c
Out[45]:
In [40]:
a = np.ones((3, 3))
a
Out[40]:
In [41]:
b = np.zeros((2, 2))
b
Out[41]:
In [43]:
c = np.eye(3)
c
Out[43]:
In [154]:
%matplotlib inline
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.cos(x)
plt.plot(x, y)
plt.show()
In [49]:
plt.plot(x, y, 'o')
plt.show()
In [155]:
plt.hist(zip([x,y]))
Out[155]:
In [134]:
x = np.random.rand(30,1)
y = np.random.rand(30,1)
plt.scatter(x,y,marker='o',c='r')
plt.show()
In [50]:
image = np.random.rand(30, 30)
plt.imshow(image, cmap=plt.cm.hot)
plt.colorbar()
Out[50]:
Matplotlib website
Quickly, slicing, arithmetic operations
In [63]:
os.chdir('/home/grg/git/alfa/notebooks/Python for science BBRC')
!cat ./populations.txt
In [82]:
data = np.loadtxt('populations.txt')
year, hares, lynxes, carrots = data.T
year
Out[82]:
In [89]:
from matplotlib import pyplot as plt
plt.axes([0.2, 0.1, 0.5, 0.8])
plt.plot(year, hares, year, lynxes, year, carrots)
plt.ylim([0,80000])
plt.legend(('Hare', 'Lynx', 'Carrot'), loc=(1.05, 0.5))
Out[89]:
In [87]:
populations = data[:, 1:]
print populations.mean(axis=0)
print populations.std(axis=0)
Which species has the highest population each year?
In [73]:
np.argmax(populations, axis=1)
Out[73]:
In [88]:
populations[populations<30000] = 30000
In [99]:
x=np.linspace(-1,1,2000)
y=np.cos(x)+0.3*np.random.rand(2000)
p=np.polynomial.Chebyshev.fit(x,y,90)
p=np.polynomial.Polynomial.fit(x,y,3)
t=np.linspace(-1,1,200)
plt.plot(x,y,'r.')
plt.plot(t,p(t),'k-',lw=3)
Out[99]:
In [111]:
import pandas as pd # p. 284
In [112]:
df = pd.read_excel('/home/grg/spm/data/covariates.xls') #read_csv etc..
df.head()
Out[112]:
In [108]:
import numpy as np
t=np.linspace(-6,6,20)
sin_t=np.sin(t)
cos_t=np.cos(t)
df = pd.DataFrame({'t':t,'sin':sin_t,'cos':cos_t})
df.head()
Out[108]:
In [116]:
df.columns
#print df['ventricles']
print df[df['apo'] == 0]['ventricles'].mean()
In [117]:
groupby_apo = df.groupby('apo')
In [119]:
groupby_apo.head()
Out[119]:
In [122]:
from pandas.tools import plotting
plotting.scatter_matrix(df[['age', 'educyears', 'ventricles']])
Out[122]:
Simple linear model
In [142]:
from scipy import stats
In [205]:
t = stats.distributions.t.rvs(2.7, scale=1, size=100)
In [206]:
plt.hist(t)
Out[206]:
In [207]:
x = np.linspace(-5,5,100)
param = stats.t.fit(t)
pdf_fitted = stats.t.pdf(x, loc=param[1], scale=param[2], df=param[0])
In [208]:
plt.plot(x,pdf_fitted,'b-')
Out[208]:
In [ ]: