Python Math: Describe linear regression
Python Math: Exercise-61 with Solution
Write a Python program to describe linear regression .
Note : A linear regression line has an equation of the form Y = a + bX, where X is the explanatory variable and Y is the dependent variable. The slope of the line is b, and a is the intercept (the value of y when x = 0).
Sample Solution:-
Python Code:
#https://gist.github.com/cartr/6513044
# Define the data
data = set()
count = int(input("Enter the number of data points: "))
for i in range(count):
x=float(input("X"+str(i+1)+": "))
y=float(input("Y"+str(i+1)+": "))
data.add((x,y))
# Find the average x and y
avgx = 0.0
avgy = 0.0
for i in data:
avgx += i[0]/len(data)
avgy += i[1]/len(data)
# Find the sums
totalxx = 0
totalxy = 0
for i in data:
totalxx += (i[0]-avgx)**2
totalxy += (i[0]-avgx)*(i[1]-avgy)
# Slope/intercept form
m = totalxy/totalxx
b = avgy-m*avgx
print("Best fit line:")
print("y = "+str(m)+"x + "+str(b))
x = float(input("Enter a value to calculate: "))
print("y = "+str(m*x+b))
Sample Output:
Enter the number of data points: 2 X1: 1 Y1: 2 X2: 3 Y2: 4 Best fit line: y = 1.0x + 1.0 Enter a value to calculate: 10 y = 11.0
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to parse math formulas and put parentheses around multiplication and division.
Next: Write a Python program to calculate a grid of hexagon coordinates of the given radius given lower-left and upper-right coordinates. The function will return a list of lists containing 6 tuples of x, y point coordinates. These can be used to construct valid regular hexagonal polygons.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework