charlie s
charlie s

Reputation: 93

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)

Upvotes: 0

Views: 554

Answers (2)

tetris programming
tetris programming

Reputation: 1496

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()

Upvotes: 1

NaWeeD
NaWeeD

Reputation: 609

The issue is with the place_y function, which is intended to predict y based on x, but you are using it to predict x based on y. The current implementation calculates y = slope * x + intercept, which doesn't return the correct result when trying to predict x from y. To predict x from y, you need to solve the equation y = slope * x + intercept for x: x = (y - intercept) / slope. Update the predict_value_y function in the line you calculate predicted:

predicted = (predict_value - intercept) / slope

Upvotes: 1

Related Questions