-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumerical-analysis2.html
More file actions
4186 lines (4170 loc) · 185 KB
/
numerical-analysis2.html
File metadata and controls
4186 lines (4170 loc) · 185 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<link rel="icon" type="image/gif" href="/favicon.gif"/>
<link rel="apple-touch-icon" sizes="120x120" href="touch-icon-iphone-retina.png" />
<link rel="apple-touch-icon" sizes="152x152" href="touch-icon-ipad-retina.png" />
<title>Lorem Ipsum</title>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<style type="text/css" id="internal-style">
@import url(hyperpolyglot.css);
</style>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<meta http-equiv="content-language" content="en"/>
</head>
<body>
<div id="container-wrap-wrap">
<div id="container-wrap">
<div id="container">
<div id="header">
<h1><a href="/"><span>Hyperpolyglot</span></a></h1>
</div>
<div id="content-wrap">
<div id="main-content">
<div id="page-title">Lorem Ipsum</div>
<div id="page-content">
<p><a name="top"></a><em>a side-by-side reference sheet</em></p>
<p><strong><a href="/numerical-analysis">sheet one:</a></strong> <a href="/numerical-analysis#grammar-invocation">grammar and invocation</a> | <a href="/numerical-analysis#var-expr">variables and expressions</a> | <a href="/numerical-analysis#arithmetic-logic">arithmetic and logic</a> | <a href="/numerical-analysis#strings">strings</a> | <a href="/numerical-analysis#regexes">regexes</a> | <a href="/numerical-analysis#dates-time">dates and time</a> | <a href="/numerical-analysis#tuples">tuples</a> | <a href="/numerical-analysis#arrays">arrays</a> | <a href="/numerical-analysis#arithmetic-sequences">arithmetic sequences</a> | <a href="/numerical-analysis#two-d-arrays">2d arrays</a> | <a href="/numerical-analysis#three-d-arrays">3d arrays</a> | <a href="/numerical-analysis#dictionaries">dictionaries</a> | <a href="/numerical-analysis#functions">functions</a> | <a href="/numerical-analysis#execution-control">execution control</a> | <a href="/numerical-analysis#file-handle">file handles</a> | <a href="/numerical-analysis#directories">directories</a> | <a href="/numerical-analysis#processes-environment">processes and environment</a> | <a href="/numerical-analysis#libraries-namespaces">libraries and namespaces</a> | <a href="/numerical-analysis#reflection">reflection</a> | <a href="/numerical-analysis#debugging">debugging</a></p>
<p><strong>sheet two:</strong> <a href="#tables">tables</a> | <a href="#import-export">import and export</a> | <a href="#relational-algebra">relational algebra</a> | <a href="#aggregation">aggregation</a></p>
<p><a href="#vectors">vectors</a> | <a href="#matrices">matrices</a> | <a href="#sparse-matrices">sparse matrices</a> | <a href="#optimization">optimization</a> | <a href="#polynomials">polynomials</a> | <a href="#descriptive-statistics">descriptive statistics</a> | <a href="#distributions">distributions</a> | <a href="#linear-regression">linear regression</a> | <a href="#statistical-tests">statistical tests</a> | <a href="#time-series">time series</a> | <a href="#fast-fourier-transform">fast fourier transform</a> | <a href="#clustering">clustering</a> | <a href="#images">images</a> | <a href="#sound">sound</a></p>
<p><a href="#bar-charts">bar charts</a> | <a href="#scatter-plots">scatter plots</a> | <a href="#line-charts">line charts</a> | <a href="#surface-charts">surface charts</a> | <a href="#chart-options">chart options</a></p>
<table class="wiki-content-table">
<tr>
<th colspan="5"><a name="tables"></a><a href="#tables-note">tables</a></th>
</tr>
<tr>
<th></th>
<th><a href="#matlab">matlab</a></th>
<th><a href="#r">r</a></th>
<th><a href="#numpy">numpy</a></th>
<th><a href="#julia">julia</a></th>
</tr>
<tr>
<td><a name="construct-from-column-arrays"></a><a href="#construct-from-column-arrays-note">construct from column arrays</a></td>
<td>sx = {'F' 'F' 'F' 'M' 'M' 'M'}<br />
ht = [69 64 67 68 72 71]<br />
wt = [148 132 142 149 167 165]<br />
cols = {'sx', 'ht', 'wt'}<br />
people = table(sx', ht', wt', 'VariableNames', cols)</td>
<td><span style="color: gray"># gender, height, weight of some people<br />
# in inches and lbs:</span><br />
sx = c("F", "F", "F", "M", "M", "M")<br />
ht = c(69, 64, 67, 68, 72, 71)<br />
wt = c(148, 132, 142, 149, 167, 165)<br />
people = data.frame(sx, ht, wt)</td>
<td>sx = ['F', 'F', 'F', 'F', 'M', 'M']<br />
ht = [69, 64, 67, 66, 72, 70]<br />
wt = [150, 132, 142, 139, 167, 165]<br />
people = pd.DataFrame({'sx': sx, 'ht': ht, 'wt': wt})</td>
<td> </td>
</tr>
<tr>
<td><a name="construct-from-row-dictionaries"></a><a href="#construct-from-row-dictionaries-note">construct from row dictionaries</a></td>
<td> </td>
<td> </td>
<td>rows = [<br />
<span style="white-space: pre-wrap;"> </span>{'sx': 'F', 'ht': 69, 'wt': 150},<br />
<span style="white-space: pre-wrap;"> </span>{'sx': 'F', 'ht': 64, 'wt': 132},<br />
<span style="white-space: pre-wrap;"> </span>{'sx': 'F', 'ht': 67, 'wt': 142},<br />
<span style="white-space: pre-wrap;"> </span>{'sx': 'F', 'ht': 66, 'wt': 139},<br />
<span style="white-space: pre-wrap;"> </span>{'sx': 'M', 'ht': 72, 'wt': 167},<br />
<span style="white-space: pre-wrap;"> </span>{'sx': 'M', 'ht': 70, 'wt': 165}]<br />
people = pd.DataFrame(rows)</td>
<td> </td>
</tr>
<tr>
<td><a name="table-size"></a><a href="#table-size-note">size</a></td>
<td>height(people)<br />
width(people)</td>
<td>nrow(people)<br />
ncol(people)<br />
<br />
<span style="color: gray"># number of rows and cols in 2-element vector:</span><br />
dim(people)</td>
<td>len(people)<br />
len(people.columns)</td>
<td> </td>
</tr>
<tr>
<td><a name="column-names-as-array"></a><a href="#column-names-as-array-note">column names as array</a></td>
<td>people.Properties.VariableNames</td>
<td>names(people)<br />
colnames(people)</td>
<td><span style="color: gray"><em>returns</em> Index <em>object:</em></span><br />
people.columns</td>
<td> </td>
</tr>
<tr>
<td><a name="access-column-as-array"></a><a href="#access-column-as-array-note">access column as array</a></td>
<td>people.ht<br />
people.(2)</td>
<td><span style="color: gray"># vectors:</span><br />
people$ht<br />
people[,2]<br />
people[['ht']]<br />
people[[2]]<br />
<span style="color: gray"># 1 column data frame:</span><br />
people[2]</td>
<td>people['ht']<br />
<br />
<span style="color: gray"># if name does not conflict with any DataFrame attributes:</span><br />
people.ht</td>
<td> </td>
</tr>
<tr>
<td><a name="access-row-as-tuple"></a><a href="#access-row-as-tuple-note">access row as tuple</a></td>
<td>people(1,:)</td>
<td><span style="color: gray"># 1 row data frame:</span><br />
people[1, ]<br />
<span style="color: gray"># list:</span><br />
as.list(people[1, ])</td>
<td>people.ix[0]</td>
<td> </td>
</tr>
<tr>
<td><a name="access-datum"></a><a href="#access-datum-note">access datum</a></td>
<td><span style="color: gray">% height of 1st person:</span><br />
people(1,2)</td>
<td><span style="color: gray"># height of 1st person:</span><br />
people[1,2]</td>
<td>people.get_value(0, 'ht')</td>
<td> </td>
</tr>
<tr>
<td><a name="order-rows-by-column"></a><a href="#order-rows-by-column-note">order rows by column</a></td>
<td>sortrows(people, 'ht')</td>
<td>people[order(people$ht), ]</td>
<td>people.sort(['ht'])</td>
<td> </td>
</tr>
<tr>
<td><a name="order-rows-by-multiple-columns"></a><a href="#order-rows-by-multiple-columns-note">order rows by multiple columns</a></td>
<td>sortrows(people, {'sx', 'ht'})</td>
<td>people[order(people$sx, people$ht), ]</td>
<td>people.sort(['sx', 'ht'])</td>
<td> </td>
</tr>
<tr>
<td><a name="order-rows-descending-order"></a><a href="#order-rows-descending-order-note">order rows in descending order</a></td>
<td>sortrows(people, 'ht', 'descend')</td>
<td>people[order(-people$ht), ]</td>
<td>people.sort('ht', ascending=[False])</td>
<td> </td>
</tr>
<tr>
<td><a name="limit-rows"></a><a href="#limit-rows-note">limit rows</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>people(1:3, :)</td>
<td>people[seq(1, 3), ]</td>
<td>people[0:3]</td>
<td> </td>
</tr>
<tr>
<td><a name="offset-rows"></a><a href="#offset-rows-note">offset rows</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>people(4:6, :)</td>
<td>people[seq(4, 6), ]</td>
<td>people[3:]</td>
<td> </td>
</tr>
<tr>
<td><a name="reshape-table"></a><a href="#reshape-table-note">reshape</a></td>
<td> </td>
<td>people$couple = c(1, 2, 3, 1, 2, 3)<br />
reshape(people, idvar="couple", direction="wide",<br />
<span style="white-space: pre-wrap;"> </span>timevar="sx", v.names=c("ht", "wt"))</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="rm-rows-with-null-fields"></a><a href="#rm-rows-with-null-fields-note">remove rows with null fields</a></td>
<td> </td>
<td>sx = c('F', 'F', 'M', 'M')<br />
wt = c(120, NA, 150, 170)<br />
<br />
df = data.frame(sx, wt)<br />
df2 = na.omit(df)</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="attach-columns"></a><a href="#attach-columns-note">attach columns</a></td>
<td> </td>
<td><span style="color: gray"># put columns ht, wt, and sx<br />
# in variable name search path:</span><br />
attach(people)<br />
sum(ht)<br />
<br />
<span style="color: gray"># alternative which doesn't put columns in<br />
# search path:</span><br />
with(people, sum(ht))</td>
<td><span style="color: gray"><em>none</em></span></td>
<td> </td>
</tr>
<tr>
<td><a name="detach-columns"></a><a href="#detach-columns-note">detach columns</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td> </td>
<td>detach(people)</td>
<td><span style="color: gray"><em>none</em></span></td>
<td> </td>
</tr>
<tr>
<td><a name="spreadsheet-editor"></a><a href="#spreadsheet-editor-note">spreadsheet editor</a></td>
<td> </td>
<td><span style="color: gray"><em>can edit data, in which case return value of</em> edit <em>must be saved</em></span><br />
people = edit(people)</td>
<td><span style="color: gray"><em>none</em></span></td>
<td> </td>
</tr>
<tr>
<th colspan="5"><a name="import-export"></a><a href="#import-export-note">import and export</a></th>
</tr>
<tr>
<th></th>
<th><a href="#matlab">matlab</a></th>
<th><a href="#r">r</a></th>
<th><a href="#numpy">numpy</a></th>
<th><a href="#julia">julia</a></th>
</tr>
<tr>
<td><a name="import-tab-delimited"></a><a href="#import-tab-delimited-note">import tab delimited</a></td>
<td><span style="color: gray"># first row defines variable names:</span><br />
readtable('/tmp/password.txt', 'Delimiter', '\t')<br />
<br />
<span style="color: gray"># file suffix must be .txt, .dat, or .csv</span></td>
<td><span style="color: gray"># first row defines variable names:</span><br />
df = read.delim('/path/to.tab', stringsAsFactors=F, quote=NULL)</td>
<td><span style="color: gray"># first row defines column names:</span><br />
df = pd.read_table('/path/to.tab')</td>
<td> </td>
</tr>
<tr>
<td><a name="import-csv"></a><a href="#import-csv-note">import csv</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray">% first row defines variable names:</span><br />
df = readtable('/path/to.csv')</td>
<td><span style="color: gray"># first row defines variable names:</span><br />
df = read.csv('/path/to.csv', stringsAsFactors=F)</td>
<td><span style="color: gray"># first row defines column names:</span><br />
df = pd.read_csv('/path/to.csv')</td>
<td> </td>
</tr>
<tr>
<td><a name="set-column-separator"></a><a href="#set-column-separator-note">set column separator</a></td>
<td>df = readtable('/etc/passwd',<br />
<span style="white-space: pre-wrap;"> </span>'Delimiter', ':',<br />
<span style="white-space: pre-wrap;"> </span>'ReadVariableNames', 0,<br />
<span style="white-space: pre-wrap;"> </span>'HeaderLines', 10)</td>
<td>df = read.delim('/etc/passwd',<br />
<span style="white-space: pre-wrap;"> </span>sep=':',<br />
<span style="white-space: pre-wrap;"> </span>header=FALSE,<br />
<span style="white-space: pre-wrap;"> </span>comment.char='#')</td>
<td><span style="color: gray"># $ grep -v '^#' /etc/passwd > /tmp/passwd</span><br />
<br />
df = pd.read_table('/tmp/passwd', sep=':', header=None)</td>
<td> </td>
</tr>
<tr>
<td><a name="set-column-separator-whitesp"></a><a href="#set-column-separator-whitesp-note">set column separator to whitespace</a></td>
<td> </td>
<td>df = read.delim('/path/to.txt', sep='')</td>
<td>df = read_table('/path/to.txt', sep='\s+')</td>
<td> </td>
</tr>
<tr>
<td><a name="set-quote-char"></a><a href="#set-quote-char-note">set quote character</a></td>
<td> </td>
<td><span style="color: gray"># default quote character for both read.csv and read.delim<br />
# is double quotes. The quote character is escaped by doubling it.</span><br />
<br />
<span style="color: gray"># use single quote as quote character:</span><br />
df = read.csv('/path/to/single-quote.csv', quote="'")<br />
<br />
<span style="color: gray"># no quote character:</span><br />
df = read.csv('/path/to/no-quote.csv', quote="")</td>
<td><span style="color: gray"><em>Both</em> read_table <em>and</em> read_csv <em>use double quotes as the quote character and there is no way to change it. A double quote can be escaped by doubling it.</em></span></td>
<td> </td>
</tr>
<tr>
<td><a name="import-file-without-header"></a><a href="#import-file-without-header-note">import file w/o header</a></td>
<td> </td>
<td><span style="color: gray"># column names are V1, V2, ...</span><br />
read.delim('/etc/passwd',<br />
<span style="white-space: pre-wrap;"> </span>sep=':',<br />
<span style="white-space: pre-wrap;"> </span>header=FALSE,<br />
<span style="white-space: pre-wrap;"> </span>comment.char='#')</td>
<td><span style="color: gray"># $ grep -v '^#' /etc/passwd > /tmp/passwd</span><br />
<span style="color: gray"># </span><br />
<span style="color: gray"># column names are X0, X1, ...</span><br />
df = pd.read_table('/tmp/passwd', sep=':', header=None)</td>
<td> </td>
</tr>
<tr>
<td><a name="set-column-names"></a><a href="#set-column-names-note">set column names</a></td>
<td>df = readtable('/path/to/no-header.csv',<br />
<span style="white-space: pre-wrap;"> </span>'ReadVariableNames', 0)<br />
<br />
df.Properties.VariableNames = {'ht', 'wt', 'age'}</td>
<td>df = read.csv('/path/to/no-header.csv',<br />
<span style="white-space: pre-wrap;"> </span>header=FALSE,<br />
<span style="white-space: pre-wrap;"> </span>col.names=c('ht', 'wt', 'age'))</td>
<td>df = pd.read_csv('/path/to/no-header.csv',<br />
<span style="white-space: pre-wrap;"> </span>names=['ht', 'wt', 'age'])</td>
<td> </td>
</tr>
<tr>
<td><a name="set-column-types"></a><a href="#set-column-types-note">set column types</a></td>
<td> </td>
<td><span style="color: gray"># possible values: NA, 'logical', 'integer', 'numeric',<br />
# 'complex', 'character', 'raw', 'factor', 'Date',<br />
# 'POSIXct'<br />
#<br />
# If type is set to NA, actual type will be inferred to be<br />
# 'logical', 'integer', 'numeric', 'complex', or 'factor'<br />
# </span><br />
df = read.csv('/path/to/data.csv',<br />
<span style="white-space: pre-wrap;"> </span>colClasses=c('integer', 'numeric', 'character'))</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="recognize-null-values"></a><a href="#recognize-null-values-note">recognize null values</a></td>
<td> </td>
<td>df = read.csv('/path/to/data.csv',<br />
<span style="white-space: pre-wrap;"> </span>colClasses=c('integer', 'logical', 'character'),<br />
<span style="white-space: pre-wrap;"> </span>na.strings=c('nil'))</td>
<td>df = read_csv('/path/to/data.csv',<br />
<span style="white-space: pre-wrap;"> </span>na_values=['nil'])</td>
<td> </td>
</tr>
<tr>
<td><a name="change-decimal-mark"></a><a href="#change-decimal-mark-note">change decimal mark</a></td>
<td> </td>
<td>df = read.csv('/path/to.csv', dec=',')</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="recognize-thousands-separator"></a><a href="#recognize-thousands-separator-note">recognize thousands separator</a></td>
<td> </td>
<td><span style="color: gray"><em>none</em></span></td>
<td>df = read_csv('/path/to.csv', thousands='.')</td>
<td> </td>
</tr>
<tr>
<td><a name="unequal-row-length-behavior"></a><a href="#unequal-row-length-behavior-note">unequal row length behavior</a></td>
<td> </td>
<td><span style="color: gray"><em>Missing fields will be set to NA unless</em> fill <em>is set to</em> FALSE. <em>If the column is of type character then the fill value is an empty string ''.<br />
<br />
If there are extra fields they will be parsed as an extra row unless</em> flush <em>is set to</em> FALSE</span></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="skip-comment-lines"></a><a href="#skip-comment-lines-note">skip comment lines</a></td>
<td> </td>
<td>df = read.delim('/etc/passwd',<br />
<span style="white-space: pre-wrap;"> </span>sep=':',<br />
<span style="white-space: pre-wrap;"> </span>header=FALSE,<br />
<span style="white-space: pre-wrap;"> </span>comment.char='#')</td>
<td><span style="color: gray"><em>none</em></span></td>
<td> </td>
</tr>
<tr>
<td><a name="skip-rows"></a><a href="#skip-rows-note">skip rows</a></td>
<td>def = readtable('/path/to/data.csv',<br />
<span style="white-space: pre-wrap;"> </span>'HeaderLines', 4)</td>
<td>df = read.csv('/path/to/data.csv', skip=4)</td>
<td>df = read_csv('/path/to/data.csv', skiprows=4)<br />
<br />
<span style="color: gray"># rows to skip can be specified individually:</span><br />
df = read_csv('/path/to/data.csv', skiprows=range(0, 4))</td>
<td> </td>
</tr>
<tr>
<td><a name="max-rows-to-read"></a><a href="#max-rows-to-read-note">max rows to read</a></td>
<td> </td>
<td>df = read.csv('/path/to/data.csv', nrows=4)</td>
<td>df = read_csv('/path/to/data.csv', nrows=4)</td>
<td> </td>
</tr>
<tr>
<td><a name="index-column"></a><a href="#index-column-note">index column</a></td>
<td> </td>
<td><span style="color: gray"><em>none</em></span></td>
<td>df = pd.read_csv('/path/to.csv', index_col='key_col')<br />
<br />
<span style="color: gray"># hierarchical index:</span><br />
df = pd.read_csv('/path/to.csv', index_col=['col1', 'col2'])</td>
<td> </td>
</tr>
<tr>
<td><a name="export-tab-delimited"></a><a href="#export-tab-delimited-note">export tab delimited</a></td>
<td> </td>
<td>write.table(df, '/tmp/data.tab', sep='\t')</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="export-csv"></a><a href="#export-csv-note">export csv</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td> </td>
<td><span style="color: gray"># first column contains row names unless row.names<br />
# set to FALSE</span><br />
write.csv(df, '/path/to.csv', row.names=F)</td>
<td> </td>
<td> </td>
</tr>
<tr>
<th colspan="5"><a name="relational-algebra"></a><a href="#relational-algebra-note">relational algebra</a></th>
</tr>
<tr>
<th></th>
<th><a href="#matlab">matlab</a></th>
<th><a href="#r">r</a></th>
<th><a href="#numpy">numpy</a></th>
<th><a href="#julia">julia</a></th>
</tr>
<tr>
<td>project columns by name</td>
<td>people(:, {'sx', 'ht'})</td>
<td>people[c('sx', 'ht')]</td>
<td>people[['sx', 'ht']]</td>
<td> </td>
</tr>
<tr>
<td>project columns by position</td>
<td>people(:, [1 2])</td>
<td>people[c(1, 2)]</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>project expression</td>
<td> </td>
<td><span style="color: gray"># convert to cm and kg:</span><br />
transform(people, ht=2.54*ht, wt=wt/2.2)</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>project all columns</td>
<td>people(people.ht > 66, :)</td>
<td>people[people$ht > 66, ]</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>rename columns</td>
<td> </td>
<td>colnames(people) = c('gender', 'height', 'weight')</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="access-sub-data-set"></a><a href="#access-sub-data-set-note">access sub data frame</a></td>
<td> </td>
<td><span style="color: gray"># data frame of first 3 rows with<br />
# ht and wt columns reversed:</span><br />
people[1:3, c(1, 3, 2)]</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="data-set-filter"></a><a href="#data-set-filter-note">select rows</a></td>
<td>people(people.ht > 66, :)</td>
<td>subset(people, ht > 66)<br />
people[people$ht > 66, ]</td>
<td>people[people['ht'] > 66]</td>
<td> </td>
</tr>
<tr>
<td>select distinct rows</td>
<td>unique(people(:,{'sx'}))</td>
<td>unique(people[c('sx')])</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>split rows</td>
<td> </td>
<td><span style="color: gray"># class(x) is list:</span><br />
x = split(people, people$sx == 'F')<br />
<br />
<span style="color: gray"># data.frame only containing females:</span><br />
x$T</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>inner join</td>
<td> </td>
<td>pw = read.delim('/etc/passwd',<br />
<span style="white-space: pre-wrap;"> </span>sep=':',<br />
<span style="white-space: pre-wrap;"> </span>header=F,<br />
<span style="white-space: pre-wrap;"> </span>comment.char='#',<br />
<span style="white-space: pre-wrap;"> </span>col.names=c('name', 'passwd', 'uid', 'gid', 'gecos',<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>'home', 'shell'))<br />
<br />
grp = read.delim('/etc/group',<br />
<span style="white-space: pre-wrap;"> </span>sep=':',<br />
<span style="white-space: pre-wrap;"> </span>header=F,<br />
<span style="white-space: pre-wrap;"> </span>comment.char='#',<br />
<span style="white-space: pre-wrap;"> </span>col.names=c('name', 'passwd', 'gid', 'members'))<br />
<br />
merge(pw, grp, by.x='gid', by.y='gid')</td>
<td><span style="color: gray"># $ grep -v '^#' /etc/passwd > /tmp/passwd<br />
# $ grep -v '^#' /etc/group > /tmp/group</span><br />
<br />
pw = pd.read_table('/tmp/passwd', sep=':', header=None, names=['name', 'passwd', 'uid', 'gid', 'gecos', 'home', 'shell'])<br />
<br />
grp = pd.read_table('/tmp/group', sep=':', header=None, names=['name', 'passwd', 'gid', 'members'])<br />
<br />
pd.merge(pw, grp, left_on='gid', right_on='gid')</td>
<td> </td>
</tr>
<tr>
<td>nulls as join values</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>left join</td>
<td> </td>
<td>merge(pw, grp, by.x='gid', by.y='gid', all.x=T)</td>
<td>pd.merge(pw, grp, left_on='gid', right_on='gid', how='left')</td>
<td> </td>
</tr>
<tr>
<td>full join</td>
<td> </td>
<td>merge(pw, grp, by.x='gid', by.y='gid', all=T)</td>
<td>pd.merge(pw, grp, left_on='gid', right_on='gid', how='outer')</td>
<td> </td>
</tr>
<tr>
<td>antijoin</td>
<td> </td>
<td>pw[!(pw$gid %in% grp$gid), ]</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>cross join</td>
<td> </td>
<td>merge(pw, grp, by=c())</td>
<td> </td>
<td> </td>
</tr>
<tr>
<th colspan="5"><a name="aggregation"></a><a href="#aggregation-note">aggregation</a></th>
</tr>
<tr>
<th></th>
<th><a href="#matlab">matlab</a></th>
<th><a href="#r">r</a></th>
<th><a href="#numpy">numpy</a></th>
<th><a href="#julia">julia</a></th>
</tr>
<tr>
<td>group by column</td>
<td> </td>
<td> </td>
<td>grouped = people.groupby('sx')<br />
grouped.aggregate(np.max)['ht']</td>
<td> </td>
</tr>
<tr>
<td>multiple aggregated values</td>
<td> </td>
<td> </td>
<td>grouped = people.groupby('sx')<br />
grouped.aggregate(np.max)[['ht', 'wt']]</td>
<td> </td>
</tr>
<tr>
<td>group by multiple columns</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>aggregation functions</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>nulls and aggregation functions</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<th colspan="5"><a name="vectors"></a><a href="#vectors-note">vectors</a></th>
</tr>
<tr>
<th></th>
<th><a href="#matlab">matlab</a></th>
<th><a href="#r">r</a></th>
<th><a href="#numpy">numpy</a></th>
<th><a href="#julia">julia</a></th>
</tr>
<tr>
<td><a href="#vector-literal">vector literal</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>same as array</em></span></td>
<td><span style="color: gray"><em>same as array</em></span></td>
<td><span style="color: gray"><em>same as array</em></span></td>
<td><span style="color: gray"><em>same as array</em></span></td>
</tr>
<tr>
<td><a href="#vector-element-wise">element-wise arithmetic operators</a></td>
<td>+ - .* ./</td>
<td>+ - * /</td>
<td>+ - * /</td>
<td>+ - .* ./</td>
</tr>
<tr>
<td><a href="#vector-length-mismatch">result of vector length mismatch</a></td>
<td><span style="color: gray"><em>raises error</em></span></td>
<td><span style="color: gray"><em>values in shorter vector are recycled; warning if one vector is not a multiple length of the other</em></span></td>
<td><span style="color: gray"><em>raises</em> ValueError</span></td>
<td><span style="color: gray">DimensionMismatch</span></td>
</tr>
<tr>
<td><a href="#vector-scalar">scalar multiplication</a></td>
<td>3 * [1, 2, 3]<br />
[1, 2, 3] * 3</td>
<td>3 * c(1, 2, 3)<br />
c(1, 2, 3) * 3</td>
<td>3 * np.array([1, 2, 3])<br />
np.array([1, 2, 3]) * 3</td>
<td>3 * [1, 2, 3]<br />
[1, 2, 3] * 3</td>
</tr>
<tr>
<td><a href="#vector-dot">dot product</a></td>
<td>dot([1, 1, 1], [2, 2, 2])</td>
<td>c(1, 1, 1) %*% c(2, 2, 2)</td>
<td>v1 = np.array([1, 1, 1])<br />
v2 = np.array([2, 2, 2])<br />
np.dot(v1, v2)</td>
<td>dot([1, 1, 1], [2, 2, 2])</td>
</tr>
<tr>
<td><a href="#vector-cross">cross product</a></td>
<td>cross([1, 0, 0], [0, 1, 0])</td>
<td> </td>
<td>v1 = np.array([1, 0, 0])<br />
v2 = np.array([0, 1, 0])<br />
np.cross(v1, v2)</td>
<td>cross([1, 0, 0], [0, 1, 0])</td>
</tr>
<tr>
<td><a href="#vector-norms">norms</a></td>
<td>norm([1, 2, 3], 1)<br />
norm([1, 2, 3], 2)<br />
norm([1, 2, 3], Inf)</td>
<td>vnorm = function(x, t) {<br />
<span style="white-space: pre-wrap;"> </span>norm(matrix(x, ncol=1), t)<br />
}<br />
<br />
vnorm(c(1, 2, 3), "1")<br />
vnorm(c(1, 2, 3), "E")<br />
vnorm(c(1, 2, 3), "I")</td>
<td>v = np.array([1, 2, 3])<br />
np.linalg.norm(v, 1)<br />
np.linalg.norm(v, 2)<br />
np.linalg.norm(v, np.inf)</td>
<td>v = [1, 2, 3]<br />
<br />
norm(v, 1)<br />
norm(v, 2)<br />
norm(v, Inf)</td>
</tr>
<tr>
<th colspan="5"><a name="matrices"></a><a href="#matrices-note">matrices</a></th>
</tr>
<tr>
<th></th>
<th><a href="#matlab">matlab</a></th>
<th><a href="#r">r</a></th>
<th><a href="#numpy">numpy</a></th>
<th><a href="#julia">julia</a></th>
</tr>
<tr>
<td><a name="matrix-literal-constructor"></a><a href="#matrix-literal-constructor-note">literal or constructor</a></td>
<td><span style="color: gray">% row-major order:</span><br />
A = [1, 2; 3, 4]<br />
B = [4 3<br />
<span style="white-space: pre-wrap;"> </span>2 1]</td>
<td><span style="color: gray"># column-major order:</span><br />
A = matrix(c(1, 3, 2, 4), 2, 2)<br />
B = matrix(c(4, 2, 3, 1), nrow=2)<br />
<br />
<span style="color: gray"># row-major order:</span><br />
A = matrix(c(1, 2, 3, 4), nrow=2, byrow=T)</td>
<td><span style="color: gray"># row-major order:</span><br />
A = np.matrix([[1, 2], [3, 4]])<br />
B = np.matrix([[4, 3], [2, 1]])</td>
<td>A = [1 2; 3 4]<br />
B = [4 3; 2 1]</td>
</tr>
<tr>
<td><a name="constant-matrices"></a><a href="#constant-matrices-note">constant matrices</a><br />
<span style="color: gray"><em>all zeros, all ones</em></span></td>
<td>zeros(3, 3) <span style="color: gray"><em>or</em></span> zeros(3)<br />
ones(3, 3) <span style="color: gray"><em>or</em></span> ones(3)</td>
<td>matrix(0, 3, 3)<br />
matrix(1, 3, 3)</td>
<td>np.matrix(np.ones([3, 3]))<br />
np.matrix(np.zeros([3, 3]))</td>
<td>zeros(Float64, (3, 3))<br />
ones(Float64, (3, 3))</td>
</tr>
<tr>
<td><a name="diagonal-matrices"></a><a href="#diagonal-matrices-note">diagonal matrices</a><br />
<span style="color: gray"><em>and identity</em></span></td>
<td>diag([1, 2, 3])<br />
<span style="color: gray">% 3x3 identity:</span><br />
eye(3)</td>
<td>diag(c(1, 2, 3)<br />
<span style="color: gray"># 3x3 identity:</span><br />
diag(3)</td>
<td>np.diag([1, 2, 3])<br />
np.identity(3)</td>
<td>diagm([1, 2, 3])<br />
eye(3)</td>
</tr>
<tr>
<td><a name="matrix-formula"></a><a href="#matrix-formula-note">matrix by formula</a></td>
<td>i = ones(10, 1) * (1:10)<br />
j = (1:10)' * ones(1, 10)<br />
<span style="color: gray">% use component-wise ops only:</span><br />
1 ./ (i + j - 1)</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a name="matrix-dim"></a><a href="#matrix-dim-note">dimensions</a></td>
<td>rows(A)<br />
columns(A)</td>
<td>dim(A)[1]<br />
dim(A)[2]</td>
<td>nrows, ncols = A.shape</td>
<td>nrows, ncols = size([1 2 3; 4 5 6])</td>
</tr>
<tr>
<td><a href="#matrix-access">element access</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>A(1, 1)</td>
<td>A[1, 1]</td>
<td>A[0, 0]</td>
<td>A[1, 1]</td>
</tr>
<tr>
<td><a href="#matrix-row-access">row access</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>A(1, 1:2)</td>
<td>A[1, ]</td>
<td>A[0]</td>
<td>A[1, :]</td>
</tr>
<tr>
<td><a href="#matrix-column-access">column access</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>A(1:2, 1)</td>
<td>A[, 1]</td>
<td>A[:, 0]</td>
<td>A[:, 1]</td>
</tr>
<tr>
<td><a href="#submatrix-access">submatrix access</a></td>
<td>C = [1, 2, 3; 4, 5, 6; 7, 8, 9]<br />
C(1:2, 1:2)</td>
<td>C = matrix(seq(1, 9), 3, 3, byrow=T)<br />
C[1:2, 1:2]</td>
<td>A = np.matrix(range(1, 10)).reshape(3, 3)<br />
A[:2, :2]</td>
<td>reshape(1:9, 3, 3)[1:2, 1:2]</td>
</tr>
<tr>
<td><a href="#matrix-scalar-multiplication">scalar multiplication</a></td>
<td>3 * A<br />
A * 3<br />
<span style="color: gray"><em>also:</em></span><br />
3 .* A<br />
A .* 3</td>
<td>3 * A<br />
A * 3</td>
<td>3 * A<br />
A * 3</td>
<td>3 * [1 2; 3 4]<br />
[1 2; 3 4] * 3</td>
</tr>
<tr>
<td><a href="#matrix-element-wise-operators">element-wise operators</a></td>
<td>.+ .- .* ./</td>
<td>+ - * /</td>
<td>+ - np.multiply() np.divide()</td>
<td>+ - .* ./</td>
</tr>
<tr>
<td><a href="#matrix-multiplication">multiplication</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>A * B</td>
<td>A %*% B</td>
<td>np.dot(A, B)</td>
<td>A * B</td>
</tr>
<tr>
<td><a href="#matrix-power">power</a></td>
<td>A ^ 3<br />
<br />
<span style="color: gray">% power of each entry:</span><br />
A .^ 3</td>
<td> </td>
<td>A ** 3</td>
<td>A ^ 3<br />
<br />
<span style="color: gray"># power of each entry:</span><br />
A .^ 3</td>
</tr>
<tr>
<td><a href="#kronecker-product">kronecker product</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>kron(A, B)</td>
<td>kronecker(A, B)</td>
<td>np.kron(A, B)</td>
<td>kron(A, B)</td>
</tr>
<tr>
<td><a href="#matrix-comparison">comparison</a></td>
<td> all(all(A == B))<br />
any(any(A ~= B))</td>
<td>all(A == B)<br />
any(A != B)</td>
<td>np.all(A == B)<br />
np.any(A != B)</td>
<td>A == B<br />
A != B</td>
</tr>
<tr>
<td><a href="#matrix-norms">norms</a></td>
<td>norm(A, 1)<br />
norm(A, 2)<br />
norm(A, Inf)<br />
norm(A, 'fro')</td>
<td>norm(A, "1")<br />
<span style="color: gray"><em>??</em></span><br />
norm(A, "I")<br />
norm(A, "F")</td>
<td> </td>
<td>norm(A, 1)<br />
norm(A, 2)<br />
norm(A, Inf)<br />
<span style="color: gray"># Froebenius norm:</span><br />
vecnorm(A, 2)</td>
</tr>
<tr>
<td><a href="#matrix-transpose">transpose</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>transpose(A)</td>
<td>t(A)</td>
<td>A.transpose()</td>
<td>transpose([1 2; 3 4])</td>
</tr>
<tr>
<td><a href="#matrix-conjugate-transpose">conjugate transpose</a></td>
<td> A = [1i, 2i; 3i, 4i]<br />
A'</td>
<td>A = matrix(c(1i, 2i, 3i, 4i), nrow=2, byrow=T)<br />
Conj(t(A))</td>
<td>A = np.matrix([[1j, 2j], [3j, 4j]])<br />
A.conj().transpose()</td>
<td>[1im 2im; 3im 4im]'<br />
ctranspose([1im 2im; 3im 4im])</td>
</tr>
<tr>
<td><a href="#matrix-inverse">inverse</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>inv(A)</td>
<td>solve(A)</td>
<td>np.linalg.inv(A)</td>
<td>inv([1 2; 3 4])</td>
</tr>
<tr>
<td><a name="pseudoinverse"></a><a href="#pseudoinverse-note">pseudoinverse</a></td>
<td>A = [0 1; 0 0]<br />
<br />
pinv(A)</td>
<td>install.packages('corpcor')<br />
library(corpcor)<br />
<br />
A = matrix(c(0, 0, 1, 0), nrow=2)<br />
pseudoinverse(A)</td>
<td>A = np.matrix([[0, 1], [0, 0]])<br />
<br />
np.linalg.pinv(A)</td>
<td>pinv([0 1; 0 0])</td>
</tr>
<tr>
<td><a href="#matrix-determinant">determinant</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>det(A)</td>
<td>det(A)</td>
<td>np.linalg.det(A)</td>
<td>det(1 2; 3 4])</td>
</tr>
<tr>
<td><a href="#matrix-trace">trace</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>trace(A)</td>
<td>sum(diag(A))</td>
<td>A.trace()</td>
<td>trace([1 2; 3 4])</td>
</tr>
<tr>
<td><a href="#matrix-eigenvalues">eigenvalues</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>eig(A)</td>
<td>eigen(A)$values</td>
<td>np.linalg.eigvals(A)</td>
<td>eigvals(A)</td>
</tr>
<tr>
<td><a href="#matrix-eigenvectors">eigenvectors</a></td>
<td>[evec, eval] = eig(A)<br />
<span style="color: gray">% each column of evec is an eigenvector</span><br />
<span style="color: gray">% eval is a diagonal matrix of eigenvalues</span></td>
<td>eigen(A)$vectors</td>
<td>np.linalg.eig(A)[1]</td>
<td>eigvecs(A)</td>
</tr>
<tr>
<td><a name="svd"></a><a href="#svd-note">singular value decomposition</a></td>
<td>X = randn(10)<br />
<br />
[u, d, v] = svd(X)</td>
<td>X = matrix(rnorm(100), nrow=10)<br />
result = svd(X)<br />
<br />
<span style="color: gray"># singular values:</span><br />
result$d<br />
<br />
<span style="color: gray"># matrix of eigenvectors:</span><br />
result$u<br />
<br />
<span style="color: gray"># unitary matrix:</span><br />
result$v</td>
<td>np.linalg.svd(np.random.randn(100).reshape(10, 10))</td>
<td>X = randn(10, 10)<br />
<br />
u, s, v = svds(X)</td>
</tr>
<tr>
<td><a href="#matrix-solution">solve system of equations</a></td>
<td> A \ [2;3]</td>
<td>solve(A, c(2, 3))</td>
<td>np.linalg.solve(A, [2, 3])</td>
<td>[1 2; 3 4] \ [2; 3]</td>
</tr>
<tr>
<th colspan="5"><a name="sparse-matrices"></a><a href="#sparse-matrices-note">sparse matrices</a></th>
</tr>
<tr>
<th></th>
<th><a href="#matlab">matlab</a></th>
<th><a href="#r">r</a></th>
<th><a href="#numpy">numpy</a></th>
<th><a href="#julia">julia</a></th>
</tr>
<tr>