精度と再現率を達成するために、マルチラベル

#!は/ usr / binに/のenvのpython 
# - * -コーディング:UTF-8 - * -
""」
@file:recall.py
@Author:郭凯锋
@time:2020年1月12日夜05時57
@Software:PyCharm
@ Gitの-Hubは:daguonice
@博客园:https://www.cnblogs.com/daguonice/

""」
PDのように輸入パンダ
NPとして輸入numpyの
sklearn.metricsインポートrecall_scoreから


:デフright_or_wrong(ypred_ele、ytrue_ele)
であればypred_eleでytrue_ele :
trueを返し
、他:
リターン偽


デフget_single_score(y_true、y_pred):
TP = np.sum(np.logical_and(np.equal(y_true、1)、y_pred(np.equal、1)))
FP = np.sum( np.logical_and(np.equal(y_true、0)、np.equal(y_pred、1)))
TN = np.sum(np.logical_and(np.equal(y_true、1)、np.equal(y_pred、0)))
FN = np.sum(np.logical_and(np.equal(y_true、0)、NP。等しい(y_pred、0)))
リコール= [TP(TP + FN)]
[(y_pred)でlen(TP + TN)]精度=
戻り精度、リコール


DEF get_multi_score(y_true、y_pred):
list_score = []
のために:[ '相互宝'、 '健康险'、 '保险线'、 '花呗']でELE
y_true_temp = np.zeros(LEN(y_true))
y_pred_temp = np.zeros()y_pred(LEN)
の範囲内IDX用(LEN(y_true)):
y_pred場合[IDX] == ELE:
y_pred_temp [IDX] = 1の
場合right_or_wrong(ELE、y_true [IDX]):
y_true_temp [IDX] = 1。
精度、再現率= get_single_score(y_true_temp、 y_pred_temp)#は、 パーセンタイルなる
list_score.append([精度、リコール])
#list_score戻り、三次元のリストを返し、互いに健康保険のポー、分数精度と再現率、保険ラインであります


fraction2decimal DEF(結果):
RES = []
:ELEに対する結果で
list_ele = []
ELEにおけるラボ用:
list_lab = []
ラボにおけるスコアのために:
[1]スコアIF == 0:
list_lab.append(0.0)
他:
list_lab.append(ラウンド(スコア[0] /スコア、2)[1。])
list_ele.append(list_lab)
res.append(list_ele)
リターンRES


DEF FUNC(y_true、y_pred)。
#y_true是一个一维列表、y_pred是一个二维列表
lenの場合(y_true)= lenは(y_pred):!
昇給の例外( "二つの入力の長さが矛盾している")
の長さ= LEN(y_true)
list_score = get_multi_score(y_true、 y_pred)
結果= []
temp_list = list_score
範囲内IDX用(長さ):
list_score = temp_list
lenの場合(y_true [IDX])== 1及びy_pred [IDX]ないy_trueにおける[IDX]:
list_score [1] [1] [1] + = 1
list_score [0] [0] [1] + = 1
result.append(list_score)
のelif LEN(y_true [IDX])== 2とy_pred [IDX]でy_true [IDX]:
list_score [0 ] [0] [0] + = 1
list_score [0] [0] [1] + = 1
list_score [0] [1] [0] + = 1
list_score [0] [1] [1] + = 1
result.append(list_score)
のelif LEN(y_true [IDX])== 2とy_pred [IDX] NOT IN y_true [IDX]
。list_score [1] [1] [1] + = 1
。list_score [2] [1] [1] + = 1
。list_score [0] [0] [1] + = 1
result.append(list_score)
他:
result.append(list_score)
結果= fraction2decimal(結果)
リターン結果


IF __name__ == '__main__':
保険の連想リスト= [ 'ポーをしている'、 'Poがある'、 'あるポー'、 '健康保険を'「行''詠唱花']
blist = ['健康保険'['健康保険''相互ポー']、['健康保険''安全ライン']、['相互ポー']、['保険ライン「]、[ '花唱える']
RES = FUNC(blistため、連想リスト)
#RES = pd.DataFrame(RES、列 = [ ' 相互の宝'、 '健康保険'、 '安全ライン'、 '花は詠唱'])
のpd.set_option( 'display.max_columns'、なし)
#1 pd.set_option ( 'display.max_rows'、なし)
#1 pd.set_option( 'display.width'、100000)
#1 pd.set_option( 'display.unicode.east_asian_width'、TRUE)
印刷()
印刷(RES)を

おすすめ

転載: www.cnblogs.com/daguonice/p/12185479.html