Escolar Documentos
Profissional Documentos
Cultura Documentos
colab
drive.mount('/content/drive') #Liga o seu gDrive a um caminho padrão
import numpy as np #Importamos o numpy e seguimos normalmente
print('Criando arquivo de números aleatórios...')
meuarray = np.random.randint(1,80, 1000).reshape((100,10)) #Cria um array de dim
print(meuarray) #Imprime o arra criado
print('Salvando o arquivo com as dimensões: ', meuarray.shape)
np.savetxt('/content/drive/MyDrive/meuarray.csv', meuarray, delimiter=",") #Salv
print('Arquivo .csv salvo com sucesso!')
print('Você poderá ver este arquivo direto no gDrive ou carregando-o em novas se
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 1 de 23
:
[28 76 63 14 42 49 9 3 47 64]
[ 7 64 53 62 6 54 31 28 61 46]
[31 32 60 14 71 39 61 40 45 16]
[46 64 41 42 74 27 54 15 76 33]
[30 45 23 27 2 44 24 1 18 8]
[16 4 11 65 26 50 2 65 8 4]
[43 50 57 60 44 30 28 50 73 53]
[74 67 56 32 8 36 9 28 23 55]
[48 9 26 11 65 73 23 58 6 30]
[20 11 36 38 31 23 65 56 30 41]
[31 13 67 58 23 40 72 1 63 43]
[17 54 15 25 10 64 24 30 36 64]
[22 3 47 12 73 58 1 16 66 48]
[18 12 23 7 24 21 78 17 30 58]
[79 24 78 76 64 21 47 40 48 37]
[ 6 13 64 72 79 32 29 51 27 41]
[47 24 41 13 59 53 14 71 15 59]
[79 25 24 75 36 12 50 36 73 56]
[11 64 26 2 42 1 24 56 74 43]
[18 71 74 46 69 26 53 62 33 31]
[50 3 47 39 63 52 68 69 71 29]
[64 31 5 13 19 17 6 23 61 48]
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 2 de 23
:
Drive already mounted at /content/drive; to attempt to forcibly remount, ca
Carregando arquivo numérico de dados reais Iris
Abaixo estamos carregando os dados na variável r das colunas específicas
(150, 4)
Calculando dados:
desvio padrão coluna 0 foi 0.8253012917851409:
Media coluna 0 foi 5.843333333333334:
valor minino da coluna 0 foi 7.9:
valor minino da coluna 0 foi 4.3:
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 3 de 23
:
from google.colab import drive #Necessario para acessar o gDrive
drive.mount("/content/drive")#Necessário para acessar o gDrive
import numpy as np#Código normal para usar o NumPy ou outra biblioteca
print('Carregando arquivo numérico de dados reais')
print("Abaixo estamos carregando os dados na variável r das colunas específicas"
r = np.loadtxt("/content/drive/MyDrive/dados/dataR2.csv", delimiter = ","
print(r.shape) #Daqui em diante pode-se
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 4 de 23
:
print("Separando 120 registros ~= 80% do dataset...")
r80 = r[0:93 , : ]
r20 = r[94: , : ]
print("Dimensoes do 80%: ", r80.shape)
print("Dimensoes do 20%: ", r20.shape)
print("Salvando arquivos no Drive...")
np.savetxt("/content/drive/MyDrive/dados/bcancer-80.csv", r80, delimiter =
np.savetxt("/content/drive/MyDrive/dados/bcancer-20.csv", r20, delimiter =
print("Arquivos salvos, verifique o seu gdrive....")
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 5 de 23
:
Desvio padrão coluna 7 foi 12.337122119975472:
Media coluna 7 foi 14.725965862068962:
Valor máximo da coluna 7 foi 82.1:
Valor mínimo da coluna 7 foi 3.21:
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 6 de 23
:
import matplotlib.pyplot as plt
x = [1,2,3,4]
Y = [2,3,4,3]
plt.bar(x,Y)
plt.show()
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 7 de 23
:
values = [0,0.6,1.4,1.6,2.2,2.5,2.6,3.2,3.5,3.9,4.2,6]
plt.hist(values, bins = 3) #bins indica quantas barras
plt.show() #exibe o plot
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 8 de 23
:
import matplotlib.pyplot as plt
year = [1950, 1970, 1990, 2010, 2020]
pop = [2.519, 3.692, 5.263, 6.972, 10.33]
plt.plot(year, pop)
plt.show()
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 9 de 23
:
import matplotlib.pyplot as plt
year = [1950, 1970, 1990, 2010, 2020]
pop = [2.519, 3.692, 5.263, 6.972, 10.33]
plt.plot(year, pop)
#Nas próximas duas linhas customizamos
plt.xlabel('Ano')
plt.ylabel('População')
plt.show() #Exibe o gráfico
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 10 de 23
:
import matplotlib.pyplot as plt
year = [1950, 1970, 1990, 2010, 2020]
pop = [2.519, 3.692, 5.263, 6.972, 10.33]
plt.plot(year, pop)
#A partir daqui temos as customizações no .plt
plt.xlabel('Ano')
plt.ylabel('População')
plt.title('Projeções para população mundial')
plt.show()
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 11 de 23
:
import matplotlib.pyplot as plt
year = [1950, 1970, 1990, 2010, 2020]
pop = [2.519, 3.692, 5.263, 6.972, 10.33]
plt.plot(year, pop)
#A partir daqui temos as customizações no .plt
plt.xlabel('Ano')
plt.ylabel('População')
plt.title('Projeções para população mundial')
plt.yticks([0, 2, 4, 6, 8, 10])
plt.show()
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 12 de 23
:
import numpy as np
from matplotlib import pyplot as plt
a = np.random.randint(0,5999,30008).reshape((3751,8))
print(a.shape)
x = a[ : , 2]
y = a[: , 4]
plt.plot(x,y)
plt.title("Grafico de linhas")
plt.xlabel("terceira coluna")
plt.ylabel("quinta coluna")
plt.show()
print()
(3751, 8)
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 13 de 23
:
import numpy as np
from matplotlib import pyplot as plt
f = [1.68,1.70, 1.55, 1.56, 1.60, 1.58, 1.82, 2.6]
plt.boxplot(f , meanline=True)
plt.show()
print()
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 14 de 23
:
import numpy as np
from matplotlib import pyplot as plt
f = [1.68,1.70, 1.55, 1.56, 1.60, 1.58, 1.82, 2.6, 1,68, 1.68, 1.68, 1.94,
plt.hist(f, bins=4)
plt.show()
print()
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 15 de 23
:
import numpy as np
from matplotlib import pyplot as plt
x = [1,2,3,4]
1)
import numpy as np
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 16 de 23
:
import matplotlib.pyplot as plt
print('LETRA A)')
#A)
print('LETRA B)')
#B)
# Plotar o histograma
plt.hist(coluna_setima, bins=20)
plt.title('Histograma da 7th coluna')
plt.xlabel('Valores')
plt.ylabel('Frequência')
plt.show()
print()
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 17 de 23
:
plt.title('Gráfico de Caixa da 7th coluna')
plt.ylabel('Valores')
plt.show()
LETRA B)
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 18 de 23
:
2)
#1)
import numpy as np
import matplotlib.pyplot as plt
# Calcular média, valor máximo, valor mínimo e desvio padrão de cada feature
mean_values = np.mean(r, axis=0)
max_values = np.max(r, axis=0)
min_values = np.min(r, axis=0)
std_deviation = np.std(r, axis=0)
# Imprimir os resultados
for i in range(4):
print(f"Feature {i + 1}:")
print(f"Média: {mean_values[i]}")
print(f"Valor Máximo: {max_values[i]}")
print(f"Valor Mínimo: {min_values[i]}")
print(f"Desvio Padrão: {std_deviation[i]}\n")
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 19 de 23
:
plt.ylim(min_values.min() - 1, max_values.max() + 1)
# Mostrar o gráfico
plt.show()
print()
print()
#2)
# Gráfico de Linha
plt.figure(figsize=(10, 4))
plt.subplot(131)
plt.plot(x, y, 'r-', label='Linha')
plt.title('Gráfico de Linha')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
# Gráfico de Dispersão
plt.subplot(132)
plt.scatter(x, y, c='b', marker='o', label='Dispersão')
plt.title('Gráfico de Dispersão')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
# Gráfico de Barras
plt.subplot(133)
plt.bar(range(len(x)), y, color='g', label='Barras')
plt.title('Gráfico de Barras')
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 20 de 23
:
plt.xlabel('Amostras')
plt.ylabel('Feature 2')
plt.legend()
plt.tight_layout()
plt.show()
print()
print()
#3)
# Criar um histograma
plt.figure(figsize=(12, 4))
plt.subplot(121)
plt.hist(last_column, bins=20, color='skyblue', edgecolor='black')
plt.title('Histograma da Última Coluna')
plt.xlabel('Valor')
plt.ylabel('Frequência')
plt.tight_layout()
plt.show()
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 21 de 23
:
Valor Mínimo: 4.3
Desvio Padrão: 0.8253012917851409
Feature 2:
Média: 3.0540000000000007
Valor Máximo: 4.4
Valor Mínimo: 2.0
Desvio Padrão: 0.4321465800705435
Feature 3:
Média: 3.7586666666666693
Valor Máximo: 6.9
Valor Mínimo: 1.0
Desvio Padrão: 1.7585291834055201
Feature 4:
Média: 1.1986666666666672
Valor Máximo: 2.5
Valor Mínimo: 0.1
Desvio Padrão: 0.760612618588172
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 22 de 23
:
https://colab.research.google.com/drive/1HtHigAZksPboP-7d18T5Um4uo-bbCWjm?usp=sharing 09/10/2023, 20 56
Página 23 de 23
: