<jupyter_start><jupyter_text>Introduction to numpy __numpy array__ NumPy array is a data container. It is similar to Python lists, but it’s specialised for working on numerical data. NumPy is at the center of scientific Python ecosystem and it is a work-horse of many scientific libraries including scikit-learn, scikit-image, matplotlib, SciPy.In general you should use this library if you want to do fancy things with **numbers**, especially if you have **matrices** or **arrays.** To use NumPy we need to start python interpreter and import numpy package:<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Let's create a simple numpy array<jupyter_code>x = np.array([2, 1, 5])
print(x)<jupyter_output><empty_output><jupyter_text>Lists vs Numpy arraysThe Python core library provides Lists. A list is the Python equivalent of an array, but it is resizeable and can contain elements of different types.Pros of an array: - **Size** - Numpy data structures take up less space - **Performance** - faster than lists - **Functionality** - SciPy and NumPy have optimized functions such as linear algebra operations built in.<jupyter_code>L = range(1000)
%timeit [i**2 for i in L]<jupyter_output><empty_output><jupyter_text>__np.arange__ works like Python built-in range, but it returns an array;<jupyter_code>np.arange(5)
a = np.arange(1000)
%timeit a**2<jupyter_output><empty_output><jupyter_text>Memory layoutNumPy array is just a memory block with extra information how to interpret its contents. To construct an array with pre-defined elements we can also use one of the built-in helper functions: __np.ones__ and __np.zeros__ return arrays of 0s or 1s;<jupyter_code>np.ones(5)
np.zeros(5)<jupyter_output><empty_output><jupyter_text>__np.random.rand__ creates an array of random numbers from an interval [0, 1]:<jupyter_code>np.random.rand(5)<jupyter_output><empty_output><jupyter_text>We can also construct a two- or more dimensional arrays:<jupyter_code>np.array([[1, 2], [5, 6]])
np.ones((2, 2))<jupyter_output><empty_output><jupyter_text>Alternatively, a n-dimensional array can be obtained by reshaping a 1-D array:<jupyter_code>a = np.arange(12)
a.reshape((4,3))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09225922592259225, 'fraction_numerical': 0.0144014401440144, 'mean_word_length': 6.360927152317881, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '31803', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 726, 'n_tokens_neox': 692, 'n_words': 253, 'path': 'Day_1_Scientific_Python/numpy_with_answers/numpys/numpy_intro.ipynb', 'repo_name': 'paris-saclay-cds/python_workshop_2019', 'size': '2222'}