<jupyter_start><jupyter_text>1. read array from text2. save as npy3. read from npy<jupyter_code>text_path = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
arr = np.genfromtxt(text_path, delimiter=',', dtype='object')
np.save('file.npy',arr)
np.load('/content/file.npy',allow_pickle=True)<jupyter_output><empty_output><jupyter_text>stack an array to matrix vertically and horizontally<jupyter_code>a = np.random.randint(0,100,size=(3,3))
b = np.random.randint(0,100,size=(1,3))
print(np.vstack([a,b])) # horizontal
np.hstack([a,b.T]) # vertical<jupyter_output><empty_output><jupyter_text>replace items that satisfy a condition with another value<jupyter_code>a = np.random.randint(0,100,size=(10,10))
print(a)
print(a > 20)
a[a>20] = -1
print(a)<jupyter_output><empty_output><jupyter_text>get the intersection between two python numpy arrays<jupyter_code>a = np.arange(10)
b = np.arange(5) * 2
intersection = np.intersect1d(a,b)
print(intersection)<jupyter_output>[0 2 4 6 8]<jupyter_text>remove itema of an array from another array<jupyter_code>a = np.arange(10)
b = np.arange(5) * 2
differences = np.setdiff1d(a,b)
print(differences)<jupyter_output><empty_output><jupyter_text>extract all elements between a given range<jupyter_code>a = np.arange(10)
b = a[(a >= 5) & (a <= 10)]
print(b)<jupyter_output><empty_output><jupyter_text>change columns order in a 2d numpy array<jupyter_code>a = np.arange(12).reshape((3,4))
print(a)
new_index = [3,0,1,2]
print(a[:,new_index])<jupyter_output>[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 3  0  1  2]
 [ 7  4  5  6]
 [11  8  9 10]]<jupyter_text>count elements<jupyter_code>a = np.random.randint(0,100,size=(100,100))
print(np.unique(a,return_counts=True)[0]) #data 
print(np.unique(a,return_counts=True)[1]) #count<jupyter_output>[ 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]
[103 108  94  85  99 110 103 101  84 100  99 116 120 110  95  81 100 108
 114 109  99  93 112  99 105 103 120  90  97 107  75  97 116 107 107 111
 103  99  95 116  94 111  90 106  90 117 120  94 109 102 117 102  91 112
  86 105 120 104  91  96  95  91  92 108 104  83 108  90 103  99  92  84
  92 105  97  92 103  73  92  99 105 101  96 104 107  81  81  85  99  92
  95 115 106 101  92 116  86 103 103  83]<jupyter_text>sort by column<jupyter_code>a = np.random.randint(0,100,size=(10,10))
print(a[a[:,3].argsort()]) # sort based on column 3<jupyter_output><empty_output><jupyter_text>generate one hot encoding<jupyter_code>a = np.random.randint(0,100,size=(10,10))
one_hot = np.eye(np.max(a) + 1) [a]
print(one_hot)<jupyter_output><empty_output><jupyter_text>numpy broadcasting . we have two arrays with shape [a] and [b]. we want to compute distance between each two elements using broadcasting<jupyter_code>a = np.random.randint(0,10,size=(7))
b = np.random.randint(0,10,size=(5))
result = np.abs(a - b[:,None])<jupyter_output><empty_output><jupyter_text>arithmetic operation<jupyter_code>a = np.random.randint(0,100,size=(10,10))
b = np.random.randint(1,100,size=(10,10))
print(a+b)
print(a-b)
print(a*b)
print(a/b)<jupyter_output>[[ 90 178 107  50  65 143 131 186 127  83]
 [ 84  27  11  62  92  19 149 109 135 103]
 [105  45  81  84  57 110  79  76  64 111]
 [ 95  38  15 140  57 155 172 135  45  65]
 [134 157 108 133  15  94 151 100  63  41]
 [ 68  62  95  41  95  25  86 122  50 139]
 [100 115  95 119 154  89  92 105 125 107]
 [ 36  92  56  90  81 143 122  88 107 105]
 [126 156  32 197 140  80  98 113 150 113]
 [148  89  88 145  80 141  46  66  96  34]]
[[-68   4  87   2 -55 -21 -33   8   5  11]
 [  0  23  -7 -58  34  -3 -39  -3  45 -91]
 [-31   5 -81  64 -49 -68   9 -42 -46 -23]
 [ 61 -18  -5 -14   3 -37 -10 -21  33  -7]
 [ 26   3  -6 -11   1 -68   1 -26  11   7]
 [  6  54  33 -33 -33   7  40 -70  42  37]
 [-28 -65 -73  31   6 -73  24  51 -37 -57]
 [ 20 -58  48 -44  51 -23  76 -40 -73 -53]
 [-20  14  28   1 -40 -34  12  -7 -14 -39]
 [ 34  21  -4 -25 -26  15 -26  36 -24 -28]]
