import pywt  # make sure PyWavelets is installed in your Orange environment
haar = []
for r in data:
    cA, cD = pywt.dwt(r, 'haar')  # single-level transform
    haar.append(np.concatenate((cA, cD)))  # combine approximation and detail

# Build domain dynamically based on length
domain = Domain([ContinuousVariable(f"Haar_{i+1}") for i in range(len(haar[0]))])
out_data = Table(domain, np.array(haar))

fin_error---6

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AgglomerativeClustering
from Orange.data import Table

# -----------------------------
# Load the dataset
# -----------------------------
# Replace with your CSV path or Orange dataset name
data = Table("your_dataset.csv")  # Example: Table("iris")
X = np.array(data.X)  # Convert Orange data table to NumPy array

# -----------------------------
# Divisive clustering function
# -----------------------------
def divisive(X, n_clusters=3):
    clusters = [X]
    while len(clusters) < n_clusters:
        # Choose the largest cluster to split
        c = max(clusters, key=len)
        clusters.remove(c)

        # Skip clusters too small to split
        if len(c) < 2:
            clusters.append(c)
            continue

        # Split the chosen cluster into 2 using Agglomerative Clustering
        labels = AgglomerativeClustering(n_clusters=2).fit_predict(c)

        # Add the resulting two clusters
        clusters.append(c[labels == 0])
        clusters.append(c[labels == 1])

    return clusters

# -----------------------------
# Perform divisive clustering
# -----------------------------
clusters = divisive(X, n_clusters=3)

# -----------------------------
# Plot the clusters
# -----------------------------
plt.figure(figsize=(7, 5))
colors = ['red', 'green', 'blue', 'orange', 'purple', 'cyan']

# Use only first two features for plotting
for c, col in zip(clusters, colors):
    if c.shape[0] > 0:
        plt.scatter(c[:, 0], c[:, 1], color=col, label=f'Cluster {colors.index(col)+1}')

plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Divisive Clustering')
plt.legend()
plt.show()

fin_error---7