Created
August 24, 2020 10:40
-
-
Save santiagosilas/4df70b101864f36add249179963245df to your computer and use it in GitHub Desktop.
Numpy Simple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| # Definindo Arrays | |
| a = np.array([1., .5, 2.5]) | |
| a = np.array([1., .5, 2.5], dtype = float) | |
| m = np.array([[1,2,3], [4,5,6], [7,8,9],[7,8,9],[7,8,9],[7,8,9],[7,8,9]]) | |
| a.shape #dimensões do array | |
| # número de dimensões do array | |
| a.ndim | |
| # Número de elementos no array | |
| a.size | |
| # Criar um array de zeros | |
| np.zeros((3,4)) | |
| # Criar um array de 1's | |
| np.ones((3,4)) | |
| # semelhante ao range | |
| np.arange(5, 50, 5) | |
| # Um array com 100 valores linearmente espaçados entre 0 e 10 | |
| x = np.linspace(0, 10, 100) | |
| # Um array com valores constantes | |
| np.full( (2, 2), 42) | |
| # Array Matriz Identidade | |
| np.eye(3) | |
| # Matriz de valores aleatórios entre 0 e 1 | |
| m = np.random.random((5,5)) | |
| # valores inteiros aleatórios | |
| np.random.randint(low = 10, high=14, size = 10) | |
| c = np.dot(a, b) # scalar product | |
| a = a.t # transpose a | |
| a = a.transpose(a) # transpose a | |
| # comparação | |
| a < b | |
| # Converte o array para outro tipo de dados | |
| a.astype(np.float32) | |
| # Funções agregadoras | |
| # soma | |
| a.sum() | |
| # média | |
| a.mean() | |
| # menor valor | |
| a.min() | |
| # mediana | |
| np.median(a) | |
| # Coeficientes de correlação | |
| np.corrcoef(a) | |
| # desvio padrão | |
| np.std(a) | |
| # Randomizar uma matriz m (com numpy.random) | |
| np.random.shuffle(m) | |
| #Slicing | |
| a[:, :] | |
| # todas as linhas, colunas 0 e 1 | |
| a[:, 0:2] | |
| a[:2] # linhas 0 e 1 | |
| # todas as linhas, última coluna | |
| a[:, -1] | |
| # stack arrays (with same dimensions) horizontally | |
| c = np.hstack( (a,b) ) | |
| np.concatenate( (a, b), axis=1) | |
| # stack arrays (with same dimensions) vertically | |
| d = np.vstack( (a,b) ) | |
| np.concatenate( (a, b), axis=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment