如果您的数据只有一个特征,请使用 array.reshape(-1, 1) 重塑您的数据

Reshape your data either using array.reshape(-1, 1) if your data has a single feature

如何在包含 1300 张图像的数据集上使用 metrics.silouhette_score,我有它们的 ResNet50 特征向量(每个长度为 2048)和一个介于 1 到 9 之间的离散 class 标签?

import pandas as pd
import numpy as np
from sklearn.metrics import pairwise_distances
from sklearn import cluster, datasets, preprocessing, metrics
from sklearn.cluster import KMeans
df = pd.read_csv("master.csv")
labels = list(df['Q3 Theme1'])
labels_reshaped = np.ndarray(labels).reshape(-1,1)
X = open('entire_dataset__resnet50_feature_vectors.txt')
X_Data = X.read()
print('Silhouette Score:', metrics.silhouette_score(X_Data, labels_reshaped,
                                                    metric='cosine'))

我收到这个错误:

Traceback (most recent call last):
  File "/dataset/silouhette_score.py", line 8, in <module>
    labels_reshaped = np.ndarray(labels).reshape(-1,1)
ValueError: sequence too large; cannot be greater than 32

Process finished with exit code 1

对于此其他代码:

import pandas as pd
import numpy as np
from sklearn.metrics import pairwise_distances
from sklearn import cluster, datasets, preprocessing, metrics
from sklearn.cluster import KMeans
df = pd.read_csv("master.csv")
labels = list(df['Q3 Theme1'])
labels_reshaped = np.ndarray(labels).reshape(1,-1)
X = open('entire_dataset__resnet50_feature_vectors.txt')
X_Data = X.read()
print('Silhouette Score:', metrics.silhouette_score(X_Data, labels_reshaped,
                                                    metric='cosine'))

我收到这个错误:

Traceback (most recent call last):
  File "/dataset/silouhette_score.py", line 8, in <module>
    labels_reshaped = np.ndarray(labels).reshape(1,-1)
ValueError: sequence too large; cannot be greater than 32

Process finished with exit code 1

如果我 运行 这个其他代码:

import pandas as pd
from sklearn import metrics
df = pd.read_csv("master.csv")
labels = list(df['Q3 Theme1'])
X = open('entire_dataset__resnet50_feature_vectors.txt')
X_Data = X.read()
print('Silhouette Score:', metrics.silhouette_score(X_Data, labels,
                                                    metric='cosine'))

我得到这个作为输出:https://pastebin.com/raw/hk2axdWL

如何修复此代码以便打印单个剪影乐谱?

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Process finished with exit code 1

我在这里粘贴了一行我的特征向量文件(.txt 文件):https://pastebin.com/raw/hk2axdWL(由 space 分隔的 2048 个数字组成)

我终于弄明白了。我需要创建与 sklearn 要求的格式完全相同的特征向量:

import pandas as pd
from sklearn import metrics


df = pd.read_csv("master.csv")
labels = list(df['Q3 Theme1'])
X = open('entire_dataset__resnet50_feature_vectors.txt')
#X_Data = X.read()

fv = []
for line in X:
    line = line.strip("\n")
    tmp_arr = line.split(' ')
    print(tmp_arr)
    fv.append(tmp_arr)

print(fv)
print('Silhouette Score:', metrics.silhouette_score(fv, labels,
                                                    metric='cosine'))