本文整理汇总了Python中sklearn.decomposition.PCA属性的典型用法代码示例。如果您正苦于以下问题:Python decomposition.PCA属性的具体用法?Python decomposition.PCA怎么用?Python decomposition.PCA使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类sklearn.decomposition
的用法示例。
在下文中一共展示了decomposition.PCA属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PCA
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def PCA(data, num_components=None):
# mean center the data
data -= data.mean(axis=0)
# calculate the covariance matrix
R = np.cov(data, rowvar=False)
# calculate eigenvectors & eigenvalues of the covariance matrix
# use 'eigh' rather than 'eig' since R is symmetric,
# the performance gain is substantial
V, E = np.linalg.eigh(R)
# sort eigenvalue in decreasing order
idx = np.argsort(V)[::-1]
E = E[:,idx]
# sort eigenvectors according to same index
V = V[idx]
# select the first n eigenvectors (n is desired dimension
# of rescaled data array, or dims_rescaled_data)
E = E[:, :num_components]
# carry out the transformation on the data using eigenvectors
# and return the re-scaled data, eigenvalues, and eigenvectors
return np.dot(E.T, data.T).T, V, E
示例2: fit
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def fit(self, x):
""" Compute PCA.
Parameters
----------
x : ndarray, shape(n_samples, n_feat)
Input matrix.
Returns
-------
self : object
Returns self.
"""
pca = PCA(n_components=self.n_components,
random_state=self.random_state)
self.maps_ = pca.fit_transform(x)
self.lambdas_ = pca.explained_variance_
return self
示例3: gen_instance
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def gen_instance(self, max_length, dimension, test_mode=True, seed=0):
if seed!=0: np.random.seed(seed)
# Randomly generate (max_length) cities with (dimension) coordinates in [0,100]
seq = np.random.randint(100, size=(max_length, dimension))
# Principal Component Analysis to center & rotate coordinates
pca = PCA(n_components=dimension)
sequence = pca.fit_transform(seq)
# Scale to [0,1[
input_ = sequence/100
if test_mode == True:
return input_, seq
else:
return input_
# Generate random batch for training procedure
示例4: getGFKDim
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def getGFKDim(Xs, Xt):
Pss = PCA().fit(Xs).components_.T
Pts = PCA().fit(Xt).components_.T
Psstt = PCA().fit(np.vstack((Xs, Xt))).components_.T
DIM = round(Xs.shape[1]*0.5)
res = -1
for d in range(1, DIM+1):
Ps = Pss[:, :d]
Pt = Pts[:, :d]
Pst = Psstt[:, :d]
alpha1 = getAngle(Ps, Pst, d)
alpha2 = getAngle(Pt, Pst, d)
D = (alpha1 + alpha2) * 0.5
check = [round(D[1, dd]*100) == 100 for dd in range(d)]
if True in check:
res = list(map(lambda i: i == True, check)).index(True)
return res
示例5: get_rot_rad
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def get_rot_rad(init_coorx, coory, z=50, coorW=1024, coorH=512, floorW=1024, floorH=512, tol=5):
gpid = get_gpid(init_coorx, coorW)
coor = np.hstack([np.arange(coorW)[:, None], coory[:, None]])
xy = np_coor2xy(coor, z, coorW, coorH, floorW, floorH)
xy_cor = []
rot_rad_suggestions = []
for j in range(len(init_coorx)):
pca = PCA(n_components=1)
pca.fit(xy[gpid == j])
rot_rad_suggestions.append(_get_rot_rad(*pca.components_[0]))
rot_rad_suggestions = np.sort(rot_rad_suggestions + [1e9])
rot_rad = np.mean(rot_rad_suggestions[:-1])
best_rot_rad_sz = -1
last_j = 0
for j in range(1, len(rot_rad_suggestions)):
if rot_rad_suggestions[j] - rot_rad_suggestions[j-1] > tol:
last_j = j
elif j - last_j > best_rot_rad_sz:
rot_rad = rot_rad_suggestions[last_j:j+1].mean()
best_rot_rad_sz = j - last_j
dx = int(round(rot_rad * 1024 / 360))
return dx, rot_rad
示例6: pca
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def pca(self, **kwargs):
if 'n_components' in kwargs:
nComp = kwargs['n_components']
else:
nComp = 0.995
if 'dates' in kwargs:
mat = self.to_matrix(kwargs['dates'])
else:
mat = self.to_matrix()
scaler = StandardScaler()
pca = PCA(n_components=nComp)
self._pipeline = Pipeline([('scaler', scaler), ('pca', pca)])
self._pipeline.fit(mat)
if 'file' in kwargs:
tofile(kwargs['file'], self._pipeline)
return self._pipeline
示例7: __init__
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def __init__(self,
weighter=LengthNormalizer(),
normalizer=StandardScaler(),
selector=AssociationCompactor(1000, RankDifference),
projector=PCA(2)):
'''
:param weighter: instance of an sklearn class with fit_transform to weight X category corpus.
:param normalizer: instance of an sklearn class with fit_transform to normalize term X category corpus.
:param selector: instance of a compactor class, if None, no compaction will be done.
:param projector: instance an sklearn class with fit_transform
'''
self.weighter_ = weighter
self.normalizer_ = normalizer
self.selector_ = selector
self.projector_ = projector
示例8: parse_args
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def parse_args():
""" Parse input arguments """
parser = argparse.ArgumentParser(description='Feature extraction for RCC algorithm')
parser.add_argument('--dataset', default=None, type=str,
help='The entered dataset file must be in the Data folder')
parser.add_argument('--prep', dest='prep', default='none', type=str,
help='preprocessing of data: scale,minmax,normalization,none')
parser.add_argument('--algo', dest='algo', default='mknn', type=str,
help='Algorithm to use: knn,mknn')
parser.add_argument('--k', dest='k', default=10, type=int,
help='Number of nearest neighbor to consider')
parser.add_argument('--pca', dest='pca', default=None, type=int,
help='Dimension of PCA processing before kNN graph construction')
parser.add_argument('--samples', dest='nsamples', default=0, type=int,
help='total samples to consider')
parser.add_argument('--format', choices=['mat', 'pkl', 'h5'], default='mat', help='Dataset format')
args = parser.parse_args()
return args
示例9: pca
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def pca(features, n_components=2):
"""
Returns the embedded points for PCA.
Parameters
----------
features: numpy.ndarray
contains the input feature vectors.
n_components: int
number of components to transform the features into
Returns
-------
embedding: numpy.ndarray
x,y(z) points that the feature vectors have been transformed into
"""
embedding = PCA(n_components=n_components).fit_transform(features)
return embedding
########################################################################################################################
示例10: create_writer
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def create_writer(self,
image_out_port: None) -> PcaTaskWriter:
"""
Method to create an instance of PcaTaskWriter.
Parameters
----------
image_out_port : None
Output port, not used.
Returns
-------
pynpoint.util.multipca.PcaTaskWriter
PCA task writer.
"""
return PcaTaskWriter(self.m_result_queue,
self.m_mean_out_port,
self.m_median_out_port,
self.m_weighted_out_port,
self.m_clip_out_port,
self.m_data_mutex,
self.m_requirements)
示例11: init_creator
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def init_creator(self,
image_in_port: None) -> PcaTaskCreator:
"""
Method to create an instance of PcaTaskCreator.
Parameters
----------
image_in_port : None
Input port, not used.
Returns
-------
pynpoint.util.multipca.PcaTaskCreator
PCA task creator.
"""
return PcaTaskCreator(self.m_tasks_queue,
self.m_num_proc,
self.m_pca_numbers)
示例12: vis
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def vis(embed, vis_alg='PCA', pool_alg='REDUCE_MEAN'):
plt.close()
fig = plt.figure()
plt.rcParams['figure.figsize'] = [21, 7]
for idx, ebd in enumerate(embed):
ax = plt.subplot(2, 6, idx + 1)
vis_x = ebd[:, 0]
vis_y = ebd[:, 1]
plt.scatter(vis_x, vis_y, c=subset_label, cmap=ListedColormap(["blue", "green", "yellow", "red"]), marker='.',
alpha=0.7, s=2)
ax.set_title('pool_layer=-%d' % (idx + 1))
plt.tight_layout()
plt.subplots_adjust(bottom=0.1, right=0.95, top=0.9)
cax = plt.axes([0.96, 0.1, 0.01, 0.3])
cbar = plt.colorbar(cax=cax, ticks=range(num_label))
cbar.ax.get_yaxis().set_ticks([])
for j, lab in enumerate(['ent.', 'bus.', 'sci.', 'heal.']):
cbar.ax.text(.5, (2 * j + 1) / 8.0, lab, ha='center', va='center', rotation=270)
fig.suptitle('%s visualization of BERT layers using "bert-as-service" (-pool_strategy=%s)' % (vis_alg, pool_alg),
fontsize=14)
plt.show()
示例13: load_wemb
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def load_wemb(params, vocab):
wemb = pkl.load(open(prm.wordemb_path, 'rb'))
dim_emb_orig = wemb.values()[0].shape[0]
W = 0.01 * np.random.randn(prm.n_words, dim_emb_orig).astype(config.floatX)
for word, pos in vocab.items():
if word in wemb:
W[pos,:] = wemb[word]
if prm.dim_emb < dim_emb_orig:
pca =PCA(n_components=prm.dim_emb, copy=False, whiten=True)
W = pca.fit_transform(W)
params['W'] = W
return params
示例14: Transform
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def Transform(self, data_container, store_folder='', store_key=''):
data = data_container.GetArray()
if data.shape[1] != self.GetModel().components_.shape[1]:
print('Data can not be transformed by existed PCA')
sub_data = self.GetModel().transform(data)
sub_feature_name = ['PCA_feature_' + str(index) for index in
range(1, super(DimensionReductionByPCA, self).GetRemainedNumber() + 1)]
new_data_container = deepcopy(data_container)
new_data_container.SetArray(sub_data)
new_data_container.SetFeatureName(sub_feature_name)
new_data_container.UpdateFrameByData()
if store_folder:
self.SaveDataContainer(data_container, store_folder, store_key)
return new_data_container
示例15: __init__
# 需要导入模块: from sklearn import decomposition [as 别名]
# 或者: from sklearn.decomposition import PCA [as 别名]
def __init__(
self,
features: ndarray,
algorithm: str = 'kmeans',
pca_k: int = None,
random_state: int = 12345
):
"""
:param features: the embedding matrix created by bert parent
:param algorithm: Which clustering algorithm to use
:param pca_k: If you want the features to be ran through pca, this is the components number
:param random_state: Random state
"""
if pca_k:
self.features = PCA(n_components=pca_k).fit_transform(features)
else:
self.features = features
self.algorithm = algorithm
self.pca_k = pca_k
self.random_state = random_state