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

2 Likes

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

temperature = np.array(range(60, 100, 2))
print(temperature)
# [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)
print(temperature)
'''
[[60]
 [62]
 [64]
 [66]
 [68]
 [70]
 [72]
 [74]
 [76]
 [78]
 [80]
 [82]
 [84]
 [86]
 [88]
 [90]
 [92]
 [94]
 [96]
 [98]]
'''

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.

2 Likes

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

2 Likes

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

2 Likes

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.

image|690x386

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

I have a question about the plotted line. There seems to be no inherent difference between:

plt.plot(temperature, sales_predict, ‘r’)

and

plt.plot(temperature, sales, ‘o’)

sales_predict is also simply an array of integers in that specified range, so how come the one I plotted turns out to be a line while the original plot was a scatter of discrete points?