简体   繁体   中英

Encode folder labels stored in a numpy array in Python

I'm working on a Parkinson dataset. In my dataset folder, there are two folders:

In two of each, there are two other folders but that's really a detail:

in which...:

Now, in my code i'm doing a feature extraction and a label extraction here's my attempt:

(I've used the split function to get the name of the folder as you can tell in line 12.)

from imutils import paths
import numpy as np
import sys
import cv2
import os
import mahotas as mt
data =[]
np.set_printoptions(threshold=sys.maxsize)
pathswave=r'C:\Users\Bsi\Desktop\PFE2\Base2\dataset\wave'
imagePaths = list(paths.list_images(pathswave))
for imagePath in imagePaths:
   label = imagePath.split(os.path.sep)[-2]
   image = cv2.imread(imagePath)
   image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
   image = cv2.blur(image,(3,3))
   image = cv2.resize(image, (200, 200))
   textures = mt.features.haralick(image)
   feat = textures.mean(axis=0)
   data.append(feat)
   data.append(label)
print(np.array(data))

Here's a portion of the output:

Now is there any way to convert the two labels, 'parkinson' and 'healthy' to two distinct integers ( preferably 0 and 1, (1 being 'parkinson).

3 numbers, in case its not either:)

x = imagePath.split(os.path.sep)[-2]
label = '0' if x == 'healthy' else '1' if x == 'parkinsons' else '-1'

You might want to use a dictionary:

my_dict = {'healthy': 0, 'parkinsons': 1}

You can later get 0 or 1, by accessing the elements using get:

my_number=my_dict.get(label)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM