<jupyter_start><jupyter_text>Tools - NumPyNumPy is a library for scientific computing with Python. NumPy is centered around a powerful N-dimensional array object, and it also contains useful linear algebra, Fourier transform, and random number functions.<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Creating Arrays<jupyter_code>#np.zeros

arr = np.zeros([5,3])
arr
#np.ones
arr = np.ones([5,3])
arr
#np.full - tuple + value

arr = np.full([2,2],5)
arr
#np.empty
arr = np.empty((2,4))
arr
# Creating an identity matrix

np.eye(3,3)
#np.array

np.array([2,3,4])
#np.arange - two args and three args

np.arange(100,1,-5)
#np.linspace

np.linspace(0,10,6)
#np.random.rand and np.random.randn

uniform = np.random.rand(1000) #random number between 0 and 1 - probability of getting same probability
normal = np.random.randn(100) #

print(uniform.mean())
print(normal.mean())<jupyter_output>0.4866308804392001
-0.036301012745919056<jupyter_text>Some vocabularyIn NumPy, each dimension is called an axis.The number of axes is called the rank.For example, the above 3x4 matrix is an array of rank 2 (it is 2-dimensional).The first axis has length 3, the second has length 4.An array's list of axis lengths is called the shape of the array.For example, the above matrix's shape is (3, 4).The rank is equal to the shape's length.The size of an array is the total number of elements, which is the product of all axis lengths (eg. 3*4=12)  Examining Properties<jupyter_code>#arr.shape

np.shape(arr)

#arr.ndim - equal to len(a.shape)

arr1 = np.full(5,5)
arr2 = np.full(5,9)
np.stack([arr1,arr2])

#arr.size 

np.size(arr)
##arr.dtype

arr.dtype<jupyter_output><empty_output><jupyter_text>Reshaping an Array<jupyter_code>#arr.shape

arr = np.arange(1,13)
#print(arr, np.size(arr))

#2X6, 3X4, 4X3

arr.shape = (3,2,2)

print(arr,np.size(arr))

arr2 = arr.reshape(3,4)

# arr1 = np.linspace(1,10,9)
# print(arr)
# np.size(arr)
#arr.reshape

