Linear Regression
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, y)
print(lin_reg.intercept_, lin_reg.coef_)
lin_reg.predict(X_new)
Polynomial Regression
Transform to polynomial feature
from sklearn.preprocessing import PolynomialFeatures
poly_features = PolynomialFeatures(degree =2, include_bias=False)
X_poly = poly_features.fit_transform(X) --- X_poly will have two terms - degree =1 and degree 2
Now use Linear Regression
lin_reg = LinearRegression()
lin_reg.fit(X_poly, y)
print(lin_reg.intercept_, lin_reg.coef_)
Training and test error
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
def plot_learning_curves(model, x, y):
X_train, X_val, Y_train, y_val = train_test_split(x, y, test_size=0.2)
train_error, val_errors = [], []
for m in range(1, len(X_train)):
model.fit(X_train[:m], y_train[:m])
y_train_predict = model.predict(X_train[:m]) -- no of sampling of training is changing from 1 to len(X_train)
y_val_predict = model.predict(X_val) -- always taken for all validation samples.
train_errors.append(mean_squared_error(y_train[:m], y_val_predict[:m]))
val_errors.append(mean_squared_error(y_val, y_val_predict))
plt.plot(np.sqrt(train_errors, "r-+", linewidth=2, label="train"))
plt.plot(np.sqrt(val_errors, "b-+", linewidth=2, label="validation"))
Using Pipeline
from sklearn.pipeline import Pipeline
polynomial_regression = Pipeline ([
("poly features", PolynomialFeatures(degree=10, include_bias=False)),
("lin reg", LinearRegression())
])
plot_learning_curve(polynomial_regression, X, y)
Gradient Regression
Batch Gradient Descent
Stochastic Gradient Descent
Mini Batch Gradient Descent
Ridge Regression
from sklearn.linear_model import Ridge
ridge_reg = Ridge(alpha=1, solver="cholesky")
ridge_reg.fit(X,y)
ridge_reg.predict([1.5])
Lasso Regression ( Lest Absolute Shrinkage and Selection Operator Regression )
from sklearn.linear_model import Lasso
lasso_reg = Lasso(alpha = 0.1)
lasso_reg.fit(X, y)
lasso_reg.predict([1.5])
Elastic net
from sklearn.linear_model import ElasticNet
elastic_net = ElasticNet(alpha=0.1, l1_ratio=0.5)
elastic_net.fit(X,y)
elastic_net.predict([1.5])
0 comments:
Post a Comment