Loading...

vastorbit.machine_learning.vast.linear_model.LogisticRegression

class vastorbit.machine_learning.vast.linear_model.LogisticRegression(name: str = None, overwrite_model: bool = False, **kwargs)

Creates a LogisticRegression object using scikit-learn for training and the scalability of VAST DataBase for the inferences.

Parameters:
  • name (str, optional) – Name of the model. The model is stored in the database.

  • overwrite_model (bool, optional) – If set to True, training a model with the same name as an existing model overwrites the existing model.

  • **kwargs (scikit-learn model parameters.)

Variables:
  • created (Many attributes are)

  • phase. (during the fitting)

  • coef_ (numpy.array) – The regression coefficients. The order of coefficients is the same as the order of columns used during the fitting phase.

  • intercept_ (float) – The expected value of the dependent variable when all independent variables are zero, serving as the baseline or constant term in the model.

  • feature_importances_ (numpy.array) – The importance of features is computed through the model coefficients, which are normalized based on their range. Subsequently, an activation function calculates the final score. It is necessary to use the features_importance() method to compute it initially, and the computed values will be subsequently utilized for subsequent calls.

  • classes_ (numpy.array) – The classes labels.

  • note:: (..) – All attributes can be accessed using the get_attributes() method.

Examples

The following examples provide a basic understanding of usage. For more detailed examples, please refer to the Machine Learning or the Examples section on the website.

Load data for machine learning

We import vastorbit:

import vastorbit as vo

Hint

By assigning an alias to vastorbit, we mitigate the risk of code collisions with other libraries. This precaution is necessary because vastorbit uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions from vastorbit are used as intended without interfering with functions from other libraries.

For this example, we will use the winequality dataset.

import vastorbit.datasets as vod

data = vod.load_winequality()
123
fixed_acidity
Decimal(6, 3)
123
volatile_acidity
Decimal(7, 4)
123
citric_acid
Decimal(6, 3)
123
residual_sugar
Decimal(7, 3)
123
chlorides
Double
123
free_sulfur_dioxide
Decimal(7, 2)
123
total_sulfur_dioxide
Decimal(7, 2)
123
density
Double
123
ph
Decimal(6, 3)
123
sulphates
Decimal(6, 3)
123
alcohol
Double
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
16.30.670.4812.60.05257.0222.00.99793.170.529.360white
27.40.40.295.40.04431.0122.00.9943.30.511.181white
37.10.260.312.20.04429.0128.00.99373.340.6410.981white
49.00.310.486.60.04311.073.00.99382.90.3811.650white
56.30.390.246.90.0699.0117.00.99423.150.3510.240white
68.20.220.366.80.03412.090.00.99443.010.3810.581white
77.10.190.283.60.03316.078.00.9932.910.7811.460white
87.30.250.3613.10.0535.0200.00.99863.040.468.971white
97.90.20.341.20.0429.0118.00.99323.140.4110.660white
107.10.260.325.90.03739.097.00.99343.310.411.660white
117.00.20.345.70.03532.083.00.99283.190.4611.560white
126.90.30.334.10.03526.0155.00.99253.250.7912.381white
138.10.290.497.10.04222.0124.00.99443.140.4110.860white
145.80.170.31.40.03755.0130.00.99093.290.3811.360white
155.90.4150.020.80.03822.063.00.99323.360.369.350white
166.60.230.261.30.04516.0128.00.99343.360.610.060white
178.60.550.3515.550.05735.5366.51.00013.040.6311.030white
186.90.350.741.00.04418.0132.00.9923.130.5510.250white
197.60.140.741.60.0427.0103.00.99163.070.410.871white
209.20.280.4911.80.04229.0137.00.9983.10.3410.140white
216.20.180.494.50.04717.090.00.99193.270.3711.660white
225.30.1650.241.10.05125.0105.00.99253.320.479.150white
239.80.250.7410.00.05636.0225.00.99773.060.4310.040white
248.10.290.497.10.04222.0124.00.99443.140.4110.860white
256.80.220.490.90.05226.0128.00.9913.250.3511.460white
267.20.220.491.00.04534.0140.00.993.050.3412.760white
277.40.250.491.10.04235.0156.00.99173.130.5511.350white
288.20.180.491.10.03328.081.00.99233.00.6810.471white
296.10.220.491.50.05118.087.00.99283.30.469.650white
307.00.390.241.00.0488.0119.00.99233.00.3110.140white
316.10.220.491.50.05118.087.00.99283.30.469.650white
326.50.360.492.90.0316.094.00.99023.10.4912.171white
337.10.290.491.20.03132.099.00.98933.070.3312.260white
347.40.250.491.10.04235.0156.00.99173.130.5511.350white
356.90.230.2414.20.05319.094.00.99823.170.59.650white
368.50.560.7417.850.05151.0243.01.00052.990.79.250white
378.20.180.491.10.03328.081.00.99233.00.6810.471white
386.30.230.497.10.0567.0210.00.99513.230.349.550white
396.10.250.497.60.05267.0226.00.99563.160.478.950white
407.20.260.7413.60.0556.0162.00.9983.030.448.850white
417.20.310.241.40.05717.0117.00.99283.160.3510.550white
428.00.250.491.20.06127.0117.00.99383.080.349.450white
437.00.180.495.30.0434.0125.00.99143.240.412.260white
447.80.430.4913.00.03337.0158.00.99553.140.3511.360white
458.30.20.744.450.04433.0130.00.99243.250.4212.260white
466.30.270.491.20.06335.092.00.99113.380.4212.260white
477.40.160.491.20.05518.0150.00.99173.230.4711.260white
487.40.160.491.20.05518.0150.00.99173.230.4711.260white
496.90.190.496.60.03649.0172.00.99323.20.2711.560white
507.80.430.4913.00.03337.0158.00.99553.140.3511.360white
517.20.40.491.10.04811.0138.00.99293.010.429.350white
527.80.430.4913.00.03337.0158.00.99553.140.3511.360white
537.60.520.4914.00.03437.0156.00.99583.140.3811.871white
548.30.210.4919.80.05450.0231.01.00122.990.549.250white
556.90.340.7411.20.06944.0150.00.99683.00.819.250white
566.30.270.491.20.06335.092.00.99113.380.4212.260white
578.30.20.744.450.04433.0130.00.99243.250.4212.260white
587.10.220.742.70.04442.0144.00.9913.310.4112.260white
597.90.110.494.50.04827.0133.00.99463.240.4210.660white
608.50.170.743.60.0529.0128.00.99283.280.412.460white
616.40.1450.495.40.04854.0164.00.99463.560.4410.860white
627.40.160.491.20.05518.0150.00.99173.230.4711.260white
638.30.190.491.20.05111.0137.00.99183.060.4611.060white
648.00.440.499.10.03146.0151.00.99263.160.2712.781white
657.00.20.740.80.04419.0163.00.99313.460.5310.250white
666.90.190.496.60.03649.0172.00.99323.20.2711.560white
677.10.250.493.00.0330.096.00.99033.130.3912.371white
686.50.240.241.60.04615.060.00.99283.190.399.850white
697.20.40.491.10.04811.0138.00.99293.010.429.350white
707.60.520.4914.00.03437.0156.00.99583.140.3811.871white
717.80.430.4913.00.03337.0158.00.99553.140.3511.360white
727.80.210.491.350.0526.048.00.99113.150.2811.450white
737.00.20.495.90.03839.0128.00.99383.210.4810.860white
746.90.250.243.60.05713.085.00.99422.990.489.540white
757.20.080.491.30.0518.0148.00.99453.460.4410.260white
767.10.850.498.70.02840.0184.00.99623.220.3610.750white
777.60.510.241.20.0410.0104.00.9923.050.2910.860white
787.90.220.244.60.04439.0159.00.99272.990.2811.560white
797.70.160.492.00.05620.0124.00.99483.320.4910.760white
807.20.080.491.30.0518.0148.00.99453.460.4410.260white
816.60.250.241.70.04826.0124.00.99423.370.610.160white
826.70.160.492.40.04657.0187.00.99523.620.8110.460white
836.90.250.243.60.05713.085.00.99422.990.489.540white
847.50.320.244.60.0538.0134.00.99583.140.59.130white
857.40.280.491.50.03420.0126.00.99182.980.3910.660white
866.20.150.490.90.03317.051.00.99323.30.79.460white
876.70.250.7419.40.05444.0169.01.00043.510.459.860white
886.50.260.7413.30.04468.0224.00.99723.180.549.560white
897.90.160.7417.850.03752.0187.00.99982.990.419.350white
905.60.1850.491.10.0328.0117.00.99183.550.4510.360white
917.50.20.491.30.0318.097.00.99183.060.6211.150white
928.00.30.499.40.04647.0188.00.99643.140.4810.050white
938.00.340.499.00.03339.0180.00.99363.130.3812.381white
947.70.350.498.650.03342.0186.00.99313.140.3812.481white
957.60.290.499.60.0345.0197.00.99383.130.3812.371white
966.70.620.241.10.0396.062.00.99343.410.3210.450white
976.80.270.491.20.04435.0126.00.993.130.4812.171white
987.70.270.491.80.04123.086.00.99143.160.4212.560white
996.70.510.242.10.04314.0155.00.99043.220.613.060white
1007.40.190.499.30.0326.0132.00.9942.990.3211.071white
Rows: 1-100 | Columns: 14

Note

vastorbit offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the vastorbit environment.

You can easily divide your dataset into training and testing subsets using the VastFrame.train_test_split() method. This is a crucial step when preparing your data for machine learning, as it allows you to evaluate the performance of your models accurately.

data = vod.load_winequality()
train, test = data.train_test_split(test_size = 0.2)

Warning

In this case, vastorbit utilizes seeded randomization to guarantee the reproducibility of your data split. However, please be aware that this approach may lead to reduced performance. For a more efficient data split, you can use the VastFrame.to_db() method to save your results into tables or temporary tables. This will help enhance the overall performance of the process.

Balancing the Dataset

In vastorbit, balancing a dataset to address class imbalances is made straightforward through the balance() function within the preprocessing module. This function enables users to rectify skewed class distributions efficiently. By specifying the target variable and setting parameters like the method for balancing, users can effortlessly achieve a more equitable representation of classes in their dataset. Whether opting for over-sampling, under-sampling, or a combination of both, vastorbit’s balance() function streamlines the process, empowering users to enhance the performance and fairness of their machine learning models trained on imbalanced data.

To balance the dataset, use the following syntax.

from vastorbit.machine_learning.vast.preprocessing import balance

balanced_train = balance(
    name = "my_schema.train_balanced",
    input_relation = train,
    y = "good",
    method = "hybrid",
)

Note

With this code, a table named train_balanced is created in the my_schema schema. It can then be used to train the model. In the rest of the example, we will work with the full dataset.

Hint

Balancing the dataset is a crucial step in improving the accuracy of machine learning models, particularly when faced with imbalanced class distributions. By addressing disparities in the number of instances across different classes, the model becomes more adept at learning patterns from all classes rather than being biased towards the majority class. This, in turn, enhances the model’s ability to make accurate predictions for under-represented classes. The balanced dataset ensures that the model is not dominated by the majority class and, as a result, leads to more robust and unbiased model performance. Therefore, by employing techniques such as over-sampling, under-sampling, or a combination of both during dataset preparation, practitioners can significantly contribute to achieving higher accuracy and better generalization of their machine learning models.

Model Initialization

First we import the LogisticRegression model:

from vastorbit.machine_learning.vast import LogisticRegression

Then we can create the model:

model = LogisticRegression(
    tol = 1e-6,
    max_iter = 100,
    solver = 'newton-cg',
    fit_intercept = True,
)

Important

The model name is crucial for the model management system and versioning. It’s highly recommended to provide a name if you plan to reuse the model later.

Model Training

We can now fit the model:

model.fit(
    train,
    [
        "fixed_acidity",
        "volatile_acidity",
        "citric_acid",
        "residual_sugar",
        "chlorides",
        "density",
    ],
    "good",
    test,
)

Important

To train a model, you can directly use the VastFrame or the name of the relation stored in the database. The test set is optional and is only used to compute the test metrics. In vastorbit, we don’t work using X matrices and y vectors. Instead, we work directly with lists of predictors and the response name.

Features Importance

We can conveniently get the features importance:

result = model.features_importance()

Note

For LinearModel, feature importance is computed using the coefficients. These coefficients are then normalized using the feature distribution. An activation function is applied to get the final score.

Metrics

We can get the entire report using:

model.report()
value
auc0.6670646766169173
prc_auc0.8024514098720474
accuracy0.7905511811023622
log_loss0.4588865938418838
precision0.0
recall0.0
f1_score0.0
mcc0.0
informedness0.0
markedness-0.20944881889763778
csi0.0
Rows: 1-11 | Columns: 2

Important

Most metrics are computed using a single SQL query, but some of them might require multiple SQL queries. Selecting only the necessary metrics in the report can help optimize performance. E.g. model.report(metrics = ["auc", "accuracy"]).

For classification models, we can easily modify the cutoff to observe the effect on different metrics:

model.report(cutoff = 0.2)
value
auc0.6491468392234953
prc_auc0.8073252152046401
accuracy0.5921259842519685
log_loss0.4969701130957392
precision0.24150268336314848
recall0.5895196506550219
f1_score0.34263959390862947
mcc0.14112241786423338
informedness0.18221897822466637
markedness0.10929452583853516
csi0.20673813169984687
Rows: 1-11 | Columns: 2

You can also use the LinearModel.score function to compute any classification metric. The default metric is the accuracy:

model.score()

Prediction

Prediction is straight-forward:

model.predict(
    test,
    [
        "fixed_acidity",
        "volatile_acidity",
        "citric_acid",
        "residual_sugar",
        "chlorides",
        "density",
    ],
    "prediction",
)
123
fixed_acidity
Decimal(6, 3)
123
volatile_acidity
Decimal(7, 4)
123
citric_acid
Decimal(6, 3)
123
residual_sugar
Decimal(7, 3)
123
chlorides
Double
123
free_sulfur_dioxide
Decimal(7, 2)
123
total_sulfur_dioxide
Decimal(7, 2)
123
density
Double
123
ph
Decimal(6, 3)
123
sulphates
Decimal(6, 3)
123
alcohol
Double
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
123
seedrand
Decimal(26, 6)
123
prediction
Integer
16.30.670.4812.60.05257.0222.00.99793.170.529.360white0.170
27.10.260.312.20.04429.0128.00.99373.340.6410.981white0.150
36.30.390.246.90.0699.0117.00.99423.150.3510.240white0.050
48.20.220.366.80.03412.090.00.99443.010.3810.581white0.020
57.10.190.283.60.03316.078.00.9932.910.7811.460white0.090
67.30.250.3613.10.0535.0200.00.99863.040.468.971white0.140
75.90.4150.020.80.03822.063.00.99323.360.369.350white0.10
86.60.230.261.30.04516.0128.00.99343.360.610.060white0.170
96.20.180.494.50.04717.090.00.99193.270.3711.660white0.130
105.30.1650.241.10.05125.0105.00.99253.320.479.150white0.00
117.20.220.491.00.04534.0140.00.993.050.3412.760white0.120
127.00.390.241.00.0488.0119.00.99233.00.3110.140white0.00
137.10.290.491.20.03132.099.00.98933.070.3312.260white0.10
146.90.230.2414.20.05319.094.00.99823.170.59.650white0.070
158.50.560.7417.850.05151.0243.01.00052.990.79.250white0.170
167.20.40.491.10.04811.0138.00.99293.010.429.350white0.110
178.30.20.744.450.04433.0130.00.99243.250.4212.260white0.050
187.10.220.742.70.04442.0144.00.9913.310.4112.260white0.060
196.90.190.496.60.03649.0172.00.99323.20.2711.560white0.070
207.10.250.493.00.0330.096.00.99033.130.3912.371white0.00
217.60.520.4914.00.03437.0156.00.99583.140.3811.871white0.150
227.90.220.244.60.04439.0159.00.99272.990.2811.560white0.170
237.20.080.491.30.0518.0148.00.99453.460.4410.260white0.020
246.90.250.243.60.05713.085.00.99422.990.489.540white0.010
257.50.20.491.30.0318.097.00.99183.060.6211.150white0.10
268.00.340.499.00.03339.0180.00.99363.130.3812.381white0.00
277.60.290.499.60.0345.0197.00.99383.130.3812.371white0.060
287.70.270.491.80.04123.086.00.99143.160.4212.560white0.090
296.70.510.242.10.04314.0155.00.99043.220.613.060white0.130
308.10.20.498.10.05151.0205.00.99543.10.5211.060white0.160
317.30.1550.491.30.03934.0136.00.99263.140.7710.560white0.130
328.40.230.497.80.03522.095.00.99353.040.3412.060white0.120
337.10.180.7415.60.04444.0176.00.99963.380.679.060white0.020
347.30.140.491.10.03828.099.00.99283.20.7210.660white0.150
356.70.290.494.70.03435.0156.00.99453.130.459.960white0.090
367.10.360.241.80.02532.0102.00.99033.340.5912.860white0.010
377.90.180.495.20.05136.0157.00.99533.180.4810.660white0.050
388.50.170.498.80.04823.0108.00.99472.880.3410.540white0.150
399.00.30.497.20.03932.084.00.99382.940.3211.560white0.120
406.40.250.747.80.04552.0209.00.99563.210.429.260white0.070
419.30.310.491.30.04234.0147.00.99483.110.469.850white0.020
426.40.270.497.30.04653.0206.00.99563.240.439.260white0.140
438.20.260.495.20.0419.0100.00.99413.120.3410.160white0.070
448.00.290.4911.70.03540.0131.00.99583.140.3410.850white0.110
457.50.190.491.60.04742.0140.00.99323.40.4710.760white0.070
466.20.20.491.60.06517.0143.00.99373.220.529.260white0.110
476.40.370.4913.30.04553.0243.00.99823.140.488.560white0.150
489.00.170.491.00.03946.0131.00.9933.090.5110.571white0.150
496.40.260.496.40.03737.0161.00.99543.380.539.760white0.180
508.20.20.493.50.05714.0108.00.99283.190.3511.560white0.070
516.90.250.241.80.0536.0121.00.9933.230.711.450white0.160
528.20.20.493.50.05714.0108.00.99283.190.3511.560white0.060
537.60.470.4913.00.23942.0220.00.99882.960.519.250white0.10
546.40.220.497.50.05442.0151.00.99483.270.5210.160white0.180
558.10.30.4912.30.04950.0144.00.99713.090.5710.271white0.020
567.30.190.4915.550.05850.0134.00.99983.420.369.171white0.060
576.60.460.497.40.05219.0184.00.99563.110.389.050white0.120
585.80.280.344.00.03140.099.00.98963.390.3912.871white0.090
597.40.210.271.20.04127.099.00.99273.190.339.860white0.180
607.00.210.288.60.04537.0221.00.99543.250.5410.460white0.040
617.80.210.271.20.05120.089.00.99363.060.469.150white0.00
626.20.350.041.20.0623.0108.00.99343.260.549.250white0.110
637.10.320.3211.00.03816.066.00.99373.240.411.530white0.00
646.10.20.251.20.03834.0128.00.99213.240.4410.150white0.060
657.20.290.5318.150.04759.0182.00.99923.090.529.650white0.10
667.00.170.314.80.03434.0132.00.99443.360.489.671white0.060
677.40.280.361.10.02842.0105.00.98932.990.3912.471white0.160
687.10.550.131.70.07321.0165.00.9942.970.589.260white0.080
696.20.370.36.60.34679.0200.00.99543.290.589.650white0.110
707.20.40.6210.80.04170.0189.00.99763.080.498.640white0.040
715.90.620.283.50.03955.0152.00.99073.440.4412.060white0.060
727.40.360.331.40.02527.055.00.99153.210.3311.260white0.170
735.80.360.321.70.03322.096.00.98983.030.3811.260white0.130
745.60.350.371.00.0386.072.00.99023.370.3411.450white0.110
755.90.320.393.30.11424.0140.00.99343.090.459.260white0.020
767.40.250.3613.20.06753.0178.00.99763.010.489.060white0.090
777.40.250.3613.20.06753.0178.00.99763.010.489.060white0.050
786.70.310.319.90.0410.0175.00.99533.460.5511.440white0.090
796.40.160.221.40.0441.0149.00.99333.490.5810.060white0.060
807.00.30.5113.60.0540.0168.00.99763.070.529.671white0.160
817.80.280.341.60.02832.0118.00.99013.00.3812.171white0.090
827.70.340.278.80.06339.0184.00.99693.090.639.260white0.110
836.50.280.275.20.0444.0179.00.99483.190.699.460white0.140
847.10.220.332.80.03348.0153.00.98993.150.3812.771white0.10
857.40.260.37.90.04938.0157.00.99633.130.488.960white0.130
867.10.310.514.50.0596.0148.00.99832.940.449.150white0.030
876.40.450.071.10.0310.0131.00.99052.970.2810.850white0.130
886.40.4750.061.00.039.0131.00.99042.970.2910.850white0.070
896.30.260.491.50.05234.0134.00.99242.990.619.860white0.030
907.30.250.297.50.04938.0158.00.99653.430.389.650white0.120
916.10.280.2517.750.04448.0161.00.99933.340.489.550white0.140
927.50.230.6811.00.04737.0133.00.99782.990.388.850white0.050
937.50.210.6810.90.04538.0133.00.99783.00.368.750white0.170
947.80.260.271.90.05152.0195.00.99283.230.510.960white0.050
956.00.360.061.40.06627.0128.00.99343.260.559.350white0.010
966.20.170.284.70.03739.0133.00.99313.410.4610.871white0.00
977.40.1050.3412.20.0557.0146.00.99733.160.379.060white0.050
987.10.260.3414.40.06735.0189.00.99863.070.539.171white0.030
996.70.260.498.30.04754.0191.00.99543.230.410.360white0.160
1006.20.270.328.80.04765.0224.00.99613.170.478.950white0.060
Rows: 1-100 | Columns: 16

Note

Predictions can be made automatically using the test set, in which case you don’t need to specify the predictors. Alternatively, you can pass only the VastFrame to the predict() function, but in this case, it’s essential that the column names of the VastFrame match the predictors and response name in the model.

Probabilities

It is also easy to get the model’s probabilities:

model.predict_proba(
    test,
    [
        "fixed_acidity",
        "volatile_acidity",
        "citric_acid",
        "residual_sugar",
        "chlorides",
        "density",
    ],
    "prediction",
)
123
fixed_acidity
Decimal(6, 3)
123
volatile_acidity
Decimal(7, 4)
123
citric_acid
Decimal(6, 3)
123
residual_sugar
Decimal(7, 3)
123
chlorides
Double
123
free_sulfur_dioxide
Decimal(7, 2)
123
total_sulfur_dioxide
Decimal(7, 2)
123
density
Double
123
ph
Decimal(6, 3)
123
sulphates
Decimal(6, 3)
123
alcohol
Double
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
123
seedrand
Decimal(26, 6)
123
prediction
Integer
123
prediction_0
Double
123
prediction_1
Double
16.30.670.4812.60.05257.0222.00.99793.170.529.360white0.1700.9250670061601380.07493299383986202
27.10.260.312.20.04429.0128.00.99373.340.6410.981white0.1500.74056098597642790.259439014023572
36.30.390.246.90.0699.0117.00.99423.150.3510.240white0.0500.85509162113182540.1449083788681747
48.20.220.366.80.03412.090.00.99443.010.3810.581white0.0200.77086539812464720.2291346018753528
57.10.190.283.60.03316.078.00.9932.910.7811.460white0.0900.71956671625205540.2804332837479447
67.30.250.3613.10.0535.0200.00.99863.040.468.971white0.1400.84950621299465310.15049378700534685
75.90.4150.020.80.03822.063.00.99323.360.369.350white0.100.80091550652178110.19908449347821885
86.60.230.261.30.04516.0128.00.99343.360.610.060white0.1700.71939849641843510.2806015035815649
96.20.180.494.50.04717.090.00.99193.270.3711.660white0.1300.70863423577821630.29136576422178373
105.30.1650.241.10.05125.0105.00.99253.320.479.150white0.000.68518522850770690.31481477149229303
117.20.220.491.00.04534.0140.00.993.050.3412.760white0.1200.6863351484918180.313664851508182
127.00.390.241.00.0488.0119.00.99233.00.3110.140white0.000.78841317999451430.21158682000548568
137.10.290.491.20.03132.099.00.98933.070.3312.260white0.100.70431928211690580.2956807178830942
146.90.230.2414.20.05319.094.00.99823.170.59.650white0.0700.86158293290861820.13841706709138174
158.50.560.7417.850.05151.0243.01.00052.990.79.250white0.1700.92652379156185920.07347620843814089
167.20.40.491.10.04811.0138.00.99293.010.429.350white0.1100.76878471411851510.23121528588148488
178.30.20.744.450.04433.0130.00.99243.250.4212.260white0.0500.70018008857339840.2998199114266016
187.10.220.742.70.04442.0144.00.9913.310.4112.260white0.0600.67504763515074330.32495236484925677
196.90.190.496.60.03649.0172.00.99323.20.2711.560white0.0700.73368812440044340.26631187559955655
207.10.250.493.00.0330.096.00.99033.130.3912.371white0.000.70886177524340430.2911382247565957
217.60.520.4914.00.03437.0156.00.99583.140.3811.871white0.1500.90216468416522530.09783531583477473
227.90.220.244.60.04439.0159.00.99272.990.2811.560white0.1700.76618948099588540.2338105190041146
237.20.080.491.30.0518.0148.00.99453.460.4410.260white0.0200.62896783610980320.3710321638901967
246.90.250.243.60.05713.085.00.99422.990.489.540white0.0100.77214117208202430.2278588279179757
257.50.20.491.30.0318.097.00.99183.060.6211.150white0.100.66740741476960390.33259258523039614
268.00.340.499.00.03339.0180.00.99363.130.3812.381white0.000.82074604742897460.17925395257102536
277.60.290.499.60.0345.0197.00.99383.130.3812.371white0.0600.80499491111797570.19500508888202422
287.70.270.491.80.04123.086.00.99143.160.4212.560white0.0900.71966373864046170.28033626135953826
296.70.510.242.10.04314.0155.00.99043.220.613.060white0.1300.83281777625578530.16718222374421463
308.10.20.498.10.05151.0205.00.99543.10.5211.060white0.1600.77940223303823590.22059776696176417
317.30.1550.491.30.03934.0136.00.99263.140.7710.560white0.1300.65343959269486680.34656040730513316
328.40.230.497.80.03522.095.00.99353.040.3412.060white0.1200.77424697212727820.22575302787272183
337.10.180.7415.60.04444.0176.00.99963.380.679.060white0.0200.81258936637619380.1874106336238062
347.30.140.491.10.03828.099.00.99283.20.7210.660white0.1500.64189332704914160.3581066729508584
356.70.290.494.70.03435.0156.00.99453.130.459.960white0.0900.74914704555188840.2508529544481116
367.10.360.241.80.02532.0102.00.99033.340.5912.860white0.0100.76458408465796580.23541591534203424
377.90.180.495.20.05136.0157.00.99533.180.4810.660white0.0500.73635844916812010.26364155083187996
388.50.170.498.80.04823.0108.00.99472.880.3410.540white0.1500.77560526591813990.22439473408186006
399.00.30.497.20.03932.084.00.99382.940.3211.560white0.1200.80119179752477530.19880820247522474
406.40.250.747.80.04552.0209.00.99563.210.429.260white0.0700.75028874360181490.24971125639818512
419.30.310.491.30.04234.0147.00.99483.110.469.850white0.0200.74505243556109790.2549475644389021
426.40.270.497.30.04653.0206.00.99563.240.439.260white0.1400.78047868444545220.2195213155545478
438.20.260.495.20.0419.0100.00.99413.120.3410.160white0.0700.76010741512157680.2398925848784233
448.00.290.4911.70.03540.0131.00.99583.140.3410.850white0.1100.83142335099017610.168576649009824
457.50.190.491.60.04742.0140.00.99323.40.4710.760white0.0700.68602563513406680.3139743648659332
466.20.20.491.60.06517.0143.00.99373.220.529.260white0.1100.70025301301809780.29974698698190216
476.40.370.4913.30.04553.0243.00.99823.140.488.560white0.1500.86563208567724010.1343679143227598
489.00.170.491.00.03946.0131.00.9933.090.5110.571white0.1500.67206826528883170.3279317347111683
496.40.260.496.40.03737.0161.00.99543.380.539.760white0.1800.75783627164752790.24216372835247216
508.20.20.493.50.05714.0108.00.99283.190.3511.560white0.0700.73217232031766990.2678276796823301
516.90.250.241.80.0536.0121.00.9933.230.711.450white0.1600.74723565622004240.2527643437799576
528.20.20.493.50.05714.0108.00.99283.190.3511.560white0.0600.73217232031766990.2678276796823301
537.60.470.4913.00.23942.0220.00.99882.960.519.250white0.100.95938483471802740.04061516528197258
546.40.220.497.50.05442.0151.00.99483.270.5210.160white0.1800.77108570281629020.2289142971837098
558.10.30.4912.30.04950.0144.00.99713.090.5710.271white0.0200.85025808514575820.14974191485424185
567.30.190.4915.550.05850.0134.00.99983.420.369.171white0.0600.84819844552368170.1518015544763183
576.60.460.497.40.05219.0184.00.99563.110.389.050white0.1200.8494446917359790.150555308264021
585.60.320.337.40.03725.095.00.992683.250.4911.160white0.0900.80159844720152940.19840155279847066
597.20.370.411.60.03234.0214.00.99633.10.519.860white0.1800.85446927355633040.14553072644366966
607.10.180.3914.50.05148.0156.00.999473.350.789.150white0.0400.83839677325385830.16160322674614166
616.40.320.274.90.03418.0122.00.99163.360.7112.560white0.000.7841233801530610.21587661984693893
626.70.40.228.80.05224.0113.00.995763.220.459.450white0.1100.86468454267014150.13531545732985856
636.60.150.326.00.03359.0128.00.991923.190.7112.181white0.000.723326783772120.27667321622787994
646.60.210.295.350.02943.0106.00.991122.930.4311.571white0.0600.74012741169999390.2598725883000061
657.60.220.2812.00.05668.0143.00.99832.990.39.260white0.100.84401544258497950.15598455741502049
666.50.30.274.00.03837.097.00.990263.20.612.681white0.0600.77096417123221650.2290358287677835
676.20.370.246.10.03219.086.00.989343.040.2613.481white0.1600.81332083492890040.18667916507109963
686.20.360.4510.40.0622.0184.00.997113.310.569.860white0.0800.85246067591693750.14753932408306242
696.20.370.246.10.03219.086.00.989343.040.2613.481white0.1100.81332083492890040.18667916507109963
706.10.330.33.00.03630.0124.00.989223.310.413.171white0.0400.763125190042260.23687480995774007
716.70.290.4514.30.05430.0181.00.998693.140.579.150white0.0600.86258254964147130.13741745035852868
727.90.350.2812.90.03213.063.00.99322.990.4313.060white0.1700.87060905192104570.1293909480789543
736.00.230.159.70.048101.0207.00.995713.050.39.150white0.1300.82236444113884160.17763555886115845
745.30.360.276.30.02840.0132.00.991863.370.411.660white0.1100.80037330657943960.19962669342056036
755.30.360.276.30.02840.0132.00.991863.370.411.660white0.0200.80037330657943960.19962669342056036
767.20.230.466.40.03617.085.00.992793.10.7811.760white0.0900.7536215891156170.2463784108843829
777.20.310.357.20.04645.0178.00.99553.140.539.750white0.0500.81250627772061130.18749372227938865
787.80.280.253.40.02427.099.00.989592.980.3713.060white0.0900.75411704407851650.2458829559214835
797.30.20.2919.50.03969.0237.01.000373.10.489.260white0.0600.88186011896486970.11813988103513026
807.20.140.321.10.02248.0116.00.992183.040.6710.060white0.1600.64447098586637170.3555290141336282
815.70.220.2216.650.04439.0110.00.998553.240.489.060white0.0900.86718008063691140.13281991936308854
827.60.370.343.20.02842.0162.00.99033.010.3312.460white0.1100.77979969645531790.2202003035446821
838.00.250.279.70.03615.085.00.994062.990.3611.260white0.1400.82031903734081360.17968096265918643
847.30.230.272.60.03539.0120.00.991383.040.5911.371white0.100.7290690642112190.270930935788781
855.80.280.284.20.04452.0158.00.9923.350.4410.771white0.1300.76538056160092280.23461943839907717
866.60.190.351.50.03737.0107.00.990063.180.6812.071white0.0300.68263204723828320.31736795276171686
875.60.280.284.20.04452.0158.00.9923.350.4410.771white0.1300.76391620817106930.23608379182893072
886.80.190.327.60.04937.0107.00.993323.120.4410.771white0.0700.77653006748299710.22346993251700292
897.40.360.231.90.01731.069.00.98922.930.3612.560white0.0300.76099372118040520.2390062788195948
906.80.140.181.40.04730.090.00.991643.270.5411.260white0.1200.69355328642664380.3064467135733562
917.00.160.2514.30.04427.0149.00.9982.910.469.260white0.1400.83631790943424190.16368209056575808
926.80.160.181.80.04631.0114.00.992263.270.5510.860white0.0500.707010483335420.29298951666458
937.00.160.2514.30.04427.0149.00.9982.910.469.260white0.1700.83631790943424190.16368209056575808
947.00.290.330.90.04120.0117.00.990483.210.511.450white0.0500.73041956353671680.26958043646328317
956.60.360.286.10.02912.093.00.990543.190.2712.871white0.0100.80642997355826610.1935700264417339
966.50.260.391.40.0212.066.00.990893.250.7511.371white0.000.6884718462771560.3115281537228441
976.40.290.243.20.03731.095.00.989422.90.6612.671white0.0500.75936344834632530.24063655165367473
986.90.190.64.00.0376.0122.00.992552.920.5910.440white0.0300.68732282090278880.3126771790972111
996.70.260.498.30.04754.0191.00.99543.230.410.360white0.1600.79037798134701290.20962201865298713
1006.20.270.328.80.04765.0224.00.99613.170.478.950white0.0600.81206830328087630.1879316967191237
Rows: 1-100 | Columns: 18

