<jupyter_start><jupyter_text>NumPy[NumPy](https://numpy.org/) is a fundamental tool to scientific computing.<jupyter_code># Import library
import numpy as np<jupyter_output><empty_output><jupyter_text>ArraysNumPy allows you to work with N-dimensional array objects.  Create arrays<jupyter_code># Using a python list
np.array(object=[1, 2, 3, 4])
# Using "arange" function
np.arange(start=10)
# Using "zeros" function
np.zeros(shape=(2, 3, 2))
# Using "ones" function
np.ones(shape=(3, 4))
# Using "linspace" function
np.linspace(start=0, stop=1, num=11)<jupyter_output><empty_output><jupyter_text>Dimension of arrays<jupyter_code># Get the dimension of an array (one dimension)
one_dimension_array = np.array(object=[1, 2, 3, 4, 5])
one_dimension_array.shape
# Get the dimension of an array (two dimension)
two_dimension_array = np.array(object=[[1, 2, 3, 4, 5]])
two_dimension_array.shape
# Compare two arrays
np.array_equal(a1=one_dimension_array, a2=two_dimension_array)
# Reshape arrays
new_shape = (1, one_dimension_array.shape[0])
reshaped_array = one_dimension_array.reshape(new_shape)
reshaped_array
# Compare two arrays
np.array_equal(a1=reshaped_array, a2=two_dimension_array)<jupyter_output><empty_output><jupyter_text>Slicing<jupyter_code># Create random three dimensional array
three_dimensional_array = np.random.rand(2, 3, 2)
three_dimensional_array
# Using slicing with int and [:] notation
three_dimensional_array[:1,1:,0]<jupyter_output><empty_output><jupyter_text>Arithmetic operations<jupyter_code>array = np.arange(start=5)

print("array      =", array)
print("array + 5  =", array + 5)
print("array - 1  =", array - 1)
print("array / 5  =", array / 5)
print("array // 2 =", array // 2)
print("-array     =", -array)
print("array ** 2 =", array ** 2)
print("array % 2  =", array % 2)<jupyter_output>array      = [0 1 2 3 4]
array + 5  = [5 6 7 8 9]
array - 1  = [-1  0  1  2  3]
array / 5  = [0.  0.2 0.4 0.6 0.8]
array // 2 = [0 0 1 1 2]
-array     = [ 0 -1 -2 -3 -4]
array ** 2 = [ 0  1  4  9 16]
array % 2  = [0 1 0 1 0]<jupyter_text>| Operator | Universal functions (ufunc) || -------- | -------------------------- ||``+``     | ``np.add()``               ||``-``     | ``np.subtract()``          ||``*``     | ``np.multiply()``          |**Note**: Universal functions are executed using compiled C code.<jupyter_code># Other ufuncs
print(np.exp(array))                # Exponential
print(np.log(array))                # Natural logarithm
print(np.sqrt(array))               # Square root
print(np.greater(array, array))     # Greater<jupyter_output>[ 1.          2.71828183  7.3890561  20.08553692 54.59815003]
[      -inf 0.         0.69314718 1.09861229 1.38629436]
[0.         1.         1.41421356 1.73205081 2.        ]
[False False False False False]<jupyter_text>Performance<jupyter_code>%%timeit
array = np.arange(start=1000000)
zeros_array = np.zeros(shape=1000000)

i = 0
for item in array:
    zeros_array[i] = item + item
    i += 1
%%timeit
array + array<jupyter_output>391 ns ± 5.96 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)<jupyter_text>Statistics and randomness<jupyter_code>array = np.arange(start=10)

print(np.mean(a=array))
print(np.median(a=array))
print(np.percentile(a=array, q=40))
print(np.random.random(size=10))<jupyter_output>4.5
4.5
3.6
[0.77365046 0.84302356 0.2555429  0.5318994  0.46716859 0.22768869
 0.15814208 0.90560498 0.34638337 0.97627776]
{'fraction_non_alphanumeric': 0.15483494011101373, 'fraction_numerical': 0.09290096406660824, 'mean_word_length': 3.884450784593438, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, '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': '627888', 'license': 'MIT', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1284, 'n_words': 306, 'path': 'notes/numpy.ipynb', 'repo_name': 'ivanmtoroc/scipy-intro', 'size': '3423'}