Last active
August 29, 2015 13:59
-
-
Save atduskgreg/10738360 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
# 1. Generate 100 points x uniformly distributed between 0 and 1, and let y = 2+3x+ζ, | |
# where ζ is a Gaussian random variable with a standard deviation of 0.5. Use an | |
# SVD to fit y = a + bx to this data set, finding a and b. | |
import numpy as np | |
# x is uniformly distributed | |
x = np.random.random(100) | |
for i in x: | |
# zeta is gaussian with sd = 0.5 | |
zeta = np.random.normal(0, 0.5) | |
y = 2 + 3*x + zeta | |
array = np.array([y]) | |
U,s,V = np.linalg.svd(array) | |
# | |
print("U:") | |
print(U.shape) | |
print(U) | |
print("s: ") | |
print(s.shape) | |
print(s) | |
print("V: ") | |
print(V.shape) | |
print(V) | |
pinv_svd = np.dot(np.dot(V.T,np.linalg.inv(np.diag(s))),U.T) | |
# ValueError: matrices are not aligned |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment