Exemple #1
0
def update_or_delete_task(id):

    service = DataService(current_app.config["app_context"])
    request_data = request.get_json()
    try:
        data_exist = service.check_exist_by_id(id)

        if request.method == "PUT":
            status = request_data.get("status")
            name = request_data.get("name")
            _id = request_data.get("id")
            if int(_id) != id:
                return json.jsonify({"error": "id is not match."}), 400
            if status == None or name == None:
                return json.jsonify({"error": "missing request"}), 400
            if not isinstance(status, int) or not isinstance(name, str):
                return json.jsonify({"error": "request type wrong"}), 400
            result = service.update_task(id=_id, status=status, name=name)
            return json.jsonify(result), 200

        if request.method == "DELETE":
            result = service.delete_task(id)
            return json.jsonify({"success": "delete success"}), 200
    except RuntimeError as exp:
        return json.jsonify({"error": str(exp)}), 400
    except Exception as e:
        return json.jsonify({"error": "server error"}), 500
    def run(self):

        data = DataService.load_csv("data/blood_pressure_cengage.csv")
        feature_data = data[:, :-1]
        labels = data[:, -1]
        self.linear_regression_learner = LinearRegressionLearner(
            data, learning_rate=0.001)

        # PlotService.plot3d_scatter(data, labels=['Age', 'Weight', 'BP'], title="Blood Pressure for Age and Weight.")

        normalized_data = DataService.normalize(data, method='min-max')
        normalized_feature_data = normalized_data[:, :-1]
        normalized_labels = normalized_data[:, -1]

        # PlotService.plot3d_scatter(normalized_data, labels=['Age', 'Weight', 'BP'],
        #                            title="BP for Age and Weight. Min-Max normalized.")

        self.train_with_gradient_descent(feature_data, labels,
                                         normalized_feature_data,
                                         normalized_labels)

        self.test(data)

        self.train_with_normal_equation(feature_data, labels,
                                        normalized_feature_data,
                                        normalized_labels)

        self.test(data)
Exemple #3
0
def list_tasks():

    service = DataService(current_app.config["app_context"])
    try:
        result = service.find_all_data()
        return json.jsonify(result=result), 200
    except Exception as e:
        return json.jsonify({"error": "server error"}), 500
Exemple #4
0
def new_task():

    service = DataService(current_app.config["app_context"])
    request_data = request.get_json()
    name = request_data.get("name", None)
    if not name:
        return json.jsonify({"error": "key error: name"}), 400
    try:
        result = service.create_task(name)
        return json.jsonify(result=result), 201
    except Exception as e:
        return json.jsonify({"error": "server error"}), 500
Exemple #5
0
    def run(self, k=2):

        data = DataService.load_csv("data/wdbc.data")
        # column 1 is the id, column 2 is the label, the rest are features
        labels_data = data[:, 1]

        self.kmeans_learner = KMeansLearner()

        normalized_data = DataService.normalize(
            data, method=self.normalization_method)
        normalized_feature_data = normalized_data[:, 2:]

        self.kmeans_learner.train(normalized_feature_data, k=k)
        cluster_assignments = self.kmeans_learner.classify(
            normalized_feature_data)

        self.report_service.report(cluster_assignments, labels_data, k)
Exemple #6
0
    def run(self):

        data = DataService.load_csv("data/wdbc.data")
        # column 1 is the id, column 2 is the label, the rest are features
        feature_data = data[:, 2:]
        labels = data[:, 1]

        config = NeuralNetConfig(num_inputs=feature_data.shape[1],
                                 num_nodes_hidden_layers=[16],
                                 num_outputs=1,
                                 learning_rate=0.01,
                                 epochs=800)

        self.neural_net_learner = NeuralNetLearner(config)

        normalized_data = DataService.normalize(data, method='z')
        normalized_feature_data = normalized_data[:, 2:]

        self.train_with_gradient_descent(data, labels, normalized_feature_data)
