In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from datasets import synthetic as synth

%matplotlib inline
%load_ext autoreload
%autoreload 2

In [2]:
A = np.arange(12).reshape((4,3))
A


Out[2]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

In [3]:
y = np.array([2,1,1,3])
print np.unique(y).tolist()
idxs = [np.where(y == val)[0] for val in np.unique(y).tolist()]
idxs
# df = pd.DataFrame()
# df["X"] = A
# df['y'] = y
# df


[1, 2, 3]
Out[3]:
[array([1, 2]), array([0]), array([3])]

In [4]:
grouped = [A[idx] for idx in idxs]
print grouped
# map(lambda rows: rows, grouped)
map(lambda rows: rows.mean(axis=0), grouped)


[array([[3, 4, 5],
       [6, 7, 8]]), array([[0, 1, 2]]), array([[ 9, 10, 11]])]
Out[4]:
[array([ 4.5,  5.5,  6.5]), array([ 0.,  1.,  2.]), array([  9.,  10.,  11.])]

In [5]:
filter(lambda x: x==1, [1,2])


Out[5]:
[1]

In [6]:
x = np.arange(6)
x = np.r_[x, x]
x


Out[6]:
array([0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5])

In [7]:
print A*A
np.sum(A * A, axis=1)


[[  0   1   4]
 [  9  16  25]
 [ 36  49  64]
 [ 81 100 121]]
Out[7]:
array([  5,  50, 149, 302])

In [8]:
x = np.arange(12)[::-1]
print x
print np.argmin(x)
print np.argmax(x)


[11 10  9  8  7  6  5  4  3  2  1  0]
11
0

In [9]:
B = np.arange(36).reshape((6,3,2))
for el in B: print el


[[0 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]]

In [11]:
B[0].flatten()


Out[11]:
array([0, 1, 2, 3, 4, 5])

In [12]:
5 // 2.


Out[12]:
2.0

In [13]:
np.linspace(6, 0, num=20)


Out[13]:
array([ 6.        ,  5.68421053,  5.36842105,  5.05263158,  4.73684211,
        4.42105263,  4.10526316,  3.78947368,  3.47368421,  3.15789474,
        2.84210526,  2.52631579,  2.21052632,  1.89473684,  1.57894737,
        1.26315789,  0.94736842,  0.63157895,  0.31578947,  0.        ])

In [14]:
np.random.randn()


Out[14]:
-0.533605196434225

In [15]:
x * False


Out[15]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [16]:
l = [A, A]
print l
print np.asarray(l).reshape((-1,A.shape[1]))


[array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]]), array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

In [17]:
print A
np.std(A, axis=0)


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
Out[17]:
array([ 3.35410197,  3.35410197,  3.35410197])

In [18]:
z = np.array([2,0,3,1])
print np.argmax(z)
print np.argsort(z)
z[np.argsort(z)]


2
[1 3 0 2]
Out[18]:
array([0, 1, 2, 3])

In [19]:
np.dot((A - 7).T, A)
A.copy()
A.std()


Out[19]:
3.4520525295346629

In [20]:
from utils.arrays import zNormalizeRows, zNormalize

In [21]:
C = A[::-1]
b = np.array([2,4,3])
b = zNormalize(b)

M = np.random.randn(*A.shape)
M = zNormalizeRows(M)

# woot. Successfully getting distances from dot products
d1 = np.sum((M - b)**2, axis=1)
print d1
dotProd = np.dot(M, b)
m = len(b)
d2 = 2*(m - dotProd)
print d2
print d1 - d2
# assert(np.array_equal(d1, d2)) # false, although max diff is < 1e-15


[ 4.71271309  7.73474604  0.16943713  3.76792516]
[ 4.71271309  7.73474604  0.16943713  3.76792516]
[  0.00000000e+00   8.88178420e-16  -3.05311332e-16   4.44089210e-16]

In [22]:
a = np.array([2,4,3])
np.r_[0, np.cumsum(a)]


Out[22]:
array([0, 2, 6, 9])

In [23]:
np.prod(a)


Out[23]:
24

In [23]:
for i in range(4):
    print i
    for j in range(3):
        print i,j
        if j > 1:
            break


