首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弓并非所有要素

弓并非所有要素
EN

Stack Overflow用户
提问于 2021-12-14 21:22:21
回答 2查看 60关注 0票数 0

我正在研究用弓形方法来生成表示和弦的矢量的可能性。然而,当我使用这种方法时,我可以生成向量,但并不是所有的和弦都被考虑在内。

以下是详细阐述的守则:

代码语言:javascript
复制
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
代码语言:javascript
复制
# DF
    music   chords
0   1.wav   N, A7, Am7, Am7b5/G, A7, N
1   2.wav   N, Em, C, D, Em, C, D, N
2   3.wav   N, E, A, E, B, A, D6, E, N
代码语言:javascript
复制
#BOW
bow = CountVectorizer(max_features=1000, ngram_range=(1,1))
train_bow = bow.fit_transform(df['chords'])
pd.DataFrame(bow.transform(df['chords']).toarray(), columns=sorted(bow.vocabulary_.keys()))

#Result
    a7  am7 am7b5   d6  em
0   2   1   1      0     0
1   0   0   0      0     2
2   0   0   0      1     0

例如,像C,D和A这样的和弦不算在内。有人知道我可能错了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-14 21:42:12

我不知道sklearn的默认令牌程序是如何工作的,但是它不适合您的输入。

代码语言:javascript
复制
tokenizer = lambda x: x.replace(" ", "").split(",")
bow = CountVectorizer(max_features=1000, tokenizer = tokenizer, ngram_range=(1,1))
train_bow = bow.fit_transform(df['chords'])
pd.DataFrame(bow.transform(df['chords']).toarray(), columns=sorted(bow.vocabulary_.keys()))

打印输出:

代码语言:javascript
复制
>>> bow.vocabulary_.keys()
dict_keys(['n', 'a7', 'am7', 'am7b5/g', 'em', 'c', 'd', 'e', 'a', 'b', 'd6'])
票数 2
EN

Stack Overflow用户

发布于 2021-12-15 09:32:16

我编写了一种方法来手动创建词汇表,另一种方法用于标记。

输出如下:

代码语言:javascript
复制
>>>
   b  d  am7  c  em  n  a  e  a7  am7b5/g  d6
0  0  0    1  0   0  2  0  0   2        1   0
1  0  2    0  2   2  2  0  0   0        0   0
2  1  0    0  0   0  2  2  3   0        0   1

代码如下:

代码语言:javascript
复制
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer

def voc(chord):
    items = []
    for item in chord:
        items += item.split(', ')
        items = [el.lower() for el in items]
    vocabulary = list(set(items))
    return vocabulary

def tokenizer(item):
    items = []
    items = item.split(', ')
    items = [el.lower() for el in items]
    return items

df = pd.read_excel("df.xlsx") #I created a df for test purpose, replace with yours


chord = list(df['chords'].values)

vocabulary = voc(chord)
#BOW
bow = CountVectorizer(vocabulary = vocabulary, tokenizer = tokenizer, max_features=1000, ngram_range=(1,1))
train_bow = bow.fit_transform(df['chords'])
bow = pd.DataFrame(bow.transform(df['chords']).toarray(),columns=bow.vocabulary_.keys())

如果这是你想要的,请告诉我!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70355681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档