1st Code

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

17 :

### Question: Create a linear regression model for "sample_data" table


### such that "GDP per capita" column is the X data and
### "Life satisfaction" columns is the Y data
### print theta zero and theta one values

from sklearn import linear_model


lin1 = linear_model.LinearRegression()
Xsample = np.c_[sample_data["GDP per capita"]]
ysample = np.c_[sample_data["Life satisfaction"]]
lin1.fit(Xsample, ysample)
t0, t1 = lin1.intercept_[0], lin1.coef_[0][0]
print(t0, t1)

26 :

### Question: Create a linear regression model with polynomial features


### of the degree 15 for Xfull, yfull data
### then predict the resulting curve using X data

from sklearn import preprocessing


from sklearn import pipeline

poly = preprocessing.PolynomialFeatures(degree=15, include_bias=False)


scaler = preprocessing.StandardScaler()
lin_reg2 = linear_model.LinearRegression()

pipeline_reg = pipeline.Pipeline([('poly', poly), ('scal', scaler), ('lin',


lin_reg2)])
pipeline_reg.fit(Xfull, yfull)
curve = pipeline_reg.predict(X[:, np.newaxis])
plt.plot(X, curve)
plt.show()

27:
### Question: Create a linear regression model with l2 regularization with
alpha=0.5
### with polynomial features of the degree 15 for Xfull, yfull data
### then predict the resulting curve using X data

from sklearn import preprocessing


from sklearn import pipeline

poly = preprocessing.PolynomialFeatures(degree=15, include_bias=False)


scaler = preprocessing.StandardScaler()
lin_reg2 = linear_model.LinearRegression()

# Linear least squares with l2 regularization.


lin_reg2 = linear_model.Ridge(alpha=0.5)

pipeline_reg = pipeline.Pipeline([('poly', poly), ('scal', scaler), ('lin',


lin_reg2)])
pipeline_reg.fit(Xfull, yfull)
curve = pipeline_reg.predict(X[:, np.newaxis])
plt.plot(X, curve)
plt.show()

You might also like