<jupyter_start><jupyter_text>NUMPY  CONTENT 1)Introduction 2)Basics 3)Array Creation 4)Basic Operations 5)Indexing, Slicing and Iterating  1)Introduction **Numpy** is a fundamental python library for scientific computing with Python. It contains:* a powerful N-dimensional array object* sophisticated (broadcasting) functions* tools for integrating C/C++ and Fortran code* useful linear algebra, Fourier transform, and random number capabilitiesFor more detailed information and documentation, you can visit https://numpy.org/.  2)Basics Numpy's main object is the multidimensional array. It is a table of elements which are usually numbers.<jupyter_code>#As explained previously, in order to use a library and its functions, we must first import it
import numpy as np<jupyter_output><empty_output><jupyter_text>Here np is called an alias, we use it because whenever we use numpy we have to write **numpy.** which is long. To shorter it we import it as **np** and when we use it we can write **np.** instead of **numpy.** Numpy's array has very useful attributes. Some of them are:* **ndarray.ndim**: it shows the number of axes (dimensions) of the array* **ndarray.shape**: the shape of the array* **ndarray.size**: the total number of elements of the array.* **ndarray.dtype**: an object describing the type of the elements in the array.Now let's take a look at application of these attributes<jupyter_code>#We already imported numpy

arr = np.arange(15).reshape(3, 5) #first we create an array with 15 elements. Then we reshape it into 3 by 5.
print(arr)
#we can always type() method to check the type of something
print(type(arr))
#Lets see the attributes of it

print(arr.ndim) #number of dimensions
print(arr.shape) #shape of the array
print(arr.size) #number of total elements
print(arr.dtype) #type of the elemets in the array<jupyter_output>2
(3, 5)
15
int32<jupyter_text>3)Array Creation You can create an **array** from a regular Python list or tuple using the array function. The type of the resulting array is deduced from the type of the elements in the sequences.<jupyter_code>a = np.array([2,3,4])
b = np.array([1.2, 3.5, 5.1])
print(a.dtype, b.dtype)<jupyter_output>int32 float64<jupyter_text>Below, you can see examples of array creations, with different dimensions.<jupyter_code>#0D arrays

arr = np.array(42)

print(arr)
#1D arrays

arr = np.array([1, 2, 3, 4, 5])

print(arr)
#2D arrays

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr)
#3D arrays

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(arr)
#number of dimensions
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)<jupyter_output>0
1
2
3<jupyter_text>4)Basic Operations Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.<jupyter_code>a = np.array( [20,30,40,50] )
b = np.arange( 4 )
print((a*b))<jupyter_output>[  0  30  80 150]<jupyter_text>5)Indexing, Slicing and Iterating One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences.For more information about indexing, slicing and iterating, you can refer to the Introduction to python jupyter notebook. Slicing in python means taking elements from one given index to another given index.We pass slice instead of index like this: [start:end].We can also define the step, like this: [start:end:step].If we don't pass start its considered 0If we don't pass end its considered length of array in that dimensionIf we don't pass step its considered 1<jupyter_code>#access 1D array

arr = np.array([1, 2, 3, 4])

print(arr[0]) 
a = np.arange(10)**3
a[2:5]
#acces to 2D array
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('2nd element on 1st dim: ', arr[0, 1])
#as with previous example, you can slice 3-dimensional arrays as well. In this example, we slice a 3-dimensional array into a cuboid shape
#We can slice this array in all three dimensions
arr = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
               [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
               [[19, 20, 21], [22, 23, 24], [25, 26, 27]]])

print(arr[:2,1:,:2])  # [[ [4 5] [7 8] ]
                     #  [ [13 14] [16 17] ]]<jupyter_output>[[[ 4  5]
  [ 7  8]]

 [[13 14]
  [16 17]]]<jupyter_text>In the example above, we selected:→planes: the first two planes with ":2"→rows: the last two rows with "1:"→columns: the first two columns ":2" ![image.png](attachment:image.png)<jupyter_code>arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5]) #with both start and end
arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[4:]) #without an end
arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[:4]) #without start
#if we use minus sign, the indexing begins from the last element
arr_minus = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr_minus[-1]) 
print(arr_minus[-3:-1])<jupyter_output>7
[5 6]<jupyter_text>Numpy has also builtin functions that can be beneficial. For example NUmpy has a set of functions called **aggregate functions**. **Aggregate functions of numpy**- np.sum(a)       Sum of the elements- np.prod(a)      Multiplication of the elements- np.mean(a)      Mean value- np.std(a)       standart deviation- np.var(a)       Variance- np.min(a)       Minimum Value- np.max(a)       Maximum Value- np.argmin(a)    Index of Minimum Value- np.argmax(a)    Index of Maximum Value- np.median(a)    Median Value<jupyter_code>arr_agg=np.arange(15)

print(arr_agg)
#here we created an array with 15 elements. We are going to use the aggregate functions on thi array. You can see them below.
print("Sum of the numbers is:"+str(np.sum(arr_agg)))
print("Prodcut of the numbers is:"+str(np.prod(arr_agg)))
print("Mean of the numbers is:"+str(np.mean(arr_agg)))
print("Standart deviation of the numbers is:"+str(np.std(arr_agg)))
print("Variance of the numbers is:"+str(np.var(arr_agg)))
print("Minimum of the numbers is:"+str(np.min(arr_agg)))
print("Maximum of the numbers is:"+str(np.max(arr_agg)))
print("The index of the minimum number is:"+str(np.argmin(arr_agg)))
print("The index of the maximum number is:"+str(np.argmax(arr_agg)))
print("Median of the numbers is:"+str(np.median(arr_agg)))<jupyter_output>Sum of the numbers is:105
Prodcut of the numbers is:0
Mean of the numbers is:7.0
Standart deviation of the numbers is:4.320493798938574
Variance of the numbers is:18.666666666666668
Minimum of the numbers is:0
Maximum of the numbers is:14
The index of the minimum number is:0
The index of the maximum number is:14
Median of the numbers is:7.0
{'fraction_non_alphanumeric': 0.12910515215426333, 'fraction_numerical': 0.04639951792708647, 'mean_word_length': 4.650212765957447, 'pattern_counts': {'":': 2, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '347938', 'license': 'MIT', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2233, 'n_words': 890, 'path': 'Requirements/numpy_notebook.ipynb', 'repo_name': 'YasamanMoradifard/Machine_Learning_1_EX', 'size': '6638'}