text_plain_output_1.png
from sklearn.linear_model import LinearRegression

from sklearn.linear_model import LinearRegression

from sklearn.linear_model import LinearRegression

from sklearn.linear_model import Ridge

from sklearn.metrics import r2_score

from sklearn.metrics import r2_score

from sklearn.model_selection import train_test_split

from sklearn.pipeline import Pipeline

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df = pd.read_csv('/kaggle/input/housesalesprediction/kc_house_data.csv')

df.corr()['price'].sort_values()

X = df[['long']]
Y = df['price']
lm = LinearRegression()
lm.fit(X, Y)
lm.score(X, Y)

from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
X = df[['sqft_living']]
y = df['price']
model = LinearRegression()
model.fit(X, y)
r2 = r2_score(y, model.predict(X))

features = ['floors', 'waterfront', 'lat', 'bedrooms', 'sqft_basement', 'view', 'bathrooms', 'sqft_living15', 'sqft_above', 'grade', 'sqft_living']

from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
X = df[features]
y = df['price']
model = LinearRegression()
model.fit(X, y)
r2 = r2_score(y, model.predict(X))

features = ['floors', 'waterfront', 'lat', 'bedrooms', 'sqft_basement', 'view', 'bathrooms', 'sqft_living15', 'sqft_above', 'grade', 'sqft_living']
model = Pipeline([('regressor', LinearRegression())])
X = df[features]
y = df['price']
model.fit(X, y)
r2 = r2_score(y, model.predict(X))

features = ['floors', 'waterfront', 'lat', 'bedrooms', 'sqft_basement', 'view', 'bathrooms', 'sqft_living15', 'sqft_above', 'grade', 'sqft_living']
X = df[features]
Y = df['price']
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.15, random_state=1)

ridge = Ridge(alpha=0.1)
ridge.fit(x_train, y_train)
y_pred = ridge.predict(x_test)
r2 = r2_score(y_test, y_pred)
print('R^2:', r2)