Exemple #7
0
    def run(self):

        data = DataService.load_csv("data/wdbc.data")
        # column 1 is the id, column 2 is the label, the rest are features
        labels_data = data[:, 1]

        self.knn_learner = KnnLearner()

        normalized_data = DataService.normalize(data, method='z')
        normalized_feature_data = normalized_data[:, 2:]

        self.knn_learner.train(normalized_feature_data, labels_data, k=5)

        predictions = []
        for test_record in normalized_feature_data:
            prediction = self.knn_learner.predict(test_record)
            predictions.append(prediction)

        predictions = np.array(predictions)

        self.report_service.report(data, predictions, labels_data, self.knn_learner)
Exemple #8
0
    def run(self, k=2):

        data = DataService.load_csv("data/wdbc.data")
        # column 1 is the id, column 2 is the label, the rest are features
        labels = data[:, 1]

        self.em_learner = EMLearner()

        normalized_data = DataService.normalize(
            data, method=self.normalization_method)
        normalized_feature_data = normalized_data[:, 2:]

        feature_data = data[:, 2:]
        # feature_data = normalized_feature_data
        self.em_learner.train(feature_data, labels, k=k)

        record_k_gaussian_probabilites, cluster_assignments = self.em_learner.estimate_probabilities(
            feature_data)
        self.report_service.accuracy_stats_report(
            data, record_k_gaussian_probabilites, cluster_assignments, labels)
        self.report_service.report(cluster_assignments, labels,
                                   self.em_learner.k)
Exemple #9
0
    def run(self):

        data = DataService.load_csv("data/blood_pressure_cengage.csv")
        feature_data = data[:, :-1]
        labels = data[:, -1]

        #add one for the bias
        feature_data = np.insert(feature_data, 0, 1, axis=1)
        features_tensor = torch.tensor(feature_data.astype(np.float32))
        labels_tensor = torch.tensor(labels.astype(np.float32))

        train_epochs = 500
        self.linear_regression_learner.train(features_tensor,
                                             labels_tensor,
                                             epochs=train_epochs)

        trained_predictions = self.linear_regression_learner.predict(
            features_tensor)

        print(
            "|Age|Weight|Actual Blood Pressure|Predicted Blood Pressure|Loss|")
        print(
            "|:-------:|:---:|:--------------------:|:---------------------:|")

        train_records = data[:, :-1]
        predicted_labels = []
        for record_index, prediction in enumerate(trained_predictions):
            age = train_records[record_index, 0]
            weight = train_records[record_index, 1]
            label = labels[record_index]
            predicted_label = np.rint(prediction.data.numpy()[0])
            predicted_labels.append(predicted_label)
            loss = np.abs(label - predicted_label)

            print("|{0}|{1}|{2}|{3}|{4}|".format(age, weight, label,
                                                 predicted_label, loss))

        PlotService.plot_line(x=np.arange(train_epochs),
                              y=self.linear_regression_learner.loss_history,
                              x_label="Torch Epochs",
                              y_label="Loss",
                              title="Absolute Loss per Epoch")

        PlotService.plot3d_scatter_compare(train_records,
                                           labels,
                                           trained_predictions.data.numpy(),
                                           labels=['Age', 'Weight', 'BP'],
                                           title="Actual vs Projected")
Exemple #10
0
    def run(self, k=2):

        data = DataService.load_csv("data/wdbc.data")
        # column 1 is the id, column 2 is the label, the rest are features
        feature_data = data[:, 2:]
        labels_data = data[:, 1]

        self.naive_bayes_learner = NaiveBayesLearner()
        self.naive_bayes_learner.train(feature_data, labels_data)

        predictions = []
        for record_index, feature_record in enumerate(feature_data):
            prediction = self.naive_bayes_learner.predict(feature_record)
            predictions.append(prediction)

        self.report_service.report(data, predictions, labels_data, self.naive_bayes_learner)