Note

Probabilities are added to the VastFrame, and vastorbit uses the corresponding probability function in SQL behind the scenes. You can use the pos_label parameter to add only the probability of the selected category.

Confusion Matrix

You can obtain the confusion matrix of your choice by specifying the desired cutoff.

model.confusion_matrix(cutoff = 0.5)

Note

In classification, the cutoff is a threshold value used to determine class assignment based on predicted probabilities or scores from a classification model. In binary classification, if the predicted probability for a specific class is greater than or equal to the cutoff, the instance is assigned to the positive class; otherwise, it is assigned to the negative class. Adjusting the cutoff allows for trade-offs between true positives and false positives, enabling the model to be optimized for specific objectives or to consider the relative costs of different classification errors. The choice of cutoff is critical for tailoring the model’s performance to meet specific needs.

Main Plots (Classification Curves)

Classification models allow for the creation of various plots that are very helpful in understanding the model, such as the ROC Curve, PRC Curve, Cutoff Curve, Gain Curve, and more.

Most of the classification curves can be found in the Machine Learning - Classification Curve.

For example, let’s draw the model’s ROC curve.

model.roc_curve()

Important

Most of the curves have a parameter called nbins, which is essential for estimating metrics. The larger the nbins, the more precise the estimation, but it can significantly impact performance. Exercise caution when increasing this parameter excessively.

Hint

In binary classification, various curves can be easily plotted. However, in multi-class classification, it’s important to select the pos_label, representing the class to be treated as positive when drawing the curve.

Other Plots

If the model allows, you can also generate relevant plots. For example, classification plots can be found in the Machine Learning - Classification Plots.

model.plot()

Important

The plotting feature is typically suitable for models with fewer than three predictors.

Contour plot is another useful plot that can be produced for models with two predictors.

model.contour()

Important

Machine learning models with two predictors can usually benefit from their own contour plot. This visual representation aids in exploring predictions and gaining a deeper understanding of how these models perform in different scenarios. Please refer to Contour Plot for more examples.

Parameter Modification

In order to see the parameters:

model.get_params()

And to manually change some of the parameters:

model.set_params({'tol': 0.001})

Model Exporting

To Memmodel

model.to_memmodel()

Note

MemModel objects serve as in-memory representations of machine learning models. They can be used for both in-database and in-memory prediction tasks. These objects can be pickled in the same way that you would pickle a scikit-learn model.

The following methods for exporting the model use MemModel, and it is recommended to use MemModel directly.

To SQL

You can get the SQL code by:

model.to_sql()

To Python

To obtain the prediction function in Python syntax, use the following code:

X = [[4.2, 0.17, 0.36, 1.8, 0.029, 0.9899]]
model.to_python()(X)

