Код: Выделить всё
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
#https://pypi.org/project/kmodes/
!pip install kmodes
#https://pypi.org/project/kprototypes/
!pip install kprototypes
#https://pypi.org/project/kmodes/
!pip install --upgrade kmodes
from kmodes.kmodes import KModes
from kmodes.kprototypes import KPrototypes
from kmodes import kprototypes
#read data input
data = pd.read_csv('data path')
data=data.copy()
print(data.head())
import array
my_var = 5
# Create an array of integers
my_array = array.array('i', [my_var])
# Add more elements to the array
my_array.append(10)
my_array.append(15)
print(my_array)
# Define the categorical features indices
categorical_features_indices = ['htn','dm','cad','appet','pe','ane','rbc','pc','pcc','ba']
# Create a KPrototypes object
kproto = KPrototypes(n_clusters=3, init='Huang', random_state=42) # Adjust parameters as needed
import array
var1=data['bp'].astype(int)
var2=data['htn']
array1 = array.array([var1])
array2 = array.array([var2])
# Fit the model to your data
clusters = kproto.fit_predict(array1, array2)
# Get the cluster labels for each data point
print(clusters)
# Get the cluster centroids
print(kproto.cluster_centroids_)
# Add the cluster labels to your DataFrame
data['cluster'] = clusters
< /code>
Когда я ее запускаю, у меня есть ошибка < /p>
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
9 var2=data['htn']
10
---> 11 array1 = array.array([var1])
12 array2 = array.array([var2])
13
TypeError: array() argument 1 must be a unicode character, not list
---------------------------------------------------------------------------
< /code>
для этой строки. < /p>
kproto = KPrototypes(n_clusters=3, init='Huang', random_state=42) # Adjust parameters as needed
https://kprototypes.readthedocs.io/en/latest/api.html
< P> Но почему я продолжаю иметь ошибку?>
Подробнее здесь: https://stackoverflow.com/questions/793 ... clustering