# 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 matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score

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
"""
# Loading the data
"""
df=pd.read_csv('/kaggle/input/csv-file-for-salary-prediction/Linear_Regression_Pr.csv')
df.head(8)
df.dtypes
df.shape
"""
# Data preprocessing

"""
"""
## Replaceing null values of experience with 0
"""
df['experience'].replace(np.NaN,0,inplace=True)
df.isnull().sum()
"""
## Replacing the Null values of the column test scorewith mean value
"""
mn1=df['test_score(out of 10)'].mean()
mn1
df['test_score(out of 10)'].replace(np.NaN,mn1,inplace=True)
df.head(8)
df.isnull().sum()
"""
# Display a scatter plot between experience and Salary
"""
"""
## as the given salary is in character format, we have to change it to numbers
"""
"""
## wordtodigits library helps in creating numbers from written numbers in character
"""
!pip install wordtodigits
import wordtodigits as wd
"""
## simply adding the values from the experience column to an object
"""
w=df['experience']
w
"""
## now, it should be converted to string, as the wordtodigits only takes string as an input
"""
w=w.to_string()
"""
## using wordtodigit's convert function to convert numbers written in characters to numbers in int
"""
w=wd.convert(w)
"""
## splitting the output using .split
"""
w=w.split(' ')
"""
## the output contains the index numbers of the data which is no use for us
"""
w
"""
## selecting only the data and removing index which is not needed
"""
w=w[1:17:2]
"""
## final results
"""
w
"""
## adding this new converted data to the dataframe
"""
df['experience']=pd.Series(w)
df['experience']=df['experience'].astype(int)
df.dtypes
df.head(8)
"""
# Scatter plot between experience and Salary
"""
sns.scatterplot(x=df['experience'],y=df['salary($)'])
"""
# A scatter plot between test scoreand Salary
"""
sns.scatterplot(x=df['test_score(out of 10)'],y=df['salary($)'])
"""
# A scatter plot between interview score and Salary
"""
sns.scatterplot(x=df['interview_score(out of 10)'],y=df['salary($)'])
"""
#  Bar plot for experience
"""
sns.barplot(x=df['experience'],y=df['salary($)'])
"""
# Working with the model¶
"""
"""
## Selecting our target Y 
"""
y=df[['salary($)']]
"""
## Selecting our feature X 
"""
x=df[['experience']]
x
y
"""
# Create a Linear regression model between Features and target data
"""
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test=train_test_split(x,y,test_size=0.2,random_state=3)
X_train
X_test
Y_train
Y_test
regression_model=LinearRegression()
regression_model.fit(X_train,Y_train)
X_test
predicted_salary=regression_model.predict(X_test)
predicted_salary
Y_test
"""
# Display the test score and training score
"""
regression_model.score(X_test,Y_test)
regression_model.score(X_train,Y_train)
"""
# Extract slope and intercept value from the model
"""
from sklearn.metrics import mean_squared_error,r2_score,mean_absolute_error
mse=mean_squared_error(Y_test,predicted_salary)
mae=mean_absolute_error(Y_test,predicted_salary)
r2=r2_score(Y_test,predicted_salary)
print('Slope:' ,regression_model.coef_) 
print('Intercept:', regression_model.intercept_) 
"""
# Display Mean Squared Error
"""
print('Mean squared error: ', mse)
"""
# Display Mean Absolute Error
"""
print('Mean Absolute Error',mae )
"""
# Display Root mean Squared error
"""
print('Root mean squared error: ',np.sqrt(mse)) 
"""
# Display R2 score
"""
print('R2 score: ', r2)