-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
53 lines (41 loc) · 1.57 KB
/
example.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
# Import necessary libraries
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
from semantic import dataloader, featuretransformer, modelbuilder, Pipeline
# Function to load the dataset
@dataloader(output=['X_train', 'X_test', 'y_train', 'y_test'])
def load_dataset():
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
return X_train, X_test, y_train, y_test
# Function to create features and preprocess data
@featuretransformer(input=['X_train'], output=['X_train_scaled'])
def create_features(X_train):
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_train)
print(X_scaled)
return X_scaled
# Function to train the model
@modelbuilder(input=['X_train_scaled', 'y_train'])
def train_model(X_train_scaled, y_train):
classifier = LogisticRegression()
classifier.fit(X_train_scaled, y_train)
return classifier
def main():
p = Pipeline()
p.search()
model = p.execute()
_, X_test, _, y_test = load_dataset()
X_test_scaled, _ = p.transform({'X_train':X_test, 'y_test':y_test})
predictions = model.predict(X_test_scaled)
accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", accuracy)
print(classification_report(y_test, predictions))
if __name__ == "__main__":
main()