-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code: Improve file handling and variable initialization
- Loading branch information
Showing
2 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Bsp: Datensatz (Bitte bearbeiten für die von Ihnen gemessene Werte) | ||
# Für die kalibrierung bitte einfach die eingebaute Funktion calibrate aus und lassen sie sich das Ergebnis ausgeben. | ||
# Die Reifennutzung verändert sich für jeden Run um -0.05 | ||
# Der Batterieladestand muss jedesmal gesondert abgelesen werden | ||
# Wenn sie diese Schritte einhalten kann die KI / das KNN richtig funktionieren | ||
data = [ | ||
{'Kalibrierung': 1.0, 'Batterieladestand': 100, 'Reifennutzung': 1.0, 'Multiplikation': 1.00}, | ||
{'Kalibrierung': 1.0, 'Batterieladestand': 90, 'Reifennutzung': 0.9, 'Multiplikation': 1.10}, | ||
{'Kalibrierung': 1.0, 'Batterieladestand': 80, 'Reifennutzung': 0.8, 'Multiplikation': 1.25}, | ||
{'Kalibrierung': 0.9, 'Batterieladestand': 100, 'Reifennutzung': 1.0, 'Multiplikation': 1.10}, | ||
{'Kalibrierung': 1.1, 'Batterieladestand': 100, 'Reifennutzung': 1.0, 'Multiplikation': 0.95} | ||
] | ||
|
||
def euclidean_distance(point1, point2): | ||
distance = 0.0 | ||
for key in point1: | ||
if key != 'Multiplikation': | ||
distance += (point1[key] - point2[key]) ** 2 | ||
return math.sqrt(distance) | ||
|
||
def knn_predict(data, new_data_point, k=3): | ||
# Berechne die Distanz zwischen dem neuen Punkt und allen anderen Punkten | ||
distances = [] | ||
for item in data: | ||
dist = euclidean_distance(new_data_point, item) | ||
distances.append((dist, item['Multiplikation'])) | ||
|
||
distances.sort(key=lambda x: x[0]) | ||
neighbors = distances[:k] | ||
|
||
total_distance = sum(neighbor[1] for neighbor in neighbors) | ||
predicted_distance = total_distance / k | ||
|
||
return predicted_distance | ||
|
||
# Beispielhafte Vorhersage | ||
new_data_point = {'Kalibrierung': 0.84, 'Batterieladestand': 85, 'Reifennutzung': 0.95} | ||
predicted_distance = knn_predict(data, new_data_point, k=3) | ||
print(f'Vorhergesagte Multiplikation: {predicted_distance}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters