This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import matplotlib.pyplot as plt | |
import numpy as np | |
# Load the datasets | |
homoscedastic = pd.read_csv('https://gist.githubusercontent.com/rvaghefi/cb9c3b213e7ec9bc3501eed68aa8dc3f/raw/af218cf7ac0770eefe167a6796c29ab871e83079/homoscedastic.csv') | |
heteroscedastic = pd.read_csv('https://gist.githubusercontent.com/rvaghefi/cb9c3b213e7ec9bc3501eed68aa8dc3f/raw/af218cf7ac0770eefe167a6796c29ab871e83079/heteroscedastic.csv') | |
# Generate bias vector | |
b = np.ones(homoscedastic.shape[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import numpy as np | |
# set the seed | |
np.random.seed(125) | |
# generate a single feature randomly | |
X0 = np.random.rand(500) | |
# actual interception and slope of linear regression |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
# set the seed | |
np.random.seed(561) | |
# generate a single feature randomly | |
X0 = np.random.rand(100) | |
# actual interception and slope of linear regression |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
y | X1 | X2 | X3 | |
---|---|---|---|---|
5.15 | 0.7 | 0.29 | 0.23 | |
3.65 | 0.55 | 0.72 | 0.42 | |
5.94 | 0.98 | 0.68 | 0.48 | |
3.9 | 0.39 | 0.34 | 0.73 | |
4.51 | 0.44 | 0.06 | 0.4 | |
5.68 | 0.74 | 0.18 | 0.18 | |
4.15 | 0.53 | 0.53 | 0.63 | |
5.48 | 0.85 | 0.72 | 0.61 | |
5.4 | 0.72 | 0.32 | 0.36 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cupy as cp | |
from numba import jit | |
from timeit import timeit | |
from scipy.spatial.distance import cdist | |
import pandas as pd | |
n = 20 | |
m = 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numba import jit | |
@jit(nopython=True) | |
def numba_for_loop(X,Y): | |
m = X.shape[0] | |
k = Y.shape[0] | |
D = np.zeros((m,k)) | |
for i in range(m): | |
for j in range(k): | |
D[i,j] = np.sum((X[i] - Y[j])**2)**0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cupy as cp | |
def cupy_vectorized(X,Y): | |
X_gpu = cp.asarray(X) | |
Y_gpu = cp.asarray(Y) | |
D = cp.sum((X_gpu[:,None,:] - Y_gpu[None,:,:])**2, axis=2)**0.5 | |
return cp.asnumpy(D) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy.spatial.distance import cdist | |
def scipy_cdist(X,Y): | |
return cdist(X,Y,metric='euclidean') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def numpy_vectorized(X,Y): | |
return np.sum((X[:,None,:] - Y[None,:,:])**2, axis=2)**0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def numpy_for_loop(X,Y): | |
m = X.shape[0] | |
k = Y.shape[0] | |
D = np.zeros((m,k)) | |
for i in range(m): | |
for j in range(k): | |
D[i,j] = np.sum((X[i] - Y[j])**2)**0.5 | |
return D |
NewerOlder