Python Algorithm Implementation

Source: Internet
Author: User

It is not difficult for an experienced Python programmer to implement a Python algorithm. We have introduced this operation method to you today. You can learn this as a reference object and sum up experience and skills in practice to master this technology.

Python algorithm operation code example:

 
 
  1. # -*- coding: UTF8 -*-  
  2. import sys  
  3. import copy  
  4. def init_pass(T):  
  5. C = {}  
  6. for t in T:  
  7. for i in t:  
  8. if i in C.keys():  
  9. C[i] += 1  
  10. else:  
  11. C[i] = 1  
  12. return C  
  13. def candidate_gen(F):  
  14. C = []  
  15. k = len(F[0]) + 1  
  16. for f1 in F:  
  17. for f2 in F:  
  18. if f1[k-2] < f2[k-2]:  
  19. c = copy.copy(f1)  
  20. c.append(f2[k-2])  
  21. flag = True 
  22. for i in range(0,k-1):  
  23. s = copy.copy(c)  
  24. s.pop(i)  
  25. if s not in F:  
  26. flag = False 
  27. break  
  28. if flag and c not in C:  
  29. C.append(c)  
  30. return C  
  31. def compare_list(A,B):  
  32. if len(A) <= len(B):  
  33. for a in A:  
  34. if a not in B:  
  35. return False  
  36. else:  
  37. for b in B:  
  38. if b not in A:  
  39. return False  
  40. return True  
  41. def apriori(T, minsup):  
  42. C = []  
  43. init = init_pass(T)  
  44. keys = init.keys()  
  45. keys.sort()  
  46. C.append(keys)  
  47. n = len(T)  
  48. F = [[]]  
  49. for f in C[0]:  
  50. if init[f]*1.0/n >= minsup:  
  51. F[0].append([f])  
  52. k = 1 
  53. while F[k-1] != []:  
  54. C.append(candidate_gen(F[k-1]))  
  55. F.append([])  
  56. for c in C[k]:  
  57. count = 0;  
  58. for t in T:  
  59. if compare_list(c,t):  
  60. count += 1  
  61. if count*1.0/n >= minsup:  
  62. F[k].append(c)  
  63. k += 1  
  64. U = []  
  65. for f in F:  
  66. for x in f:  
  67. U.append(x)  
  68. return U  
  69. T = [['A','B','C','D'],['B','C','E'],['A','B','C','E'],
    ['B','D','E'],['A','B','C','D']]  
  70. F = apriori(T, 0.9)  
  71. print F 

This section describes how to compile Python algorithm code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.