<jupyter_start><jupyter_text>More strings<jupyter_code>l = 'once upon a time'.split(' ')
'-'.join(l)
s = 'Brainstorming has lost'
print s
s = s.replace('lost', 'won')
print s

l = s.split(' ')
l.insert(2, 'almost')
print ' '.join(l)<jupyter_output>Brainstorming has lost
Brainstorming has won
Brainstorming has almost won<jupyter_text>List comprehension<jupyter_code>animals_list = ['dog', 'cat', 'bird']
a = ['%ss'%each for each in animals_list]
print a<jupyter_output>['dogs', 'cats', 'birds']<jupyter_text>`In` keyword and more `for` loops<jupyter_code>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
for i, each in enumerate(animals_list):
    print each, 'is in %s position'%i   
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)
# 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<jupyter_output>{'bird': 2, 'dog': 4, 'cat': 4}<jupyter_text>Json format<jupyter_code>import json
json.dump(d, open('/tmp/test.json','w'))

d2 = json.load(open('/tmp/test.json', 'r'))
d2<jupyter_output><empty_output><jupyter_text>The os module<jupyter_code>import os
os.getcwd()
os.listdir(os.curdir)
os.mkdir('/tmp/junkdir')
'junkdir' in os.listdir('/tmp')
os.rmdir('/tmp/junkdir')
'junkdir' in os.listdir('/tmp')
fp = open('/tmp/junk.txt', 'w')
fp.write('hello world\n')
fp.close()
fp = open('/tmp/junk.txt')
print fp.read()
os.remove('/tmp/junk.txt')
os.chdir('/tmp')
os.getcwd()
os.path.join(os.curdir, 'junk.txt')
os.path.abspath(os.path.join(os.curdir, 'junk.txt'))
os.path.dirname(os.path.abspath(os.path.join(os.curdir, 'junk.txt')))
os.path.basename(os.path.abspath(os.path.join(os.curdir, 'junk.txt')))
os.path.splitext(os.path.abspath(os.path.join(os.curdir, 'junk.txt'))) 
os.path.exists('junk.txt')
os.path.isfile('junk.txt') 
os.path.isdir('junk.txt') 
os.system('ls') 
for dirpath, dirnames, filenames in os.walk(os.curdir): 
    for fp in filenames:
        print os.path.abspath(fp) 
import shutil
shutil.move
shutil.rmtree
shutil.copy
import glob
txtfiles = glob.glob('*.txt')
print txtfiles<jupyter_output>['populations.txt']<jupyter_text>Numpy<jupyter_code>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
a = np.arange(10)
a
b = np.array([[0, 1, 2], [3, 4, 5]])
b
c = np.linspace(0, 1, 6)
c
a = np.ones((3, 3))
a
b = np.zeros((2, 2))
b
c = np.eye(3)
c<jupyter_output><empty_output><jupyter_text>Visualization<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.cos(x)
plt.plot(x, y)
plt.show()
plt.plot(x, y, 'o')
plt.show()
plt.hist(zip([x,y]))
x = np.random.rand(30,1)
y = np.random.rand(30,1)
plt.scatter(x,y,marker='o',c='r')
plt.show()
image = np.random.rand(30, 30)
plt.imshow(image, cmap=plt.cm.hot) 
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Matplotlib websiteQuickly, slicing, arithmetic operations<jupyter_code>os.chdir('/home/grg/git/alfa/notebooks/Python for science BBRC')
!cat ./populations.txt
data = np.loadtxt('populations.txt')
year, hares, lynxes, carrots = data.T
year
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)) 
populations = data[:, 1:] 
print populations.mean(axis=0) 
print populations.std(axis=0)<jupyter_output>[ 34080.95238095  20166.66666667  42400.        ]
[ 20897.90645809  16254.59153691   3322.50622558]<jupyter_text>Which species has the highest population each year?<jupyter_code>np.argmax(populations, axis=1) 
populations[populations<30000] = 30000
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) 
import pandas as pd # p. 284
df = pd.read_excel('/home/grg/spm/data/covariates.xls') #read_csv etc..
df.head()
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()
df.columns
#print df['ventricles']
print df[df['apo'] == 0]['ventricles'].mean()
groupby_apo = df.groupby('apo') 
groupby_apo.head()
from pandas.tools import plotting 
plotting.scatter_matrix(df[['age', 'educyears', 'ventricles']])<jupyter_output><empty_output><jupyter_text>Simple linear model<jupyter_code>from scipy import stats
t = stats.distributions.t.rvs(2.7, scale=1, size=100)
plt.hist(t)
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])
plt.plot(x,pdf_fitted,'b-')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18986852281515854, 'fraction_numerical': 0.03924980665119876, 'mean_word_length': 5.4340796019900495, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '521466', 'license': 'MIT', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2267, 'n_words': 437, 'path': 'notebooks/Python for science BBRC/Numerical Python, Scientific Python.ipynb', 'repo_name': 'xgrg/alfa', 'size': '5172'}