【问题标题】:run perceptron algorithm on a hash map feature vecteur: java在哈希图特征向量上运行感知器算法:java
【发布时间】:2015-04-16 16:24:26
【问题描述】:

我有以下代码,它将目录中的许多文件读入哈希映射,这是我的功能向量。从某种意义上说,它没有阻止,但这有点幼稚,但这不是我现在主要关心的问题。我想知道如何使用这个数据结构作为感知器算法的输入。我想我们称之为一袋文字,不是吗?

public class BagOfWords 
{
        static Map<String, Integer> bag_of_words = new HashMap<>();

        public static void main(String[] args) throws IOException 
        {
            String path = "/home/flavius/atheism;
            File file = new File( path );
            new BagOfWords().iterateDirectory(file);

            for (Map.Entry<String, Integer> entry : bag_of_words.entrySet()) 
            {
                System.out.println(entry.getKey()+" : "+entry.getValue());
            }

        }

        private void iterateDirectory(File file) throws IOException 
        {
            for (File f : file.listFiles()) 
            {
                if (f.isDirectory()) 
                {    
                    iterateDirectory(file);
                } 
                else 
                {
                    String line; 
                    BufferedReader br = new BufferedReader(new FileReader( f ));

                    while ((line = br.readLine()) != null) 
                    {

                        String[] words = line.split(" ");//those are your words

                        String word;

                        for (int i = 0; i < words.length; i++) 
                        {
                            word = words[i];
                            if (!bag_of_words.containsKey(word))
                            {
                                bag_of_words.put(word, 0);
                            }
                            bag_of_words.put(word, bag_of_words.get(word) + 1);
                        }

                    }

                }
            }
        }
    }

您可以看到路径指向一个名为“无神论”的目录,还有一个名为 sports 的目录,我想尝试线性分离这两类文档,然后尝试将看不见的测试文档分成任一类别。

如何做到这一点?如何将其概念化。我会很感激一个可靠的参考,全面的解释或某种伪代码。

我没有在网上找到很多内容丰富且清晰的参考资料。

【问题讨论】:

  • 您需要将您的文件(文档)矢量化为矢量表示,也许您想看看我的矢量化器类:github.com/thomasjungblut/thomasjungblut-common/blob/master/src/…
  • 这与我现在拥有的哈希映射有何不同?
  • @ThomasJungblut 我认为这就是我现在已经在做的事情,不是吗?
  • 你的 bag_of_words 是一本字典,你需要一袋你解析的每个文档(文件)。
  • 啊,所以现在我将所有文件放入一个字典中,但我真正应该做的是为每个文件创建一个单独的字典?

标签: java machine-learning perceptron


【解决方案1】:

让我们预先建立一些词汇表(我猜你正在使用 20-newsgroup 数据集):

  • “类标签”是您试图预测的内容,在您的二进制情况下,这是“无神论”与其他人的对比
  • “特征向量”就是您输入分类器的内容
  • “文档”是数据集中的一封电子邮件
  • “标记”文档的一部分,通常是一元/二元/三元
  • “字典”为您的向量提供一组“允许”字词

所以词袋的向量化算法通常遵循以下步骤:

  1. 查看所有文档(跨越所有类标签)并收集所有标记,这是您的字典和特征向量的维度
  2. 再次检查所有文档,并为每个文档执行以下操作:
    1. 使用您的字典的维数创建一个新的特征向量(例如,200,该字典中的 200 个条目)
    2. 检查该文档中的所有标记,并在特征向量的这个维度设置字数(在该文档中)
  3. 您现在有了一个特征向量列表,您可以将其输入到您的算法中

例子:

Document 1 = ["I", "am", "awesome"]
Document 2 = ["I", "am", "great", "great"]

字典是:

["I", "am", "awesome", "great"]

所以作为向量的文档看起来像:

Document 1 = [1, 1, 1, 0]
Document 2 = [1, 1, 0, 2]

这样你就可以做各种花哨的数学运算并将其输入你的感知器。

【讨论】:

  • Thomas 先生,我刚刚给出了一个新进展的答案。
  • 我已经实现了代码来创建你描述的数据结构,下一步是什么?
【解决方案2】:

这是对我最初的问题的完整和完整的答案,发布在这里是为了让未来的读者受益


