使用轮廓分数

时间:2019-05-13 17:14:39

标签: machine-learning scikit-learn computer-vision cluster-analysis

我有一个数据集,每个数据点都有9个类。对于属于数据集的每个图像,我将其传递给ResNet50并通过将后面的内容切碎来提取2048个特征向量。我已经将ResNet50特征向量的每一行保存为一个txt文件,其顺序与csv文件的顺序相同。

我想为我的数据集中的每个此类计算西洛赫特分数。我不确定如何进行设置。我没有太多的代码,但这是一个入门:

import pandas as pd
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 = 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(由2048个数字组成,用空格分隔)

3 个答案:

答案 0 :(得分:1)

我认为问题出在您要阅读X的行中。我对代码做了一些更改。

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 = 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='euclidean'))

答案 1 :(得分:0)

您忘了阅读文件。

该方法需要数组作为输入,而不是文件句柄。

答案 2 :(得分:0)

这是正确的答案,至少对我有用:

public void captureImage(View view) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (cameraIntent.resolveActivity(getPackageManager()) != null) {
            pictureFile = Helper.getPictureFile(ReceiptActivity.this);
            if (pictureFile != null) {
                photoURI = FileProvider.getUriForFile(ReceiptActivity.this, "com.example.ves.gennaio3.fileprovider", pictureFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(cameraIntent, REQUEST_PICTURE_CAPTURE);
            }
        }
    }

    //if you capture image or choose from phone, convert to bitmap, show on screen and call readTextFromImage
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            Bitmap bitmap = null;

            if (requestCode == REQUEST_PICTURE_CAPTURE) {
                bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
                bitmap = Helper.rotateBitmap(bitmap, pictureFile.getAbsolutePath());
            }

            else if (requestCode == REQUEST_GET_SINGLE_FILE) {
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
                } catch (Exception e) {
                    Log.d("Error Receipt: ", e.toString());
                }
            }
            if (bitmap != null) {

                image.setImageBitmap(bitmap);

                StorageReference filepath = mStorageRef.child("Receipts").child(photoURI.getLastPathSegment());

                filepath.putFile(photoURI).addOnSuccessListener(taskSnapshot -> {

                })
                .addOnFailureListener(e -> {

                });
            }

        }
    }