"""
#  **Importing Libraries**
"""
import pandas as pd  
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

data = pd.read_csv('../input/random-linear-regression/train.csv')
data
data.info()
"""
> Here 1 missing data is present in y column.Therefore, this data requires some `Preprocessing`.
"""
data.dropna(inplace=True)
data.info()
"""
# Data Visualization
"""
plt.subplots(figsize=(10, 6))
plt.hist(data['x'],bins= 15)
plt.show()
plt.subplots(figsize=(10, 6))
plt.hist(data['y'],bins= 15)
plt.show()
np.corrcoef(data['x'],data['y'])[0,1]
"""
> Through Data Visualization, We cannot deduct any correlation between `X` and `Y`.
Using correlation it shows that data `99%` correlates.
Therefore, It is perfect for `Linear Regression`.
"""
plt.subplots(figsize=(10, 6))
plt.scatter(data['x'],data['y'],color = "blue", edgecolors = "white", linewidths = 0.1, alpha = 0.7)
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
"""
> The plot is `strong`,`positive` and `linear`.
"""
"""
# Linear Regression Model
"""
X = data['x'].values.reshape(-1, 1)
Y = data['y'].values.reshape(-1, 1)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,Y, test_size=0.33, random_state=143)
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train,y_train)
y_predict=model.predict(X_test)
y_predict
plt.subplots(figsize=(10, 6))
plt.scatter(X_test,y_test)
plt.plot(X_test,y_predict,c='r')
from sklearn.metrics import r2_score
score=r2_score(y_test,y_predict)
score