Logistic Regression
Despite its name, logistic regression is a classification algorithm, not a regression algorithm. Logistic regression is generally a good first step for trying out a classification model before moving on to more advanced options. Logistic regression shares many restrictions with the support vector machines model. Both models require that all features are numeric. However, you can still use non-numeric data if you convert it using one-hot encoding. The class labels can be any data type.Use one-hot encoding to convert non-numeric data into numeric form.For example, if a feature is a color (such as red, green, or blue), then one-hot encoding can make three columns named
is_red, is_green, and is_blue. For each row, set one color to the value 1 and the other colors to 0.lr_input table, which represents the academic performance of students.
SQL
hours_studied and the grade in the previous course grade_in_prev_course. Both input features are numeric. There are also two classes: true and false. Class values do not need to be Boolean, and they can be any distinct values.
Students who have better grades in the previous course and spend more hours studying are more likely to have top marks in the course. However, this table also includes a randomness factor, which means that rows with the same features can have different classifications. This is normal in real-world data.
This example creates the logistic regression model on the table, including the metrics option. This option causes the model to calculate the percentage of samples that are correctly classified. The Ocient System puts this information into the system catalog tables.
SQL
machine_learning_models and logistic_regression_models system catalog tables.
SQL
correctly_classified column shows the model has almost 84 percent accuracy from working on the training data. This level of accuracy is pretty good, especially for a model using training data with significant randomness.
This model can predict which classification is most likely if you provide the number of hours studied and grades in the previous course.
First, review the average hours spent studying for each classification.
SQL
got_top_marks is true have a higher average value for hours_studied. The same should be true with grade_in_prev_course.
SQL
SQL
Support Vector Machine
Support vector machine (SVM) tries to find a hyperplane to divide data into two classes with these objectives:- The hyperplane has the maximum margin, meaning the most distance between the two classes.
- The model correctly classifies the highest percentage of data points.
regularizationCoefficient option.
For data sets with two features, the SVM hyperplane represents a straight line on a Cartesian graph.For three features, the hyperplane represents a plane.
SQL
machine_learning_models and support_vector_machine_models system catalog tables for SVM contain almost the same information as logistic regression.
SQL
function1, function2, function3, etc. Each function defines the formula for the kernel to create new features. Functions can use features from the original data set, represented by x1, x2, x3, etc.
To explain this, revisit the circle example. Suppose that the circle centered at the point (2, 2) is the class inside. Otherwise, the circle is the class outside. The data that is separable by a circle in two dimensions is linearly separable in three dimensions with the features x1, x2, x1^2 + x2^2.
This statement creates the data and the model.
SQL
x1 and x2. If you examine the correctly_classified value in the machine_learning_models and support_vector_machine_models system catalog tables, you can see a model that classifies nearly everything correctly.
SQL
The sys.machine_learning_model_options table is the location where the SVM model saves kernel information.This table saves all the options that you specify. Some models also add additional keys to this dictionary to save certain information needed for when the model is executed.
SQL
Binary Classification with Neural Networks
A neural network using theFEEDFORWARD NETWORK model type can perform binary classification. To use this model for classification, you must either convert labels to either 0 and 1 (using the log_loss option) or convert labels to -1 and 1 (using the hinge_loss option). Both options can achieve the same result.
This model can handle more complex cases, but using a different model for linearly separable data might be easier. In other words, a neural network model is useful when data is difficult to separate linearly using a kernel.
This example shows a feedforward network model using the log_loss option. Note the CASE statement to convert labels to 0 or 1.
SQL
SQL
hinge_loss option, use -1 and 1 in the CASE statement.
SQL
hinge_loss version is exactly the same as the log_loss version.
SQL
K-Nearest Neighbors
K-nearest neighbors (KNN) can only use numeric features, but KNN handles multi-classification in the Ocient System. This algorithm can handle an arbitrary number of target classes. One drawback with KNN is that it does not work at training time other than to take a snapshot of the training data. The algorithm operates on the data when you execute the model, so it can be a slow model for large data sets. To demonstrate KNN functionality, this example uses a similar circle of size (2, 2) with some modifications to use more classes:- Data within a unit circle of (2, 2) is classified as
INSIDE. - Data within a radius of 2 is classified as
MIDDLE. - Any other data is classified as
OUTSIDE.
SQL
k nearest points to the one the model is trying to predict a classification for. k is an integer value that you must specify during model creation.
The model calculates distances using normal Euclidean distances. You can override the definition of distance by specifying a formula using the distance option.
After the model identifies the points, it computes a score for each class and returns the class with the highest score. The weight option can influence each score for the specified point. By default, the weight value is based on the inverse of the distance (1.0/(d+0.1)). In other words, a point closer to the point you are trying to predict has a larger influence on the output result than a point further away. Any point outside of the nearest k points has no influence.
This example uses three classes.
SQL
SQL
dataReduction option to true. By default, the execution stops when the database finds a model that is 90 percent accurate over the training data or when the reduced model increases to 1,000 rows of data. You can override these defaults by setting the maxReducedRows or targetAccuracy options.
Additionally, if you are using data reduction, you can ask the database to collect metrics on the percent of rows correctly classified by the model because this is much faster to do on the reduced data set.
This example builds another KNN model over the same data and uses data reduction to find a model that is at least 95 percent accurate.
SQL
machine_learning_models and k_nearest_neighbor_models system catalog tables to see the snapshot table used for the model and its accuracy.
SQL
SQL
Multi-Class Classification with Neural Networks
TheFEEDFORWARD NETWORK model type can also perform multi-class classification when the feature classes are numeric. Multi-class classification works by using vectors, where each position in the vector indicates the score for that class. The argmax value of the score of each vector returns the class index.
Much of this example is similar to the previous Binary Classification with Neural Networks tutorial, with these modifications to the model options:
- Set the
lossFunctionoption tocross_entropy. - Set the
outputsoption to specify the number of classes for the model to use (in this example:3). - Set the
useSoftMaxoption totrue, which applies a softmax function to the final vector.
When the loss function is
cross_entropy_loss , the option useSoftMax defaults to true.SQL
SQL
VECTOR_ARGMAX function to retrieve the index of the largest value. This function returns the 1‑based index of the largest value in the vector.
SQL
SQL
hiddenLayershiddenLayerSizelossFuncNumSamples
Multi-Class Classification with Support Vector Machines and Logistic Regression
Both logistic regression and SVM are binary classifiers, but you can use either to build a multi-class classifier to handle any number of classes. OcientML can perform this automatically with either model. This example shows theCREATE statement for the SVM model, which uses the same data as the K Nearest Neighbors and Multi-Class Classification with Neural Networks examples.
SQL
machine_learning_models and support_vector_machine_models system catalog tables to see the accuracy.
SQL
SQL
Naive Bayes
The first of these models is naive Bayes. This model can handle binary or multi-class classification and features of any data type. By default, naive Bayes treats each feature as a discrete value. For example, if you assign features with values of1, 2, 3, and 4, the model does not give any numeric significance to them. In this case, this model cannot predict the class for the sample with a feature value of 3.5 because that value is not explicitly assigned as a feature.
However, the naive Bayes model can define certain input features as continuous features. These should be numeric values. In this case, the naive Bayes model finds the best-fit normal distribution for that feature to do the probability calculations.
Naive Bayes models essentially use the probability rules of Bayes’ Theorem to compute the most likely class based on the training data. The rules are only correct if the features are independent (uncorrelated). However, in practice, the models tend to work well with real-world data.
This example uses this simple table to demonstrate naive Bayes models.
SQL
color, type, and origin are the input features, which are all strings. true and false are the class labels. In this case, all of the input features are discrete.
This example builds the naive Bayes model.
SQL
machine_learning_models and naive_bayes_models system catalog tables for naive Bayes models.
SQL
correctly_classified value indicates the accuracy is 80 percent, meaning just two rows are classified incorrectly. The two table names in the temp schema hold all the necessary data for executing the model as a function.
This query calls the model to classify new data.
SQL
SQL
SQL
continuousFeatures option is a comma-separated list (no spaces) of input column indexes that are continuous. Indexes start at 1.
This query checks the correctly_classified value in the machine_learning_models and naive_bayes_models system catalog tables.
SQL
Decision Trees
Unlike naive Bayes, the decision tree model allows you to see how the model works and makes its decisions. A decision tree branches based on the value of a single feature, while the leaves of the tree are the output classes. For SQL syntax, this model uses multiple nestedCASE statements. You can examine the whole nested CASE statement that defines the model, but it might be very long.
The decision tree model in Ocient has limited support for continuous features. The model finds the average value of the feature and allows a two-way branch based on whether the value is lower or higher than the average.
Creating a decision tree model is exactly like a naive Bayes model, except with a different type. Decision trees also use the continuousFeatures option, which works the same way.
These decision tree examples use the same data sets as the naive Bayes examples.
SQL
machine_learning_models and decision_tree_models system catalog tables.
SQL
CASE statement that defines the model.
SQL
SQL
SQL
machine_learning_models and decision_tree_models system catalog tables to see the accuracy of this model.
SQL

