-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_code_from_uny.py
50 lines (37 loc) · 1.04 KB
/
test_code_from_uny.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# IU - International University of Applied Science
# Machine Learning - Supervised Learning
# Course Code: DLBDSMLSL01
# Generalized Linear Model (GLM)
# %% load packages
import statsmodels.api as sm
import pandas as pd
# %% load data
dataset = pd.read_csv('datasets/Salary_Data.csv')
x = dataset.drop(columns=['Salary'])
y = dataset.iloc[:, 1].values
# %% specify exogeneous and endogeneous variables
exog, endog = sm.add_constant(x), y
# %% specify the model
mod = sm.GLM(
endog,
exog,
family=sm.families.Poisson(
link=sm.families.links.log()
)
)
# %% fit the model
res = mod.fit()
# %% print model summary
print(res.summary())
import matplotlib.pyplot as plt
# Generate predictions
predicted = res.predict(exog)
# Plot actual points and predicted line
plt.scatter(x, y, color='blue', label='Actual')
plt.plot(x, predicted, color='red', label='Predicted')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.title('GLM: Actual vs Predicted')
plt.legend()
plt.savefig('glm_plot_iu_example.png') # Save the plot
plt.show()