简体   繁体   中英

Create file OLS in Python Statsmodels

I dont have much knowledge in Python but I have to crack this for an assessment completion,

Question:
Run the following code to load the required libraries and create the data set to fit the model.

from sklearn.datasets import load_boston
import pandas as pd 

boston = load_boston()
dataset = pd.DataFrame(boston.data, columns=boston.feature_names)
dataset['target'] = boston.target

print(dataset.head())

I have to perform the following steps to complete this scenario.

For the boston dataset loaded in the above code snippet, perform linear regression.
Use the target variable as the dependent variable.
Use the RM variable as the independent variable.
Fit a single linear regression model using statsmodels package in python.
Import statsmodels packages appropriately in your code.
Upon fitting the model, Identify the coefficients.
Finally print the model summary in your code.
You can write your code using vim app.py .

Press i for insert mode.
Press esc and then :wq to save and quit the editor.

Please help me to understand how to get this completed. Your valuable comments are much appreciated

Thanks in Advance

from sklearn.datasets import load_boston
import pandas as pd 

boston = load_boston()
dataset = pd.DataFrame(boston.data, columns=boston.feature_names)
dataset['target'] = boston.target

print(dataset.head())

import statsmodels.api as sm
import statsmodels.formula.api as smf

X = dataset["RM"]
y = dataset['target']

X = sm.add_constant(X)

model = smf.OLS(y,X).fit()

predictions = model.predict(X)

print(model.summary())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM