7fd70fbd0690db9694ecd4c244e033f76f9895f2
[ric-app/ml.git] / AcumosXappAdapter / iris_sklearn.py
1 # ===============LICENSE_START=======================================================
2 # Acumos Apache-2.0
3 # ===================================================================================
4 # Copyright (C) 2017-2018 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
5 # ===================================================================================
6 # This Acumos software file is distributed by AT&T and Tech Mahindra
7 # under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # This file 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','/Users/guy/Desktop')