Lab 3
Lab 3
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
print(df.head())
targets = [0, 1, 2]
colors = ['r', 'g', 'b']
for target, color in zip(targets, colors):
indicesToKeep = finalDf['target'] == target
ax.scatter(finalDf.loc[indicesToKeep, 'Principal Component 1'],
finalDf.loc[indicesToKeep, 'Principal Component 2'],
c=color, label=iris.target_names[target], s=50, edgecolors='k')
ax.legend()
ax.grid()
plt.show()
Output
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
['setosa' 'versicolor' 'virginica']
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
Principal Component 1 Principal Component 2 target
0 -2.264703 0.480027 0
1 -2.080961 -0.674134 0
2 -2.364229 -0.341908 0
3 -2.299384 -0.597395 0
4 -2.389842 0.646835 0
Explained variance ratio: [0.72962445 0.22850762]