# 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()