0

I am trying a flatten a Numpy array but flatten, ravel, and hstack are not doing it. I need to be able to create a Pandas series from the array, but I can't seem to get it flat for Pandas to accept:

print(type(centroids))
# <class 'numpy.matrix'>

print(centroids.shape)
# (5, 9328)

centroid = centroids[i]
centroid = np.hstack(centroid)

print(centroid)
# [[ 0.98487911  0.7483803  11.80978353 ...  0.97687837  0.21988038
   3.33842549]]  <-- still enclosed by two brackets

print(centroid.shape)
# (1, 9328)

centroid = pd.Series(centroid, name='value') <-- throws exception: Exception: Data must be 1-dimensional

Kevin Welch
  • 1,488
  • 1
  • 9
  • 18
Clayton C.
  • 863
  • 1
  • 8
  • 17

1 Answers1

1

The problem is that centroid is a np.matrix, not an array. Your code will work if you simply change the last line to:

centroid = pd.Series(np.array(centroid).ravel(), name='value')
Mercury
  • 3,417
  • 1
  • 10
  • 35