0
0 0
0 1
0 2
1
1 0
1 1
1 2
2
2 0
2 1
2 2
3
3 0
3 1
3 2

In [24]:
print A
print C
print np.abs(C[1] - A[1])


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[[ 9 10 11]
 [ 6  7  8]
 [ 3  4  5]
 [ 0  1  2]]
[3 3 3]

In [25]:
D = A*C*C
print D
highestStdColIdx = np.argmax(np.std(D, axis=0))
print highestStdColIdx
sortIdxs = np.argsort(D[:,highestStdColIdx])
print sortIdxs
print D[sortIdxs]


[[  0 100 242]
 [108 196 320]
 [ 54 112 200]
 [  0  10  44]]
2
[3 2 0 1]
[[  0  10  44]
 [ 54 112 200]
 [  0 100 242]
 [108 196 320]]

In [26]:
A2 = 2. / A
print A2
print np.isinf(A2)
print np.where(np.isinf(A2))[0]


[[        inf  2.          1.        ]
 [ 0.66666667  0.5         0.4       ]
 [ 0.33333333  0.28571429  0.25      ]
 [ 0.22222222  0.2         0.18181818]]
[[ True False False]
 [False False False]
 [False False False]
 [False False False]]
[0]

In [27]:
A.flatten().reshape((-1,4))


Out[27]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [28]:
x[x < 5] = 3
x


Out[28]:
array([11, 10,  9,  8,  7,  6,  5,  3,  3,  3,  3,  3])

In [29]:
np.c_[x, x, x]


Out[29]:
array([[11, 11, 11],
       [10, 10, 10],
       [ 9,  9,  9],
       [ 8,  8,  8],
       [ 7,  7,  7],
       [ 6,  6,  6],
       [ 5,  5,  5],
       [ 3,  3,  3],
       [ 3,  3,  3],
       [ 3,  3,  3],
       [ 3,  3,  3],
       [ 3,  3,  3]])

In [30]:
range(3, 10, 2)


Out[30]:
[3, 5, 7, 9]

In [31]:
msg = "t" if [] else "f"
print msg


f

In [32]:
# will the original array get modified? Answer: no
A 
x = A[0]
x = (x - np.mean(x)) / np.std(x)
print x
print A


[-1.22474487  0.          1.22474487]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

In [33]:
for el in {1,3,4}:
    print el


1
3
4

In [34]:
s = {1,3,2}
print A[list(s)]


[[ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

In [35]:
print A
print A.T.tolist()


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8, 11]]

In [36]:
l = [[1,2,3],[4,5,6], [7], [8,9]]
import itertools
print list(itertools.chain.from_iterable(l))


[1, 2, 3, 4, 5, 6, 7, 8, 9]

In [37]:
print np.hsplit(A, A.shape[1])


[array([[0],
       [3],
       [6],
       [9]]), array([[ 1],
       [ 4],
       [ 7],
       [10]]), array([[ 2],
       [ 5],
       [ 8],
       [11]])]

In [38]:
print x
print isinstance(x, (list, tuple))


[-1.22474487  0.          1.22474487]
False

In [39]:
# D = np.dstack((A,A))
D = np.empty((4,3,2))
D[:,:,0] = A
D[:,:,1] = A # doing same thing as dstack; apparently tensors just printed weird
print D.shape
print D
# so how can I stack all the 3rd dim stuff end to end vertically in 2 dims?
np.vstack((A, A)) # ie, i want this
# D.reshape((-1, D.shape[1])) # nope
# np.vstack(D[:,:].tolist()) # nope
D[:,:].tolist()


(4, 3, 2)
[[[  0.   0.]
  [  1.   1.]
  [  2.   2.]]

 [[  3.   3.]
  [  4.   4.]
  [  5.   5.]]

 [[  6.   6.]
  [  7.   7.]
  [  8.   8.]]

 [[  9.   9.]
  [ 10.  10.]
  [ 11.  11.]]]
Out[39]:
[[[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]],
 [[3.0, 3.0], [4.0, 4.0], [5.0, 5.0]],
 [[6.0, 6.0], [7.0, 7.0], [8.0, 8.0]],
 [[9.0, 9.0], [10.0, 10.0], [11.0, 11.0]]]

In [40]:
D = np.empty((2,4,3))
D[0,:,:] = A
D[1,:,:] = A
print D # ah, so this is actually what I wanted; makes sense cuz later idxs together in memory
print "vertStacked:"
print D.reshape((-1, D.shape[2])) # ah, here we go
print "horzStacked:"
print D.reshape((D.shape[1], -1)) # nope, not doing what I want


[[[  0.   1.   2.]
  [  3.   4.   5.]
  [  6.   7.   8.]
  [  9.  10.  11.]]

 [[  0.   1.   2.]
  [  3.   4.   5.]
  [  6.   7.   8.]
  [  9.  10.  11.]]]
vertStacked:
[[  0.   1.   2.]
 [  3.   4.   5.]
 [  6.   7.   8.]
 [  9.  10.  11.]
 [  0.   1.   2.]
 [  3.   4.   5.]
 [  6.   7.   8.]
 [  9.  10.  11.]]
horzStacked:
[[  0.   1.   2.   3.   4.   5.]
 [  6.   7.   8.   9.  10.  11.]
 [  0.   1.   2.   3.   4.   5.]
 [  6.   7.   8.   9.  10.  11.]]

In [41]:
A[:, 2:2]


Out[41]:
array([], shape=(4, 0), dtype=int64)

In [42]:
print A[:,::-1]
print np.sort(A[:,::-1], axis=1)


[[ 2  1  0]
 [ 5  4  3]
 [ 8  7  6]
 [11 10  9]]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

In [43]:
(A.T / np.linalg.norm(A.T, axis=0)).T


Out[43]:
array([[ 0.        ,  0.4472136 ,  0.89442719],
       [ 0.42426407,  0.56568542,  0.70710678],
       [ 0.49153915,  0.57346234,  0.65538554],
       [ 0.5178918 ,  0.57543534,  0.63297887]])

In [44]:
x = np.arange(10)[::-1]
x
print np.argsort(x)[:5]


[9 8 7 6 5]

In [45]:
%matplotlib inline
fig, ax = plt.subplots()
ax.imshow(np.random.random((10,10)))
ax.autoscale(False)
ax.plot(range(11))
plt.show()



In [46]:
def plotVertLine(x, ymin, ymax, ax=None):
	if not ax:
		ax = plt
	ax.plot([x, x], [ymin, ymax], color='k',
		linestyle='--', linewidth=2)

In [47]:
fig, ax = plt.subplots()
ax.imshow(np.random.random((10,10)))
ymin, ymax = ax.get_ylim()
plotVertLine(2, ymin, ymax, ax)



In [48]:
plt.get_ylim()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-48-340f29443503> in <module>()
----> 1 plt.get_ylim()

AttributeError: 'module' object has no attribute 'get_ylim'

In [ ]:
sums = np.sum(A, axis=1)
A / sums.reshape((4,1))

In [ ]:
a = np.arange(4)
b = a[::-1]
l = zip(a.tolist(), b.tolist())
print l
print zip(*l)

In [49]:
d = dict(zip(a, b))
d
print len(d)


3

In [50]:
d.get(5, [])


Out[50]:
[]

In [51]:
np.log2(8)


Out[51]:
3.0

In [52]:
np.vstack((A, A))


Out[52]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

In [53]:
print A.shape
print A.T.shape


(4, 3)
(3, 4)

In [54]:
w = np.zeros(3) + 2
A * w


Out[54]:
array([[  0.,   2.,   4.],
       [  6.,   8.,  10.],
       [ 12.,  14.,  16.],
       [ 18.,  20.,  22.]])

In [55]:
z = np.zeros(4) + 3
# A * z # breaks
A * z.reshape((-1,1)) # multiplies A[i] by z[i]


Out[55]:
array([[  0.,   3.,   6.],
       [  9.,  12.,  15.],
       [ 18.,  21.,  24.],
       [ 27.,  30.,  33.]])

In [56]:
np.sum(A, axis=0)


Out[56]:
array([18, 22, 26])

In [57]:
df = pd.DataFrame(whateverFile)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-57-274d2fc101f3> in <module>()
----> 1 df = pd.DataFrame(whateverFile)

NameError: name 'whateverFile' is not defined

In [58]:
import numpy as np
import matplotlib.pyplot as plt

# enable imports; must be run from notebook's directory
import sys
sys.path.append('../python') # get access to all our code
sys.path.append('../figs') # get access to all our figs
sys.path.append('..') # stuff doing relative imports needs to be imported relative to here


%matplotlib inline

from utils import arrays as ar
from datasets import synthetic as synth

In [59]:
seq = synth.multiShapesMotif(noise=.02)
seq = seq[0]
# plt.plot(seq)
# plt.title('Synthetic Time Series of 3 signals')
# plt.xlabel('Sample index')
# plt.savefig('../communicate/shapes.pdf')

seq = synth.trianglesMotif(noise=.04)[0]
plt.plot(seq, linewidth=2)
plt.title('Synthetic 1D Time Series')
plt.xlabel('Sample Index')
plt.savefig('../communicate/triangles.pdf')



In [60]:
np.random.rand(10)


Out[60]:
array([ 0.14953991,  0.14231937,  0.98014101,  0.87971351,  0.86175189,
        0.17797759,  0.03708135,  0.7435611 ,  0.53322297,  0.42921481])

In [61]:
zip(*[(1,2),(3,4),(5,6)])


Out[61]:
[(1, 3, 5), (2, 4, 6)]

In [62]:
x[np.asarray((1,4,7))]


Out[62]:
array([8, 5, 2])

In [63]:
x.tolist()


Out[63]:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In [64]:
1 <= 3 <= 5 < 7


Out[64]:
True

In [65]:
None or 3


Out[65]:
3

In [66]:
# so condition is first term when using and-or trick
print False and 1 or 3
print True and 1 or 3


3
1

In [67]:
l = [[1,2,3],[4,5,6], [7], [8,9]]
del l[2]
l


Out[67]:
[[1, 2, 3], [4, 5, 6], [8, 9]]

In [68]:
from scipy import signal
x2 = np.array([2,1,5])
x2 = np.r_[x2, -np.inf]
print x2
print signal.argrelextrema(x2, np.greater, mode='wrap')


[  2.   1.   5. -inf]
(array([0, 2]),)

In [69]:
np.empty(0)


Out[69]:
array([], dtype=float64)

In [70]:
x3 = x - 5
x4 = (x-5)[::-1]
print x3
print x4
print np.minimum(x3, x4)


[ 4  3  2  1  0 -1 -2 -3 -4 -5]
[-5 -4 -3 -2 -1  0  1  2  3  4]
[-5 -4 -3 -2 -1 -1 -2 -3 -4 -5]

In [71]:
from scipy.ndimage import filters as filt
x2 = np.array([5,4,3,2,1,4,1,2,3,2,1,5])
print x2
print filt.maximum_filter1d(x2, size=3)
print
print 6 - x2
print filt.minimum_filter(6 - x2, size=3)
print
print 4 - x2
print filt.minimum_filter(4 - x2, size=3)

# so this does exactly what I want, except that it centers the filter
# over each point, while I need to align the 1st position with the point
# to handle subseq overlaps...
    # -pretty confident that these aren't equivalent via translations 
    # or something cuz in the latter case, dist should never be compared
    # to points before it
    # -is this even what the problem reduces to? Ultimately, we want all
    # non-overlapping relative minima subseqs, and this won't address case
    # where something is the lowest nearby once the filter moves past an
    # even lower one, but still overlaps with the lowest
        # -I think what we really want is to find the relative minima, and
        # any time one of them has a dist < thresh, we report that one and
        # don't report anything else until overlap is over
            # -this is pretty simple, and mirrors online recognition case
            # -don't think we actually need a min filter here


[5 4 3 2 1 4 1 2 3 2 1 5]
[5 5 4 3 4 4 4 3 3 3 5 5]

[1 2 3 4 5 2 5 4 3 4 5 1]
[1 1 2 3 2 2 2 3 3 3 1 1]

