import numpy as np
import numpy as np
vector = np.arange(5)
vector
matrix = np.arange(9).reshape(3, 3)
matrix
tensor = np.arange(12).reshape(3, 2, 2)
tensor
my_first_vector = np.array([2, 5, 6, 23])
print(my_first_vector)
[ 2 5 6 23]
my_first_matrix = np.array([[2, 4,], [6, 8]])
print(my_first_matrix)
[[2 4]
[6 8]]
my_list = [0, 1, 2, 3, 4]
print(np.array(my_list))
[0 1 2 3 4]
print(np.arange(start=2, stop=10, step=2))
[2 4 6 8]
print(np.arange(11, 1, -2))
[11 9 7 5 3]
print(np.linspace(start=11, stop=12, num=18))
[11. 11.05882353 11.11764706 11.17647059 11.23529412 11.29411765
11.35294118 11.41176471 11.47058824 11.52941176 11.58823529 11.64705882
11.70588235 11.76470588 11.82352941 11.88235294 11.94117647 12. ]
print(np.linspace(0, 1, 11))
[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
print(np.zeros(4))
print(np.zeros((2, 2)))
[[0. 0.]
[0. 0.]]
print(np.ones(6))
[1. 1. 1. 1. 1. 1.]
print(np.full(shape=(2, 2), fill_value=5))
[[5 5]
[5 5]]
print(np.full((2, 3, 4), 0.55))
[[[0.55 0.55 0.55 0.55]
[0.55 0.55 0.55 0.55]
[0.55 0.55 0.55 0.55]]
[[0.55 0.55 0.55 0.55]
[0.55 0.55 0.55 0.55]
[0.55 0.55 0.55 0.55]]]
base = np.linspace(2, 6, 4)
print(np.full_like(base, np.pi))
[3.14159265 3.14159265 3.14159265 3.14159265]
import matplotlib.pyplot as plt
uniform = np.random.uniform(0, 10, 10000)
normal = np.random.normal(0, 3, 10000)
plt.figure(figsize=(12,5))
plt.subplot(1, 2, 1)
plt.hist(uniform, bins=50)
plt.title('Distribución uniforme')
plt.subplot(1, 2, 2)
plt.hist(normal, bins=50)
plt.title('Distribución normal')
plt.show()
print(np.random.rand(2, 2))
[[0.62740202 0.11171536]
[0.47526728 0.19739417]]
rand = np.random.rand(10000)
plt.hist(rand, bins=50)
plt.show()
print(np.random.uniform(low=0, high=1, size=6))
[0.7878737 0.3431897 0.77765595 0.60943181 0.30961326 0.60167083]
uniform = np.random.uniform(low=0, high=1, size=10000)
plt.hist(uniform, bins=50)
plt.show()
print(np.random.randn(2, 2))
[[ 0.91140011 1.72792052]
[-0.84028707 -0.27378577]]
normal = np.random.randn(10000)
plt.hist(normal, bins=50)
plt.show()
print(np.random.normal(loc=0, scale=2, size=6))
[-2.36743682 -3.12673482 -1.14254395 -3.19805542 -1.11930443 -2.70161226]
normal2 = np.random.normal(0, 1, 10000)
plt.hist(normal2, bins=50)
plt.show()
print(np.random.randint(low=0, high=10, size=(3, 3)))
[[1 0 5]
[5 5 3]
[7 5 4]]
print(np.random.randint(1,100,10))
[61 55 8 95 93 89 27 24 1 38]
a = np.arange(1,10)
B = np.reshape(a, [3,3])
print(B)
C = np.arange(1, 9).reshape(2, 2, 2)
print(C)
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
print(B.shape)
print(B.dtype)
int64
matrix_cool = np.arange(9).reshape(3, 3)
print(matrix_cool)
[[0 1 2]
[3 4 5]
[6 7 8]]
print(matrix_cool[1, 2])
5
print(matrix_cool[0, :])
[0 1 2]
print(matrix_cool[:, 1])
[1 4 7]
print(matrix_cool[:, 1:])
[[1 2]
[4 5]
[7 8]]
print(matrix_cool[0:2, 0:2])
print(matrix_cool[:, :])
[[0 1 2]
[3 4 5]
[6 7 8]]
a1 = np.array([2, 4, 6])
a2 = a1
a1[0] = 8
print(a1)
print(a2)
[8 4 6]
[8 4 6]
a1 = np.array([2, 4, 6])
a2 = a1.copy()
a1[0] = 8
print(a1)
print(a2)
[8 4 6]
[2 4 6]
# Suma
A = np.arange(5, 11)
print(A)
print(A + 10)
[ 5 6 7 8 9 10]
[15 16 17 18 19 20]
# Resta
B = np.full(4, 3)
C = np.ones(4, dtype='int')
print(B)
print(C)
print(B - C)
[3 3 3 3]
[1 1 1 1]
[2 2 2 2]
# Multiplicación y división
print(A * 10)
print(A / 10)
[ 50 60 70 80 90 100]
[0.5 0.6 0.7 0.8 0.9 1. ]
height_list = [74, 74, 72, 72, 73, 69, 69, 71, 76, 71, 73, 73, 74, 74, 69, 70, 73, 75, 78, 79, 76, 74, 76, 72, 71, 75]
print(np.mean(height_list))
print(np.median(height_list))
print(np.std(height_list))
73.1923076923077
73.0
2.572326554954764
print(np.max(height_list))
print(np.min(height_list))
79
69
import pandas as pd
df = pd.read_csv('Baseball_Players.csv')
height = df['Height(inches)'].to_numpy(dtype='int64')
weight = df['Weight(pounds)'].to_numpy(dtype='int64')
print(height)
height.shape
[74 74 72 ... 75 75 73]
print(weight)
weight.shape
[180 215 210 ... 205 190 195]
# Hora de resolver los ejercicios