Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pca python

import numpy as np
from sklearn.decomposition import PCA

pca = PCA(n_components = 3) # Choose number of components
pca.fit(X) # fit on X_train if train/test split applied

print(pca.explained_variance_ratio_)
Comment

pca python

from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)
Comment

pca python

from sklearn.decomposition import PCA

pca = PCA(n_components=3)
pca.fit(features)
features_pca = pca.transform(features)
print("original shape:   ", features.shape)
print("transformed shape:", features_pca.shape)
print(pca.explained_variance_)
print(pca.explained_variance_ratio_)
Comment

pca in python

def pca(data, n):
    data = np.array(data)

    # 均值
    mean_vector = np.mean(data, axis=0)

    # 协方差
    cov_mat = np.cov(data - mean_vector, rowvar=0)

    # 特征值 特征向量
    fvalue, fvector = np.linalg.eig(cov_mat)

    # 排序
    fvaluesort = np.argsort(-fvalue)

    # 取前几大的序号
    fValueTopN = fvaluesort[:n]

    # 保留前几大的数值
    newdata = fvector[:, fValueTopN]

    new = np.dot(data, newdata)

    return new
Comment

PREVIOUS NEXT
Code Example
Python :: .translate python 
Python :: division of 2 numbers in python 
Python :: how to get all values from class in python 
Python :: How to perform topological sort of a group of jobs, in Python? 
Python :: smooth interpolation python 
Python :: how to get parent model object based on child model filter in django 
Python :: linux python 
Python :: py quick sort 
Python :: split strings around given separator/delimiter 
Python :: plot dataframe 
Python :: python django login register 
Python :: check if value in dictionary keys python dataframe 
Python :: pandas excel writer append in row 
Python :: unique python 
Python :: python basic programs 
Python :: connect and disconnect event on socketio python 
Python :: are logN and (lognN) same 
Python :: list insert python 
Python :: django create multiple objects 
Python :: remove n characters from string python 
Python :: create django object 
Python :: Converting time python 
Python :: python print every row of dataframe 
Python :: what is data normalization 
Python :: list vs tuple 
Python :: reading from a text file 
Python :: drop columns pandas dataframe 
Python :: python module search 
Python :: strip function in python 
Python :: catching exceptions in python 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =