प्राइमरी
कॉम्पोनेंट (पीसी) ट्रांसफ़ॉर्म (जिसे कारुहन-लोवे ट्रांसफ़ॉर्म भी कहा जाता है) एक ऐसा स्पेट्रल रोटेशन है जो स्पेक्ट्रल रूप से जुड़ी इमेज का डेटा लेता है और बिना किसी संबंध वाले डेटा को आउटपुट करता है. पीसी ट्रांसफ़ॉर्म, आइगन-ऐनालिसिस की मदद से इनपुट बैंड के कोरिलेशन मैट्रिक को डायगनल करके ऐसा करता है. Earth Engine में ऐसा करने के लिए, किसी ऐरे इमेज पर कोवैरिएंस रिड्यूसर का इस्तेमाल करें और नतीजे के तौर पर मिले कोवैरिएंस ऐरे पर eigen()
कमांड का इस्तेमाल करें.
इस काम के लिए, नीचे दिया गया फ़ंक्शन इस्तेमाल करें. इसका उदाहरण, ऐप्लिकेशन में कोड एडिटर स्क्रिप्ट और Colab नोटबुक के तौर पर उपलब्ध है.
var getPrincipalComponents = function(centered, scale, region) { // Collapse the bands of the image into a 1D array per pixel. var arrays = centered.toArray(); // Compute the covariance of the bands within the region. var covar = arrays.reduceRegion({ reducer: ee.Reducer.centeredCovariance(), geometry: region, scale: scale, maxPixels: 1e9 }); // Get the 'array' covariance result and cast to an array. // This represents the band-to-band covariance within the region. var covarArray = ee.Array(covar.get('array')); // Perform an eigen analysis and slice apart the values and vectors. var eigens = covarArray.eigen(); // This is a P-length vector of Eigenvalues. var eigenValues = eigens.slice(1, 0, 1); // This is a PxP matrix with eigenvectors in rows. var eigenVectors = eigens.slice(1, 1); // Convert the array image to 2D arrays for matrix computations. var arrayImage = arrays.toArray(1); // Left multiply the image array by the matrix of eigenvectors. var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage); // Turn the square roots of the Eigenvalues into a P-band image. var sdImage = ee.Image(eigenValues.sqrt()) .arrayProject([0]).arrayFlatten([getNewBandNames('sd')]); // Turn the PCs into a P-band image, normalized by SD. return principalComponents // Throw out an an unneeded dimension, [[]] -> []. .arrayProject([0]) // Make the one band array image a multi-band image, [] -> image. .arrayFlatten([getNewBandNames('pc')]) // Normalize the PCs by their SDs. .divide(sdImage); };
import ee import geemap.core as geemap
def get_principal_components(centered, scale, region): # Collapse bands into 1D array arrays = centered.toArray() # Compute the covariance of the bands within the region. covar = arrays.reduceRegion( reducer=ee.Reducer.centeredCovariance(), geometry=region, scale=scale, maxPixels=1e9, ) # Get the 'array' covariance result and cast to an array. # This represents the band-to-band covariance within the region. covar_array = ee.Array(covar.get('array')) # Perform an eigen analysis and slice apart the values and vectors. eigens = covar_array.eigen() # This is a P-length vector of Eigenvalues. eigen_values = eigens.slice(1, 0, 1) # This is a PxP matrix with eigenvectors in rows. eigen_vectors = eigens.slice(1, 1) # Convert the array image to 2D arrays for matrix computations. array_image = arrays.toArray(1) # Left multiply the image array by the matrix of eigenvectors. principal_components = ee.Image(eigen_vectors).matrixMultiply(array_image) # Turn the square roots of the Eigenvalues into a P-band image. sd_image = ( ee.Image(eigen_values.sqrt()) .arrayProject([0]) .arrayFlatten([get_new_band_names('sd')]) ) # Turn the PCs into a P-band image, normalized by SD. return ( # Throw out an an unneeded dimension, [[]] -> []. principal_components.arrayProject([0]) # Make the one band array image a multi-band image, [] -> image. .arrayFlatten([get_new_band_names('pc')]) # Normalize the PCs by their SDs. .divide(sd_image) )
फ़ंक्शन में इनपुट के तौर पर, औसत शून्य वाली इमेज, स्केल, और वह क्षेत्र दिया जाता है जिस पर विश्लेषण करना है. ध्यान दें कि इनपुट इमेज को पहले 1-D
अर्रे इमेज में बदलना होगा. इसके बाद, ee.Reducer.centeredCovariance()
का इस्तेमाल करके इमेज को छोटा करना होगा. इस प्रोसेस से मिलने वाला ऐरे, इनपुट का सिमेट्रिक वैरियंस-कोवैरिएंस मैट्रिक्स होता है.
कोवैरिएंस मैट्रिक के एलिगन वैल्यू और एलिगन वैक्टर पाने के लिए, eigen()
कमांड का इस्तेमाल करें. eigen()
से मिली मैट्रिक्स में, एलिगन वैल्यू एक ऐक्सिस की 0वीं पोज़िशन में होती हैं. जैसा कि पिछले फ़ंक्शन में दिखाया गया है, एलिगन वैल्यू और एलिगन वैल्यू के वेक्टर को अलग करने के लिए, slice()
का इस्तेमाल करें. eigenVectors मैट्रिक्स के 0-ऐक्सिस के साथ मौजूद हर एलिमेंट, एक एआईजीन वैक्टर होता है. टैसल कैप (टीसी) के उदाहरण की तरह ही, arrayImage
को एजेंक्टोर के साथ मैट्रिक्स गुणा करके ट्रांसफ़ॉर्मेशन करें.
इस उदाहरण में, हर एआईजीनवेक्टर के गुणा करने पर एक पीसी मिलता है.