Simple question I am not getting output as expected.(Linear regression)

Question:

I am new to programming. Currently, I am learning machine learning from this video.

This is related to linear regression

CODE:

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from sklearn import linear_model

df=pd.read_csv('homeprices.csv')


reg = linear_model.LinearRegression()

Problem 1

reg.fit(df[['area']],df.price)

Expected output should be

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
         normalize=False)

My output:

LinearRegression()

Problem 2

reg.predict(3300)

It’s giving error when I use "()" but when I use 2D array "[[]]" It is giving me correct output, But I want to know why It is not giving me output(as shown in video) when I use the only parenthesis.

Asked By: Ekta

||

Answers:

  • Problem1:
    it depends on the default parameters which you might have changed it before or any other reason which has changed it, but you can easily set your desired parameters while you are initializing the Linear classifier in this way:

    reg = linear_model.LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)

  • Problem 2:
    reg.predict(3300) it’s not correct to pass the parameter to Pandas in that way and you can see that the instructor has also corrected his answer to the reg.predict([3300]) in the description of the youtube Post

Answered By: mossishahi

try this but you should define your variable and fit them to get desired output

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression()

df=pd.read_csv('homeprices.csv')
reg =LinearRegression()
Answered By: Ashish kumar panda

Problem 1 :

This is how the fitted model outputs are shown in the newest version of sklearn, i.e., 0.23. The parameters are the same, but they are not shown in the output.

You can use reg.get_params() to view the parameters.

Problem 2 :

Newer versions of Scikit-learn require 2D inputs for the predict function and we can make 3300 2D by [[3300]]:

reg.predict( [[3300]] )

Answered By: Salim Mzoughi