<jupyter_start><jupyter_text>**Vectorization** - use a single matrix multiplication to avoid for-loop vector multiplicaions.  Axis![](http://i.stack.imgur.com/DL0iQ.jpg)  RepeatRepeat a vector to matrix<jupyter_code>v = np.array([1, 2, 3])
np.repeat(v[:, np.newaxis], 3, 1)
np.repeat(v[np.newaxis, :], 3, 0)<jupyter_output><empty_output><jupyter_text>Vectorize a FunctionVectorize the function and then apply it directly to a Numpy array.Better to specify an explicit **output type** directly when vectorizing:```f = np.vectorize(f, otypes=[numpy.float])```<jupyter_code>def my_func(a):
    """Average first and last element of a 1-D array"""
    return (a[0] + a[-1]) * 0.5
b = np.array([[1,2,3], [4,5,6], [7,8,9]])
np.apply_along_axis(my_func, 0, b)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16901408450704225, 'fraction_numerical': 0.029449423815621, 'mean_word_length': 5.982142857142857, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '481201', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 312, 'n_tokens_neox': 293, 'n_words': 72, 'path': 'Vectorization.ipynb', 'repo_name': 'idf/NumPy-Quicksheet', 'size': '781'}