<jupyter_start><jupyter_text>Network Mining Lab 1: Norms Notebook Author: <NAME>The Norms are qualitative measures of an object's size. In this lab you could see examples of how to implement diverse vector norms using python and the numpy library.<jupyter_code>import random
random.seed(1)<jupyter_output><empty_output><jupyter_text>Generating random vectors<jupyter_code>n = 3 # vector size
µ = 2 # mean
σ = 2 # std deviation

Vector_A = [random.random() for i in range(n)]
Vector_A
Vector_B = [random.randint(-100,100) for i in range(n)]
Vector_B
Vector_C = [random.gauss(µ,σ) for i in range(n)]
Vector_C<jupyter_output><empty_output><jupyter_text>Generating random matrices<jupyter_code>r = 3
c = 3
a = 1
b = 10
µ = 3 # mean
σ = 2 # std deviation

Matrix_A = [[random.random() for i in range(r)] for j in range(c)]
Matrix_A
Matrix_B = [[random.randint(a,b) for i in range(r)] for j in range(c)]
Matrix_B
Matrix_C = [[random.gauss (µ,σ) for i in range(r)] for j in range(c)]
Matrix_C<jupyter_output><empty_output><jupyter_text>Infinite Norm<jupyter_code>def inf_norm(x):
    a_abs = [abs(num) for num in x]
    return max(a_abs)

print(inf_norm(Vector_A))<jupyter_output>0.8474337369372327<jupyter_text>Manhattan Norm<jupyter_code>def manhattan_norm(x):
    tmp = 0
    for elem in x:
        tmp = tmp + abs(elem)
    return tmp

print(manhattan_norm(Vector_A))<jupyter_output>1.745572600026248<jupyter_text>Euclidean Norm<jupyter_code>def euclidean_norm(x):
    tmp = 0
    for elem in x:
        tmp = tmp + abs(elem)** 2
    return tmp ** (1.0 / 2)

print(euclidean_norm(Vector_A))<jupyter_output>1.1487163954554545<jupyter_text>P-Norm<jupyter_code>import math
def pnorm(x, p=2):
    if math.isinf(p):
        x_abs = [abs(elem) for elem in x]
        return max(x_abs)
    else:
        tmp = 0
        for elem in x:
            tmp = tmp + abs(elem)** p
    return tmp ** (1.0 / p)

print(pnorm(Vector_A))<jupyter_output>1.1487163954554545<jupyter_text>Dot Product<jupyter_code>import math
def inner_prod(X,Y):
    sum = 0
    for i in range(len(X)):
        sum += X[i]*Y[i]
    return sum

print(inner_prod(Vector_A, Vector_B))<jupyter_output>-44.16497003614836<jupyter_text>Dot Product v2<jupyter_code>def inner_prodt(X,Y):
    dotproduct = 0
    for i,j in zip(X,Y):
        dotproduct += i*j
    return dotproduct

print(inner_prodt(Vector_A, Vector_B))<jupyter_output>-44.16497003614836<jupyter_text>Matrix-Vector Multiplication<jupyter_code>def MatVec(A,v): 
    return [inner_prodt(r,v) for r in A ]

print(MatVec(Matrix_A, Vector_A))<jupyter_output>[1.1203035635896992, 1.2631926863922587, 1.1667880085733444]<jupyter_text>Matrix-Matrix Multiplication<jupyter_code>def matrixmult (A, B): 
    rows_A = len(A); cols_A = len(A[0]); 
    rows_B = len(B); cols_B = len(B[0]) 
    if cols_A != rows_B: 
        print("Cannot multiply the two matrices. Incorrect dimensions.") 
        return 
    C = [[0 for row in range(cols_B)] for col in range(rows_A)] 
    for i in range(rows_A): 
        for j in range(cols_B): 
            for k in range(cols_A): 
                C[i][j] += A[i][k] * B[k][j] 
    return C

print(matrixmult(Matrix_A, Matrix_B))<jupyter_output>[[2.2588391626270643, 6.549118630850459, 10.0869452676274], [2.6778664884869263, 9.282615674238627, 10.295188125690693], [1.9256409229123066, 6.537883774749169, 9.847381603430826]]<jupyter_text>Using numpy<jupyter_code>import numpy as np 
np.random.seed(1)<jupyter_output><empty_output><jupyter_text>Generating  random vectors<jupyter_code>l = 1
h = 100
n = 3

vector_A = np.random.rand(n) # Random vector of size n
vector_A
vector_B = np.random.randn(n) # Return samples from the “standard normal” distribution
vector_B
vector_C = np.random.randint(l, h, n) # Random vector of size n with values in [l, h]
vector_C<jupyter_output><empty_output><jupyter_text>Generating  random matrices<jupyter_code>l = 1
h = 100
r = 3
c = 3

matrix_A = np.random.rand(r, c) 
matrix_A
matrix_B = np.random.randn(r, c) 
matrix_B
matrix_C = np.random.randint(l, h, (r,c))
matrix_C<jupyter_output><empty_output><jupyter_text>Norms in numpy<jupyter_code># Infinite Norm
L_inf = np.linalg.norm(vector_A, np.inf) 
L_inf
# Manhattan Norm
L_1 = np.linalg.norm(vector_A, 1) 
L_1
# Euclidean Norm
L_2 = np.linalg.norm(vector_A, 2) 
L_2
# P-Norm
p = 5
L_p = np.linalg.norm(vector_A, p) 
L_p
Fro_norm = np.linalg.norm(matrix_A, ord='fro') # Frobenius Norm (for matrix)
Fro_norm<jupyter_output><empty_output><jupyter_text>Matrix and Vector Operations in numpy<jupyter_code>Dotproduct = np.dot(vector_A, vector_B)
Dotproduct
Matvect = np.dot(matrix_A, vector_A) 
Matvect
Matmult = np.dot(matrix_A, matrix_B) 
Matmult
Matmult2 = np.matmul(matrix_A, matrix_B)
Matmult2<jupyter_output><empty_output><jupyter_text>Comparing our code execution time against numpy  Matrix Generation<jupyter_code>%%timeit -n 1 -r 1

# Our matrix generator
Matrix_D = [[random.random() for i in range(1000)] for j in range(1000)]
%%timeit -n 1 -r 1

# Numpy matrix generator
matrix_D = np.random.rand(1000, 1000)<jupyter_output>45.8 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)<jupyter_text>Matrix Product<jupyter_code>matrix_D = np.random.rand(100, 100)
matrix_E = np.random.rand(100, 100)
%%timeit -n 1 -r 1

# Our matrix multiplication
matrixmult(matrix_D, matrix_E)
%%timeit -n 1 -r 1

# numpy matrix multiplication
np.matmul(matrix_D, matrix_E)<jupyter_output>24.7 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)
{'fraction_non_alphanumeric': 0.12040928192947195, 'fraction_numerical': 0.07308605883427736, 'mean_word_length': 4.737945492662474, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '265733', 'license': 'MIT', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2187, 'n_words': 512, 'path': 'NetworkMining _Lab_01_Norms.ipynb', 'repo_name': 'MarioTheOne/Network_Mining_Labs', 'size': '5478'}