当前位置:   article > 正文

Python不掉包初探自然语言处理One-Hot编码与解码_one hot编码fasta biopython

one hot编码fasta biopython
背景导入:
在⾃然语⾔处理中,算法⽆法直接处理字符⽂本。通常将每个词表示为⼀个 One-hot 向量,句⼦便可以表示为⼀个矩阵,然后就可以对⽂本进⾏计算。
机器学习数据预处理1:独热编码(One-Hot)及其代码_梦Dancing的博客-CSDN博客_onehot编码1. 为什么使用 one-hot 编码?问题:在机器学习算法中,我们经常会遇到分类特征,例如:人的性别有男女,祖国有中国,美国,法国等。 这些特征值并不是连续的,而是离散的,无序的。目的: 如果要作为机器学习算法的输入,通常我们需要对其进行特征数字化。什么是特征数字化呢?例如: 性别特征:["男","女"] ...https://blog.csdn.net/qq_15192373/article/details/89552498?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164690792716780271965340%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164690792716780271965340&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-89552498.article_score_rank&utm_term=onehot&spm=1018.2226.3001.4187
实现步骤:
⽂本预处理:
        全部转换为⼩写
        去除特殊符号
        连续多个空⽩符号处理为 1
        标点符号与词汇分开
        缩写的处理
              it's 处理为 it 's
              i've 处理为 i 've
              don't 处理为 do n't
              i'll 处理为 i 'll
              i'd 处理为 i 'd
构建词典:根据⽂本数据统计出⼀个词典,为每个词编号;
编码:根据词典,将⽂本切分为句⼦,每个句⼦利⽤ One-hot 表示为⼀个矩阵;
解码:根据词典,将 One-hot 序列转换为⽂本句⼦。
实现代码:
  1. txt1='''Hi! This is Wang.
  2. Hello! It's Sun speaking.
  3. Do you have free time this evening?
  4. Uh... Let's me see. What's the matter?
  5. I hope you can see a new movie Super Hero with me. I've been wanting to see it for a long time.
  6. Sorry, I didn't catch that. Could you say the name again?
  7. Super Hero.
  8. Oh! Super Hero. I'm very interested in it. I haven't seen it. I can go with you. When the movie start?
  9. It begin at seven o'clock. Let's gather at the cinema at ten to seven.
  10. Ok, I'll arrive there on time. Goodbye!
  11. Bye.'''
  12. txt2="Hi! Wang. I've arrive the cinema. Where are you?"
  13. #自定义文本预处理函数
  14. def txtpre():
  15. global txt#txt为全局变量
  16. txt=txt.lower()
  17. #变特殊字符为空格
  18. for ch in "!,.?":
  19. txt=txt.replace(ch," ")
  20. #对于缩写,通过空格按要求分隔
  21. txt=txt.replace("it's","it 's")
  22. txt=txt.replace("i've","i 've")
  23. txt=txt.replace("don't","do n't")
  24. txt=txt.replace("i'll","i 'll")
  25. txt=txt.replace("i'd","i 'd")
  26. #将txt1赋给txt,作预处理
  27. txt=txt1
  28. txtpre()
  29. list1=list(txt.split())#将字符串按空格分隔转换成列表
  30. dict1=dict.fromkeys(list1,0)#创建一个新字典,默认键对应的值为0
  31. #记录词频到字典的值当中,避免重复词(键)
  32. for x in list1:
  33. dict1[x]+=1
  34. #将词(键)组合成列表,并添加"UNKNOWN"为列表的最后一个元素
  35. key_lst=[]
  36. for k in range(len(dict1)):
  37. key_lst=list(dict1.keys())
  38. key_lst.append("UNKNOWN")
  39. char_to_int = dict((c, i) for i, c in enumerate(key_lst))#词转化为编号需要用到的数据类型
  40. int_to_char = dict((i, c) for i, c in enumerate(key_lst))#编号转化为词需要用到的数据类型
  41. print(int_to_char)#以“编号 词”的方式输出词典
  42. #将txt2赋给txt,作预处理
  43. txt=txt2
  44. txtpre()
  45. list2=list(txt.split())
  46. integer_encoded = []#编号组合成的整数矩阵
  47. #依次检索表2元素,如果在key_lst中,加编号到整数矩阵中,否则加"UNKONWN"的编号到矩阵中
  48. for char in list2:
  49. if (char in key_lst):
  50. integer_encoded.append(char_to_int[char])
  51. else:
  52. integer_encoded.append(key_lst.index("UNKNOWN"))
  53. #构成onehot形式并输出,编码的过程
  54. onehot_encoded =[]
  55. for value in integer_encoded:
  56. letter = [0 for _ in range(len(key_lst))]
  57. letter[value] = 1
  58. onehot_encoded.append(letter)
  59. print(onehot_encoded)
  60. #解码,并输出由词组合成的列表
  61. list_decode=[]
  62. for i in range(len(onehot_encoded)):
  63. decode = int_to_char[integer_encoded[onehot_encoded.index(onehot_encoded[i])]]#逐步往回推
  64. list_decode.append(decode)
  65. print(list_decode)

运行结果:

  1. {0: 'hi', 1: 'this', 2: 'is', 3: 'wang', 4: 'hello', 5: 'it', 6: "'s", 7: 'sun', 8: 'speaking', 9: 'do', 10: 'you', 11: 'have', 12: 'free', 13: 'time', 14: 'evening', 15: 'uh', 16: "let's", 17: 'me', 18: 'see', 19: "what's", 20: 'the', 21: 'matter', 22: 'i', 23: 'hope', 24: 'can', 25: 'a', 26: 'new', 27: 'movie', 28: 'super', 29: 'hero', 30: 'with', 31: "'ve", 32: 'been', 33: 'wanting', 34: 'to', 35: 'for', 36: 'long', 37: 'sorry', 38: "didn't", 39: 'catch', 40: 'that', 41: 'could', 42: 'say', 43: 'name', 44: 'again', 45: 'oh', 46: "i'm", 47: 'very', 48: 'interested', 49: 'in', 50: "haven't", 51: 'seen', 52: 'go', 53: 'when', 54: 'start', 55: 'begin', 56: 'at', 57: 'seven', 58: "o'clock", 59: 'gather', 60: 'cinema', 61: 'ten', 62: 'ok', 63: "'ll", 64: 'arrive', 65: 'there', 66: 'on', 67: 'goodbye', 68: 'bye', 69: 'UNKNOWN'}
  2. [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
  3. ['hi', 'wang', 'i', "'ve", 'arrive', 'the', 'cinema', 'UNKNOWN', 'UNKNOWN', 'you']

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/617680
推荐阅读
相关标签
  

闽ICP备14008679号