<jupyter_start><jupyter_text>Numpy  Voltamos 11h20<jupyter_code># importing numpy convention
import numpy as np<jupyter_output><empty_output><jupyter_text>--- Why Numpy? https://github.com/numpy/numpy ---- What are Numpy Arrays?Numpy arrays are very close to **lists** in python. The main differences for us in Data Analysis are:- There are *LOTS* of new mathematical methods that you can use in numpy arrays;- Operations are much faster (if you are dealing with big data);- Less Memory.  1. How to create an `array`<jupyter_code>minha_lista = [1, 2, 3]
meu_array = np.array(minha_lista)
print(minha_lista)
print(meu_array)
print(type(minha_lista))
print(type(meu_array))
meu_array_aleatorio = np.random.random(size=10)
print(meu_array_aleatorio)
print(meu_array[0])
print(meu_array_aleatorio[0])
print(type(meu_array[0]))
print(type(meu_array_aleatorio[0]))
outro_array = np.array([1, 2, 3, '4'])
print(outro_array)
type(outro_array[0])
array_str = np.array([1, 2, 3, 3], dtype=object)
print(array_str)
print(type(array_str[0]))
array_str = np.array(['1', '2', '1', '1'], dtype=float)
#array_str = np.array(['1', '2', '1', 'A'], dtype=float)
print(array_str)<jupyter_output><empty_output><jupyter_text>2. `Vectors` or `1-D array`<jupyter_code>array_lista = np.array([1,2,3])
print(array_lista)
array_zeros = np.zeros(10,)
array_dez = np.full((10,), fill_value = 10)
print(array_zeros)
print(array_dez)
array_dez = np.full((10,), fill_value = 10.)
print(array_dez)
print(array_lista.shape)
print(array_zeros.shape)
print(array_dez.shape)
print(array_lista.size)
print(array_lista[0])
print(array_lista[-1])
print(array_lista[1:])<jupyter_output>1
3
[2 3]<jupyter_text>3. `Matrix` or `2-D array`: **row, column**, this is the most important! ;-)- Imagine that rows are the observations of your data set- Imagine that columns are the characteristics of your data<jupyter_code>m10_5 = np.zeros((10,5))
print(m10_5)
ma10_5 = np.random.random(size=(10, 5))
print(ma10_5)
print(ma10_5.shape)
print(ma10_5.size)
lista_de_listas = [[1,2,3],[4,5,6]]
print(lista_de_listas)
lista_de_listas[1][0]
ma10_5[-1, 0]<jupyter_output><empty_output><jupyter_text>4. `n-D array`<jupyter_code>ma_5_4_2 = np.random.random(size=(5, 4, 2))
print(ma_5_4_2)
print(ma_5_4_2.shape)
print(ma_5_4_2.size)
for i in range(5):
    for k in range(4):
        for j in range(2):
            print(ma_5_4_2[i,k,j])<jupyter_output>0.029368495381642212
0.05816870528331808
0.29193704166868617
0.7315238945067737
0.5416279580285885
0.12535609515182822
0.8877310305747452
0.36613826860717136
0.5478470146681385
0.4991025432783178
0.0419184583300779
0.442684493508388
0.236703959486799
0.4863126288052908
0.503314347675685
0.6844762594887611
0.42578117073300037
0.39392840274366003
0.1659325098828912
0.19469303258161397
0.12072121384600165
0.03230133272290647
0.21506153667510197
0.9556487325227627
0.8471846053938547
0.3429600709631483
0.9059124409400507
0.5433217861213699
0.14845715224707223
0.03654882962518258
0.3899451613483017
0.04642583786754173
0.38822135322797424
0.5489539598007129
0.4403470971232375
0.9041915170184709
0.6039663531476538
0.3596275179554199
0.047320735094429245
0.11981413895471538<jupyter_text>5. `lists` vs. `numpy arrays`<jupyter_code>ma_3_3 = np.random.random(size = (3,3))
print(ma_3_3)
[1,2,3] * 3
ma_3_3 + 3
ma_3_3 * 10
ma_3_3.ravel()[~(ma_3_3 > 0.5).ravel()]
ma_3_3.ravel()[3]
ma10_5.ravel()[42]<jupyter_output><empty_output><jupyter_text>6. The `axis` concept - very useful- The `axis` concept:     - axis = 0, first axis == `rows`        - When we set axis=0, the function will **collapses** the rows and calculates the function            - axis = 0, first axis == `columns`        - When we set axis=1, the function will **collapses** the columns and calculates the function  6.1 Some examples with mathematical operations**Mean**  $$ \bar{x} = \frac{1}{N}\left(\sum_{i=0}^{N-1} x_i\right)  $$<jupyter_code>meu_array = np.array([[10, 5],
                      [15, 2],
                      [25, 8]])
print(meu_array)
meu_array[:,1].sum()
meu_array.sum(axis = 0)
meu_array.sum(axis = 1)
meu_array.mean(axis = 0)
meu_array.mean(axis = 1)
print(ma_5_4_2)
!jt -r
ma_5_4_2.sum(axis = 0)
ma_3_3 = np.random.random(size = (2, 3))<jupyter_output><empty_output><jupyter_text>7. Operations between `np.arrays`<jupyter_code>matriz_3_3 = np.array([[1, 1, 1],
                       [2, 2, 2],
                       [1, 2, 3]])
vetor_3 = np.array([5, 12, 1.8])
print(matriz_3_3)
print(vetor_3)
vetor_3 + matriz_3_3
matriz_6_3 = np.array([[1, 1, 1],
                       [2, 2, 2],
                       [1, 2, 3],
                       [1, 2, 3],
                       [1, 2, 3],
                       [1, 2, 3]])
vetor_3 + matriz_6_3
matriz_3_6 = np.array([[1, 1, 1, 2, 2, 2],
                       [1, 2, 3, 1, 2, 3],
                       [1, 2, 3, 1, 2, 3]])
print(matriz_3_6)
vetor_3 + matriz_3_6
ma_2_2 = np.full((2,2), 2)
ma_2_3 = np.full((2,2), 6)
print(ma_2_2)
print(ma_2_3)
print(ma_2_2 + ma_2_3)
print(matriz_3_3)
print(vetor_3)
print(vetor_3 * matriz_3_3)
vetor_4 = np.array([1,2,3,4])
print(vetor_4)
vetor_4 * matriz_3_3<jupyter_output><empty_output><jupyter_text>8. Transpose Matrix<jupyter_code>print(matriz_3_6)
print(matriz_3_6.transpose())
print(matriz_3_6.transpose() * vetor_3)<jupyter_output>[[ 5.  12.   1.8]
 [ 5.  24.   3.6]
 [ 5.  36.   5.4]
 [10.  12.   1.8]
 [10.  24.   3.6]
 [10.  36.   5.4]]<jupyter_text>9. Reshaping arrays<jupyter_code>matriz_3_6.size
matriz_3_6.reshape((9,2))
matriz_3_6.reshape((matriz_3_6.size,))
from sklearn.datasets import load_sample_image
import matplotlib.pyplot as plt
image = load_sample_image('flower.jpg')
plt.imshow(image)
print(image)
print(image.shape)
print(image.size)
image_r = image[:,:,0]
image_g = image[:,:,1] + 1
image_b = image[:,:,2]
plt.imshow((image_r - image_g))
def fatores_inteiros(num):
    fatores = [valor for valor in range(200, 600) if num % valor == 0]
    return fatores
fatores_inteiros(273280)
plt.imshow(image.reshape(560, int(273280/560), 3))<jupyter_output><empty_output><jupyter_text>Voltamos 14h15<jupyter_code>plt.imshow(image_r)
np.min(image_r+1)
plt.imshow(np.log(np.full(image_r.shape, 1)+image_r) + np.log(np.full(image_g.shape, 1)+image_g))
U, s, V = np.linalg.svd(np.log(np.full(image_r.shape, 1)+image_r)) 

num_components = 3
reconst_img_5 = np.matrix(U[:, :num_components]) * np.diag(s[:num_components]) * np.matrix(V[:num_components, :])
plt.imshow(reconst_img_5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15244841392054204, 'fraction_numerical': 0.16553741915614414, 'mean_word_length': 4.788770053475936, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 43, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '298155', 'license': 'MIT', 'n_tokens_mistral': 3383, 'n_tokens_neox': 2858, 'n_words': 512, 'path': 'aulas/64 DA PT NOV-2021 Aula 20211127.ipynb', 'repo_name': 'AndresCBF/64_PT_NOV202111', 'size': '6494'}