[[ 869 7917  970  624  300 5002 4018 8633 4026 1692]
 [1764   50   18  120 1827   88 5170 2968 4050  582]
 [2516  500    0  740  212 1869 [...]<jupyter_text>max values of columns<jupyter_code>a = np.random.randint(0,100,size=(10,10))
print(a)
print(np.amax(a,axis=0)) # 0 for col 1 for row<jupyter_output>[[74 29 28 11 99 50 94 83 97 73]
 [ 2 19 62 27 91 57 15  0 98 69]
 [75 86 81 59 29 49  1  3 78 52]
 [67 90  8 68 38 77 47 95 66 20]
 [89 99  6 54 22 65 24 53 75 82]
 [70 48 98 19 13 45 75 13 69 88]
 [28  1 29 15  5 71 58 91 75 77]
 [95 81 37 34 66 88  2  9 12 33]
 [50 23 86 80 62 46 25 28 95 23]
 [ 7 82 75 81 51 22 83 52 15 64]]
[95 99 98 81 99 88 94 95 98 88]<jupyter_text>check array equality<jupyter_code>a = np.random.randint(0,100,size=(10,10))
b = a + np.random.rand(10,10) * 1e-6
print(a-b)
np.allclose(a,b,atol=1e-5)<jupyter_output>[[-1.26445663e-08 -6.58008005e-07 -1.02242865e-07 -5.47877633e-07
  -7.93865063e-08 -1.70871949e-07 -7.26900028e-07 -1.06834364e-09
  -4.70375937e-07 -6.56350092e-07]
 [-1.76913538e-07 -8.45903898e-07 -9.57245732e-08 -2.97325187e-07
  -5.58811074e-07 -3.41782879e-07 -9.87414474e-07 -9.74521202e-07
  -5.29563671e-07 -7.29567972e-07]
 [-6.03957474e-07 -7.96866125e-07 -4.33304777e-07 -5.60491465e-07
  -8.11025522e-08 -7.07274239e-10 -1.97026198e-07 -7.55636620e-09
  -8.48557065e-07 -2.26685295e-07]
 [-4.21914063e-07 -6.65508981e-08 -9.08814965e-07 -2.56023512e-07
  -7.53916858e-07 -8.06983792e-07 -9.86670287e-07 -9.70603168e-07
  -6.36795392e-07 -9.20729697e-07]
 [-5.31153209e-07 -3.53329366e-07 -1.38553176e-07 -1.83723152e-07
  -3.94262116e-08 -8.55937275e-07 -3.43371607e-07 -5.04920592e-07
  -4.82102678e-08 -4.20591334e-07]
 [-1.39187165e-07 -4.37494649e-07 -3.64646837e-07 -4.27938105e-07
  -1.44174919e-07 -6.93649710e-07 -8.32878655e-07 -4.19684071e-07
  -3.93690179e-07 -1.79903410e-07[...]<jupyter_text>matrix multiplication<jupyter_code>a = np.random.randint(0,100,size=(6,8))
b = np.random.randint(0,100,size=(8,10))
c = a @ b
print(c.shape)
print(c)<jupyter_output>(6, 10)
[[27198 23758 26631 29236 30024 25866 28988 32381 27548 32317]
 [21822 16812 22564 19234 19868 19804 21421 22619 22236 24522]
 [17532 16197 21448 17042 15852 17436 17364 20786 19006 24099]
 [20293 17073 21748 21726 22578 20436 22478 25206 22143 24774]
 [19366 17936 18270 23051 22347 20072 19989 25219 22209 27155]
 [15235 10493 10283 16152 16106 14536 17147 15988 11755 14822]]<jupyter_text>compute rank of matrix using numpy.linalg<jupyter_code>a = np.random.randint(0,100,size=(6,8))
rank = np.linalg.matrix_rank(a)
print(rank)<jupyter_output>6<jupyter_text>compute matrix norm using numpy.linalg<jupyter_code>a = np.random.randint(0,100,size=(6,8))
norm = np.linalg.norm(a) # (sum of values^2)^(1/2)
print(norm)<jupyter_output>382.5637201826645<jupyter_text>compute matrix inverse using numpy.linalg<jupyter_code>a = np.random.randint(0,100,size=(8,8))
inverse = np.linalg.inv(a)
print(inverse)<jupyter_output>[[ 1.17937680e-01  4.61561894e-02  3.94586052e-02 -6.70461534e-02
   1.11310578e-01  3.39641258e-03 -1.66527139e-01 -8.22527452e-02]
 [-5.19643711e-03 -8.01532694e-03 -3.35553931e-03  8.23834292e-04
   9.71575917e-03  5.32460250e-03  2.01551128e-03  4.06441426e-03]
 [-1.15504960e-01 -3.26333886e-02 -4.09521147e-02  5.70758726e-02
  -1.11930120e-01  3.09269453e-03  1.67429570e-01  7.60452759e-02]
 [-6.76965668e-02 -2.05573832e-02 -3.12952065e-02  3.96597215e-02
  -7.73702766e-02 -9.24532065e-03  1.07045983e-01  5.90432445e-02]
 [ 2.21267104e-03  4.33261913e-04  1.10425880e-02 -9.89101802e-03
  -3.67313797e-03  6.25130637e-03  9.50929210e-03 -1.48468858e-02]
 [-1.06658330e-02  2.03784586e-03  9.96814673e-04  9.26019771e-03
  -8.41063714e-03 -6.19393811e-03  1.68735677e-02 -2.97425966e-05]
 [-3.72946188e-02 -2.24650922e-02 -9.48245832e-03  2.44323063e-02
  -2.75577904e-02  2.30789345e-03  3.47559697e-02  3.82561038e-02]
 [ 6.89952844e-02  1.76330443e-02  1.93416236e-02 -2.43030104e-02
   [...]
{'fraction_non_alphanumeric': 0.13972569189321576, 'fraction_numerical': 0.36737692872887584, 'mean_word_length': 3.3767416934619505, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 66, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '662837', 'license': 'MIT', 'n_tokens_mistral': 5737, 'n_tokens_neox': 3807, 'n_words': 961, 'path': 'notebooks/basics/solutions/numpy/numpy_solutions.ipynb', 'repo_name': 'AIMed-Team/edu-content', 'size': '8166'}