-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine_learning.py
78 lines (62 loc) · 2.36 KB
/
machine_learning.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
import pandas as pd
import seaborn as sns
import warnings
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
warnings.simplefilter(action='ignore', category=FutureWarning)
print("\nMACHINE LEARNING | REGRESSION + PREDICTION MODEL\n")
# ----------------------------- DATASET SAMPLE
# df = pd.read_csv('../dataset/sample_model.csv').dropna()
df = pd.read_csv('../dataset/transport_E.csv')
print(f'{df.info()}\n')
print(f'{df.describe()}\n')
# print(df.corr())
predict_column = 'Collects'
target_column = 'Faults'
# ----------------------------- DATA PREPARATION
predictor = df[predict_column] # Predictor variable
target = df[target_column] # Target variable
x = np.array(predictor) # Entry variable
x = x.reshape(-1, 1)
y = target
# ----------------------------- TRAIN / TEST / MODEL
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(x_train, y_train)
plot_choose = int(input("\nChoose plot category:\n"
"1. Predictor KDE Plot\n"
"2. Scatter Plot\n"
"3. Scatter Plot + Regression\n"
"4. New Value + Prediction Deploy\n"))
# ----------------------------- PREDICTOR KDE PLOT
if plot_choose == 1:
sns.histplot(data=df, x=predictor, kde=True)
plt.show()
# ----------------------------- SCATTER PLOT
if plot_choose == 2:
plt.scatter(x, y, label="Dados reais históricos")
plt.xlabel(predict_column)
plt.ylabel(target_column)
plt.legend()
plt.show()
# ----------------------------- SCATTER PLOT / REGRESSION
if plot_choose == 3:
plt.scatter(x, y, label="Dados reais históricos")
plt.plot(x, model.predict(x), color="red", label="Regressão")
plt.xlabel(predict_column)
plt.ylabel(target_column)
plt.legend()
plt.show()
# ----------------------------- DEPLOY
deploy = True
while deploy:
if plot_choose == 4:
new_value = int(input('Insert new value for Regression Prediction:\n'))
v = np.array([[new_value]])
prediction = model.predict(v)
print(f'\nValue = {v} \nPrediction = {prediction}\n')
print("-----------------------------------")
else:
Break