Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

1489982175, 978-1489982179

More Books

Students also viewed these Databases questions