application_vnd.jupyter.stderr_output_1.png
from sklearn.linear_model import LinearRegression

from sklearn.metrics import r2_score,mean_squared_error

from sklearn.model_selection import KFold

import pandas as pd

boston = pd.read_pickle('https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-ML240EN-SkillsNetwork/labs/data/boston_housing_clean.pickle')

boston.keys()

boston_df = boston['dataframe']

X = boston_df.drop('MEDV', axis=1)

y = boston_df['MEDV']

X.shape

y.shape

kf = KFold(n_splits=3, shuffle=True)

type(kf.split(X))

LR = LinearRegression()
scores = []
for train_index, test_index in kf.split(X):
    X_train, X_test, y_train, y_test = (X.iloc[train_index, :], X.iloc[test_index, :], y[train_index], y[test_index])
    print('X_train:', X.iloc[train_index, :], 'y_train:', y[train_index], 'X_test:', X.iloc[test_index, :], 'y_test:', y[test_index])
    LR.fit(X_train, y_train)
    y_pred = LR.predict(X_test)
    score = r2_score(y_test.values, y_pred)
    scores.append(score)
scores