[-1  0  1  2  3  0  3  2  1  2  3 -1]
[-1 -1  0  1  0  0  0  1  1  1 -1 -1]

In [72]:
print l
a, b, (c, d) = l
print a, b, c, d


[[1, 2, 3], [4, 5, 6], [8, 9]]
[1, 2, 3] [4, 5, 6] 8 9

In [73]:
v = np.arange(2)
s, e = v
print s, e


0 1

In [74]:
"foo{}".format(3)


Out[74]:
'foo3'

In [75]:
s = [1, 2]
s.append(3)
print s


[1, 2, 3]

In [76]:
# sorted() makes a copy, right? Edit: yes.
s2 = sorted(s)
del s2[1]
print s2
print s


[1, 3]
[1, 2, 3]

In [77]:
l2 = list(reversed(range(5)))
print l2
print np.sort(l2)


[4, 3, 2, 1, 0]
[0 1 2 3 4]

In [78]:
A
np.std(A, axis=1)


Out[78]:
array([ 0.81649658,  0.81649658,  0.81649658,  0.81649658])

In [79]:
A.T.ravel()
A.reshape(6, -1)
A


Out[79]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

In [80]:
F = np.fft.fft(A, axis=1)
f = np.fft.ifft(F, axis=1)

print A
print f
print np.array(f, dtype=np.int)


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[[  0.+0.j   1.+0.j   2.+0.j]
 [  3.+0.j   4.+0.j   5.+0.j]
 [  6.+0.j   7.+0.j   8.+0.j]
 [  9.+0.j  10.+0.j  11.+0.j]]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
-c:6: ComplexWarning: Casting complex values to real discards the imaginary part

In [81]:
x
x.shape[0]


Out[81]:
10

In [82]:
np.where(x > 3)[0]


Out[82]:
array([0, 1, 2, 3, 4, 5])

In [83]:
"foo".lower()


Out[83]:
'foo'

In [84]:
isinstance({'a': 1}, dict)


Out[84]:
True

In [85]:
l + l


Out[85]:
[[1, 2, 3], [4, 5, 6], [8, 9], [1, 2, 3], [4, 5, 6], [8, 9]]

In [86]:
d = {'a': 1}
d.keys()


Out[86]:
['a']

In [87]:
l0 = [('a',2),('b',4)]
l1 = [('aa',12),('bb',14)]
l2 = [('aaa',22),('ccc',24)]
import itertools
prod = list(itertools.product(l0, l1, l2))
print prod
print
paramsDicts = map(lambda combo: dict(combo), prod)
print paramsDicts


[(('a', 2), ('aa', 12), ('aaa', 22)), (('a', 2), ('aa', 12), ('ccc', 24)), (('a', 2), ('bb', 14), ('aaa', 22)), (('a', 2), ('bb', 14), ('ccc', 24)), (('b', 4), ('aa', 12), ('aaa', 22)), (('b', 4), ('aa', 12), ('ccc', 24)), (('b', 4), ('bb', 14), ('aaa', 22)), (('b', 4), ('bb', 14), ('ccc', 24))]

[{'a': 2, 'aa': 12, 'aaa': 22}, {'a': 2, 'aa': 12, 'ccc': 24}, {'a': 2, 'aaa': 22, 'bb': 14}, {'a': 2, 'ccc': 24, 'bb': 14}, {'aa': 12, 'b': 4, 'aaa': 22}, {'aa': 12, 'b': 4, 'ccc': 24}, {'b': 4, 'aaa': 22, 'bb': 14}, {'b': 4, 'ccc': 24, 'bb': 14}]

In [88]:
i = -1
for i in range(5):
    pass
print i # huh...vars in for loop accessible in outer scope, apparently


4

In [89]:
zip(x, x[::-1])


Out[89]:
[(9, 0),
 (8, 1),
 (7, 2),
 (6, 3),
 (5, 4),
 (4, 5),
 (3, 6),
 (2, 7),
 (1, 8),
 (0, 9)]

In [90]:
np.arange(.1, .9, .2)


Out[90]:
array([ 0.1,  0.3,  0.5,  0.7])

