Scikit-Learn


Unsupervised Learning Example



Yüklə 1,92 Mb.
Pdf görüntüsü
səhifə24/124
tarix23.02.2023
ölçüsü1,92 Mb.
#101394
1   ...   20   21   22   23   24   25   26   27   ...   124
sklearn

Unsupervised Learning Example 
Here, as an example of this process we are taking common case of reducing the 
dimensionality of the Iris dataset so that we can visualize it more easily. For this example, 
we are going to use principal component analysis (PCA), a fast-linear dimensionality 
reduction technique. 
Like the above given example, we can load and plot the random data from iris dataset. 
After that we can follow the steps as below: 
Choose a class of model
from sklearn.decomposition import PCA 
Choose model hyperparameters 
model = PCA(n_components=2) 
model 
Output 
PCA(copy=True, iterated_power='auto', n_components=2, random_state=None, 
svd_solver='auto', tol=0.0, whiten=False) 
Model fitting 
model.fit(X_iris) 
Output 
PCA(copy=True, iterated_power='auto', n_components=2, random_state=None, 
svd_solver='auto', tol=0.0, whiten=False) 


Scikit-Learn
24 
Transform the data to two-dimensional 
X_2D = model.transform(X_iris) 
Now, we can plot the result as follows: 
iris['PCA1'] = X_2D[:, 0] 
iris['PCA2'] = X_2D[:, 1] 
sns.lmplot("PCA1", "PCA2", hue='species', data=iris, fit_reg=False); 
Output 
Complete working/executable example 
%matplotlib inline 
import matplotlib.pyplot as plt 
import numpy as np 
import seaborn as sns 
iris = sns.load_dataset('iris') 


Scikit-Learn
25 
X_iris = iris.drop('species', axis = 1) 
X_iris.shape 
y_iris = iris['species'] 
y_iris.shape 
rng = np.random.RandomState(35) 
x = 10*rng.rand(40) 
y = 2*x-1+rng.randn(40) 
plt.scatter(x,y); 
from sklearn.decomposition import PCA 
model = PCA(n_components=2) 
model 
model.fit(X_iris)
X_2D = model.transform(X_iris) 
iris['PCA1'] = X_2D[:, 0] 
iris['PCA2'] = X_2D[:, 1] 
sns.lmplot("PCA1", "PCA2", hue='species', data=iris, fit_reg=False); 
 


Scikit-Learn
26 
Scikit-learn’s objects share a uniform basic API that consists of the following three 
complementary interfaces: 


Yüklə 1,92 Mb.

Dostları ilə paylaş:
1   ...   20   21   22   23   24   25   26   27   ...   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ə