我应该如何处理音乐键(比例)作为KNN算法中的功能

发布于 2025-02-10 08:02:50 字数 249 浏览 3 评论 0原文

我正在进行数据科学项目,我想知道如何将音乐键(比例)作为KNN算法中的功能处理。 我知道KNN是基于距离的,因此给出每个钥匙一个像1-24这样的数字没有太大的意义(因为密钥编号24接近1接近7接近8的1)。 我考虑过为“大专业/次要”制作一列,而另一列本身则是 但是我仍然面临着同样的问题,我需要用一个数字指定注释,但是由于注释是循环的,所以我不能以线性为1-12。

对于那些不知道音乐钥匙如何工作的人,我的问题等同于在KNN中的处理状态,您不能仅以线性1-50来编号。

I'm doing a data science project, and I was wondering how to handle a music key (scale) as a feature in the KNN algorithm.
I know KNN is based on distances, therefore giving each key a number like 1-24 doesn't make that much sense (because key number 24 is close to 1 as much as 7 close to 8).
I have thought about making a column for "Major/Minor" and another for the note itself,
but I'm still facing the same problem, I need to specify the note with a number, but because notes are cyclic I cannot number them linearly 1-12.

For the people that have no idea how music keys work my question is equivalent to handling states in KNN, you can't just number them linearly 1-50.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

智商已欠费 2025-02-17 08:02:50

您可以考虑量表之间的距离的一种方法是将每个量表视为一个12元素的二进制向量,其中有1个音符在刻度中的位置,否则为零。

然后,您可以计算秤之间的锤距。例如,大规模及其相对次要尺度之间的锤子距离应为零,因为它们都包含相同的音符。

这是您可以在Python中进行设置的方法

from enum import IntEnum
import numpy as np
from scipy.spatial.distance import hamming

class Note(IntEnum):
    C = 0
    Db = 1
    D = 2
    Eb = 3
    E = 4
    F = 5
    Gb = 6
    G = 7
    Ab = 8
    A = 9
    Bb = 10
    B = 11

major = np.array((1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1))
minor = np.array((1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0)) #WHWWHWW Natural Minor

# Transpose the basic scale form to a key using Numpy's `roll` function

cMaj = np.roll(major, Note.C) # Rolling by zero changes nothing
aMin = np.roll(minor, Note.A)
gMaj = np.roll(major, Note.G)
fMaj = np.roll(major, Note.F)

print('Distance from cMaj to aMin', hamming(cMaj, aMin))
print('Distance from cMaj to gMaj', hamming(cMaj, gMaj)) # One step clockwise on circle of fifths
print('Distance from cMaj to fMaj', hamming(cMaj, fMaj)) # One step counter-clockwise on circle of fifths

One way you could think about the distance between scales is to think of each scale as a 12-element binary vector where there's a 1 wherever a note is in the scale and a zero otherwise.

Then you can compute the Hamming distance between scales. The Hamming distance, for example, between a major scale and its relative minor scale should be zero because they both contain the same notes.

Here's a way you could set this up in Python

from enum import IntEnum
import numpy as np
from scipy.spatial.distance import hamming

class Note(IntEnum):
    C = 0
    Db = 1
    D = 2
    Eb = 3
    E = 4
    F = 5
    Gb = 6
    G = 7
    Ab = 8
    A = 9
    Bb = 10
    B = 11

major = np.array((1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1))
minor = np.array((1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0)) #WHWWHWW Natural Minor

# Transpose the basic scale form to a key using Numpy's `roll` function

cMaj = np.roll(major, Note.C) # Rolling by zero changes nothing
aMin = np.roll(minor, Note.A)
gMaj = np.roll(major, Note.G)
fMaj = np.roll(major, Note.F)

print('Distance from cMaj to aMin', hamming(cMaj, aMin))
print('Distance from cMaj to gMaj', hamming(cMaj, gMaj)) # One step clockwise on circle of fifths
print('Distance from cMaj to fMaj', hamming(cMaj, fMaj)) # One step counter-clockwise on circle of fifths
瞳孔里扚悲伤 2025-02-17 08:02:50

iiuc,您可以将功能转换为sin,如下所示。听说我有10个值1-10,我正在改变它们以保持它们的循环关系。

a = np.around(np.sin([np.deg2rad(x*18) for x in np.array(list(range(11)))]), 3)
import matplotlib.pyplot as plt
plt.plot(a)

输出:

通过此功能工程,您可以看到功能的圆形性已编码。 0等于10。

IIUC, you can convert your features to something like sin as follows. Hear I have 10 values 1-10 and I am transforming them to keep their circular relation.

a = np.around(np.sin([np.deg2rad(x*18) for x in np.array(list(range(11)))]), 3)
import matplotlib.pyplot as plt
plt.plot(a)

Output:

enter image description here

Through this feature engineering you can see that the circularity of your feature is encoded. The value of 0 is equal to 10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文