给定以下文件:

  • 无神论/a_0.txt

    Gott ist tot.
    
  • politics/p_0.txt

    L'Etat, c'est moi , et aussi moi .
    
  • science/s_0.txt

    If I have seen further it is by standing on the shoulders of giants.
    
  • sports/s_1.txt

    You miss 100% of the shots you don't take.
    
  • 输出数据结构:

    /data/train/politics/p_0.txt, [0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
    /data/train/science/s_0.txt, [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0]
    /data/train/atheism/a_0.txt, [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    /data/train/sports/s_1.txt, [0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1]
    

代码如下所示,或者您可以在my GitHub page 上找到它。

public class FileDictCreateur 
{
    static String PATH = "/home/matthias/Workbench/SUTD/ISTD_50.570/assignments/practice_data/data/train";

    //the global list of all words across all articles
    static Set<String> GLOBO_DICT = new HashSet<String>();

    //is the globo dict full?
    static boolean globo_dict_fixed = false;

    // hash map of all the words contained in individual files
    static Map<File, ArrayList<String> > fileDict = new HashMap<>();

    //input to perceptron. final struc.
    static Map<File, int[] > perceptron_input = new HashMap<>();


    @SuppressWarnings("rawtypes")
    public static void main(String[] args) throws IOException 
    {
        //each of the diferent categories
        String[] categories = { "/atheism", "/politics", "/science", "/sports"};

        //cycle through all categories once to populate the global dict
        for(int cycle = 0; cycle <= 3; cycle++)
        {
            String general_data_partition = PATH + categories[cycle];

            File directory = new File( general_data_partition );
            iterateDirectory( directory , globo_dict_fixed);

            if(cycle == 3)
                globo_dict_fixed = true;
        }


        //cycle through again to populate the file dicts
        for(int cycle = 0; cycle <= 3; cycle++)
        {
            String general_data_partition = PATH + categories[cycle];

            File directory = new File( general_data_partition );
            iterateDirectory( directory , globo_dict_fixed);

        }



        perceptron_data_struc_generateur( GLOBO_DICT, fileDict, perceptron_input );



        //print the output
        for (Map.Entry<File, int[]> entry : perceptron_input.entrySet()) 
        {
            System.out.println(entry.getKey() + ", " + Arrays.toString(entry.getValue()));
        }
    }



    private static void iterateDirectory(File directory, boolean globo_dict_fixed) throws IOException 
    {
        for (File file : directory.listFiles()) 
        {
            if (file.isDirectory()) 
            {
                iterateDirectory(directory, globo_dict_fixed);
            } 
            else 
            {   
                String line; 
                BufferedReader br = new BufferedReader(new FileReader( file ));

                while ((line = br.readLine()) != null) 
                {
                    String[] words = line.split(" ");//those are your words

                    if(globo_dict_fixed == false)
                    {
                        populate_globo_dict( words );
                    }
                    else
                    {
                        create_file_dict( file, words );
                    }
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static void create_file_dict( File file, String[] words ) throws IOException
    {   

        if (!fileDict.containsKey(file))
        {
            @SuppressWarnings("rawtypes")
            ArrayList document_words = new ArrayList<String>();

            String word;

            for (int i = 0; i < words.length; i++) 
            {
                word = words[i];

                document_words.add(word);
            }
            fileDict.put(file, document_words);
        }
    }

    public static void populate_globo_dict( String[] words ) throws IOException
    {
        String word;

        for (int i = 0; i < words.length; i++) 
        {
            word = words[i];
            if (!GLOBO_DICT.contains(word))
            {
                GLOBO_DICT.add(word);
            }
        }   
    }

    public static void perceptron_data_struc_generateur(Set<String> GLOBO_DICT, 
                                                    Map<File,     ArrayList<String> > fileDict,
                                                    Map<File, int[] > perceptron_input)
    {
        //create a new entry in the array list 'perceptron_input'
        //with the key as the file name from fileDict
            //create a new array which is the length of GLOBO_DICT
            //iterate through the indicies of GLOBO_DICT
                //for all words in globo dict, if that word appears in fileDict,
                //increment the perceptron_input index that corresponds to that
                //word in GLOBO_DICT by the number of times that word appears in fileDict

        //so i can get the index later
        List<String> GLOBO_DICT_list = new ArrayList<>(GLOBO_DICT);

        for (Map.Entry<File, ArrayList<String>> entry : fileDict.entrySet()) 
        {
            int[] cross_czech = new int[GLOBO_DICT_list.size()];
            //initialize to zero
            Arrays.fill(cross_czech, 0);

            for (String s : GLOBO_DICT_list)
            {

                for(String st : entry.getValue()) 
                {
                    if( st.equals(s) )
                    {
                        cross_czech[ GLOBO_DICT_list.indexOf( s ) ] = cross_czech[ GLOBO_DICT_list.indexOf( s ) ] +1;
                    }
                }
            }
            perceptron_input.put( entry.getKey() , cross_czech);    
        }
    }
}

【讨论】:

  • 如果您有新问题,请点击 按钮提出问题。如果有助于提供上下文,请包含指向此问题的链接。
  • 是的,但实际上这确实是对其中包含的原始问题的完整答案。这是一种二合一
  • @flavius_valens 我将继续编辑您的答案;这个答案出现在低质量队列中,通过阅读第一行,我可以看到它很容易被视为一个新问题而忽略。
  • @ChrisForrence 你们中的任何一个都知道你试图根除的问题的答案吗?
  • @flavius_valens 你需要问托马斯。看起来你已经做了正确的事:评论他的回答。他应该得到一个通知。请注意,在 answer 中提及人员不会通知任何人。注释?是的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
相关资源
最近更新 更多