Exp1c
Exp1c
Algorithm:
Program:
Program:
import numpy as np import pandas as pd
import statsmodels.api as sm import
statsmodels.formula.api as smf
np.random.seed(42) # Ensure reproducibility
data = {
"Experience": np.random.randint(1, 20, 10), # Years of experience
"Salary": np.random.randint(40000, 100000, 10) # Salary in
dollars
}
df = pd.DataFrame(data)
print("Original DataFrame:\n", df)
X = df["Experience"] # Independent variable
Y = df["Salary"] # Dependent variable
X = sm.add_constant(X) # Adds a column of ones to Xl
model = sm.OLS(Y, X).fit() # Ordinary Least Squares (OLS)
print("\nRegression Model Summary:")
print(model.summary())
new_experience = pd.DataFrame({"Experience": [5, 10, 15]})
new_experience = sm.add_constant(new_experience)
predicted_salary = model.predict(new_experience) print("\nPredicted
Salaries for New Experience Values:") print(predicted_salary)
Sample input/output:
Original DataFrame:
Experience Salary
0 7 77817
1 4 67682
2 11 64299
3 9 90636
4 2 67017
5 12 73126
6 11 89921
7 9 51479
8 14 47292
9 3 64997
Model Residuals:
0 -2606.488476
1 -699.431093
2 459.471269 3 12644.062759
...
Result:
The implementation Statsmodel features including operations Linear
Regression, Summary Statistics ,Predicting New Values,Checking
Model Residuals,ANOVA (Analysis of Variance) successfully
executed and verified.