<jupyter_start><jupyter_text>Intermediate Linear Regression Practice Use a Linear Regression model to get the lowest RMSE possible on the following dataset:[Dataset Folder](https://github.com/ryanleeallred/datasets/tree/master/Ames%20Housing%20Data)[Raw CSV](https://raw.githubusercontent.com/ryanleeallred/datasets/master/Ames%20Housing%20Data/train.csv) Your model must include (at least):- A log-transformed y variable- Two polynomial features- One interaction feature- 10 other engineered featuresWhat is the lowest Root-Mean-Squared Error that you are able to obtain? Share your best RMSEs in Slack!Notes:There may be some data cleaning that you need to do on some features of this dataset. Linear Regression will only accept numeric values and will not acceptNote* There may not be a clear candidate for an interaction term in this dataset. Include one anyway, sometimes it's a good practice for predictive modeling feature engineering in general.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
##### Your Code Here #####

df = pd.read_csv('https://raw.githubusercontent.com/ryanleeallred/datasets/master/Ames%20Housing%20Data/train.csv')
df.head()
df.columns
df = df.drop(columns=['Id'])

df.head()
df.select_dtypes(include='number').columns
df['ln_price'] = np.log(df['SalePrice'])
target = 'ln_price'
numeric_columns = df.select_dtypes(include='number').columns
for feature in numeric_columns.drop(target):
    sns.scatterplot(x=feature, y=target, data=df, alpha=0.1)
    plt.show()
target = 'SalePrice'
features = [sns.regplot(heights, weights, color='blue').set_title('Height by Weight');]
y = df[target]
X = df[features]

def run_linear_model(X, y):
    # Split into test and train data
    X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.80, test_size=0.20, random_state=42)

    # Fit model using train data
    model = LinearRegression()
    model.fit(X_train, y_train)

    # Make predictions using test features
    y_pred = model.predict(X_test)

    # Compare predictions to test target
    rmse = (np.sqrt(mean_squared_error(y_test, y_pred)))
    r2 = r2_score(y_test, y_pred)

    print('Root Mean Squared Error', rmse)
    print('R^2 Score', r2)
    print('Intercept', model.intercept_)
    coefficients = pd.Series(model.coef_, X_train.columns)
    print(coefficients.to_string())
    
run_linear_model(X, y)
target = 'SalePrice'
features = ['OverallQual', '1stFlrSF']
y = df[target]
X = df[features]

run_linear_model(X, y)
target = 'ln_price'
features = ['OverallQual', '1stFlrSF']
y = df[target]
X = df[features]

run_linear_model(X, y)
from sklearn.preprocessing import PolynomialFeatures
import warnings
X = df[['YearBuilt', 'SalePrice']]
y = df[target]
interaction = PolynomialFeatures(degree=3, include_bias=False, interaction_only=True)
X_inter = interaction.fit_transform(X)
# Create linear regression
regr = LinearRegression()

# Fit the linear regression
model = regr.fit(X_inter, y)

run_linear_model(X, y)<jupyter_output>Root Mean Squared Error 7.973482426540466e-11
R^2 Score 1.0
Intercept 2.9103830456733704e-10
YearBuilt   -6.120213e-14
SalePrice    1.000000e+00
{'fraction_non_alphanumeric': 0.10523186682520809, 'fraction_numerical': 0.0267538644470868, 'mean_word_length': 5.546692607003891, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '451627', 'license': 'MIT', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1087, 'n_words': 308, 'path': 'module2-polynomial-log-linear-regression/polynomial-log-linear-regression-assignment.ipynb', 'repo_name': 'standroidbeta/DS-Unit-2-Sprint-2-Regression', 'size': '3364'}