Skip to content

Instantly share code, notes, and snippets.

def linear_fit(x, y):
N_ = len(x)
X_, y_ = np.vstack([x, np.ones(N_)]).T, np.array(y)
fit_results = np.linalg.lstsq(X_, y_, rcond=None)
a_, b_ = fit_results[0]
r2error_ = fit_results[1][0]
return a_, b_, r2error_
def sum_from_one(k):
sum = 0
for i in range(1, k+1):
sum += i
return sum
sum_list = []
for i in range(0, N):
sum_list.append(sum_from_one(i))
@tatamiya
tatamiya / pca_image.py
Created March 10, 2019 07:56
PCA decomposition of an image
from PIL import Image
import numpy as np
from sklearn.decomposition import PCA
n_components=10
ncomp_selected = 0 # the index of the PCA component to select (int or list)
# read image
pilimg_pumpkin = Image.open('pumpkin.jpg')
npimg_pumpkin = np.array(pilimg_pumpkin)