-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbattery.py
111 lines (77 loc) · 3.49 KB
/
battery.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# import pandas as pd
# from sklearn.model_selection import train_test_split
# from sklearn.tree import DecisionTreeClassifier
# from sklearn.metrics import accuracy_score
# from sklearn.preprocessing import LabelEncoder
# # Load dataset
# data = pd.read_csv('dummyDataset.csv')
# # Remove '%' sign and convert 'Battery %' column to numeric
# data['Battery %'] = data['Battery %'].str.replace('%', '').astype(float)
# # Label encode the "Employees" column
# le = LabelEncoder()
# data['Employees'] = le.fit_transform(data['Employees'])
# # Label the data (1 = notify, 0 = don't notify)
# data['notify'] = data['Battery %'].apply(lambda x: 1 if x <= 10 else 0)
# # Features and labels
# X = data[['Battery %', 'Employees']] # Use encoded 'Employees' column
# y = data['notify']
# # Train-test split
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# # Train a decision tree classifier
# model = DecisionTreeClassifier()
# model.fit(X_train, y_train)
# # Predictions
# y_pred = model.predict(X_test)
# # Evaluate the model
# accuracy = accuracy_score(y_test, y_pred)
# print(f"Model Accuracy: {accuracy * 100:.2f}%")
# # For real-time data (Example inputs)
# current_battery_level = 9 # Current battery level
# current_employee = 'Employee 13' # Assuming employee name
# # Encode the current employee's name using the same encoder
# current_employee_encoded = le.transform([current_employee])[0]
# # Prepare new data for prediction
# new_data = pd.DataFrame({
# 'Battery %': [current_battery_level],
# 'Employees': [current_employee_encoded]
# })
# # Predict notification
# should_notify = model.predict(new_data)
# if should_notify == 1:
# print("Notify the user: Battery is below 10%!")
# else:
# print("No need to notify the user.")
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder
# Load dataset
data = pd.read_csv('dummyDataset.csv')
# Remove '%' sign and convert 'Battery %' column to numeric
data['Battery %'] = data['Battery %'].str.replace('%', '').astype(float)
# Label encode the "Employees" column
le = LabelEncoder()
data['Employees_Encoded'] = le.fit_transform(data['Employees'])
# Label the data (1 = notify, 0 = don't notify)
data['notify'] = data['Battery %'].apply(lambda x: 1 if x <= 10 else 0)
# Features and labels
X = data[['Battery %', 'Employees_Encoded']] # Use encoded 'Employees' column
y = data['notify']
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Train a decision tree classifier
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
# Predictions
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy * 100:.2f}%")
# Now, check the entire dataset to print employees with battery levels <= 10%
low_battery_employees = data[data['Battery %'] <= 10]
# Decode the employee names from the encoded labels
low_battery_employees['Employee_Name'] = le.inverse_transform(low_battery_employees['Employees_Encoded'])
# Print notifications for employees with battery levels <= 10%
for index, row in low_battery_employees.iterrows():
print(f"Notify {row['Employee_Name']}: Battery is at {row['Battery %']}%, please charge your device!")