NumPy live coding Notebook#
NumPy arrays en matrices#
import numpy as np
np.array([[1,2,3],[4,5,6]])
arr = np.array([[1,2,3],[4,5,6]])
arr
Shape en transponeren#
arr.shape
arr.T
arr.T.shape
Indexatie en slicing#
arr
arr[0]
arr[1,:]
arr[1,1:2]
arr[1,1:]
arange, linspace, zeros, ones, eye#
np.arange(0, 10, 1)
np.arange(0, 10, 2)
np.linspace(0, 9, 10)
np.linspace(0, 9, 50)
np.zeros((2,3))
np.ones((3,2))
np.eye(3)
np.random#
rand_arr = np.random.rand(100, 4)
rand_arr[:5,:]
np.random.randn(5, 4)
np.random.randint(0, 10, size=(5,4))
Vectoren#
v = np.array([1,2,3,4])
v
v.shape
v2 = np.array([[1,2,3,4]])
v2
v2.shape
Element-wise operaties#
arr + 1
arr * 2
arr2 = np.ones((2,3)) * 3
arr2
arr * arr2
arr3 = np.ones((2,2)) * 3
arr * arr3
Matrix-vermenigvuldiging#
arr4 = np.ones((3,2)) * 3
arr @ arr4
np.matmul(arr, arr4)
np.dot(arr, arr4)
v3 = np.array([3,3,3,3]).T
np.dot(v2, v3)
Pandas#
import pandas as pd
my_dict = {'J':[1,4], 'F':[2,5], 'M':[3,6]}
df = pd.DataFrame(my_dict,index=['C1','C2'])
df
df['F']
df.loc[:,'F']
df['C1']
df.loc['C1']
df.loc['C1',:]
df.iloc[0]
arr = np.random.randn(100, 10)
df2 = pd.DataFrame(arr, columns='A B C D E F G H I J'.split())
df2.head()
df2.to_csv('example')
df3 = pd.read_csv('example', index_col=0)
df3.head()
df3.describe()