• Save
  • Run All Cells
  • Clear All Output
  • Runtime
  • Download
  • Difficulty Rating

Loading Runtime

The following code cell loads two lists of data:

  • experience: A float value representing each employee's current years of experience rounded to one decimal place.
  • salary: An integer value representing each employee's current salary in dollars.

There are a total of 30 observations. (This is fake data.)

Make sure to run this code cell –in order to load the data into the notebook's memory– before continuing.

We will specify the Simple Linear Regression equation for the line of best fit as follows:

ŷ = β0 + β1x + ε

Where:

  • ŷ represents the model's predicted salary values
  • β0 is the model's intercept coefficient
  • β1 is the model's slope coefficient
  • x represents the independent variable experience
  • ε is the error term

The Challenge:

Write a function called simple_regression that takes in a list of experience values and a list of salary values as arguments.

Calculate the intercept and slope coefficients of the line of best fit (β0 and β1), and return these two coefficients in this specific order within a list: [β0, β1]


Example:

This example returns the coefficients of a regression line fit to the first five observations (rows) of the provided dataframe.

simple_regression([1.2, 1.4, 1.6, 2.1, 2.3], [39344, 46206, 37732, 43526, 39892])

Returns: [41529.041474654376, -109.90783410138302]

In order to test your code, five observations (rows) will be selected at random from the dataframe and will their corresponding variables will be passed to your function as lists.

Reset Code