<jupyter_start><jupyter_text>Numpy**Numpy** is the shorting for **numerical python**.  1 What Is NumPy Arrays (Ndarrays)- **Numpy array** is an important data structure for data science.- **Array** is useful for **fast** and **efficient** numerical operations.- **Numpy array** is **faster** than **built-in python** data structures.- **Ndarray** is a common data structure for data analysis libraries and for machine learnings.<jupyter_code>%timeit list(range(1000))
import numpy as np
%timeit np.arange(1000)
type(np.arange(1000))<jupyter_output><empty_output><jupyter_text>2 Create NumPy Arrays (Ndarrays)<jupyter_code>a = np.random.randn(2, 3)
a<jupyter_output><empty_output><jupyter_text>**ndim**: To check the **dimension** of the **array**<jupyter_code>a.ndim<jupyter_output><empty_output><jupyter_text>**shape**: To check the **shape** of the **array**<jupyter_code>a.shape<jupyter_output><empty_output><jupyter_text>**dtype**: To check the **type** of the **array**<jupyter_code>a.dtype<jupyter_output><empty_output><jupyter_text>**np.array()**: To **convert** a **list** into **numpy array**<jupyter_code>list1 = [1, 2, 3, 4]
a = np.array(list1)
a
a.ndim
a.shape<jupyter_output><empty_output><jupyter_text>**zeros()**: To create a **numpy array** with certain size filled with **zeros**<jupyter_code>a = np.zeros(10)
a<jupyter_output><empty_output><jupyter_text>**ones()**: To create a **numpy array** with certain size filled with **ones****Note**: To create a **multi-dimensional numpy array** filled with **zeros (ones)**, you need to pass the **dimension** as a **tuple**<jupyter_code>a = np.ones((2, 3))
a<jupyter_output><empty_output><jupyter_text>3 Data Types for Ndarrays Common numpy data types are:- Integers- Floating points- Strings- Booleans<jupyter_code>list1 = [1, 2, 3, 4]
a = np.array(list1)
a
a.dtype<jupyter_output><empty_output><jupyter_text>**np.array(, dtype)**: To **specify** the **data type** of an **existing array** while converting to **numpy array**<jupyter_code>a = np.array(list1, dtype='int16')
a
a.dtype<jupyter_output><empty_output><jupyter_text>**astype(dtype)**: To **change** the **data type** of an **existing numpy array**<jupyter_code>b = a.astype(np.float64)
b
b.dtype<jupyter_output><empty_output><jupyter_text>https://www.tutorialspoint.com/numpy/numpy_data_types.htm<jupyter_code>list2 = ['19','24','34','14','56']
a = np.array(list2)
a.dtype<jupyter_output><empty_output><jupyter_text>If we try an arithmetic operation with this array we get an error, because it is not numerical<jupyter_code>a - 5
a = a.astype(np.int16)
a - 5
a = np.array(list2, dtype='int16')
a - 5
a = np.array(list2, dtype=np.int16)
a - 5<jupyter_output><empty_output><jupyter_text>4 Arithmetic with NumPy Arrays**Numpy arrays** are **fast** and **efficient** in numerical computations (**vectorization**, without any loop).<jupyter_code>a = np.array([[1,2,3], [4,5,6]])
a<jupyter_output><empty_output><jupyter_text>We can do **element-wise** operations of this array by itself<jupyter_code>a * a
a - a<jupyter_output><empty_output><jupyter_text>We can do the operations with a **scalar** because of **broadcasting** feature of numpy.<jupyter_code>a - 4
a > 4<jupyter_output><empty_output><jupyter_text>5 Indexing and SlicingIt is used to **select a subset** of dataset. Integer Indexing<jupyter_code>a = np.arange(2, 20, 2)
a
a[2:6]<jupyter_output><empty_output><jupyter_text>**Slice** is a **mirror**, **not a copy**<jupyter_code>b = a[:4]
b
a
a = np.array([[1,2,3], [4,5,6],[7,8,9],[10,11,12]])
a
a[0:2]
a[:,1]<jupyter_output><empty_output><jupyter_text>Boolean IndexingIt is a **slicing** method based on the **boolean expersions** which is used to **filter the data**.<jupyter_code>a = np.array(['a', 'b', 'c', 'a', 'd', 'b', 'a'])
b = np.array([22, 45, 22, 61, 19, 23, 31])
b[a == 'a']
a == 'a'
c = np.random.randn(7,2)
c
c[a == 'a']
d = np.array([[1,23], [4,43], [1, 17],[3,53], [5,23], [1, 42],[3,15], [1,34], [4, 41],[2, 23]])
d
d[d[:,0] == 1]<jupyter_output><empty_output><jupyter_text>Facny IndexingIt uses the **numeric indices** to access and slice **multiple elements** of an array.<jupyter_code>a = np.random.randint(50, size= 10)
a
ind = [1,3,5,7]
a[ind]
ind2 = np.array([[1,3], [5,7]])
a[ind2]
a = np.random.randint(100, size=(3,4))
a<jupyter_output><empty_output><jupyter_text>To slice two-dimensional array, We create an index that contains two sets, one for rows and one for columns:<jupyter_code>ind = ((0,1,2),(3,2,3))
a[ind]<jupyter_output><empty_output><jupyter_text>We can also create two separate indexes, one for rows and one for columns:<jupyter_code>row = [0,1,2]
column = [3,2,3]
a[row, column]<jupyter_output><empty_output><jupyter_text>6 Transposing Arrays**transpose()** or **T**: To **transpose** an array<jupyter_code>a = np.random.randint(20, size=(3,4))
a
a.transpose()
a.T<jupyter_output><empty_output><jupyter_text>If we try to **transpose** a **one dimensional** array, it will be the **same**:<jupyter_code>a = np.array([1, 2, 3, 4, 5, 6])
a.shape
b = a.T
b.shape<jupyter_output><empty_output><jupyter_text>7 Mathematical and Statistical Methods<jupyter_code>a = np.random.randn(4, 5)
a
np.min(a)
a.min()
a.min(axis=0) # min across rows
a.min(axis=0).shape
a.min(axis=1) # min across columns
a.min(axis=1).shape
a.argmin() # returns the index of the minumum value
a.argmin(axis=0)<jupyter_output><empty_output><jupyter_text>std, sum, cumsum, min, max, argmin, argmax We can also use statistics for **boolean** values, in that case, **True** is considered **one** and **Fasle** as **zero**. It will be useful when dealing with **missing values** in large datasets.<jupyter_code>a = np.array([True, False, False, True, True])
a.sum()<jupyter_output><empty_output><jupyter_text>Two important functions when dealing with missing values:**any()**: It scans a **boolean array**, if **at least one** value is **True**, it will **return True**.**all()**: It scans a **boolean array**, if **all elements** are **True**, it will **return True**.<jupyter_code>a.any()
a.all()<jupyter_output><empty_output><jupyter_text>8 Sorting Arrays<jupyter_code>a = np.random.randint(20, size=5)
a
np.sort(a)
a<jupyter_output><empty_output><jupyter_text>**np.sort()**: It returns a **sorted copy** and the **original array** is **kept** as it is.<jupyter_code>a.sort()
a<jupyter_output><empty_output><jupyter_text>**sort()**: This functions **sorts** the array **in place** meaning that the **original array** has been **changed**.By **default**, the sorting will be in **ascending order** and there is **no option** for **descending order**.<jupyter_code>a[::-1]
a = np.random.randn(4,5)
a
a.sort(0)
a
a.sort(1)
a<jupyter_output><empty_output><jupyter_text>**np.unique()**: To return a **sorted unique elements** meaning that **repeated elements** are **removed**It will be usefule when you have **duplicate entry** in your dataset.<jupyter_code>a = np.array(['Harry', 'Jake', 'Harry', 'George', 'Jake', 'Harry', 'James', 'Jake', 'Jake', 'Harry'])
np.unique(a)<jupyter_output><empty_output><jupyter_text>9 File Input and Output**Numpy** has two functions for **saving** and **loading array data**:- **np.save()** for saving numpy arrays. - **np.load()** for loading numpy arrays.Arrays are saved with the **file extension (.npy)**<jupyter_code>a = np.arange(5)
a
np.save('my_array', a)
b = np.load('my_array.npy')<jupyter_output><empty_output><jupyter_text>**np.savez**: To **save multiple arrays** in a **dictionary like** file and the **file extension** is **.npz**<jupyter_code>np.savez('multiple_arrays', a1=a, a2=b)
c = np.load('multiple_arrays.npz')
c['a1']
bp.na<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18223443223443223, 'fraction_numerical': 0.027341705913134486, 'mean_word_length': 7.159018143009605, 'pattern_counts': {'":': 0, '<': 133, '<?xml version=': 0, '>': 134, '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': '33', 'dir': 'jupyter-structured-clean-dedup', 'id': '69776', 'license': 'MIT', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2906, 'n_words': 742, 'path': 'Numpy.ipynb', 'repo_name': 'SiamakMushakhian/Numpy-Pandas-Seaborn', 'size': '7644'}