₹2,000.00
₹300.00

- 22 students
- 41 lessons
- 0 quizzes
- 10 week duration
-
Python Basics
- Installing Anaconda and running a sample Python Program
- How to use the Print function in Python?
- Arithmetic Operations in Python
- Running a Web Server using Python in Spyder
- Input Output in Python
- Starting Plots in Python
- For loop in Python
- Installing and running DJango
- Linear and Polynomial Regression using Python
- Some Problems on Lists
- Creating Classes in Python
- Inheritance in Python
- How to implement Multiple Inheritance in Python
- Using Flask-1
- Lists in Python
- Lists in Python — 1
- Sorting of Lists
- Lists in Python — Insertions
- How to make a list in python and save it in files ……
- Importing Excel Files in Python
- Mailer in Python
- How to add a simple view to a Django site?
- Python Print and I/O questions
- Print answers
- If then else in Python
- Summation of Series
- Python Print Assignment
- Loop Assignments
- Pickle in Python
- Dictionary in Python-1
- Dictionaries in Python-2
- Python OOPs Assignment
- Operator Overloading in Python
- Equation Solving using Numpy
- Starting Numpy
- Passing Data from a Python Controller to a HTML Page
- Downloading Data from a URL in Python
- Sending a Dictionary to a HTML Template and displaying it using Flask.
- Web Scraping Library-1
- Downloading data from a remote website and displaying data in an html page
- Arithmetic Operations in Python — Assignment
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