- Linear Regression
from sklearn.linear_model import LinearRegression
linear = LinearRegression()
linear.fit(x, y)
from sklearn.metrics import mean_squared_error
yp = linear.predict(xt)
mse = mean_squared_error(yt, yp)
rmse=np.sqrt(mse)
rmse
- Polynomial Regression
from sklearn.preprocessing import PolynomialFeatures
Polyno_transformer = PolynomialFeatures(degree=2)
x_poly = Polyno_transformer.fit_transform(x)
from sklearn.pipeline import Pipeline
polynomial_regression = Pipeline([
("Polyno_transformer", PolynomialFeatures(degree=3, include_bias=False)),
("linear", LinearRegression()),
])
poly = polynomial_regression.fit(x, y)
from sklearn.metrics import mean_squared_error
yp = poly.predict(xt)
mse = mean_squared_error(yt, yp)
rmse=np.sqrt(mse)
rmse
- Ridge Regression
from sklearn.linear_model import Ridge
ridge = Ridge(alpha=1, solver="cholesky", random_state=42)
ridge.fit(x, y)
from sklearn.metrics import mean_squared_error
yp = ridge.predict(xt)
mse = mean_squared_error(yt, yp)
rmse=np.sqrt(mse)
rmse
- Logistic regression
inland = housing['ocean_proximity'] == 'INLAND'
housing['inland_or_not'] = inland
from sklearn.linear_model import LogisticRegression
logistic = LogisticRegression(solver="lbfgs", random_state=42)
logistic.fit(x, y)
yp = logistic.predict(xt)
from sklearn.metrics import plot_confusion_matrix
plot_confusion_matrix(logistic, xt, yt)
- Softmax(multinomial) logistic regression
housing["price_range"] = pd.cut(housing["median_house_value"],
bins=[0, 150000, 300001, np.inf],
labels=[0, 1, 2])
softmax = LogisticRegression(multi_class="multinomial",solver="lbfgs", C=10, random_state=42)
softmax.fit(x, y)
yp = softmax.predict(xt)
from sklearn.metrics import plot_confusion_matrix
plot_confusion_matrix(softmax, xt, yt)