0% found this document useful (0 votes)
13 views

Solution To Task 1

Uploaded by

CH HUMZA
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)
13 views

Solution To Task 1

Uploaded by

CH HUMZA
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
You are on page 1/ 2

# Importing libraries

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

# Provided dataset: Years of experience and corresponding salaries

data = {

"Years of Experience": [1, 2, 3, 4, 5],

"Salary (in 1000s)": [30, 35, 40, 45, 50]

# Converting data to DataFrame

df = pd.DataFrame(data)

# Extracting years of experience and salaries as numpy arrays

years_exp = df["Years of Experience"].values.reshape(-1, 1)

salaries = df["Salary (in 1000s)"].values

# Visualizing the data

plt.scatter(years_exp, salaries)

plt.title('Salary vs. Years of Experience')

plt.xlabel('Years of Experience')

plt.ylabel('Salary (in 1000s)')

plt.grid(True)

plt.show()
# Creating and fitting the linear regression model

model = LinearRegression()

model.fit(years_exp, salaries)

# Predicting the salary of an employee with 6 years of experience

predicted_salary = model.predict([[6]])

print("Predicted salary for an employee with 6 years of experience (in 1000s):", predicted_salary[0])

# Plotting the linear regression line

plt.scatter(years_exp, salaries)

plt.plot(years_exp, model.predict(years_exp), color='red') # Regression line

plt.title('Salary vs. Years of Experience with Linear Regression')

plt.xlabel('Years of Experience')

plt.ylabel('Salary (in 1000s)')

plt.grid(True)

plt.show()

You might also like