Exemple #11
0
    def run(self, k=2):

        feature_data, labels = DataService.get_data("data/play_tennis.csv")
        self.decision_tree_learner.train(feature_data, labels)

        errors = 0
        for test_index in range(labels.size):
            prediction = self.decision_tree_learner.classify(
                feature_data[test_index, :])
            actual = labels[test_index]

            if prediction != actual:
                errors += 1

        accuracy = (labels.size - errors) / labels.size

        print("Accuracy: ", accuracy)
Exemple #12
0
    def run(self):

        data = DataService.load_csv("data/blood_pressure_cengage.csv")
        feature_data = data[:, :-1]
        labels = data[:, -1].reshape(feature_data.shape[0], 1)

        lin_reg = LinearRegression()
        lin_reg_model = lin_reg.fit(feature_data, labels)
        predictions = lin_reg_model.predict(feature_data)

        ReportsService.print_results_table(data, predictions, labels)

        PlotService.plot3d_scatter_compare(
            feature_data,
            labels,
            predictions,
            labels=['Age', 'Weight', 'BP'],
            title="Actual vs Projected LinearRegressor")
    def train_with_normal_equation(self, feature_data, labels_data,
                                   normalized_feature_data, normalized_labels):

        print("Normal Equation: ")

        # Normal equation
        feature_data_bias = np.insert(normalized_feature_data, 0, 1, axis=1)
        x_trans_x = np.dot(np.transpose(feature_data_bias), feature_data_bias)
        norm_equation_theta = np.dot(
            np.dot(np.linalg.inv(x_trans_x), np.transpose(feature_data_bias)),
            normalized_labels)
        print(norm_equation_theta)

        self.linear_regression_learner.theta = norm_equation_theta

        normalized_predictions = self.linear_regression_learner.predict(
            normalized_feature_data)
        predictions = np.around(
            DataService.denormalize_predictions(labels_data,
                                                normalized_predictions,
                                                method='min-max'))
        cost = self.linear_regression_learner.calculate_cost(
            normalized_predictions, normalized_labels)
        print("Final Norm Equation Normalized Cost: ", cost)

        x, y, z = self.build_model_plot_data(feature_data, predictions)
        PlotService.plot3d_line(
            x,
            y,
            z,
            labels=['Age', 'Weight', 'BP'],
            title="Normal Equation Model: Blood Pressure for Age and Weight.")

        PlotService.plot3d_scatter_compare(
            feature_data,
            labels_data,
            predictions,
            labels=['Age', 'Weight', 'BP'],
            title="Normal Equation Actual vs Projected")
    def train_with_gradient_descent(self, feature_data, labels_data,
                                    normalized_feature_data,
                                    normalized_labels):

        self.linear_regression_learner.train(normalized_feature_data,
                                             normalized_labels)

        normalized_predictions = self.linear_regression_learner.predict(
            normalized_feature_data)
        predictions = np.around(
            DataService.denormalize_predictions(labels_data,
                                                normalized_predictions,
                                                method='min-max'))

        PlotService.plot_line(x=range(
            1,
            len(self.linear_regression_learner.cost_history) + 1),
                              y=self.linear_regression_learner.cost_history,
                              x_label="Iteration",
                              y_label="Training Cost",
                              title="Training Learning Curve")

        x, y, z = self.build_model_plot_data(feature_data, predictions)
        PlotService.plot3d_line(
            x,
            y,
            z,
            labels=['Age', 'Weight', 'BP'],
            title="Linear Model: Blood Pressure for Age and Weight.")

        PlotService.plot3d_scatter_compare(feature_data,
                                           labels_data,
                                           predictions,
                                           labels=['Age', 'Weight', 'BP'],
                                           title="Actual vs Projected")

        cost = self.linear_regression_learner.calculate_cost(
            normalized_predictions, normalized_labels)
        print("Final Normalized Cost: ", cost)
Exemple #15
0
import tensorflow as tf
from service.data_service import DataService
import numpy as np
from service.plot_service import PlotService