In [91]:
"foo".split('_')[-1]


Out[91]:
'foo'

In [92]:
int(round(2.9))


Out[92]:
3

In [93]:
'foo_bar'.replace('_', ' ')


Out[93]:
'foo bar'

In [94]:
l + l


Out[94]:
[[1, 2, 3], [4, 5, 6], [8, 9], [1, 2, 3], [4, 5, 6], [8, 9]]

In [95]:
print x
print np.r_[x,x]
print np.r_[A,A]


[9 8 7 6 5 4 3 2 1 0]
[9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

In [96]:
for row in A.T:
    print row
print A


[0 3 6 9]
[ 1  4  7 10]
[ 2  5  8 11]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

In [97]:
Anorm = A / np.linalg.norm(A)
print np.linalg.norm(Anorm)


1.0

In [98]:
T = np.empty((2,4,3))
T[0] = A
T[1] = 2*A
print T
print
print np.sum(T, axis=0)


[[[  0.   1.   2.]
  [  3.   4.   5.]
  [  6.   7.   8.]
  [  9.  10.  11.]]

 [[  0.   2.   4.]
  [  6.   8.  10.]
  [ 12.  14.  16.]
  [ 18.  20.  22.]]]

[[  0.   3.   6.]
 [  9.  12.  15.]
 [ 18.  21.  24.]
 [ 27.  30.  33.]]

In [99]:
from scipy.stats import binom
n = 30
p = .3
c = binom.cdf(np.arange(n+1), np.zeros(n+1)+n, p)
# print c
plt.plot(c);
# c_norm = c - np.mean(c)
# plt.plot(c_norm)
plt.figure()
sig = 1. / (1. + np.exp(c))
plt.plot(ar.zeroOneScaleMat(sig))


Out[99]:
[<matplotlib.lines.Line2D at 0x116cdf950>]

In [100]:
from scipy.stats import beta
x = np.linspace(0., 1., 20)
# scaleBy = 5
# a = 3
# b = 7
p = .3
plt.figure()
for scaleBy in range(10, 101, 10):
    c = beta.sf(x, p*scaleBy, (1-p)*scaleBy)
    plt.plot(x, c)



In [101]:
x = 2 * np.arange(9)[::-1]
print x
print np.argsort(x) # ascending
print np.argsort(x)[::-1] # descending


[16 14 12 10  8  6  4  2  0]
[8 7 6 5 4 3 2 1 0]
[0 1 2 3 4 5 6 7 8]

In [102]:
from scipy.misc import imresize
a = np.arange(10).reshape((-1, 1))
# print a
# print imresize(a, (5, 1), interp='bicubic') # huge garbagey numbers
# print
# print A
# print imresize(A, (2, 3)) # huge garbagey numbers

from scipy.signal import decimate
a = np.arange(10)
print a
print decimate(a, 2, n=1)
print 
print A
print decimate(A, 2, axis=0, n=1)


[0 1 2 3 4 5 6 7 8 9]
[ 0.          1.96671647  3.94839205  5.93830345  7.93274911]

[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[[ 0.          0.87099704  1.74199408]
 [ 5.90014941  6.82912616  7.75810291]]

In [103]:
# https://gist.github.com/andrewgiessel/5684769
import numpy as np
import pylab
from scipy.optimize import curve_fit
 
def sigmoid(x, x0, k):
    y = 1 / (1 + np.exp(-k*(x-x0)))
    return y
 
xdata = np.array([-5,-4,-3,-2,-1, .0, 1.0, 3.0, 4.3, 7.0, 8.0, 8.5, 10.0, 12.0])
ydata = np.array([0,0,0,0,0, .01, .02, .04, .11, .43, .7, .89, .95, .99])

popt, pcov = curve_fit(sigmoid, xdata, ydata)
print popt
 
x = np.linspace(-1, 15, 50)
y = sigmoid(x, *popt)
 
pylab.plot(xdata, ydata, 'o', label='data')
pylab.plot(x,y, label='fit')
pylab.ylim(0, 1.05)
pylab.legend(loc='best')
pylab.show()


[ 7.14248657  1.12930058]

In [104]:
A = np.arange(12, dtype=np.int).reshape((4,3))
B = np.hstack((A, A))
print B
pad = np.zeros((B.shape[0], 1), dtype=np.int)
B = np.hstack((B, pad))
rowIdxs, colIdxs = signal.argrelextrema(B, np.greater, mode='wrap', axis=1)
print
print rowIdxs, colIdxs
print
print np.vstack((rowIdxs, colIdxs)).T


[[ 0  1  2  0  1  2]
 [ 3  4  5  3  4  5]
 [ 6  7  8  6  7  8]
 [ 9 10 11  9 10 11]]

[0 0 1 1 2 2 3 3] [2 5 2 5 2 5 2 5]

[[0 2]
 [0 5]
 [1 2]
 [1 5]
 [2 2]
 [2 5]
 [3 2]
 [3 5]]

In [105]:
print np.hamming(5)


[ 0.08  0.54  1.    0.54  0.08]

In [106]:
np.tile((1,3,2), (3, 2))


Out[106]:
array([[1, 3, 2, 1, 3, 2],
       [1, 3, 2, 1, 3, 2],
       [1, 3, 2, 1, 3, 2]])

In [107]:
ri = (0,1,1,3)
ci = (0,1,2,2)
print A
print A[(ri, ci)]


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[ 0  4  5 11]

In [108]:
v = A.flatten()
print A
print v
print v.reshape(A.shape)


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[ 0  1  2  3  4  5  6  7  8  9 10 11]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

In [109]:
from python.algo import ff2
Asmall = A / 5. - 1.
# Asmall = -A / 10.
Aproj = ff2.l1Project(Asmall)
print Asmall
print
print Aproj
print np.sum(np.abs(Aproj))


[[-1.  -0.8 -0.6]
 [-0.4 -0.2  0. ]
 [ 0.2  0.4  0.6]
 [ 0.8  1.   1.2]]

[[-0.24 -0.04 -0.  ]
 [-0.   -0.    0.  ]
 [ 0.    0.    0.  ]
 [ 0.04  0.24  0.44]]
1.0

In [110]:
x = np.arange(8)[:-1]
np.r_[x, np.zeros(4)]


Out[110]:
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  0.,  0.,  0.,  0.])

