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()