python zip方法用法

參考:https://blog.csdn.net/xlinsist/article/details/51346523
http://www.runoob.com/python/python-func-zip.html

1.zip() 函數用於將可迭代的對象作爲參數,將對象中對應的元素打包成一個個元組,然後返回由這些元組組成的列表

x=[1,2,3]
y=[4,5,6]
zipd = zip(x,y)
list(zipd)
#Out[42]: [(1, 4), (2, 5), (3, 6)]
y1=[4,5,6,7]
zipd1 = zip(x,y1)
list(zipd1)
#Out[46]:[(1, 4), (2, 5), (3, 6)]#匹配短的

2.numpy.bincount,輸出索引出現的次數組成的數組

0索引出現了2次,1索引出現了3次,2索引出現了1,同樣3出現1,4出現1
所以輸出值的數量爲list中的最大值+1,即索引[0,max+1]

list1 = [1,0,1,0,1,2,3,4]
np.bincount(list1)
#Out[54]: array([2, 3, 1, 1, 1])

在python機器學習基礎教程中,對癌症的預測
即使用上述兩個知識點

print('{}'.format({a:b for a ,b in zip(cancer.target_names,np.bincount(cancer.target))}))
#{'benign': 357, 'malignant': 212}
cancer.target_names
#array(['malignant', 'benign'],dtype='<U9')
np.bincount(cancer.target)
#Out[56]: array([212, 357])
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章