Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
26 строки
765 B
Python
26 строки
765 B
Python
import numpy as np
|
|
from matplotlib import pyplot as plt
|
|
import pandas as pd
|
|
from sklearn.datasets import load_iris
|
|
from sklearn.decomposition import PCA
|
|
|
|
|
|
iris = load_iris()
|
|
iris_pd=pd.DataFrame(data=np.c_[iris['data']], columns=iris['feature_names'])
|
|
data = iris_pd[['petal length (cm)', 'petal width (cm)', 'sepal length (cm)']].to_numpy()
|
|
|
|
pca = PCA(n_components=2)
|
|
pca.fit(data)
|
|
principalComponents = pca.fit_transform(data)
|
|
|
|
# вывод объясненной дисперсии
|
|
print(pca.explained_variance_ratio_)
|
|
|
|
pc_df = pd.DataFrame(data=principalComponents, columns=['PC1', 'PC2'])
|
|
|
|
|
|
plt.scatter(pc_df['PC1'], pc_df['PC2'])
|
|
plt.xlabel('PC1')
|
|
plt.ylabel('PC2')
|
|
plt.title("Projection on First Two Principal Components")
|
|
plt.show() |