Loading...

verticapy.machine_learning.vertica.cluster.KPrototypes.predict

KPrototypes.predict(vdf: Annotated[str | vDataFrame, ''], X: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None, name: str | None = None, inplace: bool = True) → vDataFrame

Makes predictions using the input relation.

Parameters

vdf: SQLRelation

Object used to run the prediction. You can also specify a customized relation, but you must enclose it with an alias. For example: (SELECT 1) x is valid whereas (SELECT 1) and SELECT 1 are invalid.

X: SQLColumns, optional

list of the columns used to deploy the models. If empty, the model predictors are used.

name: str, optional

Name of the added vDataColumn. If empty, a name is generated.

inplace: bool, optional

If set to True, the prediction is added to the vDataFrame.

Returns

vDataFrame

the input object.

Examples

For this example, we will use the winequality dataset.

import verticapy.datasets as vpd

data = vpd.load_winequality()
123
fixed_acidity
Numeric(8)
123
volatile_acidity
Numeric(9)
123
citric_acid
Numeric(8)
123
residual_sugar
Numeric(9)
123
chlorides
Float(22)
123
free_sulfur_dioxide
Numeric(9)
123
total_sulfur_dioxide
Numeric(9)
123
density
Float(22)
123
pH
Numeric(8)
123
sulphates
Numeric(8)
123
alcohol
Float(22)
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
13.90.2250.44.20.0329.0118.00.9893.570.3612.881white
24.70.3350.141.30.03669.0168.00.992123.470.4610.550white
34.70.4550.181.90.03633.0106.00.987463.210.8314.071white
44.70.7850.03.40.03623.0134.00.989813.530.9213.860white
54.90.3450.341.00.06832.0143.00.991383.240.410.150white
64.90.3450.341.00.06832.0143.00.991383.240.410.150white
74.90.420.02.10.04816.042.00.991543.710.7414.071red
85.00.270.41.20.07642.0124.00.992043.320.4710.160white
95.00.310.06.40.04643.0166.00.9943.30.639.960white
105.00.40.54.30.04629.080.00.99023.490.6613.660red
115.00.440.0418.60.03938.0128.00.99853.370.5710.260white
125.10.110.321.60.02812.090.00.990083.570.5212.260white
135.10.140.250.70.03915.089.00.99193.220.439.260white
145.10.1650.225.70.04742.0146.00.99343.180.559.960white
155.10.330.221.60.02718.089.00.98933.510.3812.571white
165.10.330.221.60.02718.089.00.98933.510.3812.571white
175.10.330.221.60.02718.089.00.98933.510.3812.571white
185.10.390.211.70.02715.072.00.98943.50.4512.560white
195.20.20.273.20.04716.093.00.992353.440.5310.171white
205.20.210.311.70.04817.061.00.989533.240.3712.071white
215.20.220.466.20.06641.0187.00.993623.190.429.7333333333333350white
225.20.310.22.40.02727.0117.00.988863.560.4513.071white
235.20.320.251.80.10313.050.00.99573.380.559.250red
245.20.340.376.20.03142.0133.00.990763.250.4112.560white
255.20.360.021.60.03124.0104.00.98963.440.3512.260white
265.20.3650.0813.50.04137.0142.00.9973.460.399.960white
275.20.480.041.60.05419.0106.00.99273.540.6212.271red
285.20.50.182.00.03623.0129.00.989493.360.7713.471white
295.30.160.391.00.02840.0101.00.991563.570.5910.660white
305.30.160.391.00.02840.0101.00.991563.570.5910.660white
315.30.1650.241.10.05125.0105.00.99253.320.479.150white
325.30.230.560.90.04146.0141.00.991193.160.629.750white
335.30.30.31.20.02925.093.00.987423.310.413.671white
345.30.330.31.20.04825.0119.00.990453.320.6211.360white
355.30.360.276.30.02840.0132.00.991863.370.411.660white
365.30.360.276.30.02840.0132.00.991863.370.411.660white
375.30.40.253.90.03145.0130.00.990723.310.5811.7571white
385.30.470.112.20.04816.089.00.991823.540.8813.671red
395.30.470.112.20.04816.089.00.991823.540.8813.566666666666771red
405.30.7150.191.50.1617.062.00.993953.620.6111.050red
415.40.220.291.20.04569.0152.00.991783.760.6311.071white
425.40.5950.12.80.04226.080.00.99323.360.389.350white
435.40.740.091.70.08916.026.00.994023.670.5611.660red
445.50.120.331.00.03823.0131.00.991643.250.459.850white
455.50.120.331.00.03823.0131.00.991643.250.459.850white
465.50.140.274.60.02922.0104.00.99493.340.449.050white
475.50.140.274.60.02922.0104.00.99493.340.449.050white
485.50.160.311.20.02631.068.00.98983.330.4411.6560white
495.50.160.311.20.02631.068.00.98983.330.4411.633333333333360white
505.50.180.225.50.03710.086.00.991563.460.4412.250white
515.50.240.451.70.04622.0113.00.992243.220.4810.050white
525.50.290.31.10.02220.0110.00.988693.340.3812.871white
535.50.310.293.00.02716.0102.00.990673.230.5611.260white
545.50.320.454.90.02825.0191.00.99223.510.4911.571white
555.50.350.351.10.04514.0167.00.9923.340.689.960white
565.50.3750.381.70.03617.098.00.991423.290.3910.560white
575.60.150.265.550.05151.0139.00.993363.470.511.060white
585.60.150.315.30.0388.079.00.99233.30.3910.560white
595.60.160.271.40.04453.0168.00.99183.280.3710.160white
605.60.1750.290.80.04320.067.00.991123.280.489.960white
615.60.1850.197.10.04836.0110.00.994383.260.419.560white
625.60.1850.197.10.04836.0110.00.994383.260.419.560white
635.60.220.321.20.02429.097.00.988233.20.4613.0571white
645.60.260.181.40.03418.0135.00.991743.320.3510.260white
655.60.260.265.70.03112.080.00.99233.250.3810.850white
665.60.260.511.40.02925.093.00.994283.230.4910.560white
675.60.280.284.20.04452.0158.00.9923.350.4410.771white
685.60.30.16.40.04334.0142.00.993823.140.489.850white
695.60.350.145.00.04648.0198.00.99373.30.7110.350white
705.60.490.134.50.03917.0116.00.99073.420.913.771white
715.60.490.134.50.03917.0116.00.99073.420.913.771white
725.60.660.02.20.0873.011.00.993783.710.6312.871red
735.60.660.02.20.0873.011.00.993783.710.6312.871red
745.70.150.4711.40.03549.0128.00.994563.030.3410.581white
755.70.180.262.20.02321.095.00.98933.070.5412.360white
765.70.180.361.20.0469.071.00.991993.70.6810.971white
775.70.20.32.50.04638.0125.00.992763.340.59.960white
785.70.210.320.90.03838.0121.00.990743.240.4610.660white
795.70.210.374.50.0458.0140.00.993323.290.6210.660white
805.70.220.216.00.04441.0113.00.998623.220.468.960white
815.70.220.216.00.04441.0113.00.998623.220.468.960white
825.70.220.216.00.04441.0113.00.998623.220.468.960white
835.70.220.216.00.04441.0113.00.998623.220.468.960white
845.70.220.216.00.04441.0113.00.998623.220.468.960white
855.70.220.293.50.0427.0146.00.989993.170.3612.160white
865.70.230.289.650.02526.0121.00.99253.280.3811.360white
875.70.250.2612.50.04952.5106.00.996913.080.459.460white
885.70.250.2612.50.04952.5120.00.996913.080.459.460white
895.70.250.2711.50.0424.0120.00.994113.330.3110.860white
905.70.260.2417.80.05923.0124.00.997733.30.510.150white
915.70.260.2417.80.05923.0124.00.997733.30.510.150white
925.70.260.2417.80.05923.0124.00.997733.30.510.150white
935.70.270.321.20.04620.0155.00.99343.80.4110.260white
945.70.280.2417.50.04460.0167.00.99893.310.449.450white
955.70.320.181.40.02926.0104.00.99063.440.3711.060white
965.70.320.384.750.03323.094.00.9913.420.4211.871white
975.70.360.344.20.02621.077.00.99073.410.4511.960white
985.80.140.156.10.04227.0123.00.993623.060.69.960white
995.80.150.321.20.03714.0119.00.991373.190.510.260white
1005.80.170.341.80.04596.0170.00.990353.380.911.881white
Rows: 1-100 | Columns: 14

Let’s import the model:

from verticapy.machine_learning.vertica import KMeans

Then we can create the model:

model = KMeans(
    n_cluster = 8,
    init = "kmeanspp",
    max_iter = 300,
    tol = 1e-4,
)

We can then fit the model:

model.fit(data, X = ["density", "sulphates"])


=======
centers
=======
density |sulphates
--------+---------
 0.99333| 0.36719 
 0.99605| 0.80641 
 0.99621| 1.01470 
 0.99554| 0.67558 
 0.99780| 1.97000 
 0.99464| 0.47081 
 0.99503| 0.57011 
 0.99714| 1.39143 


=======
metrics
=======
Evaluation metrics:
     Total Sum of Squares: 143.90056
     Within-Cluster Sum of Squares: 
         Cluster 0: 1.5781788
         Cluster 1: 0.82994039
         Cluster 2: 0.65632232
         Cluster 3: 0.93794951
         Cluster 4: 0.00180432
         Cluster 5: 2.1820695
         Cluster 6: 1.2819767
         Cluster 7: 0.25538079
     Total Within-Cluster Sum of Squares: 7.7236223
     Between-Cluster Sum of Squares: 136.17694
     Between-Cluster SS / Total SS: 94.63%
 Number of iterations performed: 12
 Converged: True
 Call:
kmeans('"public"."_verticapy_tmp_kmeans_v_mldb_e553ca00979611efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_e581c28e979611efa8720242ac120002_"', '"density", "sulphates"', 8
USING PARAMETERS max_iterations=300, epsilon=0.0001, init_method='kmeanspp', distance_method='euclidean')

Predicting or ranking the dataset is straight-forward:

model.predict(data, ["density", "sulphates"], name = "Cluster IDs")
123
fixed_acidity
Numeric(8)
123
volatile_acidity
Numeric(9)
123
citric_acid
Numeric(8)
123
residual_sugar
Numeric(9)
123
chlorides
Float(22)
123
free_sulfur_dioxide
Numeric(9)
123
total_sulfur_dioxide
Numeric(9)
123
density
Float(22)
123
pH
Numeric(8)
123
sulphates
Numeric(8)
123
alcohol
Float(22)
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
123
Cluster IDs
Integer
14.20.170.361.80.02993.0161.00.989993.650.8912.071white1
24.20.2150.235.10.04164.0157.00.996883.420.448.030white5
34.50.190.210.950.03389.0159.00.993323.340.428.050white5
44.60.4450.01.40.05311.0178.00.994263.790.5510.250white6
54.80.130.321.20.04240.098.00.98983.420.6411.871white3
64.80.170.282.90.0322.0111.00.99023.380.3411.371white0
74.80.210.2110.20.03717.0112.00.993243.660.4812.271white5
84.80.2250.381.20.07447.0130.00.991323.310.410.360white0
94.80.290.231.10.04438.0180.00.989243.280.3411.960white0
104.90.2350.2711.750.0334.0118.00.99543.070.59.460white5
114.90.330.311.20.01639.0150.00.987133.330.5914.081white6
124.90.3350.141.30.03669.0168.00.992123.470.4610.466666666666750white5
134.90.3350.141.30.03669.0168.00.992123.470.4610.466666666666750white5
145.00.2550.222.70.04346.0153.00.992383.750.7611.360white1
155.00.30.333.70.0354.0173.00.98873.360.313.071white0
165.00.330.2311.80.0323.0158.00.993223.410.6411.860white3
175.00.4550.181.90.03633.0106.00.987463.210.8314.071white1
185.00.740.01.20.04116.046.00.992584.010.5912.560red6
195.10.3050.131.750.03617.073.00.993.40.5112.333333333333350white5
205.10.310.30.90.03728.0152.00.9923.540.5610.160white6
215.10.330.276.70.02244.0129.00.992213.360.3911.071white0
225.10.420.011.50.01725.0102.00.98943.380.3612.371white0
235.10.520.062.70.05230.079.00.99323.320.439.350white5
245.20.160.340.80.02926.077.00.991553.250.5110.160white5
255.20.250.231.40.04720.077.00.990013.320.6211.450white6
265.20.30.341.50.03818.096.00.989423.560.4813.081white5
275.20.3350.21.70.03317.074.00.990023.340.4812.360white5
285.20.370.331.20.02813.081.00.99023.370.3811.760white0
295.20.380.267.70.05320.0103.00.99253.270.4512.260white5
305.20.440.041.40.03643.0119.00.98943.360.3312.181white0
315.20.440.041.40.03643.0119.00.98943.360.3312.181white0
325.30.210.290.70.02811.066.00.992153.30.49.850white0
335.30.2750.247.40.03828.0114.00.993133.380.5111.060white5
345.30.470.11.30.03611.074.00.990823.480.5411.240white6
355.30.60.341.40.0313.060.00.988543.270.3813.060white0
365.40.170.272.70.04928.0104.00.992243.460.5510.360white6
375.40.1850.197.10.04836.0110.00.994383.260.419.560white0
385.40.220.356.50.02926.087.00.990923.290.4412.571white5
395.40.230.361.50.0374.0121.00.989763.240.9912.171white2
405.40.270.224.60.02229.0107.00.988893.330.5413.860white6
415.40.30.31.20.02925.093.00.987423.310.413.671white0
425.40.310.473.00.05346.0144.00.99313.290.7610.050white1
435.40.330.314.00.0327.0108.00.990313.30.4312.271white5
445.40.420.272.00.09223.055.00.994713.780.6412.371red3
455.40.740.01.20.04116.046.00.992584.010.5912.560red6
465.40.8350.081.20.04613.093.00.99243.570.8513.071red1
475.50.210.251.20.0418.075.00.990063.310.5611.360white6
485.50.340.262.20.02131.0119.00.989193.550.4913.081white5
495.50.4850.01.50.0658.0103.00.9943.630.49.740white0
505.50.490.031.80.04428.087.00.99083.50.8214.081red1
515.60.120.264.30.03818.097.00.994773.360.469.250white5
525.60.120.332.90.04421.073.00.988963.170.3212.981white0
535.60.120.332.90.04421.073.00.988963.170.3212.981white0
545.60.130.274.80.02822.0104.00.99483.340.459.260white5
555.60.190.391.10.04317.067.00.99183.230.5310.360white6
565.60.20.221.30.04925.0155.00.992963.740.4310.050white5
575.60.20.362.50.04816.0125.00.992823.490.4910.060white5
585.60.240.342.00.04114.073.00.989813.040.4511.671white5
595.60.250.192.40.04942.0166.00.9923.250.4310.460white5
605.60.260.010.20.03813.0111.00.993153.440.4612.460white5
615.60.2950.261.10.03540.0102.00.991543.470.5610.660white6
625.60.310.7813.90.07423.092.00.996773.390.4810.560red5
635.60.320.337.40.03725.095.00.992683.250.4911.160white5
645.60.320.337.40.03725.095.00.992683.250.4911.160white5
655.60.340.36.90.03823.089.00.992663.250.4911.160white5
665.60.350.46.30.02223.0174.00.99223.540.511.671white5
675.60.420.342.40.02234.097.00.989153.220.3812.871white0
685.60.540.041.70.0495.013.00.99423.720.5811.450red6
695.60.540.041.70.0495.013.00.99423.720.5811.450red6
705.60.6150.01.60.08916.059.00.99433.580.529.950red5
715.60.620.031.50.086.013.00.994983.660.6210.140red6
725.60.660.02.50.0667.015.00.992563.520.5812.950red6
735.70.120.265.50.03421.099.00.993243.090.579.960white6
745.70.150.283.70.04557.0151.00.99133.220.2711.260white0
755.70.150.283.70.04557.0151.00.99133.220.2711.260white0
765.70.160.266.30.04328.0113.00.99363.060.589.960white6
775.70.160.321.20.0367.089.00.991113.260.4811.050white5
785.70.210.242.30.04760.0189.00.9953.650.7210.160white3
795.70.210.242.30.04760.0189.00.9953.650.7210.160white3
805.70.210.321.60.0333.0122.00.990443.330.5211.960white5
815.70.220.251.10.0597.0175.00.990993.440.6211.160white6
825.70.220.331.90.03637.0110.00.989453.260.5812.460white6
835.70.240.31.30.0325.098.00.989683.370.4312.471white5
845.70.260.2510.40.027.057.00.9943.390.3710.650white0
855.70.260.31.80.03930.0105.00.989953.480.5212.571white5
865.70.2650.286.90.03646.0150.00.992993.360.4410.871white5
875.70.2650.286.90.03646.0150.00.992993.360.4410.871white5
885.70.270.169.00.05332.0111.00.994743.360.3710.460white0
895.70.270.169.00.05332.0111.00.994743.360.3710.460white0
905.70.280.361.80.04138.090.00.990023.270.9811.971white2
915.70.310.284.10.0322.086.00.990623.310.3811.771white0
925.70.330.321.40.04328.093.00.98973.310.512.360white5
935.70.40.355.10.02617.0113.00.990523.180.6712.460white3
945.70.40.355.10.02617.0113.00.990523.180.6712.460white3
955.70.440.137.00.02528.0173.00.99133.330.4812.560white5
965.70.460.461.40.0431.0169.00.99323.130.478.850white5
975.80.120.211.30.05635.0121.00.99083.320.3311.460white0
985.80.130.2212.70.05824.0183.00.99563.320.4211.760white5
995.80.130.2212.70.05824.0183.00.99563.320.4211.760white5
1005.80.150.491.10.04821.098.00.99293.190.489.250white5
Rows: 1-100 | Columns: 15

Important

For this example, a specific model is utilized, and it may not correspond exactly to the model you are working with. To see a comprehensive example specific to your class of interest, please refer to that particular class.