-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinear_basic.py
99 lines (77 loc) · 1.84 KB
/
linear_basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# In machine Learning, this
# are standard library but you
# may use anothers
import pandas as pd
import numpy as np
import pylab as plt
# The linear model is one of the most fundamentals
# It just is a fit of a line in a cloud of points
from sklearn import linear_model
print "First, A very simple use case"
#Generate Some X
print "*"*50
U=np.random.randn(15000)
V=np.random.randn(15000)
W=np.random.randn(15000)
X=np.array([U,V,W])
X=X.T
# Build some Y
a=2
b=45
c=-7
d=-4
e=28
f=12
Y=a*U+b*V+c*W
plt.scatter(X.T,np.log(X))
plt.show()
# Now see how the lienar regression is able to find
# your build
lr = linear_model.LinearRegression()
lr.fit(X,Y)
print "Your coefs were : "
print "%d,%d and %d"%(a,b,c)
print "And the machine found : "
print lr.coef_
print "Very good"
print "*"*50
print " now let's see how the algo is impacted by noise"
Y=Y+np.random.uniform(-4,4,15000)
lr.fit(X,Y)
print "Your coefs were : "
print "%d,%d and %d"%(a,b,c)
print "plus some noise"
print "And the machine found : "
print lr.coef_
print "Not that bad"
print "*"*50
print " OK. now let's talk about colinearity"
print "What does happen when some features ares strictly colinears ?"
# First : add some independant features
A=np.random.randn(15000)
B=np.random.randn(15000)
C=np.random.randn(15000)
X=np.array([U,V,W,A,B,C])
X=X.T
Y=a*U+b*V+c*W+d*A+e*B+f*C
lr.fit(X,Y)
print "Your coefs were : "
print "%d,%d,%d,%d,%d and %d"%(a,b,c,d,e,f)
print "And the machine found : "
print lr.coef_
print "it still works fine"
print "*"*20
print " But what if there are highly collinear features ?"
A=4*U
B=-60*U+22*V
C=10*W+U
X=np.array([U,V,W,A,B,C])
X=X.T
Y=a*U+b*V+c*W+d*A+e*B+f*C
lr.fit(X,Y)
print "Your coefs were : "
print "%d,%d,%d,%d,%d and %d"%(a,b,c,d,e,f)
print "And the machine found : "
print lr.coef_
print "It does not work fine anymore. It's pretty bad"
print "*"*50