<jupyter_start><jupyter_text>Numpy Array Operations* Topicos:    * Operações Aritméticas    * Operações Ponto a ponto    * Metodos da Biblioteca (funções Universais)    * Produto Interno e Externo    * Matriz Inversa e Transposta<jupyter_code># Importar Library

import numpy as np

# Criar array

array1 = np.arange(1,10) # Array 1d de 9 elementos [1 - 9]
array2 = array2 = array1.reshape((3,3))

# Operações Aritmeticas (por escalar)

array3 = array2 + 2
print(array3)

array3 = array2 - 2
print(array3)

array3 = array2 * 2
print(array3)

array3 = array2 / 2
print(array3)

array3 = array2 ** 2
print(array3)
# Operações Aritmeticas (ponto a ponto)

array3 = array2 + array2
print(array3)

array3 = array2 - array2
print(array3)

array3 = array2 * array2
print(array3)

array3 = array2 / array2
print(array3)

array4 = 2*np.ones((3,3))

array3 = array2 ** array4
print(array3)

# Obs1: Operações "inválidas" (0 / 0), geram o valor "nan"
# Obs2: Divisão por 0, gera valor "inf"
# Metodos Nativos da Biblioteca (Funções Universais)

# Link: https://docs.scipy.org/doc/numpy/reference/ufuncs.html

array1 = np.arange(1,6)
print(array1)

print(array1.max(),np.max(array1))
print(array1.argmax())

print(array1.min(),np.min(array1))
print(array1.argmin())

print(np.sqrt(array1))
print(np.square(array1))

print(np.exp(array1))
print(np.log(array1))

print(np.mean(array1))
print(np.std(array1))

print(np.sin(array1))

# Metodos Nativos da Biblioteca (Funções Universais) 

x = np.arange(5,10)
print(x)

y = np.ones([5,5])*10
print(y)

z = np.repeat(x,3).reshape(5,3)
print(z)

# Funcoes
print(np.mod(y,x))            # Resto da divisao
print(np.minimum(x,7))        # Compara valor a valor com o 7
print(np.median(z))           # Mediana de todos os valores
print(np.median(z,axis=0))    # Mediana por coluna (das linhas)
print(np.median(z,axis=1))    # Mediana por linha (das colunas)
print(np.add.accumulate(x))   # soma acumulada
print(np.multiply.outer(x,x)) # Multiplicação matricial

# Inverse and Transpose

import numpy as np
from numpy.linalg import inv
from numpy.linalg import pinv

# Produto escalar:  np.inner() (vectors) / np.dot() (matrices)
# Produto vetorial: np.outer()

# Inner product of 1D arrays

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

print(A)

# Transpose of 1D vectors

A = np.array([[1,2,3]]) # create as a 2D object
B = A.T
C = A.transpose()

print(B)
print(C)

# Matrix Class

m = np.matrix([[2,3],[4,5]]) # create matrix object
print(m)

m_inv = m.I                  # calculate matrix inverse
print(m_inv)

print(np.matmul(m,m_inv))    # Identity Matrix

# Transpose and inverse Using Arrays (A = B' * inv(C))
B = np.random.randn(2,2)
C = np.random.randn(2,2)
A = np.dot(B.T, inv(C))
A = np.matmul(B.T, inv(C))

# Transpose and inverse using Matrices (A = B' * inv(C))
B = np.mat(B) # cast B from array object to matrix object
C = np.mat(C) # cast C from array object to matrix object
A = B.T*C.I<jupyter_output>32
[[1]
 [2]
 [3]]
[[1]
 [2]
 [3]]
[[2 3]
 [4 5]]
[[-2.5  1.5]
 [ 2.  -1. ]]
[[ 1.  0.]
 [ 0.  1.]]
{'fraction_non_alphanumeric': 0.15562805872756932, 'fraction_numerical': 0.04404567699836868, 'mean_word_length': 3.945161290322581, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '495471', 'license': 'MIT', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1295, 'n_words': 320, 'path': 'scripts_numpy_pandas/Numpy_03_operations.ipynb', 'repo_name': 'davidcoelho89/MLTool_python', 'size': '3065'}