2017-03-17 81 views
0

我有我自己的數據集,我想訓練我的模型。我已經成功創建.pk.gz文件,但我不知道如何將它們導入到我的模型中。如何將.pkl.gz文件從我的電腦導入python程序

我正在使用Windows 10,python 3.5.2與tensor-flow和tflearn和sublime text 3編寫代碼。

我用來創建泡菜文件中的代碼:

from numpy import genfromtxt 

import gzip 
import _pickle as cPickle 

#data = sio.loadmat('C:/DeepLearning_lib/Theano/Data/test_x.mat') 

train_set_x = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/Kdd_Train_41.csv', delimiter=',') 

train_set_y = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/NSL_TrainLabels_mat4.csv', delimiter=',') 

valid_set_x = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/Kdd_Valid_41.csv', delimiter=',') 

valid_set_y = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/NSL_ValidLabels_int2.csv', delimiter=',') 

test_set_x = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/Kdd_Test_41.csv', delimiter=',') 

test_set_y = genfromtxt('C:/Users/Jay/Desktop/MachineLearning/dataset/NSL-KDD Processed/NSL_TestLabels_mat5.csv', delimiter=',') 



train_set = test_set_x 
train_set_labels= test_set_y 

valid_set = valid_set_x 
valid_set_labels= valid_set_y 

test_set = train_set_x 
test_set_labels= train_set_y 


f = gzip.open('C:/Users/Jay/Desktop/Data/train_set.pkl.gz','wb') 
cPickle.dump(train_set, f, protocol=2) 

f.close() 

f = gzip.open('C:/Users/Jay/Desktop/Data/train_set_labels.pkl.gz','wb') 
cPickle.dump(train_set_labels, f, protocol=2) 

f.close() 

f = gzip.open('C:/Users/Jay/Desktop/Data/valid_set_labels.pkl.gz','wb') 
cPickle.dump(valid_set_labels, f, protocol=2) 

f.close() 
f = gzip.open('C:/Users/Jay/Desktop/Data/test_set_labels.pkl.gz','wb') 
cPickle.dump(test_set_labels, f, protocol=2) 

f.close() 
f = gzip.open('C:/Users/Jay/Desktop/Data/valid_set.pkl.gz','wb') 
cPickle.dump(valid_set, f, protocol=2) 

f.close() 
f = gzip.open('C:/Users/Jay/Desktop/Data/test_set.pkl.gz','wb') 
cPickle.dump(test_set, f, protocol=2) 

f.close() 

錯誤:使用 'RB'

'OSError: [Errno 9] peek() on write-only GzipFile object' 
+0

燦你顯示你用來創建文件的代碼,並告訴我們它們包含什麼樣的數據?如果'.pkl'意味着你使用pickle.dump()來使用Python ['pickle'](https://docs.python.org/2/library/pickle.html)模塊來編寫,你應該能夠使用'pickle.load()'來檢索對象。 – mrry

+0

'進口的gzip 進口_pickle作爲cPickle的 F = gzip.open( 'C:/Users/Jay/Desktop/Data/train_set.pkl.gz', 'WB') cPickle.load(train_set) 打印( train_set) f.close()'它沒有導入 –

回答

0

下面的代碼應該重構,當你train_set

with gzip.open('C:/Users/Jay/Desktop/Data/train_set.pkl.gz', 'rb') as f: 
    train_set = cPickle.load(f) 
+0

'OSError:[Errno 9] peek()在只寫GzipFile對象上收到這個錯誤! –

+0

糟糕,我從[你的評論]複製''wb''(http://stackoverflow.com/questions/42859435/how-would-i-import-pkl-gz-file-from-my-computer-into- a-python-program/42860527?noredirect = 1#comment72825802_42859435)而不考慮它的含義:)。改爲「rb'',現在應該可以工作。 – mrry

+0

我將其更改爲rb,因爲它在文檔中...沒有影響 –

相關問題