In [1]:
import numpy as np

In [2]:
a = np.random.choice(5, 2, replace=False)
print(a)


[1 2]

In [3]:
a = np.random.choice(5, 2, replace=False)
print(a)


[2 0]

In [4]:
a = np.random.choice(5, 2, replace=False)
print(a)


[4 1]

In [5]:
a = np.random.choice(5, 4, replace=False)
print(a)


[2 3 0 1]

In [6]:
b = [1, 2, 3, 4 ,5]
c = np.random.choice(5, 3)
print(c)


[1 1 3]

In [7]:
c = np.arange(8).reshape((2, 4))
print(c)


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

In [8]:
print(c.shape[1])


4

In [9]:
print(c.shape)


(2, 4)

In [10]:
print(c.flatten())


[0 1 2 3 4 5 6 7]

In [11]:
print(c)


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

In [12]:
c = c.flatten().reshape((2,2,2))

In [13]:
print(c)


[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]

In [14]:
c = c.flatten().reshape((2,4))

In [15]:
d = np.arange(2, 6)
d = d[np.random.choice(4,4)]

print(d)


[4 5 4 4]

In [16]:
print(d)
print(c)
print(d-c)


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

In [17]:
print(c)


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

In [18]:
c[1,:] = d
print(c)


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

In [19]:
c[1,2] = d
print(c)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-e17f97ee52ab> in <module>()
----> 1 c[1,2] = d
      2 print(c)

ValueError: setting an array element with a sequence.

In [20]:
# c = np.arange(8).reshape((2, 4))
print(c)


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

In [21]:
d = np.matrix(c)
print(d)


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

In [22]:
d = np.arange(8).reshape((4,2))
print(d)


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

In [23]:
e = c[1,:] + d[:,1]
print(e)


[ 5  8  9 11]

In [24]:
e = c[1,:] + d[:,1].T
print(e)


[ 5  8  9 11]

In [25]:
print(d)


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

In [26]:
print(c)


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

In [27]:
e=[[1,3],[0,2]]
e = np.array(e)
print(e)


[[1 3]
 [0 2]]

In [29]:
print(e[c])


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-29-b03086d5958c> in <module>()
----> 1 print(e[c])

IndexError: index 2 is out of bounds for axis 0 with size 2

In [32]:
e=[[1,3],[0,2]]
print(c(e))


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-1b85d105a7e7> in <module>()
      1 e=[[1,3],[0,2]]
----> 2 print(c(e))

TypeError: 'numpy.ndarray' object is not callable

In [41]:
aa = np.arange(10).reshape((10,-1))
print(aa)


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

In [34]:
print(c)


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

In [35]:
print(c[1, 2])


4

In [42]:
aa = np.arange(10).reshape((10,-1))
print(aa)


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

In [43]:
aa = np.arange(10).reshape((10,1))
print(aa)
bb = np.arange(20).reshape((10,2))
print(bb)


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

In [45]:
bb = np.arange(20).reshape((10,2))
print(bb)


[[ 0  1]
 [ 2  3]
 [ 4  5]
 [ 6  7]
 [ 8  9]
 [10 11]
 [12 13]
 [14 15]
 [16 17]
 [18 19]]

In [46]:
bb = np.arange(200).reshape((20,10))
print(bb)


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

In [52]:
cc = np.arange(10).reshape(-1)
print(cc[4])
print(bb[np.arange(20), cc])


4
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-52-5e8409a301d7> in <module>()
      1 cc = np.arange(10).reshape(-1)
      2 print(cc[4])
----> 3 print(bb[np.arange(20), cc])

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (20,) (10,) 

In [53]:
aa = np.arange(10).reshape((10,1))
print(aa)
bb = np.arange(20).reshape((10,2))
print(bb)


[[0]
 [1]
 [2]
 [3]
 [4]
 [5]
 [6]
 [7]
 [8]
 [9]]
[[ 0  1]
 [ 2  3]
 [ 4  5]
 [ 6  7]
 [ 8  9]
 [10 11]
 [12 13]
 [14 15]
 [16 17]
 [18 19]]

In [55]:
print(bb-aa+2.0)


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

In [57]:
ab = bb - aa + 2.0
y = np.zeros(10)
y[2] = 1
y[2] = 1
y[2] = 1
y[2] = 1
y[2] = 1
print(y)
# loss[np.arrange(10), y] = 0


[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]

In [ ]: