Skip to content

Instantly share code, notes, and snippets.

View rvaghefi's full-sized avatar

Reza Vaghefi rvaghefi

  • Qualcomm
  • San Francisco Bay Area
View GitHub Profile
@rvaghefi
rvaghefi / heteroscedasticity_detection.py
Created February 4, 2021 22:35
heteroscedasticity_detection.py
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])
@rvaghefi
rvaghefi / outlier_detection.py
Created February 4, 2021 22:30
outlier_detection.py
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
@rvaghefi
rvaghefi / weighted_linear_regression.py
Created February 4, 2021 19:07
weighted_linear_regression.py
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
@rvaghefi
rvaghefi / heteroscedastic.csv
Last active January 22, 2021 22:08
linear_regression_data
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
@rvaghefi
rvaghefi / distance_benchmark.py
Created January 14, 2021 18:16
distance_benchmark.py
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
@rvaghefi
rvaghefi / numba_for_loop.py
Created January 14, 2021 18:15
numba_for_loop.py
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
@rvaghefi
rvaghefi / cupy_vectorized.py
Created January 13, 2021 22:54
cupy_vectorized.py
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)
@rvaghefi
rvaghefi / scipy_cdist.py
Created January 13, 2021 20:43
scipy_cdist.py
from scipy.spatial.distance import cdist
def scipy_cdist(X,Y):
return cdist(X,Y,metric='euclidean')
@rvaghefi
rvaghefi / numpy_vectorized.py
Last active January 13, 2021 20:39
numpy_vectorized.py
def numpy_vectorized(X,Y):
return np.sum((X[:,None,:] - Y[None,:,:])**2, axis=2)**0.5
@rvaghefi
rvaghefi / numpy_for_loop.py
Created January 13, 2021 18:27
numpy_for_loop
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