<jupyter_start><jupyter_text>What is NumPy?- NumPy stands for Numerical Python used for working with arrays.- NumPy aims to provide an array object that is up to 50x faster that traditional Python lists. - Most of the parts of NumPy that require fast computation are written in C or C++.- The array object in NumPy is called `ndarray`- NumPy is often used along with SciPy (scientific python) and Matplotlib (for plotting). Source: https://www.tutorialspoint.com/numpy<jupyter_code>import numpy as np
print(np.__version__)
arr = np.array([1, 2, 3, 4, 5])
arr
type(arr)
a = np.array([[1, 2], [3, 4]]) 
a
a = np.array([1, 2, 3, 4, 5], ndmin = 2) 
a<jupyter_output><empty_output><jupyter_text>Shape and reshape<jupyter_code>a = np.array([[1,2,3],[4,5,6]]) 
a
a.shape
a = np.array([[1,2,3],[4,5,6]]) 
a.shape = (3,2) 
a
a = np.array([[1,2,3],[4,5,6]]) 
b = a.reshape(3,2) 
b
a = np.arange(24)
a
b = a.reshape(2,4,3) 
b<jupyter_output><empty_output><jupyter_text>Array from numerical ranges**numpy.arange(start, stop, step, dtype)**<jupyter_code>x = np.arange(5)
x
x = np.arange(5, dtype = float)
x
x = np.arange(10,20,2) 
x<jupyter_output><empty_output><jupyter_text>**numpy.linspace(start, stop, num, endpoint, retstep, dtype)**<jupyter_code>x = np.linspace(10,20,5) 
x
x = np.linspace(10,20, 5, endpoint = False)
x<jupyter_output><empty_output><jupyter_text>Indexing and Slicing- items in ndarray object follows **zero-based index**- indexing methods: field access, basic slicing and advanced indexing- **slice object** is constructed by giving **start**, **stop**, and **step** parameters to the *built-in slice function*<jupyter_code>a = np.arange(10) 
a
s = slice(2,7,2) 
a[s]
a = np.arange(10) 
b = a[2:7:2]
b<jupyter_output><empty_output><jupyter_text>Slicing from index<jupyter_code>a = np.arange(10) 
a[2:]
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
a
a[1:]
a[:1]<jupyter_output><empty_output><jupyter_text>Advanced Indexing<jupyter_code>x = np.array([[1, 2], [3, 4], [5, 6]])
x
y = x[[0,1,2], [0,1,0]]
y<jupyter_output><empty_output><jupyter_text>The selection *y* includes elements at (0,0), (1,1) and (2,0) from *x*.  Boolean Array Indexing<jupyter_code>x = np.array([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]]) 
x
x[x > 5]<jupyter_output><empty_output><jupyter_text>**NaN omitting**<jupyter_code>a = np.array([np.nan, 1,2,np.nan,3,4,5]) 
a
a[~np.isnan(a)]<jupyter_output><empty_output><jupyter_text>Broadcasting<jupyter_code>a = np.array([1,2,3,4]) 
a
b = np.array([10,20,30,40])
b
c = a * b
c<jupyter_output><empty_output><jupyter_text>**More complex example**<jupyter_code>a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]]) 
a
b = np.array([1.0,2.0,3.0]) 
b
c = a + b
c<jupyter_output><empty_output><jupyter_text>Iterating Over Array<jupyter_code>a = np.arange(0,60,5) 
a = a.reshape(3,4)
a
b = a.T 
b
for x in np.nditer(b): 
    print(x, end = ' ')<jupyter_output><empty_output><jupyter_text>**Another example**<jupyter_code>a = np.arange(0,60,5) 
a = a.reshape(3,4)
a
b = np.array([1, 2, 3, 4], dtype = int) 
b
for x, y in np.nditer([a, b]): 
   print("%d:%d" % (x, y), end = ' ')<jupyter_output><empty_output><jupyter_text>Statistical Functions**Min, Max**<jupyter_code>a = np.array([[3,7,5],[8,4,3],[2,4,9]])
a
np.amin(a, axis=0)
np.amin(a, axis=1)
np.amin(a)<jupyter_output><empty_output><jupyter_text>**Median**<jupyter_code>a = np.array([[30,65,70],[80,95,10],[50,90,60]])
a
np.median(a) 
np.median(a, axis=0)
np.median(a, axis=1)<jupyter_output><empty_output><jupyter_text>**Mean**<jupyter_code>a = np.array([[1,2,3],[3,4,5],[4,5,6]])
a
np.mean(a)
np.mean(a, axis=0)
np.mean(a, axis=1)<jupyter_output><empty_output><jupyter_text>**Variance**<jupyter_code>np.var([1,2,3,4])<jupyter_output><empty_output><jupyter_text>**Standard Deviation**<jupyter_code>np.std([1,2,3,4])<jupyter_output><empty_output><jupyter_text>Arithmetic Operations**Input arrays** for performing arithmetic operations such as add(), subtract(), multiply(), and divide() must be either of **the same shape** or should conform to array broadcasting rules.<jupyter_code>a = np.arange(9, dtype = np.float_).reshape(3,3) 
a
b = np.array([10,10,10])
b
np.add(a,b)
np.subtract(a,b)
np.multiply(a,b)
np.divide(a,b)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2003754106053496, 'fraction_numerical': 0.058423275457531676, 'mean_word_length': 6.558510638297872, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 78, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '405029', 'license': 'Apache-2.0', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1926, 'n_words': 368, 'path': 'cvicenia/notebooks/tyzden-02/IAU_uvod_numpy.ipynb', 'repo_name': 'xmajzel/2020-2021', 'size': '4262'}