0
我一直在經歷着一個在線教程空調指數
from sklearn.decomposition import *
from sklearn import datasets
import matplotlib.pyplot as plt
import time
digits=datasets.load_digits()
randomized_pca = PCA(n_components=2,svd_solver='randomized')
# a numpy array with shape= (1800,2)
reduced_data_rpca = randomized_pca.fit_transform(digits.data)
# make a scatter plot
colors = ['black', 'blue', 'purple', 'yellow', 'pink', 'red', 'lime', 'cyan',
'orange', 'gray']
start=time.time()
# Time Taken for this loop = 9.5 seconds
# for i in range(len(reduced_data_rpca)):
# x = reduced_data_rpca[i][0]
# y = reduced_data_rpca[i][1]
# plt.scatter(x,y,c=colors[digits.target[i]])
# Alternative way TimeTaken = 0.2 sec
# plots all the points (x,y) with color[i] in ith iteration
for i in range(len(colors)):
"""assigns all the elements (accordingly to x and y) whose label(0-9) equals the variable i (am I
correct ? does this mean it iterates the whole again to check for the
equality?) """
x = reduced_data_rpca[:, 0][digits.target == i]
y = reduced_data_rpca[:, 1][digits.target == i]
plt.scatter(x, y, c=colors[i])
end=time.time()
print("Time taken",end-start," Secs")
我的問題是,儘管這兩個註釋和非註釋循環執行相同的操作我不明白的第二個循環是如何工作的,以及爲什麼它比另一個表現更好。
實際上'digits.target'(這些數字在'reduced_data_rpca'類標籤)包含1800個值(介於0-9)和'reduced_data_rpca'包含2列,我是什麼思考是,它遍歷這個數組,並驗證相應的'digits.target'值與變量'i'是否相等。我錯了嗎? – Yashwanth
你是對的我想!一旦我嘗試一下就有意義了。 – plan