In [111]:
np.sum(A > 0., axis=0)


Out[111]:
array([3, 4, 4])

In [112]:
np.empty((3,4))


Out[112]:
array([[ 0.24,  0.04,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.04,  0.24,  0.44]])

In [113]:
M = np.random.randn(4,3)
print M
print np.argmax(M, axis=1)


[[ 0.23561484  0.40735423 -0.66979958]
 [-1.30856708  0.76732804 -0.57180583]
 [-0.90716524  1.70325015  1.37291056]
 [-1.06839287 -0.67059997 -0.58693856]]
[1 1 1 2]

In [114]:
print x
x[np.array([False])] = x[np.array([False])]
print x


[0 1 2 3 4 5 6]
[0 1 2 3 4 5 6]

In [115]:
l3 = [[] for i in range(3)]
print l3
l3[0].append(1)
print l3


[[], [], []]
[[1], [], []]

In [116]:
print np.vstack([A, A])
print np.r_[A, A]
print np.r_[x, x]


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[0 1 2 3 4 5 6 0 1 2 3 4 5 6]

In [117]:
print x.reshape((-1,1))
print x


[[0]
 [1]
 [2]
 [3]
 [4]
 [5]
 [6]]
[0 1 2 3 4 5 6]

In [118]:
np.random.shuffle(x)
print x


[1 3 4 6 5 2 0]

In [119]:
np.var(A)


Out[119]:
11.916666666666666

In [124]:
B = np.array([[1,1],[0,1]])
print B
print np.argmax(B, axis=1)


[[1 1]
 [0 1]]
[0 1]

In [126]:
np.eye(2)[(np.arange(2), np.arange(2))]


Out[126]:
array([ 1.,  1.])

In [72]:
plt.plot(synth.notSoRandomWalk(100))
# walk, trend = synth.notSoRandomWalk(200)
# plt.plot(walk)
# plt.plot(trend);



In [107]:
# verify relationship between ED and cos sim
xNorm = (x - np.mean(x)) / np.std(x)
y = np.copy(xNorm)
np.random.shuffle(y)
print xNorm
print y
m = len(x)

# via dot prods normalized by length
cosSim_xx = np.dot(xNorm, xNorm) / m # 1.0
cosSim_yy = np.dot(y, y) / m
cosSim_xy = np.dot(xNorm, y) / m # > 1.0 sometimes...
print
print cosSim_xx # yep, 1.0
print cosSim_yy # yep, 1.0
print
print cosSim_xy

# via squared euclidean distances
diff = (xNorm - y)
dist = np.sum(diff*diff)
print 1. - dist/(2*m)

# via unit vects
xNorm2 = x - np.mean(x)
xNorm2 /= np.linalg.norm(xNorm2)
yNorm2 = y - np.mean(y)
yNorm2 /= np.linalg.norm(yNorm2)
print np.dot(xNorm2, yNorm2)


[ 1.59325501  1.30357228  1.01388955  0.72420682  0.43452409  0.14484136
 -0.14484136 -0.43452409 -0.72420682 -1.01388955 -1.30357228 -1.59325501]
[ 1.59325501 -0.43452409 -0.72420682 -1.30357228  1.30357228  0.43452409
 -0.14484136 -1.59325501  0.14484136  1.01388955  0.72420682 -1.01388955]

1.0
1.0

0.0979020979021
0.0979020979021
0.0979020979021

In [109]:
np.array([1])


Out[109]:
array([1])

In [10]:
# What *exactly* is the 2d conv func doing?
# It appears it flips the filter both horizontally and
# vertically like we'd expect, and valid does in fact
# just return the part where it's entirely in the matrix

from scipy import signal as sig
Lmax = 3
# we set filter = diag(1,2,3)
filt = np.zeros((Lmax, Lmax)) + np.diag(np.arange(Lmax)+1) # zeros except 1s on diag
testMat = np.zeros((8,8))
testMat[0,0] = 1
out = sig.convolve2d(testMat, filt, mode='valid')
plt.imshow(out, interpolation='none')
plt.colorbar()


Out[10]:
<matplotlib.colorbar.Colorbar instance at 0x116507c68>

In [11]:
testMat[:,:] = 0
# testMat[3, 3] = 1.
testMat[4, 4] = 1.
out = sig.convolve2d(testMat, filt, mode='valid')
plt.imshow(out, interpolation='none')
plt.colorbar()


Out[11]:
<matplotlib.colorbar.Colorbar instance at 0x116925c20>

In [12]:
testMat[:,:] = 0
testMat[-1, -1] = 1.
out = sig.convolve2d(testMat, filt, mode='valid')
plt.imshow(out, interpolation='none')
plt.colorbar()


Out[12]:
<matplotlib.colorbar.Colorbar instance at 0x116af7290>

In [14]:
np.sort(np.arange(4))


Out[14]:
array([0, 1, 2, 3])

In [17]:
"{0}.{1}".format(0, 1)


Out[17]:
'0.1'

In [33]:
# Um, break is just innermost loop, right? -> yep
for i in range(3):
    for j in range(4):
        print j
        break


0
0
0

In [34]:
# how do we get row, col argmax?
idx = np.argmax(A)
row, col = idx // A.shape[1], idx % A.shape[1]
print A.flatten()
print A.flatten()[idx], "==", A[row, col]


[ 0  1  2  3  4  5  6  7  8  9 10 11]
11 == 11

In [35]:
sorted([3, 5]) # yep, ascending order


Out[35]:
[3, 5]

In [38]:
print A[[1,2]]
print A[(1,2)]
print A[1, 2]


[[3 4 5]
 [6 7 8]]
5
5

In [41]:
print A
print np.diagonal(A)
print A[:3]
print np.diagonal(A[:3])


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[0 4 8]
[[0 1 2]
 [3 4 5]
 [6 7 8]]
[0 4 8]

In [2]:



---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-bf072e911907> in <module>()
----> 1 A

NameError: name 'A' is not defined

In [ ]: