今天廢廢,進行了訓練資料的分割,首先廢廢將資料與label結合在一起並寫入至xy.csv,其結果如下:
程式碼(1)如下:
x=[[0, -1, -1, -1, -1, -1, 50, 6, 4, 89.9, 37.9, 0, 1, 1, 0], [0, -1, -1, -1, -1, -1, 47, 49, 3, 37.9, 89.9, 0, 1, 1, 0]] #資料
y=['2, '3'] #label
#x, y測試用資料,open路徑請自己改
ab=[]
for b in range(len(x)):
xy=[]
xy.append(int(y[b]))
xy.extend(x[b])
ab.append(xy)
file = open('xy.csv',mode='w', newline='')
writer = csv.writer(file)
for a in range(len(ab)):
if ab[a][0] != -2: #-2為無效資料
writer.writerow(ab[a])
file.close()
廢廢按照此文章來進行數據分類,但必須做一些前置作業才行。
【數據集按照csv格式標籤劃分為訓練集和驗證集】
動作有兩個
1.寫入label與檔名創一個label_id_data.csv,來為每個csv檔分配id和其對應label,如圖:
2.按照2、3、4、8、9總共5個標籤,將xy.csv的每一筆資料創建單獨的csv並賦予文件名稱,如圖的擷取資料夾2:
以下為兩步驟結合的程式碼(2):
#可透過程式碼(1)創建的xy.csv來執行以下程式
import random
import numpy as np
import numpy as reshape
import csv
from tkinter import _flatten
x=[]
print("reading file...")
with open('xy.csv', newline='') as csvfile:
rows = csv.reader(csvfile)
data_list = list(rows)
for a in range(len(data_list)):
x.append(data_list[a])
print("write label and data number to csv...")
headers = ['label', 'id']
f = open('label_id_data.csv',mode='w', newline='')
writeCsv = csv.writer(f)
writeCsv.writerow(headers)
#記得創資料夾2、3、4、8、9
print("spilt data to Folder 2/3/4/8/9...")
for a in range(len(x)):
row=[]
row.append(x[a][0])
row.append(str(a)+'.csv')
writeCsv.writerow(row)
if x[a][0] == '2':
file = open('D:/porker_cnn/2/'+str(a) +'.csv',mode='w', newline='')
writer = csv.writer(file)
writer.writerow(x[a][1:16])
file.close()
elif x[a][0] == '3':
file = open('D:/porker_cnn/3/'+str(a) +'.csv',mode='w', newline='')
writer = csv.writer(file)
writer.writerow(x[a][1:16])
file.close()
elif x[a][0] == '4':
file = open('D:/porker_cnn/4/'+str(a) +'.csv',mode='w', newline='')
writer = csv.writer(file)
writer.writerow(x[a][1:16])
file.close()
elif x[a][0] == '8':
file = open('D:/porker_cnn/8/'+str(a) +'.csv',mode='w', newline='')
writer = csv.writer(file)
writer.writerow(x[a][1:16])
file.close()
elif x[a][0] == '9':
file = open('D:/porker_cnn/9/'+str(a) +'.csv',mode='w', newline='')
writer = csv.writer(file)
writer.writerow(x[a][1:16])
file.close()
f.close()
明天的計畫是將資料用成二維資料輸入csv