Usage Examples for QuadratiK in Python#
Authors : Giovanni Saraceno, Marianthi Markatou, Raktim Mukhopadhyay, Mojgan Golzy
Date Modified: 20 February 2025
Note on Matplotlib Usage#
Matplotlib behaves differently in interactive environments (like Jupyter Notebook) versus non-interactive environments (like Python terminal):
In Jupyter Notebook:
Plots display automatically after cell execution
No explicit plt.show() is needed
In Python Terminal:
Need to explicitly call plt.show() to display plots, OR
May use plt.ion() for interactive mode
Throughout this notebook, we provide two versions of plotting code:
One that works directly in Jupyter (default).
One in comments that works in Python terminal (needs uncommenting).
For a detailed example, please see: statsmodels/statsmodels#1265
The code in this notebook has been tested in Jupyter Notebook.
Introduction#
This document contains various Python examples illustrating the use of QuadratiK
[1]:
import QuadratiK
QuadratiK.__version__
[1]:
'1.2.0'
Installation#
The python package QuadratiK and other necessary packages must be installed.
Normality Test#
Click here to download the Python script for this example: normality_test.py
We illustrate the usage of the introduced KernelTest for the Normality test. We generate one sample from a multivariate standard Normal distribution, that is \(x = (x_1, . . . , x_n)\) ∼ \(N_d(0, I_d)\) with dimension \(d = 4\), and sample size \(n = 500\).
[2]:
import numpy as np
np.random.seed(78990)
from QuadratiK.kernel_test import KernelTest
# data generation
data_norm = np.random.multivariate_normal(mean=np.zeros(4), cov=np.eye(4), size=500)
# performing the normality test
normality_test = KernelTest(
h=0.4,
num_iter=150,
method="subsampling",
random_state=42,
mu=np.zeros(4),
sigma=np.eye(4),
).test(data_norm)
# printing the summary for normality test
print(normality_test.summary())
Time taken for execution: 13.345 seconds
Test Results
Kernel-based quadratic distance Normality test
U-Statistic V-Statistic
------------------------------------ ------------- -------------
Test Statistic -0.114594 0.977955
Critical Value 1.78413 42.46
H0 is rejected (1 = True, 0 = False) 0 0
CV method: None
Selected tuning parameter h : 0.4
Summary Statistics
Feature 0 Feature 1 Feature 2 Feature 3
------- ----------- ----------- ----------- -----------
Mean -0.0148 -0.0253 0.0376 -0.0232
Std Dev 1.0053 1.0201 0.9796 1.0197
Median -0.001 0.0439 0.0846 -0.0404
IQR 1.3786 1.3761 1.2441 1.3611
Min -3.7131 -3.2847 -2.7106 -3.6615
Max 2.8297 2.6076 2.784 3.0463
K-Sample Test#
Click here to download the Python script for this example: k_sample_test.py
We generate three samples, with \(n=200\) observations each, from a 2-dimensional Gaussian distributions with mean vectors \(\mu_1 = (0, \sqrt(3)/3)\), \(\mu_2 = (-1/2, -\sqrt(3)/6)\) and \(\mu_3 = (1/2, \sqrt(3)/6)\), and the Identity matrix as Covariance matrix.
[3]:
import numpy as np
np.random.seed(0)
from QuadratiK.kernel_test import KernelTest
size = 200
eps = 1
x1 = np.random.multivariate_normal(
mean=[0, np.sqrt(3) * eps / 3], cov=np.eye(2), size=size
)
x2 = np.random.multivariate_normal(
mean=[-eps / 2, -np.sqrt(3) * eps / 6], cov=np.eye(2), size=size
)
x3 = np.random.multivariate_normal(
mean=[eps / 2, -np.sqrt(3) * eps / 6], cov=np.eye(2), size=size
)
# Merge the three samples into a single dataset
X_k = np.concatenate([x1, x2, x3])
# The memberships are needed for k-sample test
y_k = np.repeat(np.array([1, 2, 3]), size).reshape(-1, 1)
# performing the k-sample test
k_sample_test = KernelTest(h=1.5, method="subsampling", random_state=42).test(X_k, y_k)
# printing the summary for the k-sample test
print(k_sample_test.summary())
Time taken for execution: 0.871 seconds
Test Results
Kernel-based quadratic distance K-sample test
Dn Trace
------------------------------------ --------- -------
Test Statistic 13.8947 52.736
Critical Value 0.411501 1.5621
H0 is rejected (1 = True, 0 = False) 1 1
CV method: subsampling
Selected tuning parameter h : 1.5
Summary Statistics
Group 1 Group 2 Group 3 Overall
------------------------ --------- --------- --------- ---------
('Feature 0', 'Mean') -0.072 -0.6075 0.6442 -0.0117
('Feature 0', 'Std Dev') 0.9983 0.9833 0.9378 1.099
('Feature 0', 'Median') -0.1255 -0.6203 0.6998 -0.0332
('Feature 0', 'IQR') 1.3818 1.3067 1.2031 1.4906
('Feature 0', 'Min') -2.6592 -2.8696 -2.0828 -2.8696
('Feature 0', 'Max') 2.3831 2.1962 3.671 3.671
('Feature 1', 'Mean') 0.5923 -0.4182 -0.269 -0.0317
('Feature 1', 'Std Dev') 0.981 1.0265 0.926 1.0739
('Feature 1', 'Median') 0.6192 -0.336 -0.3172 -0.0594
('Feature 1', 'IQR') 1.3147 1.2806 1.1458 1.4445
('Feature 1', 'Min') -2.1952 -3.3348 -2.336 -3.3348
('Feature 1', 'Max') 2.8341 2.1238 2.4707 2.8341
Two-Sample Test#
Click here to download the Python script for this example: two_sample_test.py
This example shows the application of the two-sample test. Instead of providing the vector of membership to the two groups as for the k-sample test, the two-sample test can be additionally performed by providing the two samples to be compared. We generate the sample \(y_1, ..., y_n\) from a skew-normal distribution \(SN_d(0,I_d, \lambda)\), where \(d=4\), \(n=200\) and \(\lambda = (0.5, ..., 0.5)\).
Note: If a value of h is not provided, the select_h function can be used to determine the optimal h. Please see examples below where select_h function has been illustrated.
[4]:
import numpy as np
np.random.seed(0)
from scipy.stats import skewnorm
from QuadratiK.kernel_test import KernelTest
# data generation
X_2 = np.random.multivariate_normal(mean=np.zeros(4), cov=np.eye(4), size=200)
Y_2 = skewnorm.rvs(
size=(200, 4),
loc=np.zeros(4),
scale=np.ones(4),
a=np.repeat(0.5, 4),
random_state=20,
)
# performing the two sample test
two_sample_test = KernelTest(h=2, num_iter=150, random_state=42).test(X_2, Y_2)
# printing the summary for the two sample test
print(two_sample_test.summary())
Time taken for execution: 0.310 seconds
Test Results
Kernel-based quadratic distance two-sample test
Dn Trace
------------------------------------ -------- --------
Test Statistic 5.06121 15.7517
Critical Value 0.631127 1.96477
H0 is rejected (1 = True, 0 = False) 1 1
CV method: subsampling
Selected tuning parameter h : 2
Summary Statistics
Group 1 Group 2 Overall
------------------------ --------- --------- ---------
('Feature 0', 'Mean') -0.1158 0.3704 0.1273
('Feature 0', 'Std Dev') 1.0236 0.9005 0.9931
('Feature 0', 'Median') -0.2211 0.3726 0.1151
('Feature 0', 'IQR') 1.4552 1.1904 1.3979
('Feature 0', 'Min') -2.553 -2.2457 -2.553
('Feature 0', 'Max') 2.3831 2.2507 2.3831
('Feature 1', 'Mean') -0.1468 0.3715 0.1123
('Feature 1', 'Std Dev') 0.9932 0.9783 1.0182
('Feature 1', 'Median') -0.2067 0.3698 0.0948
('Feature 1', 'IQR') 1.2246 1.3686 1.354
('Feature 1', 'Min') -3.0461 -2.3911 -3.0461
('Feature 1', 'Max') 2.4125 3.0879 3.0879
('Feature 2', 'Mean') -0.0637 0.3176 0.1269
('Feature 2', 'Std Dev') 0.9565 0.9356 0.964
('Feature 2', 'Median') -0.103 0.2928 0.1568
('Feature 2', 'IQR') 1.22 1.1875 1.2453
('Feature 2', 'Min') -2.6592 -2.5676 -2.6592
('Feature 2', 'Max') 2.6962 3.4684 3.4684
('Feature 3', 'Mean') 0.0322 0.4218 0.227
('Feature 3', 'Std Dev') 1.0119 0.938 0.9937
('Feature 3', 'Median') 0.0953 0.3676 0.2272
('Feature 3', 'IQR') 1.2653 1.3567 1.2861
('Feature 3', 'Min') -2.7726 -2.1477 -2.7726
('Feature 3', 'Max') 2.2567 2.7018 2.7018
The qq_plot function can be used to generate the qq-plots between the given samples.
[5]:
from QuadratiK.tools import qq_plot
two_sample_qq_plot = qq_plot(X_2, Y_2)
# To save the qq plot: run the following line
# two_sample_qq_plot.savefig('two_sample_qq_plot.png')
"""
If you want to run the following line in python terminal or in a .py file, please uncomment the code below and run.
--------------------------------
from QuadratiK.tools import qq_plot
import matplotlib.pyplot as plt
two_sample_qq_plot = qq_plot(X_2, Y_2)
plt.show()
two_sample_qq_plot.savefig('two_sample_qq_plot.png')
--------------------------------
""";
Uniformity Test#
Click here to download the Python script for this example: uniformity_test.py
We generate \(n=200\) observations from the uniform distribution on \(S^{d-1}\), with \(d=3\).
[6]:
import numpy as np
np.random.seed(0)
from QuadratiK.poisson_kernel_test import PoissonKernelTest
# data generation
z = np.random.normal(size=(200, 3))
data_unif = z / np.sqrt(np.sum(z**2, axis=1, keepdims=True))
# performing the uniformity test
unif_test = PoissonKernelTest(rho=0.7, random_state=42).test(data_unif)
# printing the summary for uniformity test
print(unif_test.summary())
Time taken for execution: 0.164 seconds
Test Results
Poisson Kernel-based quadratic distance test of Uniformity on the Sphere
Tn-Statistic Sn-Statistic
------------------------------------ -------------- --------------
Test Statistic 0.597782 19.7226
Critical Value 1.59653 23.2295
H0 is rejected (1 = True, 0 = False) 0 0
Concentration parameter rho: 0.7
Summary Statistics
Feature 0 Feature 1 Feature 2
------- ----------- ----------- -----------
Mean 0.011 -0.0275 -0.1068
Std Dev 0.5931 0.5788 0.553
Median 0.013 -0.0448 -0.1833
IQR 1.0685 0.9798 0.9148
Min -0.9836 -0.9766 -0.9938
Max 0.9819 0.9975 0.9862
The qq_plot function can be used to generate the qq-plots between the given samples and the uniform distribution.
Tuning Parameter \(h\) selection#
The algorithm is implemented through the function select_h. The function select_h takes as arguments the data matrix x, the vector of labels y, and the type of alternatives (one of “location”, “scale” or “skewness”); select_h returns not only the selected value of h, but also the power plot versus the considered list of h values for each tested value of \(\delta\).
For Two-Sample Test#
Click here to download the Python script for this example: h_selection_two_sample_test.py
We present the algorithm for selecting the optimal value of the tuning parameter in the two-sample problem. The algorithm for the selection of h for the two-sample test can be also performed providing the two samples \(x\) and \(y\).
[7]:
from QuadratiK.kernel_test import select_h
# Perform the algorithm for selecting h
h_selected, all_powers, plot = select_h(
x=X_2, y=Y_2, alternative="location", power_plot=True
)
print(f"Selected h is: {h_selected}")
# To save the power plot: run the following line
# plot.savefig('two_sample_power_plot.png')
"""
If you want to run the following line in python terminal or in a .py file, please uncomment the code below and run.
--------------------------------
from QuadratiK.kernel_test import select_h
import matplotlib.pyplot as plt
# Perform the algorithm for selecting h
h_selected, all_powers, plot = select_h(
x=X_2, y=Y_2, alternative="location", power_plot=True
)
plt.show()
plot.savefig('two_sample_power_plot.png')
print(f"Selected h is: {h_selected}")
--------------------------------
""";
Selected h is: 1.2
For K-Sample Test#
Click here to download the Python script for this example: h_selection_k_sample_test.py
We present the algorithm for selecting the optimal value of the tuning parameter in the k-sample problem.
[8]:
from QuadratiK.kernel_test import select_h
# Perform the algorithm for selecting h
h_selected, all_powers, plot = select_h(
x=X_k, y=y_k, alternative="skewness", power_plot=True, method="subsampling", b=0.9
)
print(f"Selected h is: {h_selected}")
Selected h is: 1.2
Real World Examples#
Two-Sample Test#
Click here to download the Python script for this example: real_world_two_sample_test.py
We utilize the Wisconsin Breast Cancer (Diagnostic) Dataset from the UCI repository to demonstrate the application of the Two-Sample Test in a real-world context.
[9]:
from QuadratiK.datasets import load_wisconsin_breast_cancer_data
from QuadratiK.kernel_test import KernelTest, select_h
X, y = load_wisconsin_breast_cancer_data(return_X_y=True, scaled=True)
# Create masks for Malignant (M) and Benign (B) tumors
malignant_mask = y == 1
benign_mask = y == 0
# Create X1 and X2 using the masks
X1 = X[malignant_mask.all(axis=1)]
X2 = X[benign_mask.all(axis=1)]
# Perform the algorithm for selecting h
h_selected, all_powers = select_h(
x=X1, y=X2, alternative="skewness", method="subsampling", b=0.5, n_jobs=-1
)
print(f"Selected h is: {h_selected}")
# performing the two sample test
two_sample_test = KernelTest(h=h_selected, num_iter=150, random_state=42).test(X1, X2)
# printing two sample test object
print(two_sample_test)
Selected h is: 0.4
KernelTest(
Test Type=Kernel-based quadratic distance two-sample test,
Execution Time=10.068361043930054 seconds,
Dn-Statistic=11.576053314243081,
Dn-Statistic Critical Value=0.15098023318208792,
Dn-Statistic Null Hypothesis Rejected=True,
Dn-Statistic Variance=2.1204435325619474e-05,
Trace-Statistic=103.19086054532761,
Trace-Statistic Critical Value=1.3544972002157523,
Trace-Statistic Null Hypothesis Rejected=True,
Trace-Statistic Variance=7.58855907498973e-08,
Selected tuning parameter h=0.4,
Critical Value Method=subsampling
)
K-Sample Test#
Click here to download the Python script for this example: real_world_k_sample_test.py
To illustrate the application of the K-Sample Test, we use the wine dataset from the UCI repository.
[10]:
from QuadratiK.datasets import load_wine_data
from QuadratiK.kernel_test import KernelTest, select_h
X, y = load_wine_data(return_X_y=True, scaled=True)
# Perform the algorithm for selecting h
h_selected, all_powers = select_h(
x=X, y=y, alternative="skewness", n_jobs=-1, b=0.5, method="subsampling"
)
print(f"Selected h is: {h_selected}")
# performing the two sample test
k_sample_test = KernelTest(h=h_selected, num_iter=150, random_state=42).test(X, y)
# printing the summary for the two sample test
print(k_sample_test.summary())
Selected h is: 1.2
Time taken for execution: 0.531 seconds
Test Results
Kernel-based quadratic distance K-sample test
Dn Trace
------------------------------------ -------- --------
Test Statistic 8.49507 37.8885
Critical Value 0.327483 1.45937
H0 is rejected (1 = True, 0 = False) 1 1
CV method: subsampling
Selected tuning parameter h : 1.2
Summary Statistics
Group 1 Group 2 Group 3 Overall
------------------------- --------- --------- --------- ---------
('Feature 0', 'Mean') 0.0127 0.0251 0.0213 0.02
('Feature 0', 'Std Dev') 0.0025 0.0069 0.004 0.0073
('Feature 0', 'Median') 0.0126 0.0248 0.0211 0.019
('Feature 0', 'IQR') 0.0031 0.011 0.0051 0.0112
('Feature 0', 'Min') 0.0084 0.0131 0.0141 0.0084
('Feature 0', 'Max') 0.0192 0.0411 0.0328 0.0411
('Feature 1', 'Mean') 0.0019 0.0041 0.0054 0.0037
('Feature 1', 'Std Dev') 0.001 0.0027 0.0021 0.0025
('Feature 1', 'Median') 0.0016 0.0033 0.0051 0.0032
('Feature 1', 'IQR') 0.0006 0.0024 0.0024 0.0033
('Feature 1', 'Min') 0.0009 0.0012 0.0019 0.0009
('Feature 1', 'Max') 0.0058 0.0149 0.0119 0.0149
('Feature 2', 'Mean') 0.0023 0.0046 0.0039 0.0036
('Feature 2', 'Std Dev') 0.0005 0.0014 0.0008 0.0014
('Feature 2', 'Median') 0.0021 0.0045 0.0038 0.0035
('Feature 2', 'IQR') 0.0003 0.002 0.0011 0.0021
('Feature 2', 'Min') 0.0015 0.0023 0.0026 0.0015
('Feature 2', 'Max') 0.0039 0.0073 0.0062 0.0073
('Feature 3', 'Mean') 0.0159 0.0414 0.0347 0.0311
('Feature 3', 'Std Dev') 0.0044 0.0133 0.0076 0.0147
('Feature 3', 'Median') 0.0154 0.039 0.0343 0.03
('Feature 3', 'IQR') 0.0045 0.0191 0.011 0.0224
('Feature 3', 'Min') 0.0077 0.0194 0.0214 0.0077
('Feature 3', 'Max') 0.0298 0.0697 0.0577 0.0697
('Feature 4', 'Mean') 0.099 0.1895 0.1599 0.1515
('Feature 4', 'Std Dev') 0.025 0.0486 0.0317 0.0541
('Feature 4', 'Median') 0.0933 0.1869 0.1565 0.1468
('Feature 4', 'IQR') 0.0294 0.0636 0.0439 0.0733
('Feature 4', 'Min') 0.0641 0.1112 0.1095 0.0641
('Feature 4', 'Max') 0.166 0.298 0.2627 0.298
('Feature 5', 'Mean') 0.0026 0.0046 0.0027 0.0034
('Feature 5', 'Std Dev') 0.0006 0.0017 0.0007 0.0015
('Feature 5', 'Median') 0.0025 0.0041 0.0025 0.0029
('Feature 5', 'IQR') 0.0007 0.0026 0.0007 0.0015
('Feature 5', 'Min') 0.0017 0.0013 0.0016 0.0013
('Feature 5', 'Max') 0.0039 0.0084 0.005 0.0084
('Feature 6', 'Mean') 0.0027 0.0043 0.0013 0.003
('Feature 6', 'Std Dev') 0.0006 0.002 0.0006 0.0018
('Feature 6', 'Median') 0.0027 0.0039 0.001 0.0027
('Feature 6', 'IQR') 0.0008 0.0028 0.0007 0.0018
('Feature 6', 'Min') 0.0017 0.0011 0.0006 0.0006
('Feature 6', 'Max') 0.0041 0.0106 0.003 0.0106
('Feature 7', 'Mean') 0.0003 0.0007 0.0007 0.0006
('Feature 7', 'Std Dev') 0.0001 0.0003 0.0002 0.0003
('Feature 7', 'Median') 0.0003 0.0007 0.0007 0.0005
('Feature 7', 'IQR') 0.0001 0.0005 0.0003 0.0006
('Feature 7', 'Min') 0.0001 0.0001 0.0003 0.0001
('Feature 7', 'Max') 0.0006 0.002 0.0012 0.002
('Feature 8', 'Mean') 0.0018 0.0033 0.0018 0.0024
('Feature 8', 'Std Dev') 0.0005 0.0014 0.0007 0.0012
('Feature 8', 'Median') 0.0016 0.0032 0.0017 0.002
('Feature 8', 'IQR') 0.0007 0.0018 0.0007 0.0015
('Feature 8', 'Min') 0.001 0.0006 0.0008 0.0006
('Feature 8', 'Max') 0.0029 0.0075 0.004 0.0075
('Feature 9', 'Mean') 0.005 0.0062 0.0119 0.0073
('Feature 9', 'Std Dev') 0.001 0.0023 0.0041 0.0038
('Feature 9', 'Median') 0.0048 0.0059 0.0109 0.006
('Feature 9', 'IQR') 0.0015 0.0031 0.0064 0.0042
('Feature 9', 'Min') 0.0031 0.0022 0.0048 0.0022
('Feature 9', 'Max') 0.0076 0.0125 0.0219 0.0219
('Feature 10', 'Mean') 0.001 0.0021 0.0011 0.0015
('Feature 10', 'Std Dev') 0.0002 0.0007 0.0003 0.0007
('Feature 10', 'Median') 0.001 0.0019 0.0011 0.0012
('Feature 10', 'IQR') 0.0002 0.0008 0.0004 0.0009
('Feature 10', 'Min') 0.0006 0.001 0.0007 0.0006
('Feature 10', 'Max') 0.0014 0.0047 0.0017 0.0047
('Feature 11', 'Mean') 0.003 0.0057 0.0027 0.004
('Feature 11', 'Std Dev') 0.0008 0.002 0.0006 0.002
('Feature 11', 'Median') 0.0027 0.0054 0.0026 0.0034
('Feature 11', 'IQR') 0.0012 0.0027 0.0008 0.0023
('Feature 11', 'Min') 0.0017 0.0021 0.0017 0.0017
('Feature 11', 'Max') 0.0047 0.0109 0.0039 0.0109
('Feature 12', 'Mean') 0.9945 0.9792 0.9856 0.986
('Feature 12', 'Std Dev') 0.0029 0.0108 0.0061 0.0101
('Feature 12', 'Median') 0.9954 0.9815 0.9869 0.9884
('Feature 12', 'IQR') 0.0029 0.0125 0.0076 0.0119
('Feature 12', 'Min') 0.9857 0.9518 0.9625 0.9518
('Feature 12', 'Max') 0.9978 0.9932 0.9934 0.9978
Poisson Kernel Based Clustering#
Click here to download the Python script for this example: pkbc.py
We consider the Wireless Indoor Localization Data Set, publicly available in the UCI Machine Learning Repository’s website. This data set is used to study the performance of different indoor localization algorithms.
The Wireless Indoor Localization data set contains the measurements of the Wi-Fi signal strength in different indoor rooms. It consists of a data frame with 2000 rows and 8 columns. The first 7 variables report the values of the Wi-Fi signal strength received from 7 different Wi-Fi routers in an office location in Pittsburgh (USA). The last column indicates the class labels, from 1 to 4, indicating the different rooms. Notice that, the Wi-Fi signal strength is measured in dBm, decibel milliwatts, which is expressed as a negative value ranging from -100 to 0. In total, we have 500 observations for each room.
Given that the Wi-Fi signal strength takes values in a limited range, it is appropriate to consider the spherically transformed observations, by \(L_2\) normalization, and consequently perform the clustering algorithm on the 7-dimensional sphere.
[11]:
import warnings
from QuadratiK.datasets import load_wireless_data
from QuadratiK.spherical_clustering import PKBC
warnings.filterwarnings("ignore")
X, y = load_wireless_data(return_X_y=True)
# number of clusters tried are from 2 to 10
pkbc = PKBC(num_clust=range(2, 11), random_state=42).fit(X)
[12]:
validation_metrics, elbow_plots = pkbc.validation(y_true=y)
# To save the power plot: run the following line
# elbow_plots.savefig('elbow_plots.png')
"""
If you want to run the following line in python terminal or in a .py file, please uncomment the code below and run.
--------------------------------
import matplotlib.pyplot as plt
validation_metrics, elbow_plots = pkbc.validation(y_true=y)
plt.show()
elbow_plots.savefig('elbow_plots.png')
--------------------------------
""";
To guide the choice of the number of clusters, the function validation provides cluster validation measures and graphical tools. Specifically, it displays the Elbow plot from the computed within-cluster sum of squares values and returns an a table of computed evaluation measures as shown below.
[13]:
print(validation_metrics.round(2))
2 3 4 5 6 7 8 9 10
Metrics
ARI 0.31 0.70 0.94 0.91 0.88 0.80 0.73 0.69 0.60
Macro Precision 0.31 0.61 0.98 0.98 0.98 0.98 0.97 0.98 0.97
Macro Recall 0.50 0.75 0.98 0.98 0.98 0.98 0.97 0.98 0.97
Average Silhouette Score 0.42 0.35 0.38 0.30 0.20 0.13 0.11 0.08 0.10
[14]:
print(pkbc.summary())
Loglikelihood Euclidean WCSS Cosine WCSS Num Data Point/Cluster Mixing Proportions (alpha)
-- --------------- ---------------- ------------- ------------------------------------------------------------------------------ ---------------------------------------------------
2 18954.2 13.8 1993.1 {0: 1458, 1: 542} [0.73 0.27]
3 21249.9 11 1994.5 {0: 522, 1: 974, 2: 504} [0.27 0.47 0.25]
4 22325.2 7.19 1996.41 {1: 504, 2: 510, 3: 486, 0: 500} [0.25 0.26 0.26 0.24]
5 22444.7 7.09 1996.45 {1: 507, 4: 460, 3: 487, 0: 46, 2: 500} [0.03 0.26 0.25 0.24 0.23]
6 22596.6 6.99 1996.51 {0: 509, 2: 461, 4: 487, 3: 46, 5: 443, 1: 54} [0.26 0.03 0.23 0.03 0.24 0.22]
7 22652.8 6.86 1996.57 {1: 149, 6: 357, 5: 464, 3: 487, 4: 46, 0: 443, 2: 54} [0.22 0.08 0.03 0.24 0.03 0.23 0.18]
8 22723.1 6.66 1996.67 {6: 150, 7: 359, 4: 293, 5: 477, 2: 176, 1: 48, 3: 443, 0: 54} [0.03 0.03 0.1 0.22 0.14 0.23 0.08 0.18]
9 22783.9 6.26 1996.87 {3: 150, 1: 355, 6: 423, 2: 256, 8: 242, 7: 45, 5: 32, 4: 443, 0: 54} [0.03 0.18 0.12 0.08 0.22 0.02 0.2 0.03 0.13]
10 22824.2 6.08 1996.96 {1: 162, 4: 240, 9: 105, 8: 319, 0: 240, 2: 258, 6: 130, 3: 49, 7: 443, 5: 54} [0.11 0.08 0.13 0.03 0.11 0.03 0.08 0.22 0.15 0.06]
[15]:
"""
This plot is created using Plotly. For detailed instructions on saving the plot, please refer to the Plotly documentation at:
https://plotly.com/python/static-image-export/. Additionally, the current renderer is set to "png", but the plot can be saved in various formats; please consult the Plotly documentation for more information.
Please be aware that generating static images requires `Kaleido` and `nbformat`.
"""
# please feel free to change the default renderer, for options see: https://plotly.com/python/renderers/
import plotly.io as pio
# pio.renderers.default = "svg"
pkbc_clusters = pkbc.plot(num_clust=4, y_true=y)
pkbc_clusters.show()
# To save the plot: run the following line
# pkbc_clusters.write_image("pkbc_clusters.png")
"""
If you want to run the following line in python terminal, please uncomment the code below and run.
--------------------------------
import plotly.io as pio
# For viewing the plot please set:
pio.renderers.default = "browser"
pkbc_clusters = pkbc.plot(num_clust=4, y_true=y)
pkbc_clusters.show()
# Once the plot opens in the browser, you can save the plot by clicking on the "Download" button in the plot on top right corner.
--------------------------------
""";
The clusters identified with \(k=4\) achieve high performance in terms of ARI, Macro Precision and Macro Recall.
Initializing the Dashboard#
We show the initialization of the dashboard application. The corresponding code snippet is given below.
The dashboard is also available at: https://dashboard-quadratik.apps.buffalo.edu/
[16]:
# uncomment the below code to instantiate the dashboard on a local machine. We recommend to run the code on terminal.
# There are known issues with async loop in Jupyter notebook.
# Please see - https://stackoverflow.com/questions/55409641/asyncio-run-cannot-be-called-from-a-running-event-loop-when-using-jupyter-no
# If running on Jupyter notebook, please use streamlit >=1.42.0.
"""
from QuadratiK.ui import UI
UI().run()
"""
# If you are using Jupyter notebook, on streamlit <1.42.0, please use the following code to run the dashboard.
# You need to install nest_asyncio package.
# pip install nest-asyncio
"""
import nest_asyncio
nest_asyncio.apply()
from QuadratiK.ui import UI
UI().run()
"""
[16]:
'\nimport nest_asyncio\nnest_asyncio.apply()\nfrom QuadratiK.ui import UI\nUI().run()\n'

The above image shows the landing page of the user interface in the QuadratiK package