class TestCases(unittest.TestCase): @classmethod def setUpClass(self): print("******* Unit Test for Statsmodels *******") self.adapa_utility = AdapaUtility() def getData(self): # Non Seasonal Data data = [266,146,183,119,180,169,232,225,193,123,337,186,194,150,210,273,191,287, 226,304,290,422,265,342,340,440,316,439,401,390,490,408,490,420,520,480] index = pd.DatetimeIndex(start='2016-01-01', end='2018-12-01', freq='MS') ts_data = pd.Series(data, index) ts_data.index.name = 'date_index' ts_data.name = 'cars_sold' ts_data = ts_data.astype('float64') return ts_data def test_01(self): ts_data = getData() f_name='arima201_c_car_sold.pmml' model = ARIMA(ts_data,order=(2,0,1)) result = model.fit(trend = 'c', method = 'css') ArimaToPMML(result, f_name) model_name = self.adapaUtilities.upload_to_zserver(f_name) z_pred = self.adapaUtilities.score_single_record(model_name) model_pred = result.forecast()[0][0] self.assertEqual(model_pred, z_pred['predicted_cars_sold']) z_pred = self.adapa_utility.score_in_zserver(model_name, 'nyoka/tests/test_car_sold.csv','TS') model_pred = result.forecast(5)[0][-1] self.assertEqual(model_pred, z_pred) def test_02(self): data=pd.read_csv("nyoka/tests/JohnsonJohnsonWithDate.csv") data['index']=pd.to_datetime(data['index'], format='%Y-%m-%d') data.set_index(['index'], inplace=True) mod = SARIMAX(data,order=(1,0,0),seasonal_order=(1,0,0, 4)) result = mod.fit() ArimaToPMML(results, 'jnj_seasonal_arima.pmml') model_name = self.adapaUtilities.upload_to_zserver('jnj_seasonal_arima.pmml') z_pred = self.adapaUtilities.score_single_record(model_name) model_pred = result.forecast()[0] self.assertEqual(model_pred, z_pred['predicted_value']) z_pred = self.adapa_utility.score_in_zserver(model_name, 'nyoka/tests/test_jnj.csv','TS') model_pred = result.forecast(5)[-1] self.assertEqual(model_pred, z_pred) @classmethod def tearDownClass(self): print("\n******* Finished *******\n")
def setUpClass(self): print("******* Unit Test for lightgbm *******") iris = datasets.load_iris() df = pd.DataFrame(iris.data, columns=iris.feature_names) df["Species"] = iris.target df["Binary"] = numpy.array([i % 2 for i in range(df.shape[0])]) self.X = df[iris.feature_names] self.Y = df["Species"] self.Y_bin = df["Binary"] self.features = iris.feature_names self.test_file = 'nyoka/tests/test.csv' self.X.to_csv(self.test_file, index=False) self.adapa_utility = AdapaUtility()
def setUpClass(self): print("******* Unit Test for RetinaNet *******") url = 'https://github.com/fizyr/keras-retinanet/releases/download/0.5.1/resnet50_coco_best_v2.1.0.h5' r = requests.get(url) with open('resnet50_coco_best_v2.1.0.h5', 'wb') as f: f.write(r.content) classes = json.load(open("nyoka/tests/categories_coco.json", 'r')) self.classes = list(classes.values()) self.adapa_utility = AdapaUtility() self.model = load_model('resnet50_coco_best_v2.1.0.h5', backbone_name='resnet50')
def setUpClass(self): print("******* Unit Test for Keras *******") self.adapa_utility = AdapaUtility() self.data_utility = DataUtility() model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) activType = 'sigmoid' x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) predictions = Dense(2, activation=activType)(x) self.model_final = Model(inputs=model.input, outputs=predictions, name='predictions')
def setUpClass(cls): print("******* Unit Test for Statsmodels *******") cls.adapa_utility = AdapaUtility()
class TestCases(unittest.TestCase): @classmethod def setUpClass(self): print("******* Unit Test for lightgbm *******") iris = datasets.load_iris() df = pd.DataFrame(iris.data, columns=iris.feature_names) df["Species"] = iris.target df["Binary"] = numpy.array([i % 2 for i in range(df.shape[0])]) self.X = df[iris.feature_names] self.Y = df["Species"] self.Y_bin = df["Binary"] self.features = iris.feature_names self.test_file = 'nyoka/tests/test.csv' self.X.to_csv(self.test_file, index=False) self.adapa_utility = AdapaUtility() def test_01_lgbm_classifier(self): print( "\ntest 01 (lgbm classifier with preprocessing) [binary-class]\n") model = LGBMClassifier() pipeline_obj = Pipeline([('scaler', MinMaxScaler()), ("model", model)]) pipeline_obj.fit(self.X, self.Y_bin) file_name = "test01lgbm.pmml" lgb_to_pmml(pipeline_obj, self.features, 'Species', file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, self.test_file) model_pred = pipeline_obj.predict(self.X) model_prob = pipeline_obj.predict_proba(self.X) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_02_lgbm_classifier(self): print("\ntest 02 (lgbm classifier with preprocessing) [multi-class]\n") model = LGBMClassifier() pipeline_obj = Pipeline([('scaler', MaxAbsScaler()), ("model", model)]) pipeline_obj.fit(self.X, self.Y) file_name = "test02lgbm.pmml" lgb_to_pmml(pipeline_obj, self.features, 'Species', file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, self.test_file) model_pred = pipeline_obj.predict(self.X) model_prob = pipeline_obj.predict_proba(self.X) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_03_lgbm_regressor(self): print("\ntest 03 (lgbm regressor without preprocessing)\n") model = LGBMRegressor() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(self.X, self.Y) file_name = "test03lgbm.pmml" lgb_to_pmml(pipeline_obj, self.features, 'Species', file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, self.test_file) model_pred = pipeline_obj.predict(self.X) predictions = numpy.array(predictions) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), True) def test_04_lgbm_regressor(self): print("\ntest 04 (lgbm regressor with preprocessing)\n") auto = pd.read_csv('nyoka/tests/auto-mpg.csv') X = auto.drop(['mpg'], axis=1) y = auto['mpg'] feature_names = [name for name in auto.columns if name not in ('mpg')] target_name = 'mpg' x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=101) pd.DataFrame(data=x_test, columns=feature_names).to_csv("test.csv", index=False) pipeline_obj = Pipeline([ ('mapper', DataFrameMapper([('car name', CountVectorizer()), (['displacement'], [StandardScaler()])])), ('lgbmr', LGBMRegressor()) ]) pipeline_obj.fit(x_train, y_train) file_name = "test04lgbm.pmml" lgb_to_pmml(pipeline_obj, feature_names, 'mpg', file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, "test.csv") predictions = numpy.array(predictions) model_pred = pipeline_obj.predict(x_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), True) @classmethod def tearDownClass(self): print("\n******* Finished *******\n")
def setUpClass(self): print("******* Unit Test for Keras *******") self.adapa_utility = AdapaUtility() self.data_utility = DataUtility()
class TestCases(unittest.TestCase): @classmethod def setUpClass(self): print("******* Unit Test for Keras *******") self.adapa_utility = AdapaUtility() self.data_utility = DataUtility() def test_01_image_classifier_with_image_as_input(self): model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) activType = 'sigmoid' x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) predictions = Dense(2, activation=activType)(x) model_final = Model(inputs=model.input, outputs=predictions, name='predictions') cnn_pmml = KerasToPmml(model_final,model_name="MobileNetImage",description="Demo",\ copyright="Internal User",dataSet='image',predictedClasses=['dogs','cats']) cnn_pmml.export(open('2classMBNet.pmml', "w"), 0) img = image.load_img('nyoka/tests/resizedCat.png') img = img_to_array(img) img = preprocess_input(img) imgtf = np.expand_dims(img, axis=0) model_pred = model_final.predict(imgtf) model_preds = {'dogs': model_pred[0][0], 'cats': model_pred[0][1]} model_name = self.adapa_utility.upload_to_zserver('2classMBNet.pmml') predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, 'nyoka/tests/resizedCat.png', 'DN') self.assertEqual( abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True) self.assertEqual( abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True) def test_02_image_classifier_with_base64string_as_input(self): model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) activType = 'sigmoid' x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) predictions = Dense(2, activation=activType)(x) model_final = Model(inputs=model.input, outputs=predictions, name='predictions') cnn_pmml = KerasToPmml(model_final,model_name="MobileNetBase64",description="Demo",\ copyright="Internal User",dataSet='imageBase64',predictedClasses=['dogs','cats']) cnn_pmml.export(open('2classMBNetBase64.pmml', "w"), 0) img = image.load_img('nyoka/tests/resizedCat.png') img = img_to_array(img) img = preprocess_input(img) imgtf = np.expand_dims(img, axis=0) base64string = "data:float32;base64," + FloatBase64.from_floatArray( img.flatten(), 12) base64string = base64string.replace("\n", "") csvContent = "imageBase64\n\"" + base64string + "\"" text_file = open("input.csv", "w") text_file.write(csvContent) text_file.close() model_pred = model_final.predict(imgtf) model_preds = {'dogs': model_pred[0][0], 'cats': model_pred[0][1]} model_name = self.adapa_utility.upload_to_zserver( '2classMBNetBase64.pmml') predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, 'input.csv', 'DN') self.assertEqual( abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True) self.assertEqual( abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True) @unittest.skip("") def test_03_encoded_script(self): model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) predictions = Dense(2, activation='sigmoid')(x) model_final = Model(inputs=model.input, outputs=predictions, name='predictions') script_content = open("nyoka/tests/preprocess.py", 'r').read() pmml_obj = KerasToPmml(model_final, dataSet='image', predictedClasses=['cat', 'dog'], script_args={ "content": script_content, "def_name": "getBase64EncodedString", "return_type": "string", "encode": True }) pmml_obj.export(open("script_with_keras.pmml", 'w'), 0) self.assertEqual(os.path.isfile("script_with_keras.pmml"), True) reconPmmlObj = pml.parse("script_with_keras.pmml", True) content = reconPmmlObj.TransformationDictionary.DefineFunction[ 0].Apply.Extension[0].anytypeobjs_[0] content = base64.b64decode(content).decode() self.assertEqual(script_content, content) self.assertEqual(len(model_final.layers), len(reconPmmlObj.DeepNetwork[0].NetworkLayer)) @unittest.skip("") def test_04_plain_text_script(self): model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) predictions = Dense(2, activation='sigmoid')(x) model_final = Model(inputs=model.input, outputs=predictions, name='predictions') script_content = open("nyoka/tests/preprocess.py", 'r').read() pmml_obj = KerasToPmml(model_final, dataSet='image', predictedClasses=['cat', 'dog'], script_args={ "content": script_content, "def_name": "getBase64EncodedString", "return_type": "string", "encode": False }) pmml_obj.export(open("script_with_keras.pmml", 'w'), 0) self.assertEqual(os.path.isfile("script_with_keras.pmml"), True) reconPmmlObj = pml.parse("script_with_keras.pmml", True) content = reconPmmlObj.TransformationDictionary.DefineFunction[ 0].Apply.Extension[0].anytypeobjs_ content[0] = content[0].replace("\t", "") content = "\n".join(content) self.assertEqual(script_content, content) self.assertEqual(len(model_final.layers), len(reconPmmlObj.DeepNetwork[0].NetworkLayer)) @classmethod def tearDownClass(self): print("\n******* Finished *******\n")
class TestCases(unittest.TestCase): @classmethod def setUpClass(self): print("******* Unit Test for lightgbm *******") iris = datasets.load_iris() df = pd.DataFrame(iris.data, columns=iris.feature_names) df["Species"] = iris.target df["Binary"] = numpy.array([i % 2 for i in range(df.shape[0])]) self.X = df[iris.feature_names] self.Y = df["Species"] self.Y_bin = df["Binary"] self.features = iris.feature_names self.test_file = 'nyoka/tests/test.csv' self.X.to_csv(self.test_file, index=False) self.adapa_utility = AdapaUtility() def test_01_lgbm_classifier(self): print( "\ntest 01 (lgbm classifier with preprocessing) [binary-class]\n") model = LGBMClassifier() pipeline_obj = Pipeline([('scaler', MinMaxScaler()), ("model", model)]) pipeline_obj.fit(self.X, self.Y_bin) file_name = "test01lgbm.pmml" lgb_to_pmml(pipeline_obj, self.features, 'Species', file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, self.test_file) model_pred = pipeline_obj.predict(self.X) model_prob = pipeline_obj.predict_proba(self.X) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_02_lgbm_classifier(self): print("\ntest 02 (lgbm classifier with preprocessing) [multi-class]\n") model = LGBMClassifier() pipeline_obj = Pipeline([('scaler', MaxAbsScaler()), ("model", model)]) pipeline_obj.fit(self.X, self.Y) file_name = "test02lgbm.pmml" lgb_to_pmml(pipeline_obj, self.features, 'Species', file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, self.test_file) model_pred = pipeline_obj.predict(self.X) model_prob = pipeline_obj.predict_proba(self.X) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_03_lgbm_regressor(self): print("\ntest 03 (lgbm regressor without preprocessing)\n") model = LGBMRegressor() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(self.X, self.Y) file_name = "test03lgbm.pmml" lgb_to_pmml(pipeline_obj, self.features, 'Species', file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, self.test_file) model_pred = pipeline_obj.predict(self.X) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) @classmethod def tearDownClass(self): print("\n******* Finished *******\n")
class TestCases(unittest.TestCase): @classmethod def setUpClass(self): print("******* Unit Test for sklearn *******") self.data_utility = DataUtility() self.adapa_utility = AdapaUtility() def test_01_linear_regression(self): print("\ntest 01 (linear regression without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression( ) model = LinearRegression() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test01sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) def test_02_linear_regression_with_scaler(self): print("\ntest 02 (linear regression with preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression( ) model = LinearRegression() pipeline_obj = Pipeline([ ("mapper", DataFrameMapper([(["sepal width (cm)"], StandardScaler()), (["petal length (cm)", "petal width (cm)"], None)])), ("model", model) ]) pipeline_obj.fit(X, y) file_name = 'test02sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) def test_03_logistic_regression_with_scaler(self): print( "\ntest 03 (logistic regression with preprocessing) [multi-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = LogisticRegression() pipeline_obj = Pipeline([ ("mapper", DataFrameMapper([(["sepal length (cm)", "sepal width (cm)"], MinMaxScaler()), (["petal length (cm)", "petal width (cm)"], None)])), ("model", model) ]) pipeline_obj.fit(X, y) file_name = 'test03sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_04_logistic_regression_with_scaler(self): print( "\ntest 04 (logistic regression with preprocessing) [binary-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = LogisticRegression() pipeline_obj = Pipeline([ ("mapper", DataFrameMapper([(["sepal length (cm)", "sepal width (cm)"], MinMaxScaler()), (["petal length (cm)", "petal width (cm)"], None)])), ("model", model) ]) pipeline_obj.fit(X, y) file_name = 'test04sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_05_logistic_regression(self): print( "\ntest 05 (logistic regression without preprocessing) [multi-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = LogisticRegression() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test05sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_06_logistic_regression(self): print( "\ntest 06 (logistic regression without preprocessing) [binary-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = LogisticRegression() pipeline_obj = Pipeline([("mapper", DataFrameMapper([(["test", LabelBinarizer()])])), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test06sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_07_ridge_classifier(self): print("\ntest 07 (Ridge Classifier) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = RidgeClassifier() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test07sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = model._predict_proba_lr(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_08_ridge_classifier(self): print("\ntest 08 (Ridge Classifier) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = RidgeClassifier() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test08sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = model._predict_proba_lr(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_09_sgd_classifier(self): print("\ntest 09 (SGD Classifier with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = SGDClassifier(loss="log") pipeline_obj = Pipeline([("scaler", StandardScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test09sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_10_sgd_classifier(self): print("\ntest 10 (SGD Classifier with preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = SGDClassifier(loss="log") pipeline_obj = Pipeline([("scaler", StandardScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test10sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_11_lda(self): print("\ntest 11 (LDA with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = LinearDiscriminantAnalysis() pipeline_obj = Pipeline([("scaler", MaxAbsScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test11sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_12_lda(self): print("\ntest 12 (LDA with preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = LinearDiscriminantAnalysis() pipeline_obj = Pipeline([("scaler", StandardScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test12sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_13_linearsvc(self): print("\ntest 13 (LinearSVC with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = LinearSVC() pipeline_obj = Pipeline([("scaler", StandardScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test13sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.decision_function(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_14_linearsvc(self): print("\ntest 14 (LinearSVC with preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = LinearSVC() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test14sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = model._predict_proba_lr(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_15_linearsvr(self): print("\ntest 15 (linear svr without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression( ) model = LinearSVR() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test15sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) def test_16_linearsvr(self): print("\ntest 16 (linear svr with preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression( ) model = LinearSVR() pipeline_obj = Pipeline([("scaler", MinMaxScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test16sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) def test_17_decisiontreeclassifier(self): print( "\ntest 17 (decision tree classifier with preprocessing) [multi-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = DecisionTreeClassifier() pipeline_obj = Pipeline([("scaler", Binarizer()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test17sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_18_decisiontreeclassifier(self): print( "\ntest 18 (decision tree classifier with preprocessing) [binary-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = DecisionTreeClassifier() pipeline_obj = Pipeline([("scaler", Binarizer()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test18sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_19_decisiontreeclassifier(self): print( "\ntest 19 (decision tree classifier without preprocessing) [multi-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = DecisionTreeClassifier() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test19sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_20_decisiontreeclassifier(self): print( "\ntest 20 (decision tree classifier without preprocessing) [binary-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = DecisionTreeClassifier() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test20sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_21_svr(self): print("\ntest 21 (SVR without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression( ) model = SVR() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test21sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) def test_22_gaussian_nb(self): print("\ntest 22 (GaussianNB without preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = GaussianNB() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test22sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_23_gaussian_nb(self): print("\ntest 23 (GaussianNB without preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = GaussianNB() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test23sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_24_gaussian_nb(self): print("\ntest 24 (GaussianNB with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = GaussianNB() pipeline_obj = Pipeline([('scaler', StandardScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test24sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_25_random_forest_regressor(self): print("\ntest 25 (random forest regressor without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression( ) model = RandomForestRegressor() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test25sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) def test_26_random_forest_classifier(self): print( "\ntest 26 (random forest classifier with preprocessing) [multi-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = RandomForestClassifier() pipeline_obj = Pipeline([('scaler', MinMaxScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test26sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_27_random_forest_classifier(self): print( "\ntest 27 (random forest classifier with preprocessing) [binary-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = RandomForestClassifier() pipeline_obj = Pipeline([('scaler', MinMaxScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test27sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_28_gradient_boosting_classifier(self): print( "\ntest 28 (gradient boosting classifier with preprocessing) [binary-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = GradientBoostingClassifier() pipeline_obj = Pipeline([('scaler', MinMaxScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test28sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_29_gradient_boosting_classifier(self): print( "\ntest 29 (gradient boosting classifier with preprocessing) [multi-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = GradientBoostingClassifier() pipeline_obj = Pipeline([('scaler', MaxAbsScaler()), ("model", model)]) pipeline_obj.fit(X, y) file_name = 'test29sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_30_gradient_boosting_regressor(self): print( "\ntest 30 (gradient boosting regressor without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression( ) model = GradientBoostingRegressor() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test30sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) def test_31_knn_classifier(self): print( "\ntest 31 (knn classifier without preprocessing) [binary-class]\n" ) X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification( ) model = KNeighborsClassifier() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test31sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_32_knn_classifier(self): print( "\ntest 32 (knn classifier without preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = KNeighborsClassifier() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test32sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) def test_33_knn_regressor(self): print("\ntest 33 (knn regressor without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression( ) model = KNeighborsRegressor() pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test33sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) def test_34_kmeans(self): print("\ntest 34 (kmeans without preprocessing\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification( ) model = KMeans(n_clusters=2) pipeline_obj = Pipeline([("model", model)]) pipeline_obj.fit(X, y) file_name = 'test34sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.transform(X_test) self.assertEqual( self.adapa_utility.compare_predictions(predictions, model_pred), 0) self.assertEqual( self.adapa_utility.compare_probability(probabilities, model_prob), 0) @classmethod def tearDownClass(self): print("\n******* Finished *******\n")
class TestCases(unittest.TestCase): @classmethod def setUpClass(self): print("******* Unit Test for sklearn *******") self.data_utility = DataUtility() self.adapa_utility = AdapaUtility() def test_01_linear_regression(self): print("\ntest 01 (linear regression without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression() model = LinearRegression() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test01sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) def test_02_linear_regression_with_scaler(self): print("\ntest 02 (linear regression with preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression() model = LinearRegression() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test02sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) def test_03_logistic_regression_with_scaler(self): print("\ntest 03 (logistic regression with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = LogisticRegression() pipeline_obj = Pipeline([ ("mapper", DataFrameMapper([ (["sepal length (cm)", "sepal width (cm)"], MinMaxScaler()), (["petal length (cm)", "petal width (cm)"], None) ]) ), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test03sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_04_logistic_regression_with_scaler(self): print("\ntest 04 (logistic regression with preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = LogisticRegression() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test04sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_05_logistic_regression(self): print("\ntest 05 (logistic regression without preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = LogisticRegression() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test05sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_06_logistic_regression(self): print("\ntest 06 (logistic regression without preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = LogisticRegression() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test06sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_07_ridge_classifier(self): print("\ntest 07 (Ridge Classifier) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = RidgeClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test07sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = model._predict_proba_lr(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_08_ridge_classifier(self): print("\ntest 08 (Ridge Classifier) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = RidgeClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test08sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = model._predict_proba_lr(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) @unittest.skip("") def test_09_sgd_classifier(self): print("\ntest 09 (SGD Classifier with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = SGDClassifier(loss="log") pipeline_obj = Pipeline([ ("scaler", StandardScaler()), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test09sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_10_sgd_classifier(self): print("\ntest 10 (SGD Classifier with preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = SGDClassifier(loss="log") pipeline_obj = Pipeline([ ("scaler", StandardScaler()), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test10sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_11_lda(self): print("\ntest 11 (LDA with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = LinearDiscriminantAnalysis() pipeline_obj = Pipeline([ ("scaler", MaxAbsScaler()), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test11sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_12_lda(self): print("\ntest 12 (LDA with preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = LinearDiscriminantAnalysis() pipeline_obj = Pipeline([ ("scaler", StandardScaler()), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test12sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_13_linearsvc(self): print("\ntest 13 (LinearSVC with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = LinearSVC() pipeline_obj = Pipeline([ ("scaler", StandardScaler()), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test13sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.decision_function(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_14_linearsvc(self): print("\ntest 14 (LinearSVC with preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = LinearSVC() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test14sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = model._predict_proba_lr(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_15_linearsvr(self): print("\ntest 15 (linear svr without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression() model = LinearSVR() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test15sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) def test_16_linearsvr(self): print("\ntest 16 (linear svr with preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression() model = LinearSVR() pipeline_obj = Pipeline([ ("scaler", MinMaxScaler()), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test16sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) def test_17_decisiontreeclassifier(self): print("\ntest 17 (decision tree classifier with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = DecisionTreeClassifier() pipeline_obj = Pipeline([ ("scaler", Binarizer()), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test17sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_18_decisiontreeclassifier(self): print("\ntest 18 (decision tree classifier with preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = DecisionTreeClassifier() pipeline_obj = Pipeline([ ("scaler", Binarizer()), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test18sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_19_decisiontreeclassifier(self): print("\ntest 19 (decision tree classifier without preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = DecisionTreeClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test19sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_20_decisiontreeclassifier(self): print("\ntest 20 (decision tree classifier without preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = DecisionTreeClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test20sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_21_svr(self): print("\ntest 21 (SVR without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression() model = SVR() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test21sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) def test_22_gaussian_nb(self): print("\ntest 22 (GaussianNB without preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = GaussianNB() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test22sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_23_gaussian_nb(self): print("\ntest 23 (GaussianNB without preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = GaussianNB() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test23sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_24_gaussian_nb(self): print("\ntest 24 (GaussianNB with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = GaussianNB() pipeline_obj = Pipeline([ ('scaler', StandardScaler()), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test24sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) @unittest.skip("") def test_25_random_forest_regressor(self): print("\ntest 25 (random forest regressor without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression() model = RandomForestRegressor() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test25sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) @unittest.skip("") def test_26_random_forest_classifier(self): print("\ntest 26 (random forest classifier with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = RandomForestClassifier() pipeline_obj = Pipeline([ ('scaler',MinMaxScaler()), ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test26sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) @unittest.skip("") def test_27_random_forest_classifier(self): print("\ntest 27 (random forest classifier with preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = RandomForestClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test27sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_28_gradient_boosting_classifier(self): print("\ntest 28 (gradient boosting classifier with preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = GradientBoostingClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test28sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) @unittest.skip("") def test_29_gradient_boosting_classifier(self): print("\ntest 29 (gradient boosting classifier with preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = GradientBoostingClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test29sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) @unittest.skip("") def test_30_gradient_boosting_regressor(self): print("\ntest 30 (gradient boosting regressor without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression() model = GradientBoostingRegressor() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test30sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) @unittest.skip("") def test_31_knn_classifier(self): print("\ntest 31 (knn classifier without preprocessing) [binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = KNeighborsClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test31sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_32_knn_classifier(self): print("\ntest 32 (knn classifier without preprocessing) [multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = KNeighborsClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test32sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_33_knn_regressor(self): print("\ntest 33 (knn regressor without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression() model = KNeighborsRegressor() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test33sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) def test_34_kmeans(self): print("\ntest 34 (kmeans without preprocessing\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = KMeans(n_clusters=2) pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test34sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.transform(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) @unittest.skip("") def test_35_isolation_forest(self): print("\ntest 34 (Isolation Forest\n") detection_map = { 'true': -1, 'false': 1 } X = numpy.array([ [1,2,3,4], [2,1,3,4], [3,2,1,4], [3,2,4,1], [4,3,2,1], [2,4,3,1] ], dtype=numpy.float32) test_data = numpy.array([[0,4,0,7],[4,0,4,7]]) features = ['a','b','c','d'] model = IsolationForest(n_estimators=40,contamination=0) pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X) file_name = 'test35sklearn.pmml' skl_to_pmml(pipeline_obj, features, '', file_name) model_pred = pipeline_obj.predict(test_data) model_scores = model.score_samples(test_data) model_name = self.adapa_utility.upload_to_zserver(file_name) z_predictions = self.adapa_utility.score_in_zserver(model_name,'nyoka/tests/test_forest.csv','ANOMALY') cnt = 0 for idx, value in enumerate(z_predictions): score, is_anomaly = value.split(",") score = -1 * float(score) if "{:.6f}".format(score) != "{:.6f}".format(model_scores[idx]) or model_pred[idx] != detection_map[is_anomaly]: cnt += 1 self.assertEqual(cnt,0) @unittest.skip("") def test_36_one_class_svm(self): print("\ntest 36 (One Class SVM\n") detection_map = { 'true': -1, 'false': 1 } df = pd.read_csv("nyoka/tests/train_ocsvm.csv") df_test = pd.read_csv("nyoka/tests/test_ocsvm.csv") features = df.columns model = OneClassSVM(nu=0.1) pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(df) file_name = 'test36sklearn.pmml' skl_to_pmml(pipeline_obj, features, '', file_name) model_pred = pipeline_obj.predict(df_test) model_scores = pipeline_obj.decision_function(df_test) model_name = self.adapa_utility.upload_to_zserver(file_name) z_predictions = self.adapa_utility.score_in_zserver(model_name,'nyoka/tests/test_ocsvm.csv','ANOMALY') cnt = 0 for idx, value in enumerate(z_predictions): score, is_anomaly = value.split(",") score = float(score) if "{:.6f}".format(score) != "{:.6f}".format(model_scores[idx]) or model_pred[idx] != detection_map[is_anomaly]: cnt += 1 self.assertEqual(cnt,0) def test_37_mlp_regressor(self): print("\ntest 37 (mlp regressor without preprocessing)\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_regression() model = MLPRegressor() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test37sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, _ = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) def test_38_mlp_classifier(self): print("\ntest 38 (mlp classifier without preprocessing)[multi-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_multi_class_classification() model = MLPClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test38sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) def test_39_mlp_classifier(self): print("\ntest 39 (mlp classifier without preprocessing)[binary-class]\n") X, X_test, y, features, target, test_file = self.data_utility.get_data_for_binary_classification() model = MLPClassifier() pipeline_obj = Pipeline([ ("model", model) ]) pipeline_obj.fit(X,y) file_name = 'test39sklearn.pmml' skl_to_pmml(pipeline_obj, features, target, file_name) model_name = self.adapa_utility.upload_to_zserver(file_name) predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file) model_pred = pipeline_obj.predict(X_test) model_prob = pipeline_obj.predict_proba(X_test) self.assertEqual(self.adapa_utility.compare_predictions(predictions, model_pred), True) self.assertEqual(self.adapa_utility.compare_probability(probabilities, model_prob), True) @classmethod def tearDownClass(self): print("\n******* Finished *******\n")
def setUpClass(self): print("******* Unit Test for Statsmodels *******") self.adapa_utility = AdapaUtility()
class TestCases(unittest.TestCase): @classmethod def setUpClass(self): print("******* Unit Test for RetinaNet *******") url = 'https://github.com/fizyr/keras-retinanet/releases/download/0.5.1/resnet50_coco_best_v2.1.0.h5' r = requests.get(url) with open('resnet50_coco_best_v2.1.0.h5', 'wb') as f: f.write(r.content) classes = json.load(open("nyoka/tests/categories_coco.json", 'r')) self.classes = list(classes.values()) self.adapa_utility = AdapaUtility() self.model = load_model('resnet50_coco_best_v2.1.0.h5', backbone_name='resnet50') def test_01(self): RetinanetToPmml(model=self.model, input_shape=(224, 224, 3), input_format='image', backbone_name='resnet', trained_classes=classes, pmml_file_name="RetinaNet.pmml") model_name = self.adapa_utility.upload_to_zserver('RetinaNet.pmml') z_boxes, z_scores, z_labels = self.adapa_utility.score_in_zserver( model_name, 'nyoka/tests/test_image_retinanet.png', 'RN') img = load_img('nyoka/tests/test_image_retinanet.png') img = img_to_array(img) img = preprocess_input(img) test = np.expand_dims(img, axis=0) boxes, scores, labels = self.model.predict(test) scores = scores.flatten() boxes = boxes.reshape(-1, 4) labels = labels.flatten() scores_cnt = 0 for a, b in zip(scores, z_scores): a = "{:.4f}".format(a) b = "{:.4f}".format(b) if a != b: scores_cnt += 1 labels_cnt = 0 for a, b in zip(labels, z_labels): b = self.classes.index(b) if a != b: labels_cnt += 1 boxes_cnt = 0 for a, b in zip(boxes, z_boxes): for a_, b_ in zip(a, b): a_ = "{:.2f}".format(a_) b_ = "{:.2f}".format(b_) if a_ != b_: boxes_cnt += 1 self.assertEqual(scores_cnt, 0) self.assertEqual(labels_cnt, 0) self.assertEqual(boxes_cnt, 0) @classmethod def tearDownClass(self): print("\n******* Finished *******\n")
class TestCases(unittest.TestCase): @classmethod def setUpClass(self): print("******* Unit Test for Keras *******") self.adapa_utility = AdapaUtility() self.data_utility = DataUtility() def test_01_image_classifier_with_image_as_input(self): model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) activType = 'sigmoid' x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) predictions = Dense(2, activation=activType)(x) model_final = Model(inputs=model.input, outputs=predictions, name='predictions') cnn_pmml = KerasToPmml(model_final,model_name="MobileNetImage",description="Demo",\ copyright="Internal User",dataSet='image',predictedClasses=['dogs','cats']) cnn_pmml.export(open('2classMBNet.pmml', "w"), 0) img = image.load_img('nyoka/tests/resizedCat.png') img = img_to_array(img) img = preprocess_input(img) imgtf = np.expand_dims(img, axis=0) model_pred = model_final.predict(imgtf) model_preds = {'dogs': model_pred[0][0], 'cats': model_pred[0][1]} model_name = self.adapa_utility.upload_to_zserver('2classMBNet.pmml') predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, 'nyoka/tests/resizedCat.png', True) self.assertEqual( abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True) self.assertEqual( abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True) def test_02_image_classifier_with_base64string_as_input(self): model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) activType = 'sigmoid' x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) predictions = Dense(2, activation=activType)(x) model_final = Model(inputs=model.input, outputs=predictions, name='predictions') cnn_pmml = KerasToPmml(model_final,model_name="MobileNetBase64",description="Demo",\ copyright="Internal User",dataSet='imageBase64',predictedClasses=['dogs','cats']) cnn_pmml.export(open('2classMBNetBase64.pmml', "w"), 0) img = image.load_img('nyoka/tests/resizedCat.png') img = img_to_array(img) img = preprocess_input(img) imgtf = np.expand_dims(img, axis=0) base64string = "data:float32;base64," + FloatBase64.from_floatArray( img.flatten(), 12) base64string = base64string.replace("\n", "") csvContent = "imageBase64\n\"" + base64string + "\"" text_file = open("input.csv", "w") text_file.write(csvContent) text_file.close() model_pred = model_final.predict(imgtf) model_preds = {'dogs': model_pred[0][0], 'cats': model_pred[0][1]} model_name = self.adapa_utility.upload_to_zserver( '2classMBNetBase64.pmml') predictions, probabilities = self.adapa_utility.score_in_zserver( model_name, 'input.csv', True) self.assertEqual( abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True) self.assertEqual( abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True) # def test_03_SimpleDNN(self): # X_train, X_test, y_train, columns, target_name, test_file = self.data_utility.get_data_for_regression() # model = Sequential() # model.add(Dense(13, input_dim=3, kernel_initializer='normal', activation='relu')) # model.add(Dense(23)) # model.add(Dense(1, kernel_initializer='normal')) # model.compile(loss='mean_squared_error', optimizer='adam') # model.fit(X_train, y_train, epochs=1000, verbose=0) # pmmlObj=KerasToPmml(model, model_name='Sequential', description="Demo", copyright="Internal User",\ # dataSet="inputBase64") # pmmlObj.export(open('sequentialModel.pmml','w'),0) # base64string = "data:float32;base64," + FloatBase64.from_floatArray(np.array(X_test).flatten(),12) # base64string = base64string.replace("\n", "") # csvContent = "inputBase64\n\"" + base64string + "\"" # text_file = open(test_file, "w") # text_file.write(csvContent) # text_file.close() # model_pred = model.predict(X_test) # model_name = self.adapa_utility.upload_to_zserver('sequentialModel.pmml') # predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file,True) # print(model_pred) # print(predictions) # print(probabilities) @classmethod def tearDownClass(self): print("\n******* Finished *******\n")
def setUpClass(cls): print("******* Unit Test for sklearn *******") cls.data_utility = DataUtility() cls.adapa_utility = AdapaUtility()