我从来没有使用过增量PCA,它存在于sklearn中,我对它的参数有点困惑,无法找到一个很好的解释。
我看到构造函数中有batch_size,但是在使用partial_fit方法时,您可以再次传递部分数据,我发现了以下方法:
n = df.shape[0]
chunk_size = 100000
iterations = n//chunk_size
ipca = IncrementalPCA(n_components=40, batch_size=1000)
for i in range(0, iterations):
ipca.partial_fit(df[i*chunk_size : (i+1)*chunk_size].values)
ipca.partial_fit(df[iterations*chunk_size : n].values)现在,我不明白的是,当使用部分拟合时,batch_size是否发挥了任何作用?它们之间有什么关系?
此外,如果两者都考虑在内,当我想要在增加内存占用的同时提高精度时,我应该如何适当地改变它们的值(反过来,降低精度的代价是减少内存消耗)?
发布于 2017-02-03 13:24:19
文档说:
batch_size : int或None,(default=None) 每批使用的样本数。只在呼叫fit时才使用..。
此param不在partial_fit中使用,其中批处理大小由用户控制.
更大的批次会增加内存消耗,较小的批将减少内存消耗。这也写在文档中:
该算法按照batch_size的顺序,具有恒定的内存复杂度,允许在不将整个文件加载到内存的情况下使用np.memmap文件。
尽管进行了一些检查和参数启发,但整个fit-function如下所示:
for batch in gen_batches(n_samples, self.batch_size_):
self.partial_fit(X[batch], check_input=False)发布于 2017-06-19 10:53:33
本文介绍了一种基于https://github.com/kevinhughes27/pyIPCA的增量式PCA算法,它是CCIPCA方法的一种实现。
import scipy.sparse as sp
import numpy as np
from scipy import linalg as la
import scipy.sparse as sps
from sklearn import datasets
class CCIPCA:
def __init__(self, n_components, n_features, amnesic=2.0, copy=True):
self.n_components = n_components
self.n_features = n_features
self.copy = copy
self.amnesic = amnesic
self.iteration = 0
self.mean_ = None
self.components_ = None
self.mean_ = np.zeros([self.n_features], np.float)
self.components_ = np.ones((self.n_components,self.n_features)) / \
(self.n_features*self.n_components)
def partial_fit(self, u):
n = float(self.iteration)
V = self.components_
# amnesic learning params
if n <= int(self.amnesic):
w1 = float(n+2-1)/float(n+2)
w2 = float(1)/float(n+2)
else:
w1 = float(n+2-self.amnesic)/float(n+2)
w2 = float(1+self.amnesic)/float(n+2)
# update mean
self.mean_ = w1*self.mean_ + w2*u
# mean center u
u = u - self.mean_
# update components
for j in range(0,self.n_components):
if j > n: pass
elif j == n: V[j,:] = u
else:
# update the components
V[j,:] = w1*V[j,:] + w2*np.dot(u,V[j,:])*u / la.norm(V[j,:])
normedV = V[j,:] / la.norm(V[j,:])
normedV = normedV.reshape((self.n_features, 1))
u = u - np.dot(np.dot(u,normedV),normedV.T)
self.iteration += 1
self.components_ = V / la.norm(V)
return
def post_process(self):
self.explained_variance_ratio_ = np.sqrt(np.sum(self.components_**2,axis=1))
idx = np.argsort(-self.explained_variance_ratio_)
self.explained_variance_ratio_ = self.explained_variance_ratio_[idx]
self.components_ = self.components_[idx,:]
self.explained_variance_ratio_ = (self.explained_variance_ratio_ / \
self.explained_variance_ratio_.sum())
for r in range(0,self.components_.shape[0]):
d = np.sqrt(np.dot(self.components_[r,:],self.components_[r,:]))
self.components_[r,:] /= d你可以用
进口熊猫为pd,枕骨
df = pd.read_csv('iris.csv')
df = np.array(df)[:,:4].astype(float)
pca = ccipca.CCIPCA(n_components=2,n_features=4)
S = 10
print df[0, :]
for i in range(150): pca.partial_fit(df[i, :])
pca.post_process()生成的特征向量/值将与批PCA不完全相同。结果是近似的,但它们是有用的。
https://stackoverflow.com/questions/42020619
复制相似问题