如果值为0.1或0.2,则以下代码有效。如果值为0.3475777755,则无效。
def sentiment_score(value):
if value in np.arange(-5,-2,0.1,dtype=float):
return 'do_verynegative'
elif value in np.arange(-2,0,0.1,dtype=float):
return 'do_negative'
elif value in np.arange(0,1,0.1,dtype=float):
return 'do_neu'
elif value in np.arange(0,2,0.1,dtype=float):
return 'do_postive'
elif value in np.arange(2,5,0.1,dtype=float):
return 'do_verypositive'
else:
return 'error'
如果值超过1个十进制数,请说明怎么办?
答案 0 :(得分:1)
您想将单个数字翻译成标签,对吧?这段代码可以实现浮点数和整数。
def sentiment_score(value):
if value <= -2:
return 'do_verynegative'
elif value <= 0:
return 'do_negative'
elif value <= 1:
return 'do_neutral'
elif value <= 2:
return 'do_positive'
else:
return 'do_verypositive'