gpt4 book ai didi

Python:对预定 csv 中的多个变量进行 k 均值聚类

转载 作者:行者123 更新时间:2023-12-01 01:48:49 24 4
gpt4 key购买 nike

我正在为我的论文做一个项目,但我非常难过,因为我无法通过 Spotify API 对我的数据集进行 k 均值聚类。

artist_name track_popularity explicit artist_genres album_genres acousticness danceability energy instrumentalness key liveness loudness mode speechness tempo time_signature valence played_at

我的数据集有这些变量,我必须对从声学到价态的变量进行聚类(所以有 12 个变量)。我怎样才能做到这一点?我可以用 2 或 3 个变量来做到这一点,但我不能用四个或四个以上的变量来做到这一点。

> from copy import deepcopy
import numpy as np
import matplotlib.pyplot as plot
import pandas as pd
from sklearn.cluster import KMeans
#importing Dataset
dataset = pd.read_csv('csvProva2.csv')
X = dataset.iloc[:, [10,11]].values #colonne che mi interessano

#Find the number of clusters
wcss = []

for i in range (1,16): #15 cluster
kmeans = KMeans(n_clusters = i, init='k-means++', random_state=0)
kmeans.fit(X)
wcss.append(kmeans.inertia_)

plot.plot(range(1,16),wcss)
plot.title('Elbow Method')
plot.xlabel('Number of clusters')
plot.ylabel('wcss')
plot.show()

#KMeans clustering
kmeans= KMeans(n_clusters=4,init='k-means++', random_state=0)
y=kmeans.fit_predict(X)

plot.scatter(X[y == 0,0], X[y==0,1], s=25, c='red', label='Cluster 1')
plot.scatter(X[y == 1,0], X[y==1,1], s=25, c='blue', label='Cluster 2')
plot.scatter(X[y == 2,0], X[y==2,1], s=25, c='magenta', label='Cluster 3')
plot.scatter(X[y == 3,0], X[y==3,1], s=25, c='cyan', label='Cluster 4')

plot.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], s=25, c='yellow', label='Centroid')
plot.title('KMeans Clustering')
plot.xlabel('Acousticness')
plot.ylabel('Danceability')
plot.legend()
plot.show()

这是我使用 2 个变量进行聚类的代码。

最佳答案

K 均值在超过 3 个变量上运行良好。

但是它们需要是连续变量。您无法计算分类变量的平均值。此外,将变量与不同的 block (单位)混合也是有问题的。那么小规模的特征将大多被忽略。从统计角度来看,结果变得毫无意义:如果您以不同的方式缩放数据,您将得到不同的结果。

关于Python:对预定 csv 中的多个变量进行 k 均值聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50938205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com