ML用于数值变换

时间:2017-03-13 20:15:50

标签: machine-learning classification

我有一个大小为200的小数据集。数据集非常简单:每一行都包含一个映射到单个标签的[0,1]范围内的实数值。总共有24个标签,我的任务的本质是训练分类器基本上找到映射到标签的范围。

我能想到两种方法。第一个是SVC,因为它能够将输入平面分成24个区域,这正是我所需要的。然而,当我尝试编码时,我得到了一些可怕的结果:分类器没有学到任何东西,并且无论输入值如何都会吐出相同的标签。

我正在考虑的第二种方法是神经网络,但鉴于缺乏功能和训练数据,我非常怀疑这种方法的可行性。

如果需要,我可以分享我用scikit-learn开发的SVC代码。

以下是我将数据转储到终端上的数据:

Label: Min, Mean, Max
{0: [0.96, 0.98, 1.0],
 1: [0.15, 0.36, 0.92],
 2: [0.14, 0.56, 0.98],
 3: [0.37, 0.7, 1.0],
 4: [0.23, 0.23, 0.23],
 6: [0.41, 0.63, 0.97],
 7: [0.13, 0.38, 0.61],
 8: [0.11, 0.68, 1.0],
 9: [0.09, 0.51, 1.0],
 10: [0.19, 0.61, 0.97],
 11: [0.26, 0.41, 0.57],
 12: [0.29, 0.72, 0.95],
 13: [0.63, 0.9, 0.99],
 14: [0.06, 0.55, 1.0],
 15: [0.1, 0.64, 1.0],
 16: [0.26, 0.58, 0.95],
 17: [0.29, 0.88, 1.0],
 21: [0.58, 0.79, 1.0],
 22: [0.24, 0.59, 0.94],
 23: [0.12, 0.62, 0.95]}

正如您所看到的,数据到处都是,但我想知道是否有可能找到每个标签最佳代表的范围。

如果有人能告诉我我是否走在正确的轨道上,我会很感激。谢谢!

2 个答案:

答案 0 :(得分:2)

如果我们假设你的每个类的样本在某种程度上居中(但仍然很嘈杂;可能存在重叠),那么sklearn中可用的最自然的分类器可能是Gaussian Naive Bayes,我们假设每个类的这些点都遵循正常分布。

这是一些代码,它构建一些假数据,对其进行分类并进行评估:

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
np.random.seed(1)


""" Data-params + Data-generation """
N_CLASSES = 24
N_SAMPLES_PER_CLASS = 10
SIGMA = 0.01

class_centers = np.random.random(size=N_CLASSES)
# ugly code with bad numpy-style
X = []
for class_center in class_centers:
    samples = np.random.normal(size=N_SAMPLES_PER_CLASS)*SIGMA
    for sample in samples + class_center:
        X.append(sample)
Y = []
for ind, c in enumerate(class_centers):
    for s in range(N_SAMPLES_PER_CLASS):
        Y.append(ind)

X = np.array(X).reshape(-1, 1)
Y = np.array(Y)

""" Split & Fit & Eval """
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.1, random_state=0)

et = GaussianNB()
et.fit(X_train, y_train)

print('Prediction on test')
preds = et.predict(X_test)
print(preds)

print('Original samples')
print(y_test)

print('Accuracy-score')
print(accuracy_score(y_test, preds))

输出

Prediction on test
[10  7  3  7  8  3 23  3 11 19  7 20  8 15 11 13 18 11  3 16  8  9  8 12]
Original samples
[10  7  3  7 10 22 15 22 15 19  7 20  8 15 23 13 18 11 22  0 10 17  8 12]
Accuracy-score
0.583333333333

当然,结果高度依赖于N_SAMPLES_PER_CLASSSIGMA

修改

正如您现在提供的数据一样,很明显我的假设并不成立。 请参阅此代码完成的以下绘图(文件已从[]()中删除;人们应该发布与csv兼容的数据!):

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data = pd.read_csv('idVXjwgZ.txt', usecols=[0,1], names=['x', 'y'])
sns.swarmplot(data=data, x='y', y='x')
plt.show()

简介:

enter image description here

现在只考虑观察一些x,你需要决定y。对于大多数 x-ranges 来说非常难。

显然,类平衡问题也解释了大多数预测的第14类输出。

答案 1 :(得分:1)

如果标签范围不重叠,那么这不是ML问题;它是一个简单的列表排序任务。对实数进行排序;按标签分组。在每个标签内,取最小值和最大值;这是你的范围。

如果您需要分区,则按其实际值的顺序对范围进行排序。对于每对相邻类,取边界值的中值,并使其成为类之间的分区。

例如,给出3个类中12个值的列表

(0.10, 3), (0.40, 2), (0.11, 3), (0.24, 1),
(0.20, 1), (0.21, 1), (0.12, 3), (0.41, 2),
(0.18, 3), (0.42, 2), (0.46, 2), (0.22, 1)

按每对中的第一个值对列表进行排序:

(0.10, 3), (0.11, 3), (0.12, 3), (0.18, 3),
(0.20, 1), (0.21, 1), (0.22, 1), (0.24, 1),
(0.40, 2), (0.41, 2), (0.42, 2), (0.46, 2),

现在每个标签都有一个范围:

3 [0.10 - 0.18]
1 [0.20 - 0.24]
2 [0.40 - 0.46]

如果你想要分区值,只需要取边界平均值,你就可以得到0.19和0.32的值来分隔你的类。