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 :: matplotlib log2 xaxis 
Python :: try datetime python 
Python :: django jinja subset string 
Python :: Could not locate a bind configured on mapper mapped class class-tablename, SQL expression or this Session. 
Python :: sklearn version 
Python :: edge detection opencv python 
Python :: install tkinter python 3 mac 
Python :: python plot cut off when saving 
Python :: numpy softmax 
Python :: how to take user input in a list in python 
Python :: set seed python 
Python :: opencv trim video duration 
Python :: python plot_confusion_matrix 
Python :: pyttsx3 speech to mp3 
Python :: is string python 
Python :: pandas read csv without index 
Python :: python pandas csv to xlsx semicolon 
Python :: print(DATA.popitem()) 
Python :: def __init__ python not overwrite parrent class 
Python :: Goal Perser 
Python :: check if directory exists python 
Python :: how to loop through files in a directory python 
Python :: dashes seaborn 
Python :: datafram from one date to another 
Python :: python immutable default parameters 
Python :: how to extract words from sentence in python 
Python :: pandas number of observations 
Python :: json load from file python 3 
Python :: gonad 
Python :: python get ip info 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =