Add release notes for Acumos xAPP for Amber release
[ric-app/ml.git] / AcumosXappAdapter / iris_sklearn.py
1 # ========================LICENSE_START=================================
2 #   O-RAN-SC
3 #   %%
4 #   Copyright (c) 2019 AT&T Intellectual Property.
5 #   %%
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #          http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17 # ========================LICENSE_END===================================
18
19 from acumos.session import AcumosSession
20 from acumos.modeling import Model, List, create_dataframe
21
22 import numpy as np
23 import pandas as pd
24 from sklearn.datasets import load_iris
25 from sklearn.ensemble import RandomForestClassifier
26
27 iris = load_iris()
28 X = iris.data
29 y = iris.target
30
31 clf = RandomForestClassifier(random_state=0)
32 clf.fit(X, y)
33
34 # here, an appropriate NamedTuple type is inferred from a pandas DataFrame
35 X_df = pd.DataFrame(X, columns=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'])
36 IrisDataFrame = create_dataframe('IrisDataFrame', X_df)
37
38 # ==================================================================================
39 # # or equivalently:
40 #
41 # IrisDataFrame = create_namedtuple('IrisDataFrame', [('sepal_length', List[float]),
42 #                                                     ('sepal_width', List[float]),
43 #                                                     ('petal_length', List[float]),
44 #                                                     ('petal_width', List[float])])
45 # ==================================================================================
46
47 def classify_iris(df: IrisDataFrame) -> List[int]:
48     '''Returns an array of iris classifications'''
49     X = np.column_stack(df)
50     return clf.predict(X)
51
52 model = Model(classify=classify_iris)
53
54 session = AcumosSession()
55
56 session.dump(model,'iris_sklearn','/temp')