-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpython-reference.html
364 lines (335 loc) · 17.1 KB
/
python-reference.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<!DOCTYPE html>
<html lang="en">
<head>
<title>Python Reference</title>
<!-- Latest compiled and minified Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="./theme/css/main.css" />
</head>
<body id="index" class="home">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="./">
Data 8 Summer 2018
</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="./faq.html">FAQ</a></li>
<li><a href="./office-hours.html">Office Hours</a></li>
<li><a href="./policies.html">Policies</a></li>
<li><a href="./schedule.html">Schedule</a></li>
<li><a href="./staff.html">Staff</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Resources <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="./materials.html">Materials</a></li>
<li class="active"><a href="./python-reference.html">Python Reference</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Links <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="http://data8.org/connector/">Connectors</a></li>
<li><a href="http://datahub.berkeley.edu/">DataHub</a></li>
<li><a href="http://data8.org/datascience/">Datascience Docs</a></li>
<li><a href="">Feedback</a></li>
<li><a href="https://piazza.com/berkeley/summer2018/data8">Piazza</a></li>
<li><a href="http://data.berkeley.edu">Berkeley Division of Data Sciences</a></li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<h2>Python Reference</h2>
<style>
th {
text-align: left
}
table > thead > tr > th, table > tbody > tr > th, table > tfoot > tr > th, table > thead > tr > td,
table > tbody > tr > td, table > tfoot > tr > td {
padding: 8px;
line-height: 1.42857143;
vertical-align: top;
border-top: 1px solid #ddd
}
table > thead > tr > th {
vertical-align: bottom;
border-bottom: 2px solid #ddd
}
table > caption + thead > tr:first-child > th, table > colgroup + thead > tr:first-child > th,
table > thead:first-child > tr:first-child > th, table > caption + thead > tr:first-child > td,
table > colgroup + thead > tr:first-child > td, table > thead:first-child > tr:first-child > td {
border-top: 0
}
table > tbody + tbody {
border-top: 2px solid #ddd
}
table {
border: 1px solid #ddd;
border-spacing: 0;
border-collapse: collapse;
background-color: #fff;
width: 100%;
max-width: 100%;
margin-bottom: 20px
}
td, th {
padding: 0
}
table > thead > tr > th, table > tbody > tr > th, table > tfoot > tr > th,
table > thead > tr > td, table > tbody > tr > td, table > tfoot > tr > td {
border: 1px solid #ddd
}
table > thead > tr > th, table > thead > tr > td {
border-bottom-width: 2px
}
table > tbody > tr:nth-child(odd) {
background-color: #f9f9f9
}
</style>
<p>Created by Nishant Kheterpal</p>
<h3>Table Functions and Methods</h3>
<p>In the examples in the left column, <code>np</code> refers to the NumPy module, as usual. Everything else is a function, a method, an example of an argument to a function or method, or an example of an object we might call the method on. For example, <code>tbl</code> refers to a table, <code>array</code> refers to an array, and <code>num</code> refers to a number. <code>array.item(0)</code> is an example call for the method <code>item</code>, and in that example, <code>array</code> is the name previously given to some array.</p>
<table>
<thead>
<tr>
<th>Name</th>
<th align="center">Chapter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Table()</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>Create an empty table, usually to extend with data</td>
</tr>
<tr>
<td><code>Table().read_table(filename)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>Create a table from a data file</td>
</tr>
<tr>
<td><code>tbl.with_columns(name, values) tbl.with_columns(n1, v1, n2, v2,...)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>A table with an additional or replaced column or columns. <code>name</code> is a string for the name of a column, <code>values</code> is an array</td>
</tr>
<tr>
<td><code>tbl.column(column_name_or_index)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>The values of a column (an array)</td>
</tr>
<tr>
<td><code>tbl.num_rows</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>Compute the number of rows in a table</td>
</tr>
<tr>
<td><code>tbl.num_columns</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>Compute the number of columns in a table</td>
</tr>
<tr>
<td><code>tbl.labels</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>Lists the column labels in a table</td>
</tr>
<tr>
<td><code>tbl.select(col1, col2, ...)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>Create a copy of a table with only some of the columns. Each column is the column name or index.</td>
</tr>
<tr>
<td><code>tbl.drop(col1, col2, ...)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>Create a copy of a table without some of the columns. Each column is the column name or index.</td>
</tr>
<tr>
<td><code>tbl.relabel(old_label, new_label)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>Modifies the existing table <em>in place</em>, changing the column heading in the first argument to the second</td>
</tr>
<tr>
<td><code>tbl.relabeled(old_label, new_label)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/tables.html">6</a></td>
<td>Returns a new table with the column heading in the first argument changed to the second</td>
</tr>
<tr>
<td><code>tbl.sort(column_name_or_index)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/1/sorting-rows.html">6.1</a></td>
<td>Create a copy of a table sorted by the values in a column. Defaults to ascending order unless "descending = True" is included</td>
</tr>
<tr>
<td><code>tbl.where(column, predicate)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/2/selecting-rows.html">6.2</a></td>
<td>Create a copy of a table with only the rows that match some <em>predicate</em> See <code>Table.where</code> predicates below</td>
</tr>
<tr>
<td><code>tbl.take(row_indices)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/06/2/selecting-rows.html">6.2</a></td>
<td>A table with only the rows at the given indices. <code>row_indices</code> is an array of indices.</td>
</tr>
<tr>
<td><code>tbl.scatter(x_column, y_column)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/07/visualization.html">7</a></td>
<td>Draws a scatter plot consisting of one point for each row of the table. Note that <code>x_column</code> and <code>y_column</code> must be strings specifying column names.</td>
</tr>
<tr>
<td><code>tbl.plot(x_column, y_column)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/07/visualization.html">7</a></td>
<td>Draw a line graph consisting of one point for each row of the table.</td>
</tr>
<tr>
<td><code>tbl.barh(categories) tbl.barh(categories, values)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/07/1/visualizing-categorical-distributions.html">7.1</a></td>
<td>Displays a bar chart with bars for each category in a column, with height proportional to the corresponding frequency. values argument unnecessary if table has only a column of categories and a column of values.</td>
</tr>
<tr>
<td><code>tbl.hist(column, unit, bins)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/07/2/visualizing-numerical-distributions.html">7.2</a></td>
<td>Generates a histogram of the numerical values in a column. <code>unit</code> and <code>bins</code> are optional arguments, used to label the axes and group the values into intervals (bins), respectively. Bins have the form [a, b).</td>
</tr>
<tr>
<td><code>tbl.apply(function, column)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/08/1/applying-a-function-to-a-column.html">8.1</a></td>
<td>Returns an array of values resulting from applying a function to each item in a column.</td>
</tr>
<tr>
<td><code>tbl.group(column_or_columns, func)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/08/2/classifying-by-one-variable.html">8.2</a></td>
<td>Group rows by unique values or combinations of values in a column(s). Multiple columns must be entered in array or list form. Other values aggregated by count (default) or optional argument <code>func</code>.</td>
</tr>
<tr>
<td><code>tbl.pivot(col1, col2, vals, collect) tbl.pivot(col1, col2)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/08/3/cross-classifying-by-more-than-one-variable.html">8.3</a></td>
<td>A pivot table where each unique value in <code>col1</code> has its own column and each unique value in c<code>col2</code> has its own row. Count or aggregate values from a third column, collect with some function. Default <code>vals</code> and <code>collect</code> return counts in cells.</td>
</tr>
<tr>
<td><code>tblA.join(colA, tblB, colB) tblA.join(colA, tblB)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/08/4/joining-tables-by-columns.html">8.4</a></td>
<td>Generate a table with the columns of tblA and tblB, containing rows for all values of a column that appear in both tables. Default <code>colB</code> is <code>colA</code>. <code>colA</code> and <code>colB</code> must be strings specifying column names.</td>
</tr>
<tr>
<td><code>tbl.sample(n) tbl.sample(n, with_replacement)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/10/empirical-distributions.html">10</a></td>
<td>A new table where <code>n</code> rows are randomly sampled from the original table. Default is with replacement. For sampling without replacement, use argument <code>with_replacement=False</code>. For a non-uniform sample, provide a third argument <code>weights=distribution</code> where <code>distribution</code> is an array or list containing the probability of each row.</td>
</tr>
<tr>
<td><code>sample_proportions(sample_size, model_proportions</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/11/1/assessing-models.html">11.1</a></td>
<td><code>Sample_size</code> should be an integer, <code>model_proportions</code> an array of probabilities that sum up to 1. The function samples <code>sample_size</code> objects from the distribution specified by <code>model_proportions</code>. It returns an array with the same size as <code>model_proportions</code>. Each item in the array corresponds to the proportion of times it was sampled out of the <code>sample_size</code> times.</td>
</tr>
</tbody>
</table>
<p><br /></p>
<h3>Array Functions and Methods</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th align="center">Chapter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>max(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/03/3/call-expressions.html">3.3</a></td>
<td>Returns the maximum value of an array</td>
</tr>
<tr>
<td><code>min(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/03/3/call-expressions.html">3.3</a></td>
<td>Returns the minimum value of an array</td>
</tr>
<tr>
<td><code>sum(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/03/3/call-expressions.html">3.3</a></td>
<td>Returns the sum of the values in an array</td>
</tr>
<tr>
<td><code>abs(num), np.abs(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/03/3/call-expressions.html">3.3</a></td>
<td>Take the absolute value of number or each number in an array.</td>
</tr>
<tr>
<td><code>round(num), np.round(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/03/3/call-expressions.html">3.3</a></td>
<td>Round number or array of numbers to the nearest integer.</td>
</tr>
<tr>
<td><code>len(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/03/3/call-expressions.html">3.3</a></td>
<td>Returns the length (number of elements) of an array</td>
</tr>
<tr>
<td><code>make_array(val1, val2, ...)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/05/sequences.html">5</a></td>
<td>Makes a numpy array with the values passed in</td>
</tr>
<tr>
<td><code>np.average(array) np.mean(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/05/1/arrays.html">5.1</a></td>
<td>Returns the mean value of an array</td>
</tr>
<tr>
<td><code>np.diff(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/05/1/arrays.html">5.1</a></td>
<td>Returns a new array of size <code>len(arr)-1</code> with elements equal to the difference between adjacent elements; val_2 - val_1, val_3 - val_2, etc.</td>
</tr>
<tr>
<td><code>np.sqrt(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/05/1/arrays.html">5.1</a></td>
<td>Returns an array with the square root of each element</td>
</tr>
<tr>
<td><code>np.arange(start, stop, step) np.arange(start, stop) np.arange(stop)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/05/2/ranges.html">5.2</a></td>
<td>An array of numbers starting with <code>start</code>, going up in increments of <code>step</code>, and going up to but excluding <code>stop</code>. When <code>start</code> and/or <code>step</code> are left out, default values are used in their place. Default step is 1; default start is 0.</td>
</tr>
<tr>
<td><code>array.item(index)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/05/3/more-on-arrays.html">5.3</a></td>
<td>Returns the i-th item in an array (remember Python indices start at 0!)</td>
</tr>
<tr>
<td><code>np.random.choice(array, n) np.random.choice(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/09/randomness.html">9</a></td>
<td>Picks one (by default) or some number 'n' of items from an array at random. By default, with replacement.</td>
</tr>
<tr>
<td><code>np.count_nonzero(array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/09/randomness.html">9</a></td>
<td>Returns the number of non-zero (or <code>True</code>) elements in an array.</td>
</tr>
<tr>
<td><code>np.append(array, item)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/09/2/iteration.html">9.2</a></td>
<td>Returns a copy of the input array with <code>item</code> (must be the same type as the other entries in the array) appended to the end.</td>
</tr>
<tr>
<td><code>percentile(percentile, array)</code></td>
<td align="center"><a href="https://www.inferentialthinking.com/chapters/12/1/percentiles.html">12.1</a></td>
<td>Returns the corresponding percentile of an array.</td>
</tr>
</tbody>
</table>
</div>
<footer id="contentinfo" class="body">
</footer><!-- /#contentinfo -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>