📉降維範例¶
In [ ]:
Copied!
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
載入資料集¶
In [ ]:
Copied!
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target
print(X[0])
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target
print(X[0])
使用 PCA¶
In [ ]:
Copied!
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(X)
X = pca.transform(X)
print(X[0])
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(X)
X = pca.transform(X)
print(X[0])
In [ ]:
Copied!
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.scatter(X[:, 0], X[:, 1], c=y)