大数据增量PCA
Incremental PCA on big data
我刚刚尝试使用 sklearn.decomposition 中的 IncrementalPCA,但它像之前的 PCA 和 RandomizedPCA 一样抛出了 MemoryError。我的问题是,我要加载的矩阵太大,无法放入 RAM。现在它作为形状为 ~(1000000, 1000) 的数据集存储在 hdf5 数据库中,所以我有 1.000.000.000 个 float32 值。我以为 IncrementalPCA 会分批加载数据,但显然它会尝试加载整个数据集,这无济于事。这个库是如何使用的? hdf5 格式有问题吗?
from sklearn.decomposition import IncrementalPCA
import h5py
db = h5py.File("db.h5","r")
data = db["data"]
IncrementalPCA(n_components=10, batch_size=1).fit(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/software/anaconda/2.3.0/lib/python2.7/site-packages/sklearn/decomposition/incremental_pca.py", line 165, in fit
X = check_array(X, dtype=np.float)
File "/software/anaconda/2.3.0/lib/python2.7/site-packages/sklearn/utils/validation.py", line 337, in check_array
array = np.atleast_2d(array)
File "/software/anaconda/2.3.0/lib/python2.7/site-packages/numpy/core/shape_base.py", line 99, in atleast_2d
ary = asanyarray(ary)
File "/software/anaconda/2.3.0/lib/python2.7/site-packages/numpy/core/numeric.py", line 514, in asanyarray
return array(a, dtype, copy=False, order=order, subok=True)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (-------src-dir-------/h5py/_objects.c:2458)
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (-------src-dir-------/h5py/_objects.c:2415)
File "/software/anaconda/2.3.0/lib/python2.7/site-packages/h5py/_hl/dataset.py", line 640, in __array__
arr = numpy.empty(self.shape, dtype=self.dtype if dtype is None else dtype)
MemoryError
感谢帮助
您的程序可能无法尝试将整个数据集加载到 RAM 中。每个 float32 × 1,000,000 × 1000 的 32 位是 3.7 GiB。这在只有 4 GiB RAM 的机器上可能是个问题。要检查它是否确实是问题所在,请尝试单独创建一个这样大小的数组:
>>> import numpy as np
>>> np.zeros((1000000, 1000), dtype=np.float32)
如果您看到 MemoryError
,您要么需要更多 RAM,要么需要一次处理一个块的数据集。
对于 h5py 数据集,我们应该避免将整个数据集传递给我们的方法,而是传递数据集的切片。一次一个。
因为我没有你的数据,让我从创建一个相同大小的随机数据集开始:
import h5py
import numpy as np
h5 = h5py.File('rand-1Mx1K.h5', 'w')
h5.create_dataset('data', shape=(1000000,1000), dtype=np.float32)
for i in range(1000):
h5['data'][i*1000:(i+1)*1000] = np.random.rand(1000, 1000)
h5.close()
它创建了一个不错的 3.8 GiB 文件。
现在,如果我们处于 Linux,我们可以限制程序可用的内存量:
$ bash
$ ulimit -m $((1024*1024*2))
$ ulimit -m
2097152
现在,如果我们尝试 运行 您的代码,我们将得到 MemoryError。 (按 Ctrl-D 退出新的 bash 会话并稍后重置限制)
让我们尝试解决问题。我们将创建一个 IncrementalPCA 对象,并将多次调用其 .partial_fit()
方法,每次都提供不同的数据集切片。
import h5py
import numpy as np
from sklearn.decomposition import IncrementalPCA
h5 = h5py.File('rand-1Mx1K.h5', 'r')
data = h5['data'] # it's ok, the dataset is not fetched to memory yet
n = data.shape[0] # how many rows we have in the dataset
chunk_size = 1000 # how many rows we feed to IPCA at a time, the divisor of n
ipca = IncrementalPCA(n_components=10, batch_size=16)
for i in range(0, n//chunk_size):
ipca.partial_fit(data[i*chunk_size : (i+1)*chunk_size])
它似乎对我有用,如果我查看 top
报告,内存分配保持在 200M 以下。
可以使用 NumPy 的 memmap
class,它允许操作一个
大数组存储在磁盘上的二进制文件中,就好像它完全在内存中一样; class 仅在需要时在内存中加载它需要的数据。由于 incrementalPCA 在任何给定时间使用批处理,因此内存使用量保持在控制之下。这是一个示例代码
from sklearn.decomposition import IncrementalPCA
import numpy as np
X_mm = np.memmap(filename, dtype="float32", mode="readonly", shape=(m, n))
batch_size = m // n_batches
inc_pca = IncrementalPCA(n_components=10, batch_size=batch_size)
inc_pca.fit(X_mm)
我刚刚尝试使用 sklearn.decomposition 中的 IncrementalPCA,但它像之前的 PCA 和 RandomizedPCA 一样抛出了 MemoryError。我的问题是,我要加载的矩阵太大,无法放入 RAM。现在它作为形状为 ~(1000000, 1000) 的数据集存储在 hdf5 数据库中,所以我有 1.000.000.000 个 float32 值。我以为 IncrementalPCA 会分批加载数据,但显然它会尝试加载整个数据集,这无济于事。这个库是如何使用的? hdf5 格式有问题吗?
from sklearn.decomposition import IncrementalPCA
import h5py
db = h5py.File("db.h5","r")
data = db["data"]
IncrementalPCA(n_components=10, batch_size=1).fit(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/software/anaconda/2.3.0/lib/python2.7/site-packages/sklearn/decomposition/incremental_pca.py", line 165, in fit
X = check_array(X, dtype=np.float)
File "/software/anaconda/2.3.0/lib/python2.7/site-packages/sklearn/utils/validation.py", line 337, in check_array
array = np.atleast_2d(array)
File "/software/anaconda/2.3.0/lib/python2.7/site-packages/numpy/core/shape_base.py", line 99, in atleast_2d
ary = asanyarray(ary)
File "/software/anaconda/2.3.0/lib/python2.7/site-packages/numpy/core/numeric.py", line 514, in asanyarray
return array(a, dtype, copy=False, order=order, subok=True)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (-------src-dir-------/h5py/_objects.c:2458)
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (-------src-dir-------/h5py/_objects.c:2415)
File "/software/anaconda/2.3.0/lib/python2.7/site-packages/h5py/_hl/dataset.py", line 640, in __array__
arr = numpy.empty(self.shape, dtype=self.dtype if dtype is None else dtype)
MemoryError
感谢帮助
您的程序可能无法尝试将整个数据集加载到 RAM 中。每个 float32 × 1,000,000 × 1000 的 32 位是 3.7 GiB。这在只有 4 GiB RAM 的机器上可能是个问题。要检查它是否确实是问题所在,请尝试单独创建一个这样大小的数组:
>>> import numpy as np
>>> np.zeros((1000000, 1000), dtype=np.float32)
如果您看到 MemoryError
,您要么需要更多 RAM,要么需要一次处理一个块的数据集。
对于 h5py 数据集,我们应该避免将整个数据集传递给我们的方法,而是传递数据集的切片。一次一个。
因为我没有你的数据,让我从创建一个相同大小的随机数据集开始:
import h5py
import numpy as np
h5 = h5py.File('rand-1Mx1K.h5', 'w')
h5.create_dataset('data', shape=(1000000,1000), dtype=np.float32)
for i in range(1000):
h5['data'][i*1000:(i+1)*1000] = np.random.rand(1000, 1000)
h5.close()
它创建了一个不错的 3.8 GiB 文件。
现在,如果我们处于 Linux,我们可以限制程序可用的内存量:
$ bash
$ ulimit -m $((1024*1024*2))
$ ulimit -m
2097152
现在,如果我们尝试 运行 您的代码,我们将得到 MemoryError。 (按 Ctrl-D 退出新的 bash 会话并稍后重置限制)
让我们尝试解决问题。我们将创建一个 IncrementalPCA 对象,并将多次调用其 .partial_fit()
方法,每次都提供不同的数据集切片。
import h5py
import numpy as np
from sklearn.decomposition import IncrementalPCA
h5 = h5py.File('rand-1Mx1K.h5', 'r')
data = h5['data'] # it's ok, the dataset is not fetched to memory yet
n = data.shape[0] # how many rows we have in the dataset
chunk_size = 1000 # how many rows we feed to IPCA at a time, the divisor of n
ipca = IncrementalPCA(n_components=10, batch_size=16)
for i in range(0, n//chunk_size):
ipca.partial_fit(data[i*chunk_size : (i+1)*chunk_size])
它似乎对我有用,如果我查看 top
报告,内存分配保持在 200M 以下。
可以使用 NumPy 的 memmap
class,它允许操作一个
大数组存储在磁盘上的二进制文件中,就好像它完全在内存中一样; class 仅在需要时在内存中加载它需要的数据。由于 incrementalPCA 在任何给定时间使用批处理,因此内存使用量保持在控制之下。这是一个示例代码
from sklearn.decomposition import IncrementalPCA
import numpy as np
X_mm = np.memmap(filename, dtype="float32", mode="readonly", shape=(m, n))
batch_size = m // n_batches
inc_pca = IncrementalPCA(n_components=10, batch_size=batch_size)
inc_pca.fit(X_mm)