0% found this document useful (0 votes)
9 views1 page

Liner Regression Chapter N1

The document outlines a Python script for performing linear regression analysis using a dataset. It includes steps to load data, extract features and target variables, split the dataset into training and testing sets, train a linear regression model, make predictions, and evaluate the model's performance using the R2 score. The final output displays the calculated R2 score of the model.

Uploaded by

bizzpy n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Liner Regression Chapter N1

The document outlines a Python script for performing linear regression analysis using a dataset. It includes steps to load data, extract features and target variables, split the dataset into training and testing sets, train a linear regression model, make predictions, and evaluate the model's performance using the R2 score. The final output displays the calculated R2 score of the model.

Uploaded by

bizzpy n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

# Import necessary libraries

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

from sklearn.metrics import r2_score

# Task 1: Load the dataset using pandas

data = pd.read_csv('data.csv')

# Task 2: Extract data from 'years experience' column as a variable named X

X = data[['years experience']]

# Task 3: Extract data from 'salary' column as a variable named Y

Y = data['salary']

# Task 4: Divide the dataset into training and testing sets in a 66% and 33% proportion

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33, random_state=42)

# Task 5: Create and train Linear Regression Model on training set

model = LinearRegression()

model.fit(X_train, Y_train)

# Task 6: Make predictions based on the testing set using the trained model

Y_pred = model.predict(X_test)

# Task 7: Check the performance by calculating the r2 score of the model

r2_score_value = r2_score(Y_test, Y_pred)

# Display the R2 score

print(f'R2 Score: {r2_score_value}')

You might also like