OLS Regression hands on code ...help

Executed this piece of code as suggested , but unable to complete the handson … any suggestion please?

from sklearn.datasets
import load_boston
import pandas as pd
boston = load_boston()
dataset = pd.DataFrame(data=boston.data, columns=boston.feature_names)
dataset[‘target’] = boston.target
print(dataset.head())

X = dataset[“RM”]
Y = dataset[“target”]

import statsmodels.api as sm
X= sm.add_constant(X)
statsModel =sm.OLS(Y,X)
fittedModel = statsModel.fit()

print (fittedModel.summary())

r_squared = fittedModel.rsquared
with open(“output.txt”, “w”) as text_file:
text_file.write(“rsquared= %f\n” % r_squared)

What exact issue are you having?

1 Like

Thank you for the response …I execute this code on Jupyter note … and return to Fresco play to validate … the code is not accepted

Jupyter note question is as below :-

So you’ve run this code, but you say you can’t “compete” it, and it “doesn’t work.” It’s not clear what that means. Do get any errors?

1 Like

Yes ,I have run the code I got the output as well printed on the text file I didn’t get any error message …but unable to complete the hands on … may be the handson questions is expecting something different … thats why I provide the link to the question as well … Will you be able to check and validate please?

Hi pylang

i am getting an error

###Start code here
r_squared = 0.484
###End code(approx 1 line)
with open(“output.txt”, “w”) as text_file:
text_file.write(“rsquared= %f\n” % r_squared):

The error is -

File “”, line 5
text_file.write(“rsquared= %f\n” % r_squared):
^
IndentationError: expected an indented block

Can you please help ?

Indentation is really key in Python. Try pasting this block and see what happens:

with open("output.txt", "w") as text_file:
    text_file.write("rsquared= ", r_squared):

Thanks pylang . This arises another error

###Start code here
X = dataset[‘RM’]
Y = dataset[‘target’]
###End code(approx 2 lines)

  • import statsmodel.api as sm
    ###Start code here
    X = sm.add_constant(X)
    ###End code(approx 1 line)

ERROR WHICH I AM GETTING —

NameError Traceback (most recent call last)
in
1 ###Start code here
----> 2 X = sm.add_constant(X)
3 ###End code(approx 1 line)

NameError: name ‘sm’ is not defined

Please help

I don’t expect that error if entered correctly. Perhaps you are copy/pasting entire code and running into problems. Try running the code after typing in each line separately. You’ll start to see where the errors are coming from.

A NameError means you have tried to use the value of a variable before
you have defined the variable and given it a value.

That is a sign that you need to read through your code and find out why
the variable hasn’t got a value, and fix the code so it does.

Hi Steven

Thanks for your reply , but i have defined it before using . Please see below

import statsmodel.api as sm

X= sm.add_constant(X)

But then i get this error


NameError Traceback (most recent call last)
in
----> 1 X= sm.add_constant(X)

NameError: name ‘sm’ is not defined

Thanks pylang for your reply . But i am typing each sentence
I am actually lost and unable to find out why it is throwing me an error

my code snippet is as below :-1:

###Start code here
X = dataset[“RM”]
Y = dataset[“target”]
###End code(approx 2 lines)

import statsmodel.api as sm

X= sm.add_constant(X)

then the error pops up


NameError Traceback (most recent call last)
in
----> 1 X= sm.add_constant(X)

NameError: name ‘sm’ is not defined


NameError Traceback (most recent call last)
in
----> 1 X= sm.add_constant(X)

NameError: name ‘sm’ is not defined

Thnaks much pylang . I was able to fix the previous error with your guidance , much appreciated

can you guide me with below
###Start code here
r_squared = 0.484
###End code(approx 1 line)
with open(“output.txt”, “w”) as text_file:
text_file.write("rsquared= ", r_squared)

______--------- I am getting a typeerror

TypeError Traceback (most recent call last)
in
3 ###End code(approx 1 line)
4 with open(“output.txt”, “w”) as text_file:
----> 5 text_file.write("rsquared= ", r_squared)

TypeError: write() takes exactly one argument (2 given)

Are you getting an error from the “import statsmodel.api as sm” first?

Otherwise, I cannot see any possible way you would get that NameError
from the code you show. Not unless there is extra code that you haven’t
shown us.

Or perhaps the error actually says

NameError: name ‘X’ is not defined

that is possible too.

Writing to a file is not like print. With print you can give any number
of arguments, and it will automatically convert each one to a string and
print them all on one line:

print("rsquared= ", r_squared)

will print “rsquared= 0.484”. But for writing to a file, you have to
generate a single string. Here are some ways to do it:

# C-style % string interpolation
s = "rsquared= %s" % r_squared

# Like above but control the number of decimal places
s = "rsquared= %.8f" % r_squared  # 8 decimal places

# Format method:
s = "rsquared= {}".format(r_squared)

# F-string
s = f"rsquared= {r_squared}"

then write to the file:

text_file.write(s + "\n")

The “\n” concatenates an end-of-line character (newline) after the
string. If you are on Windows, Python will take care to convert that to
a CR+NL pair automatically.

1 Like

Thanks much Steven , I was able to complete the hands on

My apologies Sameer. My suggestion had an error.

with open("output.txt", "w") as text_file:
    text_file.write("rsquared= ", r_squared):

Is not correct. Try this instead if you are using Python 3:

    text_file.write(f"rsquared= {r_squared}"):

Please let us know how you resolved the problem. We’re happy you got it solved :slight_smile: