<jupyter_start><jupyter_text>Agenda1. Numpy2. Array3. Working with NumPy4. Difference between NumPy and Array5. NumPy Array6. How to Create NumPy Array7. Operation on NumPy: Slicing8. Join in an Array  NumPy+ An acronym for **"Numerical Python"**+ Python library for scientific computing application+ fast mathematical computation on array and matrices+ ndarray (n-dimensional array) objects+ [Numpy Reference](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf)  Array+ Container that hold fixed number of items+ Items have same data type+ Contiguous (one after the other) location holding the elements  An array consists of:+ **Element**: Each item stored in an array is called an element.+ **Index**: Each location of an element in an array has a numerical index, which is used to identify the element. Array Representation![array](https://media.geeksforgeeks.org/wp-content/uploads/C-Arrays.jpg)  Anatomy of an Array![a-array](https://i.imgur.com/mg8O3kd.png) Terms1. **Axes**:  ***axis = 0*** ( row-wise) and ***axis = 1*** (column-wise)2. **Rank**: Number of axes it possesses.> **Rank = Number of axes**3. **Shape**: Number of elements it contains along each of its axis.  Array Representation in a language (C++)Arrays can be declared in various ways in different languages. Given below is an illustration:![array-1](https://www.tutorialspoint.com/data_structures_algorithms/images/array_declaration.jpg)  ![array-2](https://www.tutorialspoint.com/data_structures_algorithms/images/array_representation.jpg)   Working with NumPy1. Install the numpy on your PC```pythonpip install numpy```2. Import Numpy```pythonimport numpy as np```3. Now, you are ready to use NumPy  Numpy Arrays1. **One-dimesional (1D) Array**: An array of dimension one is called one-dimesional array. It is also known as **Vector**.![1D](https://miro.medium.com/max/3000/1*qlRqv3c89DK7lw6pihnPhw.jpeg)2. **Multidimensional arrays or ndarrays**: An array of n-dimension is called ndarray. It is also known as **Matrices**![ND](https://media.geeksforgeeks.org/wp-content/uploads/two-d.png)  Difference between NumPy Array and List| NumPy Array                                                     | List                                                            || --------------------------------------------------------------- | --------------------------------------------------------------- || NumPy array works on homogeneous (same) types.                  | Python lists are made up of heterogeneous (different) types     || NumPy array does not support addition and removal of elements   | Python lists supports adding and removal of elements            || Can't contain elements of different types                       | Can contain elements of different types                         || Less memory consumption                                         | More memory consumption                                         || Faster runtime execution                                        | Runtime execution is comparatively slower than arrays           |   How to CREATE A NumPy ARRAYThere are several ways of creating a NumPy array.+  **np.array()**: To create 1D/2D NumPy array+  **np.fromstring()**: To create 1D array from string3. **np.empty()**: An empty array or uninitialized array4. **np.zeros()**: New array with all elements as zeroes5. **np.ones()**: New array with all ones as its elements6. **np.arange()**: Used to create array from a range7. **np.linspace()**: To create array of range8. **np.copy()**: Used to create a copy of an existing array9. **np.reshape()**: We can create 2D array from 1D array10. **np.eye() or np.identity()**: All diagonals value will be one  array()<jupyter_code>#To create a one-dimensional array

import numpy as np

l1 = [2,3,4,5,6]
arr1 = np.array(l1)

print(arr1)
#To create a two-dimesional array
import numpy as np

A = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(A)<jupyter_output>[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]<jupyter_text>fromstring()<jupyter_code>import numpy as np

data = np.fromstring('1 2 3 4 10 12 13', dtype = int, sep = " ")
print(data)<jupyter_output>[ 1  2  3  4 10 12 13]<jupyter_text>empty()<jupyter_code>import numpy as np

arr = np.empty( [3,2], dtype = int, order = 'F')  # default order: C
print(arr)<jupyter_output>[[0 0]
 [0 0]
 [0 0]]<jupyter_text>zeros()<jupyter_code># array of five zeros. Default type is float

import numpy as np

arr1 = np.zeros(5)
print(arr1)<jupyter_output>[0. 0. 0. 0. 0.]<jupyter_text>ones()<jupyter_code># array of five ones. Default dtype is float

import numpy as np

arr1 = np.ones(5)
print(arr1)<jupyter_output>[1. 1. 1. 1. 1.]<jupyter_text>arange()![Image](https://miro.medium.com/max/679/1*16lfH-A_kiDmyxe1Sm2dQA.png)<jupyter_code>import numpy as np

a1 = np.arange(1,13)
print(a1)<jupyter_output>[ 1  2  3  4  5  6  7  8  9 10 11 12]<jupyter_text>linspace()<jupyter_code>import numpy as np

line = np.linspace(1,10,10)
print(line)<jupyter_output>[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]<jupyter_text>copy()<jupyter_code>import numpy as np
x = np.array([1,2,3])
y = x
z = np.copy(x)
x[0] = 10

print(x)
print(y)
print(z)<jupyter_output>[10  2  3]
[10  2  3]
[1 2 3]<jupyter_text>reshape()Reshaping means changing the arrangement of items so that the shape of the array changes while maintaining the same number of dimensions.1D![Image](https://miro.medium.com/max/679/1*16lfH-A_kiDmyxe1Sm2dQA.png)Reshaped into (2D)![reshape](https://miro.medium.com/max/251/1*b0Eh1M1w4d9gKIhN8Fdjcg.png)<jupyter_code>import numpy as np

a1 = np.arange(1,13)
reshape = a1.reshape(3,4)

print("1D: array\n", a1)
print("\nAfter Reshaped (2D): \n", reshape)
reshape = a1.reshape(3,4, order = 'F')
reshape
# Creating of 2D array from 1D array using reshape() function

import numpy as np

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

print("-------------")

B = np.reshape(A, (2,3)) #reshape shall convert 1D array into 2D array with 2 rows and 3 columns
print(B)<jupyter_output>[1 2 3 4 5 6]
-------------
[[1 2 3]
 [4 5 6]]<jupyter_text>eye() / identity()<jupyter_code># create a 4x4 identity matrix using the eye method
import numpy as np

# 4x4 matrix wil 1's as all diagonal elements
mat1 = np.identity(4)
print("\nMatrix 1: \n", mat1)
#Alternatively

mat2 = np.eye(4, dtype = float)
print("\nMatrix 2: \n", mat2)<jupyter_output>Matrix 2: 
 [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]<jupyter_text>Operations ON NumPy  Array Slicing+ Slicing means sub-sequence of the structure can be indexed and retrieved.+ Slicing is specified by using the colon operator `:` with a `from` and `to`.+ **data[from:to]** Slicing in 1D Array<jupyter_code># Slicing in 1D array

import numpy as np
data = np.array([5,2,7,3,9])

print(data[:])   #Shall extract slice from start to end
print(data[1:3])  #Shall extract slice from 1st position to 4th, excluding the 4th position
print(data[:2])
print(data[-2:])<jupyter_output>[3 9]<jupyter_text>Slicing in 2D array![slice-2d](https://learning.oreilly.com/api/v2/epubs/urn:orm:book:9781449323592/files/httpatomoreillycomsourceoreillyimages2172114.png)<jupyter_code># Slicing in 2D array

import numpy as np
arr = np.array([[1,2,3], [4,5,6], [7,8,9]])
print(arr)
print(arr[:2, 1:])
print(arr[2])
print(arr[2, :])
print(arr[2:, :])
print(arr[:, :2])
print(arr[1, :2])
print(arr[1:2, :2])
# Another example of Slicing

import numpy as np

a = np.array([[1,2,3,4],[5,6,7,8], [9,10,11,12]])
print(a)
b = a[1:2, 0:4]
print(b)
b = a[1:2,1:2]
print(b)
b = a[:2, 0:2]
print(b)
b = a[:2, 1:3]
print(b)
b = a[1:3, 2:4]
print(b)<jupyter_output>[[ 7  8]
 [11 12]]<jupyter_text>Joins in ArraysJoining of two arrays in NumPy is done using **concatenate()** function.This function is used to join two or more arrays of the same shape.**Note**In case of a 2D array, this function concatenates two arrays either by rows or by columns.<jupyter_code>#Concatenating two 1D arrays

import numpy as np

a = np.array([1,2,3])
b = np.array([5,6])

c = np.concatenate([a,b,a])
print(c)
#Concatenating two 2D arrays using single array

import numpy as np

A = np.array([[7,5], [1,6]])

## concatenate along the first axis
print("\nAlong First Axis")
print(np.concatenate([A,A]))

## concatenate along the second axis (zero-indexed)
print("\nAlong Second Axis")
print(np.concatenate([A,A], axis = 1))

x = np.array([1,2])

# vertically stack the arrays
print("Vertical Stack")
print(np.vstack([x, A]))
      
# horizontally stack the arrays
print("\nHorizontal Stack")
y = np.array([[99], [99]])
print(np.hstack([A, y]))<jupyter_output>Vertical Stack
[[1 2]
 [7 5]
 [1 6]]

Horizontal Stack
[[ 7  5 99]
 [ 1  6 99]]<jupyter_text>Vertical Stack![vstack](https://www.tutorialsandyou.com/images/1-Dimensional-vertical-stacking.jpg)<jupyter_code>a = np.array([[1,2],[2,4]])
b = np.array([[7,8]])

c = np.concatenate((a,b), axis = 0)
print(c)
np.vstack([a,b])<jupyter_output><empty_output><jupyter_text>Horizontal Stack![hstack](https://www.tutorialsandyou.com/images/1-Dimensional-horizontal-stacking.jpg)<jupyter_code>a = np.array([[1,2],[2,4]])
b = np.array([[7,8]])

c = np.concatenate((a,b.T), axis = 1)
print(c)
np.hstack([a,b.T])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16018679409209383, 'fraction_numerical': 0.04669852302345786, 'mean_word_length': 3.7764522821576763, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 49, 'https://': 13, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '489660', 'license': 'MIT', 'n_tokens_mistral': 3505, 'n_tokens_neox': 3263, 'n_words': 983, 'path': 'NumPy/01.ipynb', 'repo_name': 'Shiv-Learning-Cente/Information-Practices-XII', 'size': '9208'}