dict vectorizerを使用して変換する前にデータセットを正規化しようとしています。私のマシンには244GBのメモリがありますが、データを正規化するとこのメモリエラーが発生します。ここに私のコードスニペット、scikit-learn normalizer.fit()メモリエラー
Xは私の特徴データです。
# Normalizer that will normalize the data
normalizer = Normalizer().fit(X)
エラー: -
File "train_models.py", line 336, in splittingdata
normalizer = Normalizer().fit(X)
File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/sklearn/preprocessing/data.py", line 1426, in fit
X = check_array(X, accept_sparse='csr')
File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py", line 407, in check_array
_assert_all_finite(array)
File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py", line 56, in _assert_all_finite
and not np.isfinite(X).all()):
MemoryError
とデータセットのサイズは、ここでは23列
$ free -h
total used free shared buff/cache available
Mem: 240G 563M 238G 8.6M 979M 238G
Swap: 0B 0B 0B
と56万行は私のpythonのアーキテクチャです。
Python 2.7.13 |Anaconda custom (64-bit)| (default, Dec 20 2016, 23:09:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import platform
>>> import sys
>>> platform.architecture(), sys.maxsize
(('64bit', 'ELF'), 9223372036854775807)
####Here is my code.
def splittingdata(X,Y):
# Split X and Y into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33)
# Normalizer that will normalize the data
normalizer = Normalizer().fit(X)
# Normalized Features:
X_norm = normalizer.transform(X)
# Split X and Y into training and testing sets for normalized data
X_norm_train, X_norm_test, Y_norm_train, Y_norm_test = train_test_split(X_norm, Y, test_size=0.33)
# Store Normalizer
joblib.dump(normalizer, '../models/normalizer.pkl')
actualdata = list([X_train, X_test, Y_train, Y_test])
normalizeddata = list([X_norm_train, X_norm_test, Y_norm_train, Y_norm_test])
return list([actualdata,normalizeddata])
def data_encoding(data):
# Build X and Y
# X : Features
# Y : Target
start_time = datetime.datetime.now()
print "Start time of data encoding : ", start_time
# Removing id column (listing_id)
datav1 = data.drop(['id'], axis = 1)
# Taking out the numeric columns separately
numeric_cols = ['list','of','numeric','columns']
#x_numeric = datav1[ numeric_cols ].as_matrix()
x_numeric = datav1[ numeric_cols ]
# Constructing list of dictionaries (one dictionary for each column) to use dictvectorizer
cat_cols = ['list','of','categorical','columns']
cat_dict = datav1[ cat_cols ].to_dict(orient = 'records')
# The DictVectorizer converts data from a dictionary to an array
vectorizer = DictVectorizer()
# Convert X to Array
x_categorical = vectorizer.fit_transform(cat_dict).toarray()
# Combining numeric and categorical data
X = np.hstack((x_numeric, x_categorical))
# Store Vectorizer
joblib.dump(vectorizer, '../models/vectorizer.pkl')
# Taking out the target variable
Y = datav1.target_col
outdata = list([X,Y])
end_time = datetime.datetime.now()
print "End time of data encoding : ", end_time
total_time = end_time - start_time
print "Total time taken for data encoding : ", total_time
return outdata
def main():
#Reading the preprocessed data
processed_data = pd.read_csv('../data/data.csv', sep=',', encoding='utf-8',index_col=0)
#processed_data = processed_data.head(5)
#Encoding dataset
encoded_data = data_encoding(processed_data)
#Splitting dataset
splitted_data = splittingdata(encoded_data[0], encoded_data[1])
actualdata = splitted_data[0]
normalizeddata = splitted_data[1]
output = runmodels(actualdata,normalizeddata)
コメントで示唆したように、私はこのようなガベージコレクションを実行しようとしました、 追加encoding_data機能の終わりに、このスニペット、
#Garbage collection
del x_numeric
del x_categorical
del cat_dict
del datav1
gc.collect()
は同様に私のメイン機能でこれを追加splittingdataを呼び出す前にfunciton、私はベクトライザーを実行すると、それが完了したら、わからないなぜそれがCでないときが、私の記憶が100%にスパイクに気づい何
del processed_data
gc.collect()
ガベージコレクションでも下がり、47%に下がり、ノーマライザは100%になり、失敗します。だから私は答えに示唆されたコードをリファクタリングしても、まだ問題は残っていると思う。実行時にどのオブジェクトがほとんどのメモリを保持しているかを調べる方法はありますか?
: あなたは、このような何かをそれは
actual_data
とnormalized_data
のために別々のモデル/モデルを呼び出すようにコードをリファクタリングを検討し、最後に出力を結合する必要がありますコードを実行する前に空きメモリをチェックしてください。おそらく他のいくつかのプロセスがスペースを取っています(Linuxマシンの場合はfree -hを実行します)。 – MedAli私はそれをほとんど無料でした。質問が更新されました。 –
'X'とは何ですか?どのバージョンのPythonをお使いですか?あなたのPython 32ビットまたは64ビットですか? –