Get startedGet started for free

Fit and predict for regression

Now you have seen how linear regression works, your task is to create a multiple linear regression model using all of the features in the sales_df dataset, which has been preloaded for you. As a reminder, here are the first two rows:

     tv        radio      social_media    sales
1    13000.0   9237.76    2409.57         46677.90
2    41000.0   15886.45   2913.41         150177.83

You will then use this model to predict sales based on the values of the test features.

LinearRegression and train_test_split have been preloaded for you from their respective modules.

This exercise is part of the course

Supervised Learning with scikit-learn

View Course

Exercise instructions

  • Create X, an array containing values of all features in sales_df, and y, containing all values from the "sales" column.
  • Instantiate a linear regression model.
  • Fit the model to the training data.
  • Create y_pred, making predictions for sales using the test features.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create X and y arrays
X = sales_df.____("____", axis=____).____
y = sales_df["____"].____

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Instantiate the model
reg = ____

# Fit the model to the data
____

# Make predictions
y_pred = reg.____(____)
print("Predictions: {}, Actual Values: {}".format(y_pred[:2], y_test[:2]))
Edit and Run Code