NumPy的实施近邻分类算法的分类都以完全相同的方式

问题描述 投票:0回答:1

我的任务是使用K近邻算法来确定是基于它的各种功能,什么样的花东西使用NumPy的(例如茎长,花瓣长度等)。 (根据记录,我已经与Python在过去的工作,虽然这不是我的“最好”的语言,但是我完全新的NumPy的)。

无论我的训练,我的测试数据是CSV格式如下所示:

4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
5.5,2.3,4.0,1.3,Iris-versicolor

我知道怎么做的基本算法。下面是我为它创建了C#:

namespace Project_3_Prototype
{
    public class FourD
    {
        public double f1, f2, f3, f4;

        public string name;

        public static double Distance(FourD a, FourD b)
        {
            double squared = Math.Pow(a.f1 - b.f1, 2) + Math.Pow(a.f2 - b.f2, 2) + Math.Pow(a.f3 - b.f3, 2) + Math.Pow(a.f4 - b.f4, 2);

            return Math.Sqrt(squared);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<FourD> distances = new List<FourD>();

            using (var parser = new TextFieldParser("iris-training-data.csv"))
            {
                parser.SetDelimiters(",");

                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();

                    var curr = new FourD
                    {
                        f1 = double.Parse(fields[0]),
                        f2 = double.Parse(fields[1]),
                        f3 = double.Parse(fields[2]),
                        f4 = double.Parse(fields[3]),
                        name = fields[4]
                    };

                    distances.Add(curr);
                }
            }

            double correct = 0, total = 0;

            using (var parser = new TextFieldParser("iris-testing-data.csv"))
            {
                parser.SetDelimiters(",");

                int i = 1;

                while (!parser.EndOfData)
                {
                    total++;
                    string[] fields = parser.ReadFields();

                    var curr = new FourD
                    {
                        f1 = double.Parse(fields[0]),
                        f2 = double.Parse(fields[1]),
                        f3 = double.Parse(fields[2]),
                        f4 = double.Parse(fields[3]),
                        name = fields[4]
                    };

                    FourD min = distances[0];

                    foreach (FourD comp in distances)
                    {
                        if (FourD.Distance(comp, curr) < FourD.Distance(min, curr))
                        {
                            min = comp;
                        }
                    }

                    if (min.name == curr.name)
                    {
                        correct++;
                    }

                    Console.WriteLine(string.Format("{0},{1},{2}", i, curr.name, min.name));

                    i++;
                }
            }

            Console.WriteLine("Accuracy: " + correct / total);

            Console.ReadLine();
        }
    }
}

这是工作完全按照预期,具有以下的输出:

# The format is Number,Correct label,Predicted Label
1,Iris-setosa,Iris-setosa
2,Iris-setosa,Iris-setosa
3,Iris-setosa,Iris-setosa
4,Iris-setosa,Iris-setosa
5,Iris-setosa,Iris-setosa
6,Iris-setosa,Iris-setosa
7,Iris-setosa,Iris-setosa
8,Iris-setosa,Iris-setosa
9,Iris-setosa,Iris-setosa
10,Iris-setosa,Iris-setosa
11,Iris-setosa,Iris-setosa
12,Iris-setosa,Iris-setosa
...

Accuracy: 0.946666666666667

我试图做同样的事情在NumPy的。然而,分配不允许我使用for循环,只有向量化功能。

所以,基本上我想要做的是:在测试数据中的每一行,获取该行的那是最接近的训练数据的索引(即具有最小欧几里得距离)。

这是我在Python尝试:

import numpy as np

def main():    
    # Split each line of the CSV into a list of attributes and labels
    data = [x.split(',') for x in open("iris-training-data.csv")]

    # The last item is the label
    labels = np.array([x[-1].rstrip() for x in data])

    # Convert the first 3 items to a 2D array of floats
    floats = np.array([x[0:3] for x in data]).astype(float)

    classifyTrainingExamples(labels, floats)

def classifyTrainingExamples(labels, floats):
    # We're basically doing the same thing to the testing data that we did to the training data
    testingData = [x.split(',') for x in open("iris-testing-data.csv")]

    testingLabels = np.array([x[-1].rstrip() for x in testingData])

    testingFloats = np.array([x[0:3] for x in testingData]).astype(float)

    res = np.apply_along_axis(lambda x: closest(floats, x), 1, testingFloats)

    correct = 0

    for number, index in enumerate(res):    
        if labels[index] == testingLabels[number]:
            correct += 1

        print("{},{},{}".format(number + 1, testingLabels[number], labels[index]))

        number += 1

    print(correct / len(list(res)))

def closest(otherArray, item):
    res = np.apply_along_axis(lambda x: distance(x, item), 1, otherArray)

    i = np.argmin(res)

    return i

# Get the Euclidean distance between two "flat" lists (i.e. one particular row
def distance(a, b):
    # Subtract one from the other elementwise, then raise each one to the power of 2
    lst = (a - b) ** 2

    # Sum all of the elements together, and take the square root
    result = np.sqrt(lst.sum())

    return result

main()

不幸的是,输出的样子

1,Iris-setosa,Iris-setosa
2,Iris-setosa,Iris-setosa
3,Iris-setosa,Iris-setosa
4,Iris-setosa,Iris-setosa
....
74,Iris-setosa,Iris-setosa
75,Iris-setosa,Iris-setosa
0.93333333

每一行都有什么,但Iris-setosa的标签,精度0.9333333。

我试图通过这个与调试器步进,每一个项目被算作由if说法是正确的(但正确率仍显示为0.93333333)。

所以基本上:

  • 据显示,每一个结果是“正确的”(当它显然不是)。
  • 它显示Iris-setosa每一个值
  • 我的百分比显示为93%。正确的值实际上大约是94%,但是我希望这表明100%给予每一个结果是所谓的“正确的”。

谁能帮我看看我在这里失踪?

而有人问之前,备案,是的,我也尝试过这种使用调试:)另外备案,没错,这就是功课步进。

c# python-3.x numpy machine-learning knn
1个回答
2
投票

如果你真的想这样做在同一行,这里是你能做什么(我从网上下载scikit学习数据集):

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split

# Load dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Split training and test set
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.2)
# 1-neareast neighbour    
ypred = np.array([ytrain[np.argmin(np.sum((x-Xtrain)**2,axis=1))] for x in Xtest])
# Compute classification error
sum(ypred != ytest)/ len(ytest)

现在,这是1近邻,它只查看从训练集中的最近点。对于k近邻,你必须把它改成这样:

# k-neareast neighbour    
k = 3
ypredk = np.array([np.argmax(np.bincount(ytrain[np.argsort(np.sum((x-Xtrain)**2,axis=1))[0:k]])) for x in Xtest])
sum(ypredk != ytest)/ len(ytest)

为了把它的话,你的距离进行排序,你会发现k个最低值的索引(这是np.argsort部分)和相应的标签,然后你看k个者中最常见的标签(这是np.argmax(np.bincount(x))部分)。

最后,如果你想确保,你可以用scikit-learn比较:

# scikit-learn NN
from sklearn import neighbors
knn = neighbors.KNeighborsClassifier(n_neighbors=k, algorithm='ball_tree')
knn.fit(Xtrain,ytrain)
ypred_sklearn = knn.predict(Xtest)
sum(ypred_sklearn != ytest)/ len(ytest)
© www.soinside.com 2019 - 2024. All rights reserved.