cat1234
cat1234

Reputation: 49

How to represent items in 1s & 0s for generating an initial population in a genetic algorithm in Python?

I want to find frequent itemsets from items using genetic algorithms.

My sample transaction database is dataset = [['a','b'],['c'],['d'],['b'],['e','c','b'],['a','c']] My items are [a,b,c,d,e]

Firstly, I want to represent each item in 1s and 0s. For e.g. 'a' as {1,0,0,0,0} and d as {0,0,0,1,0}.

How can I do this in Python??

When I have to crossover the items the 1s and 0s will have to transform so this form will have to be mutable.

Can someone please help me or point me to a GA implementation which is kind of like this situation? A pointer would be of great help, thanks!

Upvotes: 1

Views: 68

Answers (1)

dchoruzy
dchoruzy

Reputation: 349

items = ['a', 'b', 'c', 'd', 'e']
zeros = [0 for item in items]
for index, item in enumerate(items):
  result = zeros.copy()
  result[index] = 1
  print(result)

Input:

['a', 'b', 'c', 'd', 'e']

Output:

[1, 0, 0, 0, 0]
[0, 1, 0, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 0, 1, 0]
[0, 0, 0, 0, 1]

Upvotes: 1

Related Questions