FAQ: Linear Regression - Scikit-Learn

This community-built FAQ covers the “Scikit-Learn” exercise from the lesson “Linear Regression”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Machine Learning

FAQs on the exercise Scikit-Learn

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

There are so many modes and data_types which are to learn in CodeAcademy. Are there a comparative table of all the models, which could help us to sellect the right method on the approptiate data. E.g. with columns:

method/model | method_description | code_example | numeric/categorical data | min/max quantity of variables | advantages/disadvantages | alternative method

What does .reshape do in Linear Regression https://www.codecademy.com/paths/machine-learning/tracks/regression-skill-path/modules/linear-regression-skill-path/lessons/linear-regression/exercises/sklearn. This has not been explained as far as I can see


temperature.reshape(-1, 1) returns a 2D array which is the transposed temperature.

temperature = np.array(range(60, 100, 2))
# [60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98]

temperature = temperature.reshape(-1, 1)

The reason why we need to do this is because the .fit() and .predict() methods expect a 2D array. I agree with you that there’s not enough explanation on this point.

1 Like

I understand (-1, 1), the 1 means the number of column, but how about the -1? the minus row?


When reshaping an array, the returned array must have the same number of elements as the original array. By using -1, even if you don’t know the number of elements in the original array, you can make the returned array to have the same number of elements. (Only one shape dimension can be -1.) For example, if the original array has n elements, .reshape(-1, 1) is equivalent to .reshape(n, 1).


Using Skikit-learn’s linear regression model, I tried plotting the line I got using list comprehension

y_predictions = [element * m + b for element in X]

Then I got this error which seems like I need to encapsulate each point in its own array? How do I go about doing that? I imagine it has to do with getting the predicted sales without using list comprehension.


I needed to use the .predict function

why we did not .reshape(-1,1) both in y = sales???thanks for your answer:)

why we did not .reshape(-1,1) both in y = sales???thanks for your answer:)!!!

.fit() method of LinearRegression() takes two arguments X and y, and a 2D array is expected for X. y does not have to be a 2D array.

1 Like

thanks you for replying:)