<jupyter_start><jupyter_text>Chapter 12: Advanced NumPy Run next cell to create arr1 and arr2<jupyter_code>#
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[7, 8, 9], [10, 11, 12]])<jupyter_output><empty_output><jupyter_text>Concatenate arr1 and arr2 in axis 1<jupyter_code>np.concatenate([arr1, arr2], axis=1)<jupyter_output><empty_output><jupyter_text>Run next cell to create arr<jupyter_code>#
arr = randn(5, 2)<jupyter_output><empty_output><jupyter_text>Split arr to three parts along the axis 0<jupyter_code>first, second, third = np.split(arr, [1, 3])
print(first)
print("===")
print(second)
print("===")
print(third)<jupyter_output>[[-0.34767245 -0.39894751]]
===
[[-1.63815218 -0.79970512]
 [-1.24955653  0.80095886]]
===
[[ 2.15927307 -0.30645637]
 [-0.11025443 -0.49557355]]<jupyter_text>Convert array([0, 1, 2]) to array([0, 0, 0, 1, 1, 1, 2, 2, 2])<jupyter_code>#
arr = np.arange(3)
np.repeat(arr, 3)<jupyter_output><empty_output><jupyter_text>Convert array([0, 1, 2]) to array([0, 0, 1, 1, 1, 2, 2, 2, 2])<jupyter_code>np.repeat(arr, [2, 3, 4])<jupyter_output><empty_output><jupyter_text>Run next cell to create array a<jupyter_code>#
a = randn(2, 2)<jupyter_output><empty_output><jupyter_text>Create a new array of [a, a, a; a, a, a] from a<jupyter_code>np.tile(a, (2, 3))<jupyter_output><empty_output><jupyter_text>Run next cell to create an array arr<jupyter_code>#
arr = np.arange(10)*100
arr<jupyter_output><empty_output><jupyter_text>Now, using one line code to replace arr[7] as 40, arr[1] as 41, arr[2] as 42, and arr[6] as 43<jupyter_code>arr.put([7, 1, 2, 6], [40, 41, 42, 43])
arr<jupyter_output><empty_output><jupyter_text>Run next cell to create arr<jupyter_code>#
arr = randn(1000, 50)<jupyter_output><empty_output><jupyter_text>Randomize the rows of arr by two ways: fancy indexing and the take method. Compare the performance of these methods by %timeit<jupyter_code>inds = np.random.permutation(np.shape(arr)[0])
%timeit arr[inds]
%timeit arr.take(inds)<jupyter_output>1.52 µs ± 106 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)<jupyter_text>Run next cell to create arr<jupyter_code>#
arr = randn(4, 3)
arr<jupyter_output><empty_output><jupyter_text>2D broadcasting: subtract column mean from each element in arr. Then prove that the the column mean of the result is zero.<jupyter_code>c_mean = arr.mean(axis=0)
c_mean
result = arr - c_mean
result.mean(axis=0)<jupyter_output><empty_output><jupyter_text>2D broadcasting: subtract row mean from each element in arr. Then prove that the the row mean of the result is zero.<jupyter_code>r_mean = arr.mean(axis=1)
r_mean
r_mean = r_mean[:, np.newaxis]
r_mean
result = arr - r_mean
result.mean(axis=1)<jupyter_output><empty_output><jupyter_text>Run next cell to create arr<jupyter_code>#
arr = randn(3, 4, 5)<jupyter_output><empty_output><jupyter_text>3D broadcasting: subtract row mean from each element in arr (axis=1). Then prove that the the row mean of the result is zero.<jupyter_code>r_mean = arr.mean(axis=1)
r_mean
r_mean = r_mean[:, np.newaxis, :]
r_mean.shape
result = arr-r_mean
result.mean(axis=1)<jupyter_output><empty_output><jupyter_text>Run next cell to create arr and arr2<jupyter_code>#
arr = np.zeros((4, 3))
arr2 = np.array([1, 2, 3, 4])<jupyter_output><empty_output><jupyter_text>Set values by broadcasting: set each column of arr as arr2<jupyter_code>arr[:] = arr2[:, np.newaxis]
arr<jupyter_output><empty_output><jupyter_text>Set values by broadcasting: set first row of arr as 10 the the second row as 11<jupyter_code>arr[:2, :] = np.array([[10], [11]])
arr<jupyter_output><empty_output><jupyter_text>Run next cell to define a function.<jupyter_code># 
def add_elements(x, y):
    return x + y<jupyter_output><empty_output><jupyter_text>Define a new numpy ufunc 'add_them' to apply add_elements on two arrays element-wisely.<jupyter_code>add_them = np.frompyfunc(add_elements, 2, 1)
add_them(np.arange(8), np.arange(8))<jupyter_output><empty_output><jupyter_text>Repeat last question and make sure the output of the new function has a data type of np.float64<jupyter_code>add_them = np.vectorize(add_elements, otypes=[np.float64])
add_them(np.arange(8), np.arange(8))<jupyter_output><empty_output><jupyter_text>Run next cell to define arr.<jupyter_code>#
arr = randn(10000)<jupyter_output><empty_output><jupyter_text>Compare the time cost of user-defined ufunc add_them(arr, arr) and the built-in numpy function np.add(arr, arr)<jupyter_code>%timeit add_them(arr, arr)
%timeit np.add(arr, arr)<jupyter_output>3.53 µs ± 985 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)<jupyter_text>Run next cell to create "name"<jupyter_code>#
first_name = np.array(['Bob', 'Jane', 'Steve', 'Bill', 'Barbara'])
last_name = np.array(['Jones', 'Arnold', 'Arnold', 'Jones', 'Walters'])
name = np.array([last_name, first_name])
name<jupyter_output><empty_output><jupyter_text>Sort name. Use last name as the primary key and first name as the secondary key.<jupyter_code>sorter = np.lexsort((name[1], name[0]))
name[:, sorter]<jupyter_output><empty_output><jupyter_text>Run next cell to create a sorted array "arr"<jupyter_code>arr = np.sort(np.array([0, 1, 7, 12, 15]))
arr<jupyter_output><empty_output><jupyter_text>Now insert [0, 8, 11, 16] into arr and keep the ascending order<jupyter_code>value = np.array([0, 8, 11, 16])
pos = np.searchsorted(arr, value)
pos
np.insert(arr, pos, value)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14285714285714285, 'fraction_numerical': 0.05276705276705277, 'mean_word_length': 7.083209509658246, 'pattern_counts': {'":': 0, '<': 114, '<?xml version=': 0, '>': 114, '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': '29', 'dir': 'jupyter-structured-clean-dedup', 'id': '394612', 'license': 'MIT', 'n_tokens_mistral': 2361, 'n_tokens_neox': 2147, 'n_words': 536, 'path': 'Chapter12.ipynb', 'repo_name': 'VandyChris/PythonForDataAnalysis', 'size': '5439'}