forked from cirosantilli/python-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scipy_cheat.py
executable file
·827 lines (592 loc) · 15.1 KB
/
scipy_cheat.py
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
#!/usr/bin/env python
"""
## NumPy and SciPy
Current best math package for Python.
## Install
On Ubuntu 12.04:
sudo aptitude install python-scipy
Pip may not work because of missing binary dependencies.
sudo pip install numpy
sudo pip install scipy
## NumPy vs SciPy
SciPy uses and extends NumPy (think LAPACK BLAS).
It offers all functions in NumPy and many more conveient ones through the single `scipy` module.
Since NumPy is quite low level, just use SciPy all the time and avoid confusion.
## Sources
- <http://www.scipy.org/Tentative_NumPy_Tutorial>
- <www.scipy.org/PerformancePython>
"""
import math
import StringIO
import scipy as sp
import scipy.constants
import scipy.stats
import scipy.linalg as la
def norm2(a):
"""
Mean squared norm.
"""
return la.norm(a) / sp.size(a)
def dist2(a, a2):
return norm2(a - a2)
def array_equal(a, a2, err=10e-6, dist=dist2):
"""
True iff two sp.arrays are equal within a given `err` precision for given `dist` distance.
"""
return dist(a, a2) < err
if '## arrays':
"""
Basic n-dimensional computational object.
Are like C sp.arrays fixed length and efficient.
To extend them, must make new one.
Allocate all at once with zeros.
$a*b$ and $a+b$ ARE MUCH MORE EFFICIENT THAN PYTHON LOOPS!
because they are already compiled
Try to replace every loop with those operations.
"""
if '## data types':
"""
There are explicit data types for `sp.arrays`.
System dependant width (most efficient for system):
- bool_ Boolean (True or False) stored as a byte
- int_ Platform integer (normally either int32 or int64).
- float_ Shorthand for float64.
- complex_ Shorthand for complex128.
Those are the same as python types and may be interchanged.
Fixed widths:
- int32 Integer (-2147483648 to 2147483647)
- uint32 Unsigned integer (0 to 4294967295)
- float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
- float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
- complex64 Complex number, represented by two 32-bit floats (real and imaginary components)
Those have fixed width on all systems, and may not be compatible with the python types.
"""
assert sp.array_equal(
sp.array([1, 2, 3], dtype = sp.int_),
sp.int_([1, 2, 3])
)
# Different types evaluate to equal sp.arrays
assert sp.array_equal(
sp.array([1, 2, 3], dtype = sp.int_ ),
sp.array([1, 2, 3], dtype = sp.float_)
)
# Get type
v = sp.array([1,2], dtype = sp.int32)
assert v.dtype == sp.int32
# Subtype:
sp.issubdtype(sp.int32, sp.int_)
# Convert type
v = sp.array([1,2], dtype = sp.int32)
vf = v.astype(sp.float_)
assert vf.dtype == sp.float_
### type_ vs dtype
# `type_` is the same as using the dtype arg.
# That said, *always use the sp.array* methods without dtye for uniformity
# And if you need explicit type, use the dtype arg.
if '## Create arrays':
### multidimensional
v = sp.array([
[1, 2, 3],
[4, 5, 6],
])
### dimensions must match
# TODO why does this work? type object
#try:
#v = sp.array([
#[1, 2, 3],
#[4,5],
#])
#except ValueError:
#pass
#else:
#assert False
if '## zeros':
assert sp.array_equal(
sp.zeros((1, 2)),
sp.array([[0,0]])
)
assert sp.array_equal(
sp.zeros((2, 1)),
sp.array([[0],[0]])
)
assert sp.array_equal(
sp.zeros((1, 2, 3)),
sp.array([[[0,0,0],
[0,0,0]]])
)
if '## ones':
assert sp.array_equal(
sp.ones((1, 2)),
sp.array([[1,1]])
)
if '## arange':
# BAD idea:
'''
assert array_equal(
sp.arange(3),
sp.array([0, 1, 2])
)
'''
# May fail because of precision.
# Good idea:
assert array_equal(
sp.arange(2.1),
[0, 1, 2]
)
assert array_equal(
sp.arange(2, 5.1),
[2, 3, 4, 5]
)
assert array_equal(
sp.arange(0.1, 2.2),
[0.1, 1.1, 2.1]
)
assert array_equal(
sp.arange(0, 5, 2),
[0, 2, 4]
)
if '## linspace':
assert array_equal(
sp.linspace(0, 1, 6),
[0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
)
if '## meshgrid':
x = sp.arange(0, 2.1)
y = sp.arange(0, 3.1)
(X, Y) = sp.meshgrid(x, y)
assert array_equal(
X,
sp.array([
[ 0., 1., 2.],
[ 0., 1., 2.],
[ 0., 1., 2.],
[ 0., 1., 2.]])
)
assert array_equal(
Y,
sp.array([
[ 0., 0., 0.],
[ 1., 1., 1.],
[ 2., 2., 2.],
[ 3., 3., 3.]])
)
if '## indices':
assert array_equal(
sp.indices((2, 3)),
sp.array([
[
[0, 0, 0],
[1, 1, 1]
],
[
[0, 1, 2],
[0, 1, 2]
]
])
)
if '## size':
# Get total number of elements:
assert sp.zeros((2, 3, 4)).size == 24
assert sp.size(sp.zeros((2, 3, 4))) == 24
assert sp.size(1) == 1
# 2 vs 1x2 vs 1x2:
assert not sp.array_equal(
[1, 2], # number of dimensions: 1. size of dimension 1: 2
[[1, 2]] # 2. : 1 size of dimension 2: 2
)
if '## shape':
# Get / set size of each dimension.
# Think like this:
# > The Nth most external list, has how many elements? this is the size of the Nth dimension.
assert sp.array([1,2]).shape == (2,)
assert sp.array([[1,2]]).shape == (1,2)
assert sp.array([[1],[2]]).shape == (2,1)
### Change shape
# In place:
v = sp.arange(5.1)
v.shape = (2, 3)
assert sp.array_equal(
v,
[
[0, 1, 2],
[3, 4, 5]
]
)
# Create new:
assert sp.array_equal(
sp.arange(5.1).reshape((2, 3)),
[
[0, 1, 2],
[3, 4, 5]
]
)
# Make into one dimension (create new):
x = sp.arange(6).reshape(2, 3)
assert sp.array_equal(
sp.ravel(x),
sp.arange(6)
)
if '## file io':
# TODO: examples
"""
a = sp.zeros((2, 3))
# Space separated.
sp.savetxt("a.tmp", a)
sp.savetxt("b.tmp", delimiter = ", ")
# single width format
sp.savetxt("c.tmp", delimiter = 3)
# multi width format
sp.savetxt("d.tmp", delimiter = (4, 3, 2))
# strip trailing/starting whitespace
sp.savetxt("e.tmp", autostrip = True)
# stop reading line when # is found
sp.savetxt("f.tmp", comments = '# ')
# skip first line, and last two lines
sp.savetxt("g.tmp", skip_header = 1, skip_footer = 2)
# only use first and last columns
sp.savetxt("h.tmp", usecols = (0, -1))
# same, give names
sp.savetxt("b.tmp", names = "a, b, c", usecols = ("a", "c"))
b = genfromtxt("a.tmp")
b = loadtxt("a.tmp")
"""
if 'loadtxt':
assert array_equal(
sp.loadtxt(StringIO.StringIO("0 1\n2 3")),
[
[0, 1],
[2, 3],
]
)
assert array_equal(
sp.loadtxt(
StringIO.StringIO("0 1\n2 3"),
usecols = (1,)
),
[
[1, 3],
]
)
# It is slow for large files:
# http://stackoverflow.com/questions/18259393/numpy-loading-csv-too-slow-compared-to-matlab
if '## indexing':
x = sp.arange(5.1)
x.shape = (2, 3)
assert x[0, 0] == 0
assert x[0, 1] == 1
assert x[1, 0] == 3
x[0,0] = 1
assert x[0,0] == 1
# With array
x = sp.arange(2.0, 5.1)
assert sp.array_equal(
x[[1, 1, 0, 3]],
[3, 3, 2, 5]
)
# TODO:
x = sp.arange(5.1)
x.shape = (2,3)
# assert sp.array_equal(
# x[ sp.array([[1,0], [0,1]]) ],
# sp.array([3, 1])
# )
if '## slicing':
x = sp.array([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
])
assert sp.array_equal(
x[:, 0],
[0, 3, 6]
)
assert sp.array_equal(
x[0, :],
[0, 1, 2]
)
assert sp.array_equal(
x[0:3:2, 0:3:2],
[
[0, 2],
[6, 8]
]
)
if '## broadcasting':
"""
Means to decide the right operation based on input types.
"""
if '## sum':
# Arrays of same size:
assert array_equal(
sp.arange(5.1).reshape((2,3)) +
[
[0, 1, 0],
[1, 0, 1]
],
[
[0, 2, 2],
[4, 4, 6]
],
)
# Arrays of different size:
assert array_equal(
sp.arange(5.1).reshape(2,3) +
sp.arange(2.1),
[
[0, 2, 4],
[3, 5, 7]
]
)
# Broadcasting for scalars:
assert array_equal(
sp.arange(5.1) + 1,
sp.arange(1,6.1)
)
# Over all elements:
assert array_equal(
sp.sum([
[0, 1],
[2, 3]
]),
6
)
# Some dimensions only:
assert array_equal(
sp.sum([[0, 1], [2, 3]], axis = 0),
sp.array([2, 4])
)
assert array_equal(
sp.sum(
[
[0, 1],
[2, 3]
],
axis = 1
),
[1, 5]
)
if '## multiplication':
# Scalar broadcast:
assert array_equal(
sp.arange(3.1) * 2,
sp.arange(0, 6.1, 2.0)
)
# Between arrays of same dimensions:
assert array_equal(
sp.arange(3.1) *
sp.arange(3.1),
sp.array([0, 1, 4, 9])
)
# Between arrays of different size:
assert array_equal(
sp.arange(5.1).reshape(2,3) *
sp.arange(2.1),
[
[0, 1, 4],
[0, 4, 10]
]
)
# Dot product;
A = sp.array([
[1, 2],
[3, 4]
])
x = sp.array([[1, 2]]).T
assert array_equal(
A.dot(x),
sp.array([[5],
[11]])
)
if '## vectorize':
# vectorize a function that was meant for scalar use
# making it more efficient? TODO confirm.
def add(a, b):
return a + b
vec_add = sp.vectorize(add)
assert array_equal(
vec_add(sp.array([0,1,2]), sp.array([3,4,5])),
sp.array([3,5,7])
)
## linalg
# matrices, vectors and norms
#### matrix vs 2D arrays
# summary: *prefer 2D arrays*
# everything that can be done with matrix can be done with 2d arrays
# matrix only allows for some shortcuts.
# but in the end, this brings confusion, and gives less flexibility, so prefer arrays.
# just for reference
A = sp.mat('[1 2;3 4')
A = sp.mat('[1, 2; 3, 4')
A = sp.mat([[1, 2], [3, 4]])
b = sp.mat('[5;6]')
A.T # transpose
A.H # conjugate transpose
A.I # inverse
A*b # matrix multiplication
A*A # matrix multiplication
# we will forget the matrix class from now on.
## without mat
### transpose
assert sp.array_equal(
sp.array([[1,2],[3,4]]).T,
sp.array([[1,3],[2,4]]),
)
assert sp.array_equal(
sp.array([[1,2]]).T,
sp.array([[1],[2]]),
)
# But *watch out*!!!:
assert sp.array_equal(
sp.array([1,2]).T,
sp.array([1,2])
)
# T only works as expected on nxm objects, not on n objects!
### conjugate
assert array_equal(
sp.array([[1j,2j]]).conjugate(),
sp.array([[-1j,-2j]]),
)
### conjugate transpose
assert array_equal(
sp.array([[1j,2j]]).conjugate().T,
sp.array([[-1j],[-2j]]),
)
### identity
assert sp.array_equal(
sp.eye(2),
sp.array([[ 1., 0.],
[ 0., 1.]])
)
### determinant
assert array_equal(
la.det(sp.array([[1,2],[3,4]])),
-2
)
### inverse
# **DO NOT USE THIS TO SOLVE LINEAR SYSTEMS**
# use <# solve> instead, or an explicit LU decomposition.
# (solve likely uses LU it under the hood)
# This will be faster and more stable.
# Learn what LU decomposition is now if you don't know so.
A = sp.array([[1, 2], [3, 4]])
assert array_equal(
la.inv(A).dot(A),
sp.eye(2)
)
### solve
# solve linear system:
A = sp.array([[1, 2], [3, 4]])
b = sp.array([[5, 11]]).T
x = la.solve(A, b)
assert array_equal(
A.dot(x),
b
)
# Solve multiple linear systems:
# TODO
A = sp.array([[1, 2], [3, 4]])
b = sp.array([[5, 11],[5,11]])
x = la.solve(A, b)
assert array_equal(
A.dot(x),
b
)
# Singular raises exception:
A = sp.zeros((2,2))
b = sp.array([[5, 11]]).T
try:
x = la.solve(A, b)
except la.LinAlgError:
pass
else:
assert False
### eigenvalues and vectors
A = sp.array([[1, 1], [0, 2]])
vals, vecs = la.eig(A)
n = A.shape[0]
# Check they are eigenvectors:
for i in xrange(0,n):
assert array_equal(
A.dot(vecs[:,i].T),
vals[i] * vecs[:,i]
)
# Check that they are normalized:
assert array_equal(
sp.sum(abs(vecs**2), axis = 0),
sp.ones((1,n))
)
### norms
# \max |Ax|_y, |x| = 1, |x|_y = \sqrt{\sum (|x_i|)^y}{y}
# the choice of y gives rise to the different norms
# often they have a simple interpretation for matrices
A = sp.array([[0, 1], [2, 3]])
# sum squares and take square root:
assert array_equal(
la.norm(A),
sp.sqrt(sp.sum(A*A))
)
assert array_equal(
la.norm(A,'fro'),
sp.sqrt(sp.sum(A*A))
)
# norm inf == max row sum:
assert array_equal(
la.norm(A,sp.inf),
max(sp.sum(A, axis = 1))
)
# norm 1 == max column sum:
assert array_equal(
la.norm(A,1),
max(sp.sum(A, axis = 0))
)
# norm -1 == min column sum:
assert array_equal(
la.norm(A,-1),
min(sp.sum(A, axis = 0))
)
if '## polynomials':
# 1x^2 + 2x + 3
p = sp.poly1d([1, 2, 3])
if '## get info':
assert sp.array_equal(
p.coeffs,
sp.array([1,2,3])
)
assert sp.array_equal(
p.order,
2
)
# Evaluate:
assert array_equal(p([1,2]), sp.array([6,11]))
# Roots:
assert array_equal(p(p.r), sp.zeros(p.order))
# Operations:
p**2 + p*p + p
p.integ(k = 6)
p.deriv()
if '## random':
# Mean 1, standard deviation 2:
sp.random.normal(1, 2)
# 2 x 3 random sp.arrays:
assert sp.random.normal(1, 2, (2, 3)).shape == (2,3)
if '## constants':
# Many physical ones.
# http://docs.scipy.org/doc/scipy/reference/constants.html
assert array_equal(scipy.constants.pi, math.pi)
if '## stats':
if '## pearsonr':
# https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient
assert array_equal(
scipy.stats.pearsonr(
[1, 2, 3],
[2, 4, 6],
)[0],
1
)
assert array_equal(
scipy.stats.pearsonr(
[1, 2, 3],
[-2, -4, -6],
)[0],
-1
)