প্রধান উপাদান (PC) ট্রান্সফর্ম (কারহুনেন-লোভ ট্রান্সফর্ম নামেও পরিচিত) হল একটি বর্ণালী ঘূর্ণন যা বর্ণালীভাবে সম্পর্কযুক্ত ইমেজ ডেটা নেয় এবং অসংলগ্ন ডেটা আউটপুট করে। পিসি ট্রান্সফর্ম আইজেন-বিশ্লেষণের মাধ্যমে ইনপুট ব্যান্ড পারস্পরিক সম্পর্ক ম্যাট্রিক্সকে তির্যক করে এটি সম্পন্ন করে। আর্থ ইঞ্জিনে এটি করার জন্য, একটি অ্যারে ইমেজে একটি কোভেরিয়েন্স রিডুসার এবং ফলস্বরূপ কোভেরিয়েন্স অ্যারেতে 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()
ব্যবহার করে হ্রাস করতে হবে। এই হ্রাস দ্বারা প্রত্যাবর্তিত অ্যারেটি ইনপুটের প্রতিসম বৈচিত্র্য-কোভারিয়েন্স ম্যাট্রিক্স। কোভেরিয়েন্স ম্যাট্রিক্সের eigenvalues এবং eigenvectors পেতে eigen()
কমান্ডটি ব্যবহার করুন। eigen()
দ্বারা প্রত্যাবর্তিত ম্যাট্রিক্স 1-অক্ষের 0-তম অবস্থানে eigenvalues ধারণ করে। আগের ফাংশনে যেমন দেখানো হয়েছে, eigenvalues এবং eigenvectors আলাদা করতে slice()
ব্যবহার করুন। eigenVectors ম্যাট্রিক্সের 0-অক্ষ বরাবর প্রতিটি উপাদান একটি eigenvector. ট্যাসেলড ক্যাপ (TC) উদাহরণের মতো, ম্যাট্রিক্স দ্বারা arrayImage
ইজেনভেক্টর দ্বারা গুণ করে রূপান্তর সম্পাদন করুন। এই উদাহরণে, প্রতিটি eigenvector গুণনের ফলে একটি PC হয়।