data = DataService.load_csv("data/blood_pressure_cengage.csv")

def get_data():
    feature_data = data[:, :-1]
    # add 1 at the beginning for the bias
    feature_data = np.insert(feature_data, 0, 1, axis=1)
    labels = data[:, -1]
    return {"Age": feature_data[:, 0], "Weight": feature_data[:, 1]}, labels


def get_feature_data():
    feature_data, _ = get_data()
    return feature_data


def get_labels():
    _, labels = get_data()
    return labels


age_column = tf.feature_column.numeric_column('Age')
weight_column = tf.feature_column.numeric_column('Weight')

lin_reg = tf.estimator.LinearRegressor(feature_columns=[age_column, weight_column])

steps_history = []
    def test(self, data):

        x_mins = np.amin(data, axis=0)
        x_maxs = np.amax(data, axis=0)

        min_age = x_mins[0]
        max_age = x_maxs[0]

        min_weight = x_mins[1]
        max_weight = x_maxs[1]

        min_blood_pressure = x_mins[2]
        max_blood_pressure = x_maxs[2]

        x_means = np.mean(data, axis=0)
        mean_age = x_means[0]
        mean_weight = x_means[1]
        mean_blood_pressure = x_means[2]

        x_stds = np.std(data, axis=0)
        std_age = x_stds[0]
        std_weight = x_stds[1]
        std_blood_pressure = x_stds[2]

        while True:

            age = input("Enter Age or type quit to exit: ")
            if age == 'quit':
                break
            weight = input("Enter Weight or type quit to exit: ")
            if weight == 'quit':
                break

            try:
                age = int(age)
                weight = int(weight)
            except ValueError:
                print("Age and Weight should be integers.", age, weight)
                break

            test_age = DataService.normalize_single(
                age,
                min=min_age,
                max=max_age,
                mean=mean_age,
                std=std_age,
                method=self.normalization_method)
            test_weight = DataService.normalize_single(
                weight,
                min=min_weight,
                max=max_weight,
                mean=mean_weight,
                std=std_weight,
                method=self.normalization_method)

            current_theta_normalized_projection = \
                self.linear_regression_learner.predict(np.array([test_age, test_weight]).reshape(1, 2))

            print(
                "Current Theta Projection: ",
                DataService.denormalize_single(
                    current_theta_normalized_projection,
                    method=self.normalization_method,
                    min=min_blood_pressure,
                    max=max_blood_pressure,
                    mean=mean_blood_pressure,
                    std=std_blood_pressure))
            # best documented theta min-max norm: -0.04973713  0.7198039   0.33875262
            # best documented theta zscore norm: -0.03447741  0.70838301  0.32258052
            best_theta = np.array([-0.03447741, 0.70838301,
                                   0.32258052]).reshape((1, 3))
            if self.normalization_method == 'min-max':
                best_theta = np.array([-0.04973713, 0.7198039,
                                       0.33875262]).reshape((1, 3))

            best_theta_normalized_projection = self.linear_regression_learner\
                .predict_for_theta(np.array([test_age, test_weight]).reshape(1, 2), best_theta)

            print(
                "Best Theta Projection: ",
                DataService.denormalize_single(
                    best_theta_normalized_projection,
                    method=self.normalization_method,
                    min=min_blood_pressure,
                    max=max_blood_pressure,
                    mean=mean_blood_pressure,
                    std=std_blood_pressure))
Exemple #17
0
 def setUp(self):
     self.service = DataService(app_context)
Exemple #18
0
class ServiceTestCase(unittest.TestCase):
    task_data = None

    def setUp(self):
        self.service = DataService(app_context)

    def test_0100_find_all_data_success(self):
        find_all_patch = patch.object(DataDao,
                                      'find_all',
                                      side_effect=self.mock_find_all)
        find_all_patch.start()
        return_data = self.service.find_all_data()
        assert return_data == [{
            "id": 1,
            "name": "name",
            "status": 0
        }, {
            "id": 2,
            "name": "name2",
            "status": 1
        }]
        find_all_patch.stop()

    def test_0200_check_exist_by_id_success(self):
        """
        Test case:
        return_data must be true
        """
        check_exist_by_id_patch = patch.object(
            DataDao,
            'check_exist_by_id',
            side_effect=self.mock_check_exist_by_id)
        check_exist_by_id_patch.start()
        id = 1
        return_data = self.service.check_exist_by_id(id)
        assert return_data == True
        check_exist_by_id_patch.stop()

    def test_0201_check_exist_by_id_fail(self):
        """
        Test case:
        function will raise RuntimeError
        """
        self.assertRaisesRegex(RuntimeError, "id is not found in database",
                               self.check_exist_by_id_fail)

    def check_exist_by_id_fail(self):
        check_exist_by_id_patch = patch.object(
            DataDao,
            'check_exist_by_id',
            side_effect=self.mock_check_exist_by_id)
        check_exist_by_id_patch.start()
        id = 2
        self.service.check_exist_by_id(id)
        check_exist_by_id_patch.stop()

    def test_0300_create_task_success(self):
        """
        Test case:
        create task will return data
        """
        name = "test"
        insert_data_patch = patch.object(DataDao,
                                         'insert_data',
                                         side_effect=self.mock_insert_data)
        get_by_id_patch = patch.object(DataDao,
                                       'get_by_id',
                                       side_effect=self.mock_get_by_id)
        get_by_id_patch.start()
        insert_data_patch.start()
        return_data = self.service.create_task(name)
        insert_data_patch.stop()
        get_by_id_patch.stop()
        assert return_data == {"id": 1, "name": name, "status": 0}

    def test_0400_update_task_success(self):
        """
        Test case:
        update task will return data
        """
        _id = 1
        status = 1
        name = "test2"
        update_data_patch = patch.object(DataDao,
                                         'update_data',
                                         side_effect=self.mock_update_data)
        get_by_id_patch = patch.object(DataDao,
                                       'get_by_id',
                                       side_effect=self.mock_get_by_id)
        get_by_id_patch.start()
        update_data_patch.start()
        return_data = self.service.update_task(_id, status=status, name=name)
        update_data_patch.stop()
        get_by_id_patch.stop()
        assert return_data == {"id": 1, "name": "test2", "status": 1}

    def test_0500_delete_task_success(self):
        """
        Test case:
        delete task success
        """
        _id = 1
        delete_data_patch = patch.object(DataDao,
                                         'delete_data',
                                         side_effect=self.mock_delete_data)
        delete_data_patch.start()
        return_data = self.service.delete_task(_id)
        delete_data_patch.stop()
        assert return_data == "success"

    def test_0501_delete_task_fail(self):
        """
        Test case:
        delete task fail
        """
        self.assertRaisesRegex(RuntimeError, "id is not found in database",
                               self.delete_task_fail)

    def delete_task_fail(self):
        _id = 2
        delete_data_patch = patch.object(DataDao,
                                         'delete_data',
                                         side_effect=self.mock_delete_data)
        delete_data_patch.start()
        return_data = self.service.delete_task(_id)
        delete_data_patch.stop()

    def mock_find_all(self, *args, **kwargs):
        return [{
            "id": 1,
            "name": "name",
            "status": 0
        }, {
            "id": 2,
            "name": "name2",
            "status": 1
        }]

    def mock_check_exist_by_id(self, id):
        if id == 1:
            return True
        else:
            return False

    def mock_insert_data(self, name):
        self.__class__.task_data = {"id": 1, "name": name, "status": 0}
        return 1

    def mock_get_by_id(self, id):
        if id == 1:
            return self.task_data

    def mock_update_data(self, id, **kwargs):
        self.__class__.task_data = {
            "id": id,
            "name": kwargs["name"],
            "status": kwargs["status"]
        }

    def mock_delete_data(self, id):
        if id == 1:
            return True
        else:
            return False