<jupyter_start><jupyter_text>A tutorial of Numpy  1 Arrays  1.1 Create Arrays<jupyter_code>import numpy as np
# from python list
a = np.array([1,2,3])
print(type(a))
print(a.shape)
print(a[0], a[1], a[2])
a[0] = 5
print(a)
# create 2-dimensional array
b = np.array([[1,2,3], [4,5,6]])
print(b)
print(b.shape)
print(b[0,0],b[1,:],b[:,1])
# from others method
a = np.zeros((2,2))
print('a:', a)
b = np.ones((3,1))
print('b:', b)
c = np.full((2,2), 7)
print('c:', c)
d = np.eye(2)
print('d:', d)
e = np.random.random((2,2))
print('e:', e)<jupyter_output>a: [[ 0.  0.]
 [ 0.  0.]]
b: [[ 1.]
 [ 1.]
 [ 1.]]
c: [[ 7.  7.]
 [ 7.  7.]]
d: [[ 1.  0.]
 [ 0.  1.]]
e: [[ 0.42498497  0.64819431]
 [ 0.54289753  0.81944948]]<jupyter_text>1.2 Visit Elements<jupyter_code>a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(a)
b = a[:2,1:3]
print(b)
# a slice of array is view of the same data. so modify
# it will modify the original array
print(a[0,1])
b[0,0]=77
print(a[0,1])
# slice will generate the low rank array 
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
row_r1 = a[1, :]
row_r2 = a[1:2,:]
print(row_r1, row_r1.shape)
print(row_r2, row_r2.shape)
# integer will create subset of array
a = np.array([[1,2], [3, 4], [5, 6]])
print(a[[0,1,2],[0,1,0]])
print(np.array([a[0,0],a[1,1],a[2,0]]))
print(a[[0,0],[1,1]])
# change every row's elements
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
print(a)
b =  np.array([0,2,0,1])
print(a[np.arange(4),b])
a[np.arange(4), b] +=10
print(a)
# Using boolean data
a = np.array([[1,2], [3, 4], [5, 6]])
bool_idx = (a>2)
print(bool_idx)
print(a[bool_idx])
print(a[a>2])
print(a[a%3==0])<jupyter_output>[3 6]<jupyter_text>2 Operations<jupyter_code>x = np.array([[1,2], [3,4]], dtype=np.float64)
y = np.array([[5,6], [7,8]], dtype=np.float64)
print(x+y)
print(np.add(x,y))
print(x-y)
print(x*y)
print(x/y)
print(np.dot(x,y))
# sum 
print(np.sum(x))
print(x)
# compute the each column
print(np.sum(x, axis=0))
# compute the each row
print(np.sum(x, axis=1))<jupyter_output>[[ 1.  2.]
 [ 3.  4.]]
[ 4.  6.]
[ 3.  7.]<jupyter_text>3 Broadcastingwe can do mathematical arrays' operations between different shape size.<jupyter_code>x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
#create an empty with the same shape as x
y = np.empty_like(x)
for i in range(4):
    y[i,:]=x[i,:]+v
print(y)
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
vv = np.tile(v, (4, 1))  # Stack 4 copies of v on top of each other
print(vv)
print(x)
y=x+vv
print(y)
# broadcasting will help us reduce code
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
print(x+v)<jupyter_output>[[ 2  2  4]
 [ 5  5  7]
 [ 8  8 10]
 [11 11 13]]
{'fraction_non_alphanumeric': 0.26175720014582576, 'fraction_numerical': 0.1057236602260299, 'mean_word_length': 4.704781704781705, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '594863', 'license': 'MIT', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1393, 'n_words': 313, 'path': 'PreTraining/NumpyTutorial.ipynb', 'repo_name': 'gaufung/StandfordCS231n', 'size': '2743'}