-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfish_obesity.py
67 lines (51 loc) · 2.3 KB
/
fish_obesity.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
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from matplotlib import pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
class FishObesity:
def __init__(self):
self.fish_obesity_df = pd.read_csv('data_frames/fish obesity.csv')
self.prepare()
def prepare(self):
"""
:return: merge happiness to value -> boolean list
"""
fish_obesity_df = self.fish_obesity_df
labels = fish_obesity_df['Obese']
features = fish_obesity_df[['Weight', 'Height']]
# Splitting the data into training and testing sets, in a ratio of 80% to 20%. Using train_test_split from
# scikit-learn
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)
classifier = SVC(random_state=42)
classifier.fit(X_train.values, y_train)
plt.figure(figsize=(10, 6))
plt.scatter(X_train['Weight'], X_train['Height'], c=y_train, cmap='viridis')
# Plotting the contour line
ax = plt.gca()
x_lim = ax.get_xlim()
y_lim = ax.get_ylim()
xx = np.linspace(x_lim[0], x_lim[1], 100)
yy = np.linspace(y_lim[0], y_lim[1], 100)
XX, YY = np.meshgrid(xx, yy)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = classifier.decision_function(xy).reshape(XX.shape)
ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--'])
plt.xlabel('Weight')
plt.ylabel('Height')
plt.title('Fish Obesity Classification')
plt.colorbar(label='Class')
plt.show()
is_obesity_new_fish = [[200, 163]]
obesity_new_fish = classifier.predict(is_obesity_new_fish)
print(f"Obesity: {True if obesity_new_fish[0] == 1 else False}")
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=classifier.classes_)
disp.plot(cmap=plt.cm.Blues)
plt.title('Matriz de Confusão')
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.show()
return classifier