Scikit-Learn


Output  (1797, 10)  Here, we can partially fit on smaller batches of data (as we did on 100 per batch) or you  can let the  fit()



Yüklə 1,92 Mb.
Pdf görüntüsü
səhifə124/124
tarix23.02.2023
ölçüsü1,92 Mb.
#101394
1   ...   116   117   118   119   120   121   122   123   124
sklearn

Output 
(1797, 10) 
Here, we can partially fit on smaller batches of data (as we did on 100 per batch) or you 
can let the 
fit()
function to divide the data into batches. 
 


Scikit-Learn
143 
Kernel PCA 
Kernel Principal Component Analysis, an extension of PCA, achieves non-linear 
dimensionality reduction using kernels. It supports both 
transform and 
inverse_transform. 
The Scikit-learn ML library provides 
sklearn.decomposition.KernelPCA
 
module. 
Example 
The below example will use 
sklearn.decomposition.KernelPCA
module on Sklearn digit 
dataset. We are using sigmoid kernel

from sklearn.datasets import load_digits 
from sklearn.decomposition import KernelPCA 
X, _ = load_digits(return_X_y=True) 
transformer = KernelPCA(n_components=10, kernel='sigmoid') 
X_transformed = transformer.fit_transform(X) 
X_transformed.shape 
Output 
(1797, 10) 
PCA using randomized SVD 
Principal Component Analysis (PCA) using randomized SVD is used to project data to a 
lower-dimensional space preserving most of the variance by dropping the singular vector 
of 
components 
associated 
with 
lower 
singular 
values. 
Here, 
the 
sklearn.decomposition.PCA
 
module 
with 
the 
optional 
parameter 
svd_solver=’randomized’
is going to be very useful. 
Example 
The below example will use 
sklearn.decomposition.PCA
module with the optional 
parameter svd_solver=’randomized’ to find best 7 Principal components from Pima Indians 
Diabetes dataset

from pandas import read_csv 
from sklearn.decomposition import PCA 
path = r'C:\Users\Leekha\Desktop\pima-indians-diabetes.csv' 
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 
'class'] 
dataframe = read_csv(path, names=names) 
array = dataframe.values 
X = array[:,0:8] 


Scikit-Learn
144 
Y = array[:,8] 
pca = PCA(n_components=7,svd_solver= 'randomized') 
fit = pca.fit(X) 
print(("Explained Variance: %s") % (fit.explained_variance_ratio_)) 
print(fit.components_) 
Output
Explained Variance: [8.88546635e-01 6.15907837e-02 2.57901189e-02 1.30861374e-
02 
7.44093864e-03 3.02614919e-03 5.12444875e-04] 
[[-2.02176587e-03 9.78115765e-02 1.60930503e-02 6.07566861e-02 
9.93110844e-01 1.40108085e-02 5.37167919e-04 -3.56474430e-03] 
[-2.26488861e-02 -9.72210040e-01 -1.41909330e-01 5.78614699e-02 
9.46266913e-02 -4.69729766e-02 -8.16804621e-04 -1.40168181e-01] 
[-2.24649003e-02 1.43428710e-01 -9.22467192e-01 -3.07013055e-01 
2.09773019e-02 -1.32444542e-01 -6.39983017e-04 -1.25454310e-01] 
[-4.90459604e-02 1.19830016e-01 -2.62742788e-01 8.84369380e-01 
-6.55503615e-02 1.92801728e-01 2.69908637e-03 -3.01024330e-01] 
[ 1.51612874e-01 -8.79407680e-02 -2.32165009e-01 2.59973487e-01 
-1.72312241e-04 2.14744823e-02 1.64080684e-03 9.20504903e-01] 
[-5.04730888e-03 5.07391813e-02 7.56365525e-02 2.21363068e-01 
-6.13326472e-03 -9.70776708e-01 -2.02903702e-03 -1.51133239e-02] 
[ 9.86672995e-01 8.83426114e-04 -1.22975947e-03 -3.76444746e-04 
1.42307394e-03 -2.73046214e-03 -6.34402965e-03 -1.62555343e-01]] 
 

Yüklə 1,92 Mb.

Dostları ilə paylaş:
1   ...   116   117   118   119   120   121   122   123   124




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə