# Python

champak
₹2,000.00 ₹300.00
• 22 students
• 41 lessons
• 0 quizzes
• 10 week duration

## Python

### Linear and Polynomial Regression using Python

Regression is about finding a function to fit observed data. Linear Regression tries to fit a straight line to the observed data. To implement Linear Regression we begin by importing  numpy and LinearRegression.

```
import numpy as np
from sklearn.linear_model import LinearRegression

```

For plotting we shall use

```import matplotlib.pyplot as plt
```

Next, get the input lists.

```x=[1,2,3,4,5]
y=[1,2,3,4,5]

```

x needs to be converted to a numpy array.

```x=np.array(x)
x=x.reshape(-1,1)
```

Reshaping the x array is required for making it vertical.

At this point we are ready to fit the data and create a data fit.

```
lin_reg.fit(x, y)

```

We make a function to print the data and the fitted regression line.

```def plotLinear():
plt.scatter(x.reshape(-1,1), y, color='red')
plt.plot(x, lin_reg.predict(x), color='green')
plt.title('Linear Regression')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
```

Here is the complete code.

```</pre>
#Linear Regression
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
lin_reg = LinearRegression()
x=[1,2,3,4,5]
y=[1,2,3,4,5]
x=np.array(x)
x=x.reshape(-1,1)

lin_reg.fit(x, y)

# Visualizing the Linear Regression results
def plotLinear():
plt.scatter(x.reshape(-1,1), y, color='red')
plt.plot(x, lin_reg.predict(x), color='green')
plt.title('Linear Regression')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
plotLinear()
y=lin_reg.predict(10)
print(y)
<pre>

```

To use PolyRegression we work in the following way.

```
poly_reg = PolynomialFeatures(degree=4)
x_poly = poly_reg.fit_transform(x)
pol_reg = LinearRegression()
pol_reg.fit(x_poly, y)

```

The complete code.

```
#Polynomial Regression
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
x=[1,2,3,4,5]
x=np.array(x)
x=x.reshape(-1,1)
y=[1,4,9,16,25]
poly_reg = PolynomialFeatures(degree=4)
x_poly = poly_reg.fit_transform(x)
pol_reg = LinearRegression()
pol_reg.fit(x_poly, y)

def plotPolynomial():
plt.scatter(x, y, color='red')
plt.plot(x, pol_reg.predict(poly_reg.fit_transform(x)), color='blue')
plt.title('Polynomial Regression')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
return
plotPolynomial()
y=pol_reg.predict(poly_reg.fit_transform(10))
print(y)

```

end