np.size(arr.reshape([3,4]))
arr1.reshape(3,3)<jupyter_output><empty_output><jupyter_text>Arithmatic Operations<jupyter_code>a = np.array([14, 23, 32, 41])
b = np.array([5,  4,  3,  2])
print("a + b  =", a + b)
print("a - b  =", a - b)
print("a * b  =", a * b)
print("a / b  =", a / b)
print("a // b  =", a // b)
print("a % b  =", a % b)
print("a ** b =", a ** b)<jupyter_output>a + b  = [19 27 35 43]
a - b  = [ 9 19 29 39]
a * b  = [70 92 96 82]
a / b  = [ 2.8         5.75       10.66666667 20.5       ]
a // b  = [ 2  5 10 20]
a % b  = [4 3 2 1]
a ** b = [537824 279841  32768   1681]<jupyter_text>Broadcasting - broad + casting ![image.png](attachment:image.png)<jupyter_code>arr1 = np.full(3,5)
print('arr1', arr1,np.shape(arr1))
arr2 = np.full((2,3),1)
print('arr2',arr2,np.shape(arr2))

print(arr1+arr2)
print(arr2+2)<jupyter_output>[[3 3 3]
 [3 3 3]
 [3 3 3]]<jupyter_text>Logical Operators<jupyter_code>arr = np.array([3,25,27,89])
arr > 25

# arr > [1,2,45,3]

arr[arr>25]<jupyter_output><empty_output><jupyter_text>Mathematical and Statistical Functions<jupyter_code>arr.mean()

arr = np.random.rand(999)

arr.shape = (3,3,111)

arr.mean()
arr.add()
# arr.square()


# np.square(arr)<jupyter_output>[4, 5]
[[0.81712149 0.24494648 0.10128271]
 [0.25075327 0.86919395 0.51112573]
 [0.37507806 0.21781126 0.71421263]]<jupyter_text>Indexing<jupyter_code>lst = [1,2,3,4,5,6]
print(lst[3:5])

arr = np.random.rand(9)

arr.shape = (3,3)
print(arr)
arr[1:,:2]<jupyter_output>[4, 5]
[[0.60649918 0.54007136 0.05854507]
 [0.03212051 0.10741489 0.43647601]
 [0.66311842 0.0282556  0.39479993]]<jupyter_text>difference between regular python arrays and numpy arrays<jupyter_code>lst = [1,2,3,4,5,6]
new_lst = lst[3:5]
print(new_lst)
new_lst[0] = 22
print(new_lst)
print(lst)

arr = np.array([1,2,3,4,5,6])
new_arr = arr[3:5]
print(new_arr)
new_arr[0] = 22
print(new_arr)
print(arr)<jupyter_output>[4, 5]
[22, 5]
[1, 2, 3, 4, 5, 6]
[4 5]
[22  5]
[ 1  2  3 22  5  6]<jupyter_text>Linear Algebra<jupyter_code>#Dot product
import numpy.linalg as linalg

#Calculating inv

linalg.inv(arr)
#Finding the identity matrix - dot product of inv of the matrix with itself. How would you do it?<jupyter_output><empty_output><jupyter_text>Why numpy?<jupyter_code>def multiply_loops(A, B):
   c = np.zeros((A.shape[0], B.shape[1]))
   for i in range(A.shape[1]):
       for j in range(B.shape[0]):
            c[i,j] = A[i,j] * B[j,i]
   return c

def multiply_vector(A, B):
     return A @ B
    
X = np.random.random((100, 100))
Y = np.random.random((100, 100))

%timeit multiply_loops(X, Y)
%timeit multiply_vector(X, Y)
def func1(num1,num2):
    return num1+num2

def func2(num1,num2):
    print (num1+num2)
    
%timeit func1(2,3)
%timeit func2(2,3)<jupyter_output><empty_output><jupyter_text>Reading from files<jupyter_code>import numpy as np
import os

import pandas as pd
# defining housing.csv file path
HOUSING_PATH = './'
# reading the large housing.csv file using pandas
housing_raw = pd.read_csv(os.path.join(HOUSING_PATH, "housing.csv"))
# extracting only a few rows (5 rows) of data from the pandas dataframe 'my_df'
my_df = housing_raw.iloc[ : 5]
# creating a new small csv file - 'housing_short.csv' - containing the above extracted 5 rows of data
my_df.to_csv('housing_short.csv', index=False)
import numpy as np
import os

FILE = 'housing_short.csv'
def load_housing_data(file = FILE ):
    return np.loadtxt(file, dtype={'names': ('longitude','latitude','housing_median_age','total_rooms','total_bedrooms','population','households','median_income','median_house_value','ocean_proximity'),'formats': ('f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', '|S15')}, delimiter=',', skiprows=1, unpack=True)

longitude_arr,latitude_arr,housing_median_age_arr,total_rooms_arr,total_bedrooms_arr,population_arr,households_arr,median_income_arr,median_house_value_arr,ocean_proximity_arr = load_housing_data()

print(median_house_value_arr, type(median_house_value_arr))<jupyter_output>[452600. 358500. 352100. 341300. 342200.] <class 'numpy.ndarray'>
{'fraction_non_alphanumeric': 0.1566505743299484, 'fraction_numerical': 0.09106042949891793, 'mean_word_length': 5.062563067608476, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 50, 'https://': 0, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '29987', 'license': 'Apache-2.0', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2502, 'n_words': 574, 'path': '.ipynb_checkpoints/Numpy_Essentials-checkpoint.ipynb', 'repo_name': 'Atif0604/PythonForMachineLearning', 'size': '6007'}