Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Predicting y and x values using linear regressions

I am making a program to predict the x and y value using linear regression.
I can predict y from x. However, when trying to predict x given y i do not get the intended result. Output:

Given (x) predict (y): 
x = 10
85.59308314937454
Given (y) predict (x): 
y = 85
-45.75349521707133

code:

def place_y(x, slope, intercept):
    return slope * x + intercept


def predict_value_x():
    """Using the line of regression a value can be predicted based on a given value.
    i.e. Predict the speed of a car (y) given it is (x) years old"""
    from scipy import stats
    age_x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]  # population
    speed_y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]  # population

    slope, intercept, r, p, std_err = stats.linregress(age_x, speed_y)  # get stats values

    predict_value = int(input("Given (x) predict (y): \nx = "))  # age of car(x)
    predicted = place_y(predict_value, slope, intercept)  # the speed of car given x
    print(predicted)


predict_value_x()


def predict_value_y():
    """Using the line of regression a value can be predicted based on a given value.
    i.e. Predict the age of a car (x) given its speed (y)"""
    from scipy import stats
    age_x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]  # population
    speed_y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]  # population

    slope, intercept, r, p, std_err = stats.linregress(age_x, speed_y)  # get stats values

    predict_value = int(input("Given (y) predict (x): \ny = "))  # age of car(x)
    predicted = place_y(predict_value, slope, intercept)  # the speed of car given x
    print(predicted)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

y=ax+b -> x=(y-b)/a

The problem is that you try to solve by y twice.
You need an aditional function that solves by y:

def place_x(y, slope, intercept):
    return  (y - intercept)/slope

and replace placey in your predict_value_y function:

predicted = place_x(predict_value, slope, intercept) 

the entire code could look like:

def place_y(x, slope, intercept):
    return slope * x + intercept

def place_x(y, slope, intercept):
    return  (y - intercept)/slope


def predict_value_x():
    """Using the line of regression a value can be predicted based on a given value.
    i.e. Predict the speed of a car (y) given it is (x) years old"""
    from scipy import stats
    age_x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]  # population
    speed_y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]  # population

    slope, intercept, r, p, std_err = stats.linregress(age_x, speed_y)  # get stats values

    predict_value = int(input("Given (x) predict (y): \nx = "))  # age of car(x)
    predicted = place_y(predict_value, slope, intercept)  # the speed of car given x
    print(predicted)


predict_value_x()


def predict_value_y():
    """Using the line of regression a value can be predicted based on a given value.
    i.e. Predict the age of a car (x) given its speed (y)"""
    from scipy import stats
    age_x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]  # population
    speed_y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]  # population

    slope, intercept, r, p, std_err = stats.linregress(age_x, speed_y)  # get stats values

    predict_value = int(input("Given (y) predict (x): \ny = "))  # age of car(x)
    predicted = place_x(predict_value, slope, intercept)  # the speed of car given x
    print(predicted)

predict_value_y()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading