python AttributeError:'str'对象没有作为熊猫数据框的对象的属性''

意通

我想将我创建的包含我需要的值的数据帧对象的一部分分配给新对象。但是,当我尝试运行我的 python 文件时出现错误,它表示字符串对象没有我创建的包含这些值的对象的属性。不知道出了什么问题。

AttributeError: 'str' object has no attribute 'vowel_map'

training.txt 文件是:

COED:K OW1 EH2 D
PURVIEW:P ER1 V Y UW2
HEHIR:HH EH1 HH IH0 R
MUSCLING:M AH1 S AH0 L IH0 NG
NONPOISONOUS:N AA0 N P OY1 Z AH0 N AH0 S
LAVECCHIA:L AA0 V EH1 K IY0 AH0
BUCKLED:B AH1 K AH0 L D
EATEN:IY1 T AH0 N
SCIMED:S AY1 M EH2 D
MORTIS:M AO1 R T IH0 S
CONSERVATOR:K AH0 N S ER1 V AH0 T ER0

我正在运行的python文件是:

import pandas as pd
import string

vowels = ('AA','AE','AH','AO','AW','AY','EH','ER','EY','IH','IY','OW','OY','UH','UW')

def remove_stress(string):
    if type(string) in [list, tuple]:
        string = ' '.join(string)
    return ''.join([i for i in string if not i.isdigit()]).split()

def phoneme_map(phon_list, phoneme_list):
    return [1 if phoneme in phoneme_list else 0 for phoneme in phon_list]

def get_words(file_path):
    words = pd.read_csv(file_path, sep=':', names = ['word', 'string_of_phon'])
    words['phon_list'] = words.string_of_phon.apply(str.split)
    words['stressless_phon_list'] = words.string_of_phon.apply(remove_stress)
    words['vowel_map'] = words.stressless_phon_list.apply(phoneme_map, args = (vowels,))

    return words

if __name__ == '__main__':
    data_loc = 'training.txt'
    words = get_words(data_loc)

    word_vowels = [word.vowel_map for word in words]
最大U

如果要对元音进行单热编码:

from sklearn.feature_extraction.text import CountVectorizer

vowels = ['AA','AE','AH','AO','AW','AY','EH','ER','EY','IH','IY','OW','OY','UH','UW']

df = pd.read_csv(file_path, sep=':', names = ['word', 'string_of_phon'])
vect = CountVectorizer(vocabulary=[v.lower() for v in vowels])    
X = vect.fit_transform(df['string_of_phon'].str.replace(r'\d+', ''))    
r = pd.DataFrame(X.A, columns=vect.get_feature_names(), index=df.index)

产量

In [138]: r
Out[138]:
    ao  er  uw  eh  oy  ey  ow  ih  uh  ah  ay  iy  ae  aw  aa
0    0   0   0   1   0   0   1   0   0   0   0   0   0   0   0
1    0   1   1   0   0   0   0   0   0   0   0   0   0   0   0
2    0   0   0   1   0   0   0   1   0   0   0   0   0   0   0
3    0   0   0   0   0   0   0   1   0   2   0   0   0   0   0
4    0   0   0   0   1   0   0   0   0   2   0   0   0   0   1
5    0   0   0   1   0   0   0   0   0   1   0   1   0   0   1
6    0   0   0   0   0   0   0   0   0   2   0   0   0   0   0
7    0   0   0   0   0   0   0   0   0   1   0   1   0   0   0
8    0   0   0   1   0   0   0   0   0   0   1   0   0   0   0
9    1   0   0   0   0   0   0   1   0   0   0   0   0   0   0
10   0   2   0   0   0   0   0   0   0   2   0   0   0   0   0

您可以将其与原始 DF 一起使用:

In [139]: df.join(r)
Out[139]:
            word               string_of_phon  ao  er  uw  eh  oy  ey  ow  ih  uh  ah  ay  iy  ae  aw  aa
0           COED                  K OW1 EH2 D   0   0   0   1   0   0   1   0   0   0   0   0   0   0   0
1        PURVIEW                P ER1 V Y UW2   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0
2          HEHIR              HH EH1 HH IH0 R   0   0   0   1   0   0   0   1   0   0   0   0   0   0   0
3       MUSCLING         M AH1 S AH0 L IH0 NG   0   0   0   0   0   0   0   1   0   2   0   0   0   0   0
4   NONPOISONOUS  N AA0 N P OY1 Z AH0 N AH0 S   0   0   0   0   1   0   0   0   0   2   0   0   0   0   1
5      LAVECCHIA        L AA0 V EH1 K IY0 AH0   0   0   0   1   0   0   0   0   0   1   0   1   0   0   1
6        BUCKLED              B AH1 K AH0 L D   0   0   0   0   0   0   0   0   0   2   0   0   0   0   0
7          EATEN                  IY1 T AH0 N   0   0   0   0   0   0   0   0   0   1   0   1   0   0   0
8         SCIMED                S AY1 M EH2 D   0   0   0   1   0   0   0   0   0   0   1   0   0   0   0
9         MORTIS              M AO1 R T IH0 S   1   0   0   0   0   0   0   1   0   0   0   0   0   0   0
10   CONSERVATOR    K AH0 N S ER1 V AH0 T ER0   0   2   0   0   0   0   0   0   0   2   0   0   0   0   0

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python 3.4:str:AttributeError:'str'对象没有属性'decode

来自分类Dev

Python 3.4:str:AttributeError:'str'对象没有属性'decode

来自分类Dev

Python-AttributeError:“ str”对象没有属性“ isDigit”

来自分类Dev

Python-AttributeError:“ str”对象没有属性“ append”

来自分类Dev

Python EXscript-AttributeError:“ str”对象没有属性“ connect”

来自分类Dev

Python AttributeError:“ str”对象没有属性“ DataFrame”

来自分类Dev

python error AttributeError:'str'对象没有属性'setdefault'

来自分类Dev

Python AttributeError:“ str”对象没有属性“ items”

来自分类Dev

Python AttributeError:“ str”对象没有属性“ append”

来自分类Dev

Python-AttributeError:“ str”对象没有属性“ append”

来自分类Dev

Python-AttributeError:“ str”对象没有属性“ isDigit”

来自分类Dev

AttributeError:“ str”对象没有属性“ search_nodes”-Python

来自分类Dev

AttributeError:“ str”对象没有属性“ location”-Python

来自分类Dev

Python:AttributeError:'str'对象没有属性'readlines'

来自分类Dev

AttributeError: 'str' 对象没有属性 'read' Python..

来自分类Dev

Python - AttributeError: 'str' 对象没有属性 'append' - 数学游戏

来自分类Dev

Python AttributeError:“str”对象没有“decode”属性

来自分类Dev

熊猫:AttributeError:'str'对象没有属性'iloc'

来自分类Dev

Python + 数据框:AttributeError:'float' 对象没有属性 'replace'

来自分类Dev

包含 Pandas 数据框列中的函数(AttributeError: 'str' 对象没有属性 'str'

来自分类Dev

AttributeError:“ str”对象没有属性“ toLowerCase”

来自分类Dev

AttributeError:'str'对象没有属性'get'

来自分类Dev

AttributeError:“ str”对象没有属性“ items”

来自分类Dev

AttributeError:'str'对象没有属性'sleep'

来自分类Dev

AttributeError:“ str”对象没有属性“ maketrans”

来自分类Dev

AttributeError:'str'对象没有属性(功能)

来自分类Dev

attributeError:'str'对象没有属性'dbname'

来自分类Dev

AttributeError:“ str”对象没有属性“ values”

来自分类Dev

AttributeError:'str'对象没有属性'description'

Related 相关文章

  1. 1

    Python 3.4:str:AttributeError:'str'对象没有属性'decode

  2. 2

    Python 3.4:str:AttributeError:'str'对象没有属性'decode

  3. 3

    Python-AttributeError:“ str”对象没有属性“ isDigit”

  4. 4

    Python-AttributeError:“ str”对象没有属性“ append”

  5. 5

    Python EXscript-AttributeError:“ str”对象没有属性“ connect”

  6. 6

    Python AttributeError:“ str”对象没有属性“ DataFrame”

  7. 7

    python error AttributeError:'str'对象没有属性'setdefault'

  8. 8

    Python AttributeError:“ str”对象没有属性“ items”

  9. 9

    Python AttributeError:“ str”对象没有属性“ append”

  10. 10

    Python-AttributeError:“ str”对象没有属性“ append”

  11. 11

    Python-AttributeError:“ str”对象没有属性“ isDigit”

  12. 12

    AttributeError:“ str”对象没有属性“ search_nodes”-Python

  13. 13

    AttributeError:“ str”对象没有属性“ location”-Python

  14. 14

    Python:AttributeError:'str'对象没有属性'readlines'

  15. 15

    AttributeError: 'str' 对象没有属性 'read' Python..

  16. 16

    Python - AttributeError: 'str' 对象没有属性 'append' - 数学游戏

  17. 17

    Python AttributeError:“str”对象没有“decode”属性

  18. 18

    熊猫:AttributeError:'str'对象没有属性'iloc'

  19. 19

    Python + 数据框:AttributeError:'float' 对象没有属性 'replace'

  20. 20

    包含 Pandas 数据框列中的函数(AttributeError: 'str' 对象没有属性 'str'

  21. 21

    AttributeError:“ str”对象没有属性“ toLowerCase”

  22. 22

    AttributeError:'str'对象没有属性'get'

  23. 23

    AttributeError:“ str”对象没有属性“ items”

  24. 24

    AttributeError:'str'对象没有属性'sleep'

  25. 25

    AttributeError:“ str”对象没有属性“ maketrans”

  26. 26

    AttributeError:'str'对象没有属性(功能)

  27. 27

    attributeError:'str'对象没有属性'dbname'

  28. 28

    AttributeError:“ str”对象没有属性“ values”

  29. 29

    AttributeError:'str'对象没有属性'description'

热门标签

归档