Hint

The to_python() method is used to retrieve predictions, probabilities, or cluster distances. For specific details on how to use this method for different model types, refer to the relevant documentation for each model.

__init__(name: str = None, overwrite_model: bool = False, **kwargs) None

Methods

__init__([name, overwrite_model])

classification_report([metrics, cutoff, nbins])

Computes a classification report using multiple model evaluation metrics (auc, accuracy, f1...).

confusion_matrix([cutoff])

Computes the model confusion matrix.

contour([nbins, chart])

Draws the model's contour plot.

cutoff_curve([nbins, show, chart])

Draws the model Cutoff curve.

deploySQL([X, cutoff])

Returns the SQL code needed to deploy the model.

drop()

Drops the model from the VAST DataBase.

export_models(name, path[, kind])

Exports machine learning models.

features_importance([show, chart])

Computes the model's features importance.

fit(input_relation, X, y[, test_relation, ...])

Trains the model.

get_attributes([attr_name])

Returns the model attributes.

get_match_index(x, col_list[, str_check])

Returns the matching index.

get_params()

Returns the parameters of the model.

get_plotting_lib([class_name, chart, ...])

Returns the first available library (Plotly, Matplotlib) to draw a specific graphic.

import_models(path[, schema, kind])

Imports machine learning models.

lift_chart([nbins, show, chart])

Draws the model Lift Chart.

plot([max_nb_points, chart])

Draws the model.

prc_curve([nbins, show, chart])

Draws the model PRC curve.

predict(vdf[, X, name, cutoff, inplace])

Makes predictions on the input relation.

predict_proba(vdf[, X, name, pos_label, inplace])

Returns the model's probabilities using the input relation.

report([metrics, cutoff, nbins])

Computes a classification report using multiple model evaluation metrics (auc, accuracy, f1...).

roc_curve([nbins, show, chart])

Draws the model ROC curve.

score([metric, cutoff, nbins])

Computes the model score.

set_params([parameters])

Sets the parameters of the model.

summarize()

Summarizes the model.

to_binary(path)

Exports the model to the VAST Binary format.

to_memmodel()

Converts the model to an InMemory object that can be used for different types of predictions.

to_python([return_proba, ...])

Returns the Python function needed for in-memory scoring without using built-in VAST functions.

to_sql([X, return_proba, ...])

Returns the SQL code needed to deploy the model without using built-in VAST functions.

Attributes