0% found this document useful (0 votes)
2 views4 pages

Exercise 2 Code

The document outlines a procedure for performing Exploratory Data Analysis (EDA) on a dataset of Years of Experience versus Salary, culminating in the development of a Simple Linear Regression model. Key steps include loading the dataset, visualizing relationships, computing correlations, building the regression model, and evaluating its performance using R² score. The process emphasizes the importance of residual analysis and interpreting results to understand the relationship between experience and salary.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Exercise 2 Code

The document outlines a procedure for performing Exploratory Data Analysis (EDA) on a dataset of Years of Experience versus Salary, culminating in the development of a Simple Linear Regression model. Key steps include loading the dataset, visualizing relationships, computing correlations, building the regression model, and evaluating its performance using R² score. The process emphasizes the importance of residual analysis and interpreting results to understand the relationship between experience and salary.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Objective

To perform Exploratory Data Analysis (EDA) on a two-variable dataset (Years of Experience


vs Salary) and to build a Simple Linear Regression model to analyze and predict the
relationship between experience and salary.

Short Procedure
1. Load the dataset containing Years of Experience and Salary.
2. Check dataset structure, summary statistics, and missing values.
3. Visualize the data using a scatter plot to observe the relationship.
4. Compute correlation to measure the strength of association.
5. Plot a regression line to verify linearity.
6. Build a Simple Linear Regression model using experience as input and salary as output.
7. Predict salary values using the trained model.
8. Perform residual analysis to validate model assumptions.
9. Evaluate model performance using R² score.
10. Interpret the results and draw conclusions.

import pandas as pd

import numpy as np

import [Link] as plt

import seaborn as sns

df = pd.read_csv("/Salary_Data.csv")

[Link]()

print("Shape:", [Link])
print("\nInfo:\n")
print([Link]())

print("\nSummary Statistics:\n")
print([Link]())

print([Link]().sum())

[Link](df['YearsExperience'], df['Salary'])
[Link]("Years of Experience")
[Link]("Salary")
[Link]("Experience vs Salary")
[Link]()

print("Correlation Matrix:\n")

print([Link]())

df['Salary'].hist()

[Link]("Salary Distribution")

[Link]()

df['YearsExperience'].hist()

[Link]("Experience Distribution")

[Link]()

[Link](x=df['Salary'])

[Link]("Salary Boxplot")

[Link]()

[Link](x=df['YearsExperience'])

[Link]("Experience Boxplot")

[Link]()

from sklearn.linear_model import LinearRegression

X = df[['YearsExperience']]

y = df['Salary']

model = LinearRegression()
[Link](X, y)

print("Intercept:", model.intercept_)

print("Slope:", model.coef_[0])

y_pred = [Link](X)

[Link](X, y)
[Link](X, y_pred) # regression line
[Link]("Years of Experience")
[Link]("Salary")
[Link]("Regression Fit")
[Link]()

residuals = y - y_pred

[Link](y_pred, residuals)

[Link](y=0)

[Link]("Predicted Salary")

[Link]("Residuals")

[Link]("Residual Plot")

[Link]()

[Link](residuals, kde=True)

[Link]("Residual Distribution")

[Link]()

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=42)

model = LinearRegression()
[Link](X_train, y_train)

y_test_pred = [Link](X_test)
print("Test R²:", r2_score(y_test, y_test_pred))

You might also like