In [1]:
import numpy as np

In [10]:
x = np.array([0,0,0,0,0,0,0,0,0])

In [11]:
x


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

In [14]:
idx = np.random.choice(x,5,replace = False)

In [16]:
idx


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

In [17]:
np.arange(len(x))


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

In [20]:
t = np.random.choice(np.arange(len(x)),5,replace = True)

In [21]:
t


Out[21]:
array([7, 3, 3, 2, 4])

In [22]:
x[t]


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

In [23]:
x = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]])

In [24]:
x


Out[24]:
array([[1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4]])

In [25]:
t = np.random.choice(np.arange(len(x)),5,replace = True)

In [26]:
t


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

In [27]:
x[t,:]


Out[27]:
array([[4, 4, 4, 4],
       [1, 1, 1, 1],
       [4, 4, 4, 4],
       [4, 4, 4, 4],
       [3, 3, 3, 3]])

In [31]:
x[0,2] = 5

In [33]:
np.argmax(x,axis=0)


Out[33]:
array([3, 3, 0, 3], dtype=int64)

In [34]:
x


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

In [1]:
import numpy as np

In [36]:
x = np.array([1,2,3,4,5,6])

In [37]:
np.exp(x)


Out[37]:
array([   2.71828183,    7.3890561 ,   20.08553692,   54.59815003,
        148.4131591 ,  403.42879349])

In [2]:
x = np.rand((5,5))


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-7f52e51b3f93> in <module>()
----> 1 x = np.rand((5,5))

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

In [6]:
np.random.randn(5,5)


Out[6]:
array([[-0.44848151,  0.96502332,  0.61610453,  0.35158466,  0.82428406],
       [-1.97643952, -0.75227204,  0.23570676, -0.68976733, -1.58864503],
       [-0.28514507, -1.14815048,  0.62006344,  0.2626151 , -1.85030856],
       [ 0.60290351, -1.88334705, -1.18899365,  1.16799447,  0.78233971],
       [ 0.7521398 , -0.15458736,  0.59434215,  1.8364121 , -1.70602337]])

In [9]:
X = np.random.randn(500,3073)

In [8]:
y = np.random.randn(500,1)

In [13]:
W = W = np.random.randn(3073, 10)

In [15]:
scores = X[1,:].dot(W)

In [18]:
scores = np.exp(scores)
# normalize
scores_sum = np.sum(scores)
scores = scores / scores_sum

In [19]:
scores.shape


Out[19]:
(10L,)

In [27]:
scores[2] #2=y[i]


Out[27]:
1.2099666370668609e-51

In [29]:
np.exp


Out[29]:
<ufunc 'exp'>

In [30]:
exp()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-30-2a06c663e383> in <module>()
----> 1 exp()

NameError: name 'exp' is not defined

In [32]:
np.log(2)


Out[32]:
0.69314718055994529

In [34]:
range(0,5) ==


Out[34]:
[0, 1, 2, 3, 4]

In [35]:
np.zeros(5)


Out[35]:
array([ 0.,  0.,  0.,  0.,  0.])

In [38]:
(3==3)*1


Out[38]:
1

In [52]:
X = np.array([[0,1,2,3],[0,1,2,3],[0,1,2,3]])

In [55]:
scores = (X.T - np.max(X,axis=1)).T

In [48]:
scores = np.exp(scores)

In [57]:
sum_s = np.sum(scores,axis=1)

In [62]:
(scores.T / sum_s).T


Out[62]:
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

In [70]:
scores


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

In [73]:
scores = (scores.T*1.0/sum_s).T

In [72]:
y = [2,3,0]

In [75]:
scores[range(len(scores)),y]


Out[75]:
array([ 0.16666667, -0.        ,  0.5       ])

In [76]:
scores


Out[76]:
array([[ 0.5       ,  0.33333333,  0.16666667, -0.        ],
       [ 0.5       ,  0.33333333,  0.16666667, -0.        ],
       [ 0.5       ,  0.33333333,  0.16666667, -0.        ]])

In [77]:
scores.shape


Out[77]:
(3L, 4L)

In [3]:
xrange(1e-7,1e-4,10)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-f637cc6d3b94> in <module>()
----> 1 xrange(1e-7,1e-4,10)

TypeError: integer argument expected, got float

In [1]:
import numpy as np

In [10]:
np.arange(1e-7,1e-5,0.01)


Out[10]:
array([  1.00000000e-07])

In [27]:
np.linspace(1e-5,1e-7,10)


Out[27]:
array([  1.00000000e-05,   8.90000000e-06,   7.80000000e-06,
         6.70000000e-06,   5.60000000e-06,   4.50000000e-06,
         3.40000000e-06,   2.30000000e-06,   1.20000000e-06,
         1.00000000e-07])

In [2]:
x = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3]])

In [3]:
x


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

In [4]:
x + np.array([1,1,1,1])


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

In [5]:
f = lambda x: np.max(0,x)

In [12]:
x[0,0] = -5

In [13]:
np.maximum(x,0)


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

In [17]:
x**2


Out[17]:
array([[25,  1,  1,  1],
       [ 4,  4,  4,  4],
       [ 9,  9,  9,  9]])

In [20]:
s = np.ones((5,4))

In [21]:
t = np.ones(5)

In [22]:
t


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

In [25]:
s.T * t


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

In [28]:
s.shape


Out[28]:
(5L, 4L)

In [29]:
t.shape


Out[29]:
(5L,)

In [30]:
s*t


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-30-f93fe870a2c7> in <module>()
----> 1 s*t

ValueError: operands could not be broadcast together with shapes (5,4) (5,) 

In [32]:
s


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

In [33]:
t


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

In [34]:
t.dot(s)


Out[34]:
array([ 5.,  5.,  5.,  5.])

In [ ]: