Skip to content

Commit ffddd2e

Browse files
committed
Chapter 2 done
Starting on stack with numpy in Chapter 3
1 parent 59ee37b commit ffddd2e

9 files changed

+3771
-2
lines changed

.ipynb_checkpoints/Chapter2.BasicPython-checkpoint.ipynb

+1,255
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
{
2+
"metadata": {
3+
"name": ""
4+
},
5+
"nbformat": 3,
6+
"nbformat_minor": 0,
7+
"worksheets": [
8+
{
9+
"cells": [
10+
{
11+
"cell_type": "heading",
12+
"level": 1,
13+
"metadata": {},
14+
"source": [
15+
"The Scientific Python Stack"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"The scientific Python stack consists of Numpy, Scipy, and Matplotlib. Numpy brings to Python what Matlab is well known for : strong array manipulation and matrix operations, elementary mathematical functions, random numbers, ..."
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"collapsed": false,
28+
"input": [
29+
"import numpy as np\n",
30+
"\n",
31+
"A = np.ones(10)\n",
32+
"B = np.arange(5, 10, 0.1) # like range(), but allows non-integer stride\n",
33+
"C = np.linspace(0, 2 * np.pi, 30) # between a and b in c steps\n",
34+
"D = np.random.normal(0, 1, 20) # also beta, binomial, gamma, poisson, uniform, lognormal, negative binomial, geometric, ...\n",
35+
"E = np.sin(C)\n",
36+
"\n",
37+
"print A, \"\\n\"\n",
38+
"print B, \"\\n\"\n",
39+
"print C, \"\\n\"\n",
40+
"print D, \"\\n\"\n",
41+
"print E"
42+
],
43+
"language": "python",
44+
"metadata": {},
45+
"outputs": [
46+
{
47+
"output_type": "stream",
48+
"stream": "stdout",
49+
"text": [
50+
"[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] \n",
51+
"\n",
52+
"[ 5. 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6. 6.1 6.2 6.3 6.4\n",
53+
" 6.5 6.6 6.7 6.8 6.9 7. 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9\n",
54+
" 8. 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9. 9.1 9.2 9.3 9.4\n",
55+
" 9.5 9.6 9.7 9.8 9.9] \n",
56+
"\n",
57+
"[ 0. 0.21666156 0.43332312 0.64998469 0.86664625 1.08330781\n",
58+
" 1.29996937 1.51663094 1.7332925 1.94995406 2.16661562 2.38327719\n",
59+
" 2.59993875 2.81660031 3.03326187 3.24992343 3.466585 3.68324656\n",
60+
" 3.89990812 4.11656968 4.33323125 4.54989281 4.76655437 4.98321593\n",
61+
" 5.1998775 5.41653906 5.63320062 5.84986218 6.06652374 6.28318531] \n",
62+
"\n",
63+
"[-0.98189204 -0.58611512 1.58800176 -0.9281483 1.06510968 0.8507472\n",
64+
" -1.4315527 -0.31871079 0.20131501 -0.08079119 -0.73679295 0.23427112\n",
65+
" 0.18814395 0.46756174 0.39062454 0.13378789 -0.49214194 -0.72168981\n",
66+
" -0.25113278 0.67123937] \n",
67+
"\n",
68+
"[ 0.00000000e+00 2.14970440e-01 4.19889102e-01 6.05174215e-01\n",
69+
" 7.62162055e-01 8.83512044e-01 9.63549993e-01 9.98533414e-01\n",
70+
" 9.86826523e-01 9.28976720e-01 8.27688998e-01 6.87699459e-01\n",
71+
" 5.15553857e-01 3.19301530e-01 1.08119018e-01 -1.08119018e-01\n",
72+
" -3.19301530e-01 -5.15553857e-01 -6.87699459e-01 -8.27688998e-01\n",
73+
" -9.28976720e-01 -9.86826523e-01 -9.98533414e-01 -9.63549993e-01\n",
74+
" -8.83512044e-01 -7.62162055e-01 -6.05174215e-01 -4.19889102e-01\n",
75+
" -2.14970440e-01 -2.44929360e-16]\n"
76+
]
77+
}
78+
],
79+
"prompt_number": 16
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
84+
"source": [
85+
"Before we go on, a note on *namespaces*. A namespace is a way to \"partition\" your functions into different spaces. This is particularly useful because you may have functions with the same name, but they may not do the same thing :"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"collapsed": false,
91+
"input": [
92+
"print sin(0.5)\n",
93+
"print np.sin(0.5)\n",
94+
"\n",
95+
"sin(0.5) == np.sin(0.5)"
96+
],
97+
"language": "python",
98+
"metadata": {},
99+
"outputs": [
100+
{
101+
"output_type": "stream",
102+
"stream": "stdout",
103+
"text": [
104+
"0.479425538604\n",
105+
"0.479425538604\n"
106+
]
107+
},
108+
{
109+
"metadata": {},
110+
"output_type": "pyout",
111+
"prompt_number": 41,
112+
"text": [
113+
"True"
114+
]
115+
}
116+
],
117+
"prompt_number": 41
118+
},
119+
{
120+
"cell_type": "code",
121+
"collapsed": false,
122+
"input": [
123+
"x = [] # x is an empty list ( not a numpy array here )\n",
124+
"for i in range(10) :\n",
125+
" x.append(float(i) / 10)\n",
126+
"\n",
127+
"# Equivalent numpy array generation\n",
128+
"y = np.arange(0, 1, 0.1)\n",
129+
"\n",
130+
"# These should still be equal\n",
131+
"print sin(y), \"\\n\"\n",
132+
"print np.sin(y)"
133+
],
134+
"language": "python",
135+
"metadata": {},
136+
"outputs": [
137+
{
138+
"output_type": "stream",
139+
"stream": "stdout",
140+
"text": [
141+
"[ 0. 0.09983342 0.19866933 0.29552021 0.38941834 0.47942554\n",
142+
" 0.56464247 0.64421769 0.71735609 0.78332691] \n",
143+
"\n",
144+
"[ 0. 0.09983342 0.19866933 0.29552021 0.38941834 0.47942554\n",
145+
" 0.56464247 0.64421769 0.71735609 0.78332691]\n"
146+
]
147+
}
148+
],
149+
"prompt_number": 49
150+
},
151+
{
152+
"cell_type": "code",
153+
"collapsed": false,
154+
"input": [
155+
"# Numpy arrays can be easily manipulated\n",
156+
"blah = np.random.lognormal(0, 1, (5, 3))\n",
157+
"\n",
158+
"print np.dot(blah, blah.T), \"\\n\"\n",
159+
"print blah.shape, \"\\n\"\n",
160+
"print type(blah)"
161+
],
162+
"language": "python",
163+
"metadata": {},
164+
"outputs": [
165+
{
166+
"output_type": "stream",
167+
"stream": "stdout",
168+
"text": [
169+
"[[ 8.51713884 6.91034565 7.68810156 2.69320072 4.27556815]\n",
170+
" [ 6.91034565 8.90777709 10.88645377 3.13778003 3.36210778]\n",
171+
" [ 7.68810156 10.88645377 14.61116563 2.24539017 2.38654341]\n",
172+
" [ 2.69320072 3.13778003 2.24539017 3.20013026 3.11656473]\n",
173+
" [ 4.27556815 3.36210778 2.38654341 3.11656473 3.70435789]] \n",
174+
"\n",
175+
"(5, 3) \n",
176+
"\n",
177+
"<type 'numpy.ndarray'>\n"
178+
]
179+
}
180+
],
181+
"prompt_number": 34
182+
},
183+
{
184+
"cell_type": "code",
185+
"collapsed": false,
186+
"input": [
187+
"# The numpy array is its own type, but it's still a container. It expands on the capabilities of the standard Python list.\n",
188+
"# Access is easier though : for multidimensional arrays, you just use a comma per dimension\n",
189+
"print type(blah[0, 0])"
190+
],
191+
"language": "python",
192+
"metadata": {},
193+
"outputs": [
194+
{
195+
"output_type": "stream",
196+
"stream": "stdout",
197+
"text": [
198+
"<type 'numpy.float64'>\n"
199+
]
200+
}
201+
],
202+
"prompt_number": 35
203+
},
204+
{
205+
"cell_type": "code",
206+
"collapsed": false,
207+
"input": [
208+
"# The : operator still works great. Standard arithmetic is ELEMENT-wise. Dimensions are broadcast.\n",
209+
"print blah[:, 1] * blah[:, 2] - 2.5"
210+
],
211+
"language": "python",
212+
"metadata": {},
213+
"outputs": [
214+
{
215+
"output_type": "stream",
216+
"stream": "stdout",
217+
"text": [
218+
"[ 1.17124417 -0.56444793 -0.2318266 -2.40826853 -2.12920678]\n"
219+
]
220+
}
221+
],
222+
"prompt_number": 38
223+
},
224+
{
225+
"cell_type": "code",
226+
"collapsed": false,
227+
"input": [],
228+
"language": "python",
229+
"metadata": {},
230+
"outputs": []
231+
}
232+
],
233+
"metadata": {}
234+
}
235+
]
236+
}

Chapter0.ShamelessPlug.ipynb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"metadata": {
3+
"name": ""
4+
},
5+
"nbformat": 3,
6+
"nbformat_minor": 0,
7+
"worksheets": [
8+
{
9+
"cells": [
10+
{
11+
"cell_type": "heading",
12+
"level": 1,
13+
"metadata": {},
14+
"source": [
15+
"Chapter 0. Shameless Plug"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"We're a growing community, and we have a website ! Check us out at"
23+
]
24+
},
25+
{
26+
"cell_type": "heading",
27+
"level": 1,
28+
"metadata": {},
29+
"source": [
30+
"**`princetonpy.com`**"
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {},
36+
"source": [
37+
"We have a discussion forum on which you're welcome to discuss Python-related stuffs and ask questions. It's a great place to do so, because everyone can then see the discussion, rather than having it by email."
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"metadata": {},
43+
"source": [
44+
"Also : quick thanks to our sponsers : the **Department of Ecology and Evolutionary Biology**, the **Department of Geosciences**, and **Bryan Grenfell (EEB)** for some extra funding. We really appreciate it !"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"collapsed": false,
50+
"input": [],
51+
"language": "python",
52+
"metadata": {},
53+
"outputs": []
54+
}
55+
],
56+
"metadata": {}
57+
}
58+
]
59+
}

0 commit comments

Comments
 (0)