# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

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

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
# Solving this problem as my first practice problem 
#Steps to solve any machine learning problem 
# 1: Load dataset
# 2: EDA 
# 3: Build linear model 
# 4: Evaluate the model 
# 5: Predict with some data 
import numpy as np
import pandas as pd 
import matplotlib as mt 
import seaborn as sns 

%matplotlib inline 
train_data = pd.read_csv("/kaggle/input/random-linear-regression/train.csv")
test_data = pd.read_csv("/kaggle/input/random-linear-regression/test.csv")
# EDA 
# 1: check the data type of the data 

train_data.info()
train_data.head()
# check the relation ship among the data. If we can apply linear regression 
sns.pairplot(train_data)
"""
We can see X and Y are positive co related. Lets check if what is the co relation among the two data point 

"""
train_data.corr()
from sklearn.linear_model import  LinearRegression
lg = LinearRegression()

# while training model fit failed due to null value in the data. Check if there is any null
train_data.isnull().values.any()
train_data = train_data.dropna()
lg.fit(train_data[['x']],train_data.y)
# check the coeff and y intercept 
lg.intercept_
lg.coef_
X_test = test_data[['x']]
Y_test = test_data.y
Y_predict = lg.predict(X_test);
# not lets check the r^2 value  
from sklearn.metrics import r2_score
r2_score(Y_test,Y_predict)
"""
Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a  score of 0.0. :) 
"""