Linear Regression Assignment
1. Load the dataset using pandas
2. Extract data fromYearsExperience column is a variable named X
3. Extract data from salary column is a variable named Y
4. Divide the dataset into two parts for training and testing in 66% and 33% proportion
5. Create and train LinearRegression Model on training set
6. Make predictions based on the testing set using the trained model
7. Check the performance by calculating the r2 score of the mode
# Import statements
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
import pandas as pd
# Read the data.
data = pd.read_csv('data.csv')
# Assign the features to the variable X, and the labels to the variable Y.
X = data['YearsExperience']
Y = data['Salary']
X_train,X_test,Y_train,Y_test = train_test_split(X,Y, test_size=0.33)
model = LinearRegression()
# TODO: Fit the model.
model.fit(X_train,Y_train)
# TODO: Make predictions. Store them in the variable y_pred.
y_pred = model.predict(X_test)
# TODO: Calculate the accuracy and assign it to the variable acc.
r2_score = r2_score(Y_test,y_pred)
print(r2_score)
Comments
Leave a comment