Workaround for index2 is out of bounds for axis1 with size2

Asked 2 years ago, Updated 2 years ago, 67 views

It's for python beginners.
If you run the code that reads csv containing 2011 image data and divides the results into 1000 first half and 1011 second half of PCA and outputs them to the graph,

IndexError: index2 is out of bounds for axis1 with size2

I was scolded.If you know how to deal with the error, please let me know.

import numpy as np
from sklearn.cluster importKEANS
from sklearn.decomposition import PCA
import matplotlib.pyplot asplt
import pandas aspd

users=np.loadtxt('/home/srl/mimamori1/b1/bb1.csv', delimiter=",")
model=KMeans(n_clusters=2).fit(users) 


pca=PCA(n_components=2)
users_r=pca.fit_transform(users)
plt.figure()
ccode=[1]*1000+[2]*1011

plt.scatter(users_r[:,1],users_r[:,2],c=ccode)
plt.xlabel('pc1')
plt.ylabel('pc2')


print('contribution rate for each dimension: {0}'.format(pca.expanded_variance_ratio_))
print('Cumulative contribution: {0}'.format(sum(pca.expanded_variance_ratio_)))
plt.show()

python matplotlib pca

2022-09-29 22:34

1 Answers

Python's index is 0 (unlike matlab), so

users_r[:,1],users_r[:,2]

users_r[:,0],users_r[:,1]

Please fix it to .


2022-09-29 22:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.