## 0. Import Packages


```python
import sympy
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
```

## 1. Gradient Vector Field and Contour


```python
#define symbolic vars, function
x, y = sympy.symbols('x y')
fun = (x-1)**2+(y-1)**2

#take the gradient symbolically
gradfun = [sympy.diff(fun,var) for var in (x,y)]

#turn into a bivariate lambda for numpy
numgradfun = sympy.lambdify([x,y],gradfun)


x = np.linspace(-4, 6, 15)
y = np.linspace(-4, 6, 15) 
x1 = np.linspace(-4, 6, 100)
y1 = np.linspace(-4, 6, 100) 

X,Y = np.meshgrid(x, y)
X1, Y1 = np.meshgrid(x1, y1)
graddat = numgradfun(X,Y)
Z = (X1-1)**2 + (Y1-1)**2

plt.figure()
plt.axes().set_aspect('equal', 'datalim')
plt.quiver(X,Y,graddat[0],graddat[1])
plt.contour(x1, y1, Z, cmap='seismic')

plt.show()
```

## 2. Linear Regression
- x = np.array([1, 2, 3])
- y = np.array([4.1, 6.9, 9.9])
- Hypothesis H = wx+b



```python
x = np.array([1, 2, 3])
y = np.array([4.1, 6.9, 9.9])
w = 0
b = 0
a = 0.05      # learning rate

H = w*x + b

for i in range(20):
    dw = 2*sum(x*(H-y))/len(y)
    db = 2*sum(H-y)/len(y)
    w = w - a*dw
    b = b - a*db

    H = w*x+b
    c = sum((H-y)*(H-y))/len(y)
    
    plt.plot(x, y, "ro")
    plt.plot(x, w*x+b, "b-") 
    
    print("w: ", w, "b: ", b, "Cost: ", c)
    
print("Prediction:", w*4+b)    
```

- x = np.array([1, 2, 3])
- y = np.array([4.1, 6.9, 9.9])
- Hypothesis H = wx


```python
x = np.array([1, 2, 3])
y = np.array([4.1, 6.9, 9.9])
w = 0
a = 0.05      # learning rate

H = w*x

for i in range(20):
    g = 2*sum(x*(H-y))/len(y)
    w = w - a*g
    H = w*x
    c = sum((H-y)*(H-y))/len(y)
    plt.plot(x, y, "ro")
    plt.plot(x, w*x, "b-") 

    print("w: ", w, "Cost: ", c)
    
print("Prediction:", w*4)    
```

- x = np.array([30, 20, 40])
- y = np.array([6.1, 3.1, 6.8])
- Hypothesis H = w*x + b


```python
x = np.array([30, 20, 40])
y = np.array([6.1, 3.1, 6.8])
w = 0
b = 0
a = 0.001      # learning rate

H = w*x + b

for i in range(1000):
    dw = 2*sum(x*(H-y))/len(y)
    db = 2*sum(H-y)/len(y)
    w = w - a*dw
    b = b - a*db

    H = w*x+b
    c = sum((H-y)*(H-y))/len(y)
    if i % 100 == 0:
        print("w: ", w, "b: ", b, "Cost: ", c)
    
print("Prediction:", w*25+b)    
```

    w:  0.3446666666666667 b:  0.010666666666666666 Cost:  27.16708562962964
    w:  0.1784041700910588 b:  0.0024629078261677556 Cost:  0.2972409816719897
    w:  0.17829247047790894 b:  -0.0005424250099560584 Cost:  0.2971102467195272
    w:  0.1783840228514668 b:  -0.0035003652573958647 Cost:  0.2970226733833154
    w:  0.17847457052784338 b:  -0.006417814659193166 Cost:  0.29693748080630333
    w:  0.17856387925085382 b:  -0.009295335080892234 Cost:  0.2968546042070466
    w:  0.17865196567185065 b:  -0.012133473010365266 Cost:  0.29677398062521215
    w:  0.1787388465192008 b:  -0.01493276744664607 Cost:  0.2966955488120573
    w:  0.1788245382926983 b:  -0.017693750011875956 Cost:  0.29661924918389987
    w:  0.17890905726631662 b:  -0.02041694505227746 Cost:  0.29654502377685077
    Prediction: 4.451713595271376


## 2. Multi-Variable Linear regression


```python
X = np.matrix([[30, 20, 40], [1, 2, 3], [5, 10, 15]])
Y = np.matrix([6.1, 3.1, 6.8])

W = np.matrix([0, 0, 0])
b = 0

a = 1e-4 # learning rate
H = W*X + b

for t in range(3000):
    dW = 2*((H-Y)*X.T)/Y.shape[1] # gradient
    db = 2*np.mean(H-Y) 
    W = W - a*dW 
    b = b - a*db
    c = (H-Y)*(H-Y).T/Y.shape[1] 
    H = W*X+b
    if t % 100 == 0:
        print('Iteration:', t, 'Cost:', c[0,0])
        
print('')
X_test = np.matrix([[25], [2], [10]])
print("Price Prediction:", (W*X_test+b)[0,0])
```

    Iteration: 0 Cost: 31.019999999999996
    Iteration: 100 Cost: 0.34082133458696035
    Iteration: 200 Cost: 0.21074274888050268
    Iteration: 300 Cost: 0.1305638504780756
    Iteration: 400 Cost: 0.08114184956251845
    Iteration: 500 Cost: 0.05067761941048248
    Iteration: 600 Cost: 0.031898479662662904
    Iteration: 700 Cost: 0.02032173588706472
    Iteration: 800 Cost: 0.013184369717335368
    Iteration: 900 Cost: 0.008783327191182105
    Iteration: 1000 Cost: 0.00606888933209325
    Iteration: 1100 Cost: 0.004394036161773092
    Iteration: 1200 Cost: 0.003359961078985585
    Iteration: 1300 Cost: 0.0027208498684453456
    Iteration: 1400 Cost: 0.0023251889139681518
    Iteration: 1500 Cost: 0.0020795890801245617
    Iteration: 1600 Cost: 0.001926487676877947
    Iteration: 1700 Cost: 0.0018304045201090295
    Iteration: 1800 Cost: 0.0017694705183611975
    Iteration: 1900 Cost: 0.001730206110082214
    Iteration: 2000 Cost: 0.0017043028257053155
    Iteration: 2100 Cost: 0.0016866395247191208
    Iteration: 2200 Cost: 0.0016740596548655451
    Iteration: 2300 Cost: 0.0016646175859395386
    Iteration: 2400 Cost: 0.0016571140691929865
    Iteration: 2500 Cost: 0.0016508099063763692
    Iteration: 2600 Cost: 0.0016452494626485812
    Iteration: 2700 Cost: 0.0016401518844429645
    Iteration: 2800 Cost: 0.0016353440489375395
    Iteration: 2900 Cost: 0.0016307192358216817
    
    Price Prediction: 4.185693634972486


c064631215f52a89c2516fd2f8492a90f8f1162d
ipynb
Jupyter Notebook
4_Regression/Gradient_Vector_and_Linear_Regression.ipynb
OH-Seoyoung/Mathematics_for_4th_Industrial_Revolution
30478a34518323837fc28d60fc87c44efac19216
MIT
2020-09-26T01:34:05.000Z
2020-09-26T01:34:05.000Z
4_Regression/Gradient_Vector_and_Linear_Regression.ipynb
OH-Seoyoung/Mathematics_for_4th_Industrial_Revolution
30478a34518323837fc28d60fc87c44efac19216
MIT
4_Regression/Gradient_Vector_and_Linear_Regression.ipynb
OH-Seoyoung/Mathematics_for_4th_Industrial_Revolution
30478a34518323837fc28d60fc87c44efac19216
MIT
Qwen/Qwen-72B
1. YES
2. YES