### Linear Regression

# Linear Regression from scratch¶

#### importing packages¶

In [37]:

```
import numpy as np
```

### Generating Data Points¶

In [38]:

```
np.random.seed(0)
m = 3
c = 4
x = np.arange(0,50)
x =x.reshape(1,len(x))
N = x.shape[1]
y = m*x + c + 50*np.random.rand(1,N) # Random numbers will make sure that the points are scattered
```

In [39]:

```
x.shape
```

Out[39]:

### Plotting the data points¶

In [40]:

```
import matplotlib.pyplot as plt
plt.plot(x,y,'go')
plt.ylabel('Data')
plt.show()
```

### Finding the optimum slope and intercept through several iterations¶

In [41]:

```
import matplotlib.pyplot as plt
m1 = np.random.randn(1,1)
c1 = np.random.randn(1,1)
learningrate = 0.001
numiter = 1000
for i in range(numiter):
dm = (2.0/N) * -np.sum(np.multiply(x,(y - (m1*x +c1))))
dc = (2.0/N) * -np.sum(y - (m1*x + c1))
m1 = m1 -learningrate * dm
c1 = c1 - learningrate * dm
```

### Slope and intercept after several iterations¶

In [42]:

```
print m1
print c1
```

### Plotting the line with new slope and intercept that we got after iterations¶

In [50]:

```
plt.plot(x,y,'go',x,m1*x+c1,'r^')
plt.ylabel('Data')
plt.show()
```

## Comments

## Post a Comment