-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_exercises.py
More file actions
92 lines (76 loc) · 2.18 KB
/
Copy pathfinal_exercises.py
File metadata and controls
92 lines (76 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Final Exercises for Trabajo con Python
# Author: Miguel A. Castellanos (from text) or me? It's the notes.
import numpy as np
import matplotlib.pyplot as plt
import torch
# 1. Create a dictionary with personal info
persona = {
"edad": 30,
"sexo": "M",
"estudios": "Ingeniería",
"nombre_amigos": ["Ana", "Luis", "Maria"]
}
# 2. Function to print the dictionary
def imprimir_diccionario(dic):
for clave, valor in dic.items():
print(f"{clave}: {valor}")
# 3. Class that receives the dictionary and prints it
class Persona:
def __init__(self, info):
self.info = info
def imprimir(self):
imprimir_diccionario(self.info)
# 4. List of dictionaries with different people
personas = [
{
"edad": 25,
"sexo": "F",
"estudios": "Medicina",
"nombre_amigos": ["Juan", "Pedro"]
},
{
"edad": 40,
"sexo": "M",
"estudios": "Derecho",
"nombre_amigos": ["Laura", "Sofia"]
},
persona # reutilizamos la primera persona
]
# 5. Print list using for
for p in personas:
imprimir_diccionario(p)
print("-" * 20)
# 6. Generate X from -3 to 3 with step 0.1
x = np.arange(-3, 3.1, 0.1)
# 7. Generate Y as normal distribution (0,1)
y = np.random.normal(0, 1, size=x.shape)
# 8. Plot the distribution
plt.figure()
plt.plot(x, y, label="N(0,1)")
plt.title("Distribución Normal")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.grid(True)
plt.show()
# 9. Create vector 0..10, reshape to 2x5, multiply by ones, convert to tensor
vector = np.arange(0, 10)
matriz = vector.reshape((2, 5))
resultado = matriz * np.ones((2, 5))
tensor_resultado = torch.from_numpy(resultado)
print("Tensor result:\n", tensor_resultado)
# 10. Compute derivative of x**3 - 2*x and plot
x_tensor = torch.linspace(-5, 5, steps=100, requires_grad=True)
funcion = x_tensor**3 - 2 * x_tensor
funcion.backward(torch.ones_like(x_tensor))
dx = x_tensor.detach().numpy()
dy = funcion.detach().numpy()
derivada = x_tensor.grad.detach().numpy()
plt.figure()
plt.plot(dx, dy, label="x^3 - 2x")
plt.plot(dx, derivada, label="Derivada", linestyle="--")
plt.title("Función y Derivada")
plt.xlabel("x")
plt.grid(True)
plt.legend()
plt.show()