Python報錯:TypeError: data type not understood


K-Means聚類算法
def randCent(dataSet, k):
m, n = dataSet.shape # numpy中的shape函數的返回一個矩陣的規模,即是幾行幾列
centrodids = np.zeros(k, n)
for i in range(k):
index = int(np.random.uniform(0, m)) #
centrodids[i, :] = dataSet[index, :]
return centrodids
報錯TypeError: data type not understood
錯誤在第三行centrodids = np.zeros(k, n)
原來numpy.zeros的用法用錯了
numpy.zeros(shape,dtype = float,order ='C' )
返回給定形狀和類型的新數組,並用零填充。
shape:整數或者整數元組例如:(2,1)
dtype:數據類型,可選
order:{‘C’,‘F’}可選,默認C
所以應該吧第三行改成centrodids = np.zeros((k, n))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM