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

1 Like

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.

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

1 Like

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

1 Like