From 37c2fbd126a9a2cf22d3c1d78a895cea5925b3cc Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Wed, 19 Jun 2024 13:06:00 +0700 Subject: [PATCH] Create ai_oracle.sol --- .../contracts/ai_oracle/ai_oracle.sol | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 network/blockchain/contracts/ai_oracle/ai_oracle.sol diff --git a/network/blockchain/contracts/ai_oracle/ai_oracle.sol b/network/blockchain/contracts/ai_oracle/ai_oracle.sol new file mode 100644 index 0000000..4328b53 --- /dev/null +++ b/network/blockchain/contracts/ai_oracle/ai_oracle.sol @@ -0,0 +1,24 @@ +import numpy as np +from sklearn.ensemble import RandomForestClassifier +from sklearn.model_selection import train_test_split +from sklearn.metrics import accuracy_score + +class AIOracle: + def __init__(self, data, target): + self.data = data + self.target = target + self.model = RandomForestClassifier(n_estimators=100) + + def train(self): + X_train, X_test, y_train, y_test = train_test_split(self.data, self.target, test_size=0.2, random_state=42) + self.model.fit(X_train, y_train) + y_pred = self.model.predict(X_test) + print("Accuracy:", accuracy_score(y_test, y_pred)) + + def predict(self, input_data): + return self.model.predict(input_data) + + def update(self, new_data, new_target): + self.data = np.concatenate((self.data, new_data)) + self.target = np.concatenate((self.target, new_target)) + self.train()