Question: import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.decomposition import PCA from sklearn.preprocessing import StandardScaler # Load the data from

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
# Load the data from the CSV file
file_path = 'homework2/food-consumption.csv'
data = pd.read_csv(file_path, index_col=0)
# Standardize the data
X = StandardScaler().fit_transform(data)
# Perform PCA
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(X)
# Create a DataFrame with the principal components
principalDf = pd.DataFrame(data=principalComponents, columns=['PC1','PC2'])
# Concatenate the country names
finalDf = pd.concat([data.index.to_frame(name='Country'), principalDf], axis=1)
# Plot
plt.figure(figsize=(8,6))
plt.scatter(finalDf['PC1'], finalDf['PC2'])
# Annotate each point with the country name
for i, row in finalDf.iterrows():
plt.annotate(row['Country'],(row['PC1'], row['PC2']), textcoords="offset points", xytext=(0,10), ha='center')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('2 component PCA - Countries')
plt.show()
something is wrong with my code, my output labels all the points with nan

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!