量化編程技術—itertools尋找最優參數

# -*- coding: utf-8 -*-
# @Date:   2017-08-26
# @Original:

'''
在量化數據處理中,常用itertools來完成數據的各類排列組合以尋找最優參數
'''

import itertools
items = [1, 2, 3]

ab = ['a', 'b']
cd = ['c', 'd']

#1. permutations: 考慮順序組合元素
for item in itertools.permutations(items):
    print(item)

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)

# #2. combinations,不考慮順序,不放回數據
# for item in itertools.combinations(items, 2):
    # print(item)

# (1, 2)
# (1, 3)
# (2, 3)

#3. combinations_with_replacement,不考慮順序,有放回數據
# for item in itertools.combinations_with_replacement(items, 2):
    # print(item)
# (1, 1)
# (1, 2)
# (1, 3)
# (2, 2)
# (2, 3)
# (3, 3)
#4. product()函數,迪卡爾積,在參數組合中尋找最優參數

# 針對ab,cd兩個集合進行排列組合
for item in itertools.product(ab, cd):
    print(item)
    
# ('a', 'c')
# ('a', 'd')
# ('b', 'c')
# ('b', 'd')
相關文章
相關標籤/搜索