这是我的代码,功能get_labels_for_msg是从另一个py文件中收集的。
def bayes_clas(mean,alpha,beta,t):
l = []
m = (mean+(alpha*beta))/2
for i in range(t.shape[0]):
if train[1][i] >= m:
l.append(1)
else:
l.append(0)
return np.array(l)
def _int2bytes(i):
hex_string = '%x' % i
n = len(hex_string)
return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
def _text_from_bits(bits, encoding='ascii', errors='surrogatepass'):
n = int(bits, 2)
return _int2bytes(n).decode(encoding, errors)
def get_msg_for_labels(l):
"""
Convert a one-dimensional label array to a message-string.
Args:
l (1D array): Predicted (binary) labels which should be converted to a
message. The first eight elements are assumed to be the
8-bit ASCII representation of the first letter in the
message, the next eight elements are assumed to represent
the next letter, and so on. len(l) must be a multiple of 8
Returns:
msg (string): Decoded message.
"""
st = "".join([str(i) for i in l])
return _text_from_bits(st)
test = pd.read_csv("/Users/leanderkirkeland/MLF/A1/optdigits-1d-test.csv")
bt = bayes_clas(m,9,b,test)
print(get_labels_for_msg(bt))
我收到错误代码AttributeError: 'numpy.ndarray' object has no attribute 'encode'
。
这是我在此页面上遇到的第一个问题,所以不知道该怎么做