# Q12. Salary Estimation Using Linear Regression

# Step 1: Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error

# Step 2: Create Dataset OR Read CSV File

# Option 1: Create Dataset
df = pd.DataFrame({
    "Experience": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    "Salary": [25000, 30000, 35000, 40000, 45000,
               50000, 55000, 60000, 65000, 70000]
})

# Option 2: Read Dataset from CSV
# df = pd.read_csv("Salary_Data.csv")

print(df.head())
print("Shape:", df.shape)
print(df.dtypes)
print(df.describe())

# Step 3: Check Missing and Duplicate Values
print("Null Values:\n", df.isnull().sum())
print("Duplicate Rows:", df.duplicated().sum())

# Remove Null and Duplicate Rows
df = df.dropna()
df = df.drop_duplicates()

# Step 4: Visualize Data
plt.scatter(df["Experience"], df["Salary"])
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.title("Experience vs Salary")
plt.show()

# Step 5: Select Features and Target
X = df[["Experience"]]
y = df["Salary"]

# Step 6: Split Dataset
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Step 7: Train Linear Regression Model
model = LinearRegression()
model.fit(X_train, y_train)

# Step 8: Display Regression Equation
m = model.coef_[0]
c = model.intercept_

print("\nRegression Equation: Salary =", m, "* Experience +", c)

# Step 9: Display Regression Line
plt.scatter(X, y, label="Actual Salary")
plt.plot(X, model.predict(X), color="red", label="Regression Line")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.title("Salary Prediction")
plt.legend()
plt.show()

# Step 10: Evaluate Model
y_pred = model.predict(X_test)

# Step 10: Predict Salary for User Input
x = float(input())
y = model.predict(pd.DataFrame({"Experience": [x]}))
print(y)


print("\nR2 Score:", r2_score(y_test, y_pred))
print("RMSE:", np.sqrt(mean_squared_error(y_test, y_pred)))
print("MAE:", mean_absolute_error(y_test, y_pred))

# Conclusion
print("Linear Regression estimates salary based on experience.")


