<jupyter_start><jupyter_text>Agenda >What is Numpy and its Business Application.>Numpy vs List>Numpy Operations>Broadcasting>Numerical Data Analysis>Python Webframeworks : Django     Numpy - Introduction<jupyter_code>li = ['hello',23.0]<jupyter_output><empty_output><jupyter_text>What is NumPy? NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays.It is the fundamental package for scientific computing with Python. It contains various features including these important ones:>A powerful N-dimensional array object>Sophisticated (broadcasting) functions>Tools for integrating C/C++ and Fortran code>Useful linear algebra,Linear equation  and random number capabilities Scalar: A scalar is a real number    Vector : A vector is an object that has both a magnitude and a direction.Explanation<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Array Creation  One dimensional Array<jupyter_code>ar = np.array( [ 1, 2, 3] )
ar
ar.ndim<jupyter_output><empty_output><jupyter_text>Multi-dimensional Array<jupyter_code># basic array characteristics 
# Creating array object 
ar = np.array( [[ 1, 2, 3],[2, 3, 4]] )

print("Array is of type: ", type(ar)) 
print("No. of dimensions: ", ar.ndim) 
print("Shape of array: ", ar.shape)
print("item size stored in a memory: ", ar.itemsize)
print("Size of array: ", ar.size) 
print("Array stores elements of type: ", ar.dtype) 

a = np.array([1,2.3])
a.dtype
a<jupyter_output><empty_output><jupyter_text>NumPy Array v/s ListWhy NumPy is used in Python?We use python NumPy array instead of a list because of the below three reasons:1)Less Memory2)Fast3)Convenient<jupyter_code>list(range(1,10))
!pip install <pkg name>
#Memory occupency
import numpy as np
 
import time
import sys
S= range(1000)
print(sys.getsizeof(5)*len(S))
 
D= np.arange(1000)
print(D.size*D.itemsize)
[i for i in range(5)]
#Time taken for execution
import time
import sys
 
SIZE = 1000000
 
L1= range(SIZE)
L2= range(SIZE)
A1= np.arange(SIZE)
A2=np.arange(SIZE)
 
start= time.time()
result=[(x,y) for x,y in zip(L1,L2)]
print((time.time()-start)*1000)
 
start=time.time()
result= A1+A2
print((time.time()-start)*1000)<jupyter_output>308.02416801452637
81.00223541259766<jupyter_text>Exploring basic Numpy Functions<jupyter_code>np.random.random(5)
a = np.array([[1, 2, 4], [5, 8, 7]], dtype = 'float') 
print ("Array created using passed list:\n", a) 

b = np.array((1 , 3, 2)) 
print ("\nArray created using passed tuple:\n", b) 

c = np.zeros((3, 4)) 
print ("\nAn array initialized with all zeros:\n", c) 

e = np.random.random((2, 2)) 
print ("\nA random array:\n", e) 

f = np.arange(0, 30, 5) 
print ("\nA sequential array with steps of 5:\n", f) 

g = np.linspace(0, 5, 10) 
print ("\nA sequential array with 10 values between 0 and 5:\n", g) 


arr = np.array([[1, 2, 3, 4], [5, 2, 4, 2,6], [1, 2, 0, 1]]) 
# Reshaping 3X4 array to 2X2X3 array 
arr = np.array([[1, 2, 3, 4], [5, 2, 4, 2], [1, 2, 0, 1]]) 

newarr = arr.reshape(2, 2, 3) 

print ("\nOriginal array:\n", arr) 
print ("Reshaped array:\n", newarr) 

# Flatten array 
arr = np.array([[1, 2, 3], [4, 5, 6]]) 
flarr = arr.flatten() 
a = arr.ravel()
print("ravel",a)
print ("\nOriginal array:\n", arr) 
print ("Fattened array:\n", flarr) 

a2 = np.array([1,2,34])
a2[0:1]
li = [0,1,2,3,4,5]
li[2:]
# indexing in numpy 
import numpy as np 


arr = np.array([[-1, 2, 0, 4], 
[4, -0.5, 6, 0], 
[2.6, 0, 7, 8], 
[3, -7, 4, 2.0]]) 

print(arr[0][0])

temp = arr[:2, ::3] 
print ("Array with first 2 rows and alternatecolumns(0 and 2):\n", temp) 
 
cond = arr > 0 
temp = arr[cond] 
print ("\nElements greater than 0:\n", temp)<jupyter_output>-1.0
Array with first 2 rows and alternatecolumns(0 and 2):
 [[-1.  4.]
 [ 4.  0.]]

Elements greater than 0:
 [2.  4.  4.  6.  2.6 7.  8.  3.  4.  2. ]<jupyter_text>Basic Operations<jupyter_code>[1,2,3] +1
# basic arthematic operations 
import numpy as np 

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

print ("Adding 1 to every element:", a+1) 
print ("Subtracting 3 from each element:", a-3) 
print ("Multiplying each element by 10:", a*10) 
print ("Squaring each element:", a**2) 

a *= 2
print ("Doubled each element of original array:", a) 

 
a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]]) 

print ("\nOriginal array:\n", a) 
print ("Transpose of array:\n", a.transpose()) 
a.T<jupyter_output>Adding 1 to every element: [2 3 6 4]
Subtracting 3 from each element: [-2 -1  2  0]
Multiplying each element by 10: [10 20 50 30]
Squaring each element: [ 1  4 25  9]
Doubled each element of original array: [ 2  4 10  6]

Original array:
 [[1 2 3]
 [3 4 5]
 [9 6 0]]
Transpose of array:
 [[1 3 9]
 [2 4 6]
 [3 5 0]]<jupyter_text>* axis = 0 columnwise vectors* axis = 1 rowwise vectors<jupyter_code># unary operators in numpy 
import numpy as np 
  
arr = np.array([[1, 5, 6], 
                [4, 7, 2], 
                [3, 1, 9]]) 
  
print ("Largest element is:", arr.max()) 
print ("Row-wise maximum elements:", arr.max(axis = 1)) 
print ("Column-wise minimum elements:", arr.min(axis = 0)) 
print ("Sum of all array elements:", arr.sum(axis = 0)) 
print ("Cumulative sum along each row:\n", arr.cumsum(axis = 1))
# binary operators in Numpy 
a = np.array([[1, 2], [3, 4]]) 
b = np.array([[4, 3], [2, 1]]) 

print ("Array sum:\n", a + b) 
print ("Array multiplication:\n", a*b) 
print ("Matrix multiplication:\n", a.dot(b)) 
 
# unfunctions in numpy 
a = np.array([0, np.pi/2, np.pi]) 
print ("Sine values of array elements:", np.sin(a)) 

a = np.array([0, 1, 2, 3]) 
print ("Exponent of array elements:", np.exp(a)) 
print ("Square root of array elements:", np.sqrt(a))<jupyter_output>Sine values of array elements: [0.0000000e+00 1.0000000e+00 1.2246468e-16]
Exponent of array elements: [ 1.          2.71828183  7.3890561  20.08553692]
Square root of array elements: [0.         1.         1.41421356 1.73205081]<jupyter_text>sorting<jupyter_code># sorting in numpy 


a = np.array([[1, 4, 2], [3, 4, 6], [0, -1, 5]]) 

print ("Array elements in sorted order:\n", np.sort(a, axis = None)) 
print ("Row-wise sorted array:\n", np.sort(a, axis = 0)) 
print ("Column wise sort by applying merge-sort:\n", np.sort(a, axis = 0, kind = 'mergesort'))<jupyter_output>Array elements in sorted order:
 [-1  0  1  2  3  4  4  5  6]
Row-wise sorted array:
 [[ 0 -1  2]
 [ 1  4  5]
 [ 3  4  6]]
Column wise sort by applying merge-sort:
 [[ 0 -1  2]
 [ 1  4  5]
 [ 3  4  6]]<jupyter_text>structured array<jupyter_code>dtypes = [('name', 'S10'), ('grad_year', int), ('cgpa', float)] 

values = [('Hrithik', 2009, 8.5), ('Ajay', 2008, 8.7), ('Pankaj', 2008, 7.9), ('Aakash', 2009, 9.0)] 

# Creating array 
arr = np.array(values, dtype = dtypes) 
print ("\nArray sorted by names:\n",np.sort(arr, order = 'name')) 
print ("Array sorted by grauation year and then cgpa:\n", np.sort(arr, order = ['grad_year', 'cgpa'])) 

x = np.array([('a', 0.8), ('b', 0.2)], dtype=[('label', 'a8'), ('prob', 'f8')])
x['label']<jupyter_output><empty_output><jupyter_text>Stacking & Splitting Stacking: Several arrays can be stacked together along different axes.    np.vstack: To stack arrays along vertical axis.    np.hstack: To stack arrays along horizontal axis.    np.column_stack: To stack 1-D arrays as columns into 2-D arrays.    np.concatenate: To stack arrays along specified axis (axis is passed as argument).<jupyter_code>a = np.array([[1, 2], [3, 4]]) 

b = np.array([[5, 6],[7, 8]]) 

print("Vertical stacking:\n", np.vstack((a, b))) 
print("\nHorizontal stacking:\n", np.hstack((a, b))) 

c = [5, 6] 
print("\nColumn stacking:\n", np.column_stack((a, c))) 
print("\nConcatenating to 2nd axis:\n", np.concatenate((a, b),axis = 1)) 

a = np.array([[1, 3, 5, 7, 9, 11], [2, 4, 6, 8, 10, 12]]) 
print("Splitting along horizontal axis into 2 parts:\n", np.hsplit(a, 2)) 
print("\nSplitting along vertical axis into 2 parts:\n", np.vsplit(a, 2))<jupyter_output>Splitting along horizontal axis into 2 parts:
 [array([[1, 3, 5],
       [2, 4, 6]]), array([[ 7,  9, 11],
       [ 8, 10, 12]])]

Splitting along vertical axis into 2 parts:
 [array([[ 1,  3,  5,  7,  9, 11]]), array([[ 2,  4,  6,  8, 10, 12]])]<jupyter_text>Broadcasting The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. Taken from the Numpy documentation - When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when* they are equal, or* one of them is 1<jupyter_code>a = np.array([[1,2,3,4],[2,3,4,5]])
a.shape<jupyter_output><empty_output><jupyter_text>Examples<jupyter_code>np.eye(3)
## Match
A(4-D array): 7 x 1 x 6 x 1
B(3-D array):     3 x 1 x 5
Result      : 7 x 3 x 6 x 5

## Miss Match
A: 4 x 3
B:     4
### Match
import numpy as np 

a = np.array([1.0, 2.0, 3.0]) 


b = 2.0
print(a * b) 


c = [2.0, 2.0, 2.0] 
print(a * c)

## Miss match
a = np.array([[1, 2,2], [3, 4,3],[5,6,4],[6,8,5]])
a.shape
b = np.array([1,2,3,4])
b.shape
a*b<jupyter_output><empty_output><jupyter_text>Working with datetime:     Numpy has core array data types which natively support datetime functionality.     The data type is called “datetime64”, so named because “datetime” is already taken    by the datetime library included in Python.<jupyter_code># creating a date 
today = np.datetime64('2017-02-12') 
print("Date is:", today) 
print("Year is:", np.datetime64(today, 'Y'))
print("Year is:", np.datetime64(today, 'M'))
print("Year is:", np.datetime64(today, 'D'))
np.datetime64('2005') == np.datetime64('2005-01-01')<jupyter_output><empty_output><jupyter_text>Linear Algebra Linear algebra in NumPy:     The Linear Algebra module of NumPy offers various methods to apply linear algebra on any numpy array.    >*You can explore:>*rank, determinant, trace, etc. of an array.>*matrix and vector products (dot, inner, outer,etc. product), matrix exponentiation>*solve linear equations and much more!<jupyter_code>A = np.array([[6, 1, 1], [4, -2, 5], [2, 8, 7]]) 

print("Rank of A:", np.linalg.matrix_rank(A)) 

print("\nTrace of A:", np.trace(A)) 

print("\nDeterminant of A:", np.linalg.det(A)) 

print("\nInverse of A:\n", np.linalg.inv(A)) 

print("\nMatrix A raised to power 3:\n", np.linalg.matrix_power(A, 3))<jupyter_output>Rank of A: 3

Trace of A: 11

Determinant of A: -306.0

Inverse of A:
 [[ 0.17647059 -0.00326797 -0.02287582]
 [ 0.05882353 -0.13071895  0.08496732]
 [-0.11764706  0.1503268   0.05228758]]

Matrix A raised to power 3:
 [[336 162 228]
 [406 162 469]
 [698 702 905]]<jupyter_text>Solving Linear Equations >x + 2*y = 8>3*x + 4*y = 18<jupyter_code># coefficients 
a = np.array([[1, 2], [3, 4]]) 
# constants 
b = np.array([8, 18]) 

print("Solution of linear equations:", np.linalg.solve(a, b))<jupyter_output>Solution of linear equations: [2. 3.]<jupyter_text>Integration<jupyter_code>from scipy import special
from scipy import integrate 
i = integrate.quad(lambda x:special.exp10(x),0,1 )
i<jupyter_output><empty_output><jupyter_text>C++ extensions<jupyter_code>import numpy as N
from numpyctypes import c_ndarray<jupyter_output><empty_output><jupyter_text>Statistics<jupyter_code>stats = np.array([[1,2,3],[4,5,6]])
stats
print(np.min(stats))
print(np.max(stats, axis=1))
print(np.sum(stats, axis=0))<jupyter_output><empty_output><jupyter_text>Can we Save Numpy Array?<jupyter_code>arr = np.array([1,2,3,4])
np.save('test',arr)
#load an array
np.load('test.npy')
np.savetxt('hello.csv',arr,delimiter = ',')

######## saves file in file format #####
arr = open('hello.csv','r')
for i in arr:
    print(i)
arr.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1660116199589884, 'fraction_numerical': 0.06672932330827068, 'mean_word_length': 4.32287403365166, 'pattern_counts': {'":': 0, '<': 74, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '420661', 'license': 'Apache-2.0', 'n_tokens_mistral': 4997, 'n_tokens_neox': 4442, 'n_words': 1396, 'path': 'Numpy_Inclass_Notebook.ipynb', 'repo_name': 'sajid-a/PythonDemo', 'size': '11704'}