This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create_polynomial_regression_model(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.preprocessing import PolynomialFeatures | |
def create_polynomial_regression_model(degree): | |
"Creates a polynomial regression model for the given degree" | |
poly_features = PolynomialFeatures(degree=degree) | |
# transforms the existing features to higher degree features. | |
X_train_poly = poly_features.fit_transform(X_train) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# model evaluation for training set | |
y_train_predict = lin_model.predict(X_train) | |
rmse = (np.sqrt(mean_squared_error(Y_train, y_train_predict))) | |
r2 = r2_score(Y_train, y_train_predict) | |
print("The model performance for training set") | |
print("--------------------------------------") | |
print('RMSE is {}'.format(rmse)) | |
print('R2 score is {}'.format(r2)) | |
print("\n") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
correlation_matrix = boston.corr().round(2) | |
# annot = True to print the values inside the square | |
sns.heatmap(data=correlation_matrix, annot=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boston.isnull().sum() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.linear_model import LinearRegression | |
from sklearn.metrics import mean_squared_error | |
lin_model = LinearRegression() | |
lin_model.fit(X_train, Y_train) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.model_selection import train_test_split | |
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state=5) | |
print(X_train.shape) | |
print(X_test.shape) | |
print(Y_train.shape) | |
print(Y_test.shape) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns = ['LSTAT','RM']) | |
Y = boston['MEDV'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plt.figure(figsize=(20, 5)) | |
features = ['LSTAT', 'RM'] | |
target = boston['MEDV'] | |
for i, col in enumerate(features): | |
plt.subplot(1, len(features) , i+1) | |
x = boston[col] | |
y = target | |
plt.scatter(x, y, marker='o') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sns.set(rc={'figure.figsize':(11.7,8.27)}) | |
sns.distplot(boston['MEDV'], bins=30) | |
plt.show() |
NewerOlder