<jupyter_start><jupyter_text>Numpy Array Slicing and Indexing* Topicos:    * Array Unidimensional    * Array Bidimensional    * Referenciação    * Dimensionalidade    * Indexação Booleana<jupyter_code># Importar Library

import numpy as np

# Criar Array unidimensional

arr1 = np.arange(0,30,3)
print(arr1)

# Acessar elementos (semelhante às listas)

print(arr1[2:5])  # elementos nas posicoes 2, 3 e 4 (5-1)
print(arr1[2:])   # elemento 2 ao ultimo
print(arr1[:5])   # elemento inicial ao 4 (5-1)

# Manipular elementos

arr1[2:] = 100   # elemento 2 ao final = 100
print(arr1)
# Criar Array bidimensional 

# arr_2d[row][col] ou arr_2d[row,col]

arr2 = np.arange(50).reshape((5,10))
print(arr2)

# Acessar elementos

print(arr2.shape)   # dimensões
print(arr2[3])      # linha 3
print(arr2[:3][:])  # linhas 0 a 2 (3-1) e todas as colunas
print(arr2[1:4,5:]) # linhas 1 a 3 (4-1) e 5 ao final

# Manipular Elementos

arr2[:][1:3] = 10
print(arr2)
# Referenciacao

arr = np.arange(0,11) # nova array
arr_slice = arr[0:6]  # somente referencia, nao cria novo objeto
print(arr)
print(arr_slice)

# Modificando slice, modifica array original

arr_slice[:] = 99
print(arr)
print(arr_slice)

# Fazendo uma copia, cria novo objeto

arr_copy = arr.copy()
print(arr_copy)

# Dimensionalidade

a = np.zeros((5,5))
display(a)

b = a[:,0]
display(b.shape)

c = b[:,np.newaxis]
display(c.shape)

d = a[:,0].reshape(5,1)
display(d.shape)
# Indexação Booleana 1

# Verificação Booleana

array1 = np.array([10,25,56,32])
array2 = np.array([1,40,35,88])
print(array1 > array2)

# Seleciona Indices 1

bol = array2 > 50
print(bol)
print(array2[bol])

# Seleciona Indices 2

x = 2
print(array2[array2>x])
# Indexação Booleana 2

# Gera arrays

nomes = np.array(['david','nice','ana','bia','david','junior','nice'])
data = np.random.randn(7,4)

print(nomes)
print(data)

# Pesquisa Valores

print(nomes == "david")
print(nomes != "nice")

# Imprime linhas 0 e 4

cond = (nomes == "david")

print(data[nomes == "david"])
print(data[cond])

# Slicing

print(data[~cond, 2:])<jupyter_output>['david' 'nice' 'ana' 'bia' 'david' 'junior' 'nice']
[[-0.09586133 -1.23874469 -1.05053967 -0.03613485]
 [-0.37758066 -0.45776507 -0.63475017  0.6504702 ]
 [-0.17551104  0.74086068  0.57578946 -0.47457432]
 [ 1.43673367 -0.21066854  0.65331406 -0.76266138]
 [-0.83228601 -0.06139514  0.23683777 -1.77058029]
 [-0.7214711   0.72090886 -1.08675304 -0.71799546]
 [-0.97279627  0.59557044 -0.6877836   1.16704046]]
[ True False False False  True False False]
[ True False  True  True  True  True False]
[[-0.09586133 -1.23874469 -1.05053967 -0.03613485]
 [-0.83228601 -0.06139514  0.23683777 -1.77058029]]
[[-0.09586133 -1.23874469 -1.05053967 -0.03613485]
 [-0.83228601 -0.06139514  0.23683777 -1.77058029]]
[[-0.63475017  0.6504702 ]
 [ 0.57578946 -0.47457432]
 [ 0.65331406 -0.76266138]
 [-1.08675304 -0.71799546]
 [-0.6877836   1.16704046]]
{'fraction_non_alphanumeric': 0.1617445054945055, 'fraction_numerical': 0.20432692307692307, 'mean_word_length': 4.174067495559503, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '289448', 'license': 'MIT', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1323, 'n_words': 278, 'path': 'scripts_numpy_pandas/Numpy_02_slice_index.ipynb', 'repo_name': 'davidcoelho89/MLTool_python', 'size': '2912'}