gpt4 book ai didi

python - 如何从一个热编码 csv 文件创建文件夹?

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:46 26 4
gpt4 key购买 nike

我正在研究皮肤癌分类,我有“GroundTruth.csv”文件和“训练数据”作为 jpg 图像,csv 文件采用一种热编码格式,我想将图像放入文件夹中,并将列作为文件夹名称。

这张图的意思很清楚

https://user-images.githubusercontent.com/45392637/57570855-0a6cb400-7407-11e9-8eb3-adb7b1bd70b6.JPG

# Create new folders in the training directory for each of the classes
nv = os.path.join(train_dir, 'nv')
os.mkdir(nv)
mel = os.path.join(train_dir, 'mel')
os.mkdir(mel)
bkl = os.path.join(train_dir, 'bkl')
os.mkdir(bkl)
bcc = os.path.join(train_dir, 'bcc')
os.mkdir(bcc)
akiec = os.path.join(train_dir, 'akiec')
os.mkdir(akiec)
vasc = os.path.join(train_dir, 'vasc')
os.mkdir(vasc)
df = os.path.join(train_dir, 'df')
os.mkdir(df)

# Create new folders in the validation directory for each of the classes
nv = os.path.join(val_dir, 'nv')
os.mkdir(nv)
mel = os.path.join(val_dir, 'mel')
os.mkdir(mel)
bkl = os.path.join(val_dir, 'bkl')
os.mkdir(bkl)
bcc = os.path.join(val_dir, 'bcc')
os.mkdir(bcc)
akiec = os.path.join(val_dir, 'akiec')
os.mkdir(akiec)
vasc = os.path.join(val_dir, 'vasc')
os.mkdir(vasc)
df = os.path.join(val_dir, 'df')
os.mkdir(df)

下一步我想将图像放入文件夹中。

最佳答案

根据编辑的问题和评论中的其他信息进行更新:

import pandas as pd
import os

# Read in the data
ground_truth = pd.read_csv('GroundTruth.csv')

# Loop through the DataFrame created from the csv file
for row in ground_truth.iterrows():
image_name = row[1].image

# Skip the first column, which is not one hot encoded
target_folder = row[1].index[row[1].values[1:].argmax() + 1]
if not os.path.exists(target_folder):
# Create the folder
os.makedirs(target_folder)
# Move the file
os.rename(f'./{image_name}.jpg',
f'./{target_folder}/{image_name}.jpg')

关于python - 如何从一个热编码 csv 文件创建文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56091102/

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