import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score, mean_squared_error
# Data load
df = pd.read_csv('./data/xxx.csv')
# Target data separation
y = df['y']
X = df.drop('y', axis = 1)
# Split train : test data set = 7:3 and set random seed
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size = 0.3,
random_state = 42)
# Define model structure
lr = LinearRegression()
# Train model
lr.fit(X_train, y_train)
# Model prediction
lr_pred = lr.predict(X_test)
# Confirm coefficient
lr.coef_
# Error metric
print("r2 score:", r2_score(y_test, lr_pred))
print("RMSE:", np.sqrt(mean_squared_error(y_test, lr_pred)))
# Plot
plt.scatter(lr_pred, y_test)
No comments:
Post a Comment