I am having a problem with the clustering process in the multidirectional profile analysis method used for clustering with airborne LiDAR data. Since the profile analysis is done in one dimension and the clustering process is done in at least two dimensions, I am getting errors in coding. I would be happy if you help.
def multidirectional_profile_analysis(las_file_path, directions=[0, 45, 90, 135, 180, 225, 270, 315]):
profile_data = []
for direction in directions:
radian = np.radians(direction)
cos_angle = np.cos(radian)
sin_angle = np.sin(radian)
profile_x = x * cos_angle + y * sin_angle
profile_y = x * -sin_angle + y * cos_angle
profile_z = z
profile_data.append((profile_x, profile_y, profile_z))
return profile_data
a = np.asarray(profile_data)
# Cluster the profiles using 3D profile analysis method
# This method takes into account the shape of the tree crown, which can help to improve the accuracy of the segmentation.
cluster_centers = []
for i in range(len(a)):
# Find the minimum and maximum values in the profile
min_val = np.min(a[i])
max_val = np.max(a[i])
# Find the points in the profile that are within the minimum and maximum values
in_range_points = np.asarray(a[i][(a[i] >= min_val) & (a[i] <= max_val)])
# If there are at least 10 points in the range, then cluster them
if len(in_range_points) >= 10:
kmeans = KMeans(n_clusters=8)
cluster_centers.append(kmeans.fit_predict(in_range_points))
ValueError Traceback (most recent call last)
<ipython-input-45-a206a3d15e5f> in <cell line: 4>() 13 if len(in_range_points) >= 10: 14 kmeans = KMeans(n_clusters=8) ---> 15 cluster_centers.append(kmeans.fit_predict(in_range_points)) 3 frames /usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py in fit_predict(self, X, y, sample_weight) 1031 Index of the cluster each sample belongs to. 1032 """ -> 1033 return self.fit(X, sample_weight=sample_weight).labels_ 1034 1035 def predict(self, X, sample_weight=None): /usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py in fit(self, X, y, sample_weight) 1415 self._validate_params() 1416 -> 1417 X = self._validate_data( 1418 X, 1419 accept_sparse="csr", /usr/local/lib/python3.10/dist-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params) 563 raise ValueError("Validation should be done on X, y or both.") 564 elif not no_val_X and no_val_y: --> 565 X = check_array(X, input_name="X", **check_params) 566 out = X 567 elif no_val_X and not no_val_y: /usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py in
check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name) 900 # If input is 1D raise error 901 if array.ndim == 1: --> 902 raise ValueError( 903 "Expected 2D array, got 1D array instead:\narray={}.\n" 904 "Reshape your data either using array.reshape(-1, 1) if "
ValueError: Expected 2D array, got 1D array instead: array=[491883.2864686 491883.2924686 491883.2934686 ... 1463.268 1463.365 1463.454 ]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single
sample.