1. 程式人生 > >python學習-----bug記錄

python學習-----bug記錄

1.TypeError: unsupported operand type(s) for -: 'map' and 'map'

這是機器學習實戰實現K-Means上的程式碼的報的錯。錯誤之處是:fltLine = map(float,curLine)),此後對fltLine進行了加減操作。

原因:map方法來對從文字檔案中讀取的資料進行對映處理,也就是把讀取到的string轉換為float。這一個簡單的型別轉換按照書上實現的方法在Python 2中不會報錯。但是在Python 3中,map方法返回的是一個map物件,因此對於這個錯誤,解決辦法很簡單。

解決方法:fltLine = list(map(float,curLine))

參考:https://blog.csdn.net/zjn941213/article/details/78176073

2.AttributeError: module 'random' has no attribute 'rand'

報錯的具體地方:centroids[:,j] = minJ + rangeJ * random.rand(k,1) 

用書上程式碼實現K-Means的時候,由於書上的程式碼使用的是Python2,因此使用numpy時,全部都沒有按照np.方式,而是直接寫方法,這在Python3中會報錯。只需要在random.rand(k,1)前面加上np.就行了。

解決方法:centroids[:,j] = minJ + rangeJ * np.random.rand(k,1) 

3.ValueError: Masked arrays must be 1-D

具體報錯的地方: for cent, c, marker in zip(range(k),['r','g','b','y'],['^','o','*','s']):
                                 ax.scatter(data[cent][:, 0], data[cent][:, 1], s=80, c=c, marker=marker)
                         ax.scatter(centroids[:, 0], centroids[:, 1], s=1000, c='black', marker='+', alpha=1) #畫出質心點

查詢資料說是,在繪製散點圖時需要先把矩陣轉換成list,進行如下更改即可。

解決辦法:for cent, c, marker in zip(range(k),['r','g','b','y'],['^','o','*','s']):
        ax.scatter(data[cent][:, 0].tolist(), data[cent][:, 1].tolist(), s=80, c=c, marker=marker)
    ax.scatter(centroids[:, 0].tolist(), centroids[:, 1].tolist(), s=1000, c='black', marker='+', alpha=1) #畫出質心點

參考:https://blog.csdn.net/feixiang5527/article/details/78840840
    

4.TypeError: only length-1 arrays can be converted to Python scalars

 這類錯誤一般說的是對陣列操作的方法不能直接對數操作,math庫裡的東西不能直接作用在ndarray上,例如:

import numpy as np
import math
vector = np.arange(1,10)
result = math.log10(vector)

或者

num = float(vector_a*vector_b.T)

解決辦法,將函式向量化(推薦)、迴圈計算或者去掉沒必要的一些操作

result = np.vectorize(math.log10)(evctor)    #向量化
num = vector_a*vector_b.T    #去掉對強制矩陣元素為float

5.ValueError: math domain error

遇到的這個錯誤記錄一下,說的是某些操作不符合數學定義,負數取對數、負數開平方等

6.TypeError: unsupported operand type(s) for /: 'int' and 'dict_values'

出錯的程式碼是:

def tf(word, count):
    return count[word] / sum(count.values())

count是個字典,我的執行環境是Python3 ,查閱資料發現,在Python2中:

w={
    'a':1,
    'b':2,
    'c':3
}
b={
    'aa':4,
    'bb':5,
    'cc':6
}
r=w.values()+b.values()
print(r)

 得到的是一個列表 list

在Python3中這樣則會報錯,解決辦法是將字典強制型別轉換為list:

def tf(word, count):
    return count[word] / sum(list(count.values()))

這樣就可以解決問題了!

7.AttributeError: module 'jieba' has no attribute 'cut'

不要把將執行的檔案命名為jieba.py,也不要在工程檔案下有jieba.py檔案,自己擼自己當然會出錯。

8.UnicodeDecodeError: 'gbk' codec can't decode byte 0xfe in position 5572758: illegal multibyte sequence

對於Python來說這個錯誤真是出現太多次了,每次都要上谷歌查詢。現總結如下,這個問題的原因是因為在原中文文字中出現了對於‘gbk’之外的字元,即可認為是中文的亂碼,是無法通過‘gbk’解碼的。

按照網上說的open(filename,'r','utf-8')或者open(filename,'r','gbk')並不能解決任何問題,想想也對,問題是出在原檔案中,但我們又不能去找到原檔案中編碼不對的地方,看到有的人是在文字中刪掉不一致的地方,但對於長文字這顯然是不適合的。

一種有效的解決方法是:以二進位制的方式讀取檔案,然後將檔案特定編碼進行解碼,忽略或者代替解碼不一致的地方。

with open('xyj.txt','rb') as f:
    text = f.read().decode('gbk','ignore')

如上的操作就不會再報錯了。

9.ValueError: Only call `softmax_cross_entropy_with_logits` with named arguments (labels=..., logits=..., ...)

這是使用TensorFlow時的一個錯誤,

原來這個函式,不能按以前的方式進行呼叫了,只能使用命名引數的方式來呼叫。原來是這樣的:

tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(predict, Y))

因此修改需要成這樣:

tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=Y, labels=predict))