Fasttext Basic use of Java, Python as an example

Source: Internet
Author: User

Fasttext Basic use of Java, Python as an example

This morning on the subway to see someone using Fasttext for text classification, to the company tried the next situation on GitHub to find the next, the first is the C + + version of the implementation, but there are Java, Python version of the implementation, just take the test hackers,

Python case:

Python version reference, the author provides a detailed implementation, and provides the Chinese word after the data, just take down to use, thanks to the author, the code provided by the data authors are provided, point after link on the above have Baidu disk, downloadable, Java interface used to the same data:

[HTML]View PlainCopy
    1. http://blog.csdn.net/lxg0807/article/details/52960072

[Python]View PlainCopy
  1. Import logging
  2. Import Fasttext
  3. Logging.basicconfig (format='% (asctime) s:% (levelname) s:% (message) s ', Level=logging.info)
  4. #classifier = fasttext.supervised ("Fasttext/news_fasttext_train.txt", "Fasttext/news_fasttext.model", Label_prefix = "__label__")
  5. #load训练好的模型
  6. Classifier = Fasttext.load_model (' Fasttext/news_fasttext.model.bin ', label_prefix=' __label__ ')
  7. result = Classifier.test ("Fasttext/news_fasttext_test.txt")
  8. Print (result.precision)
  9. Print (Result.recall)
  10. Labels_right = []
  11. texts = []
  12. With open ("Fasttext/news_fasttext_test.txt") as fr:
  13. lines = Fr.readlines ()
  14. For line in lines:
  15. Labels_right.append (Line.split ("\ t") [1].rstrip (). Replace ("__label__", "" "))
  16. Texts.append (Line.split ("T") [0])
  17. # Print Labels
  18. # Print Texts
  19. # break
  20. Labels_predict = [e[0] for e in classifier.predict (texts)] #预测输出结果为二维形式
  21. # Print Labels_predict
  22. Text_labels = List (set (Labels_right))
  23. Text_predict_labels = List (set (LABELS_PREDICT))
  24. Print (Text_predict_labels)
  25. Print (Text_labels)
  26. A = Dict.fromkeys (Text_labels,0) #预测正确的各个类的数目
  27. B = Dict.fromkeys (Text_labels,0) #测试数据集中各个类的数目
  28. C = Dict.fromkeys (Text_predict_labels,0) #预测结果中各个类的数目
  29. For I in range (0,len (labels_right)):
  30. B[labels_right[i]] + = 1
  31. C[labels_predict[i]] + = 1
  32. if labels_right[i] = = Labels_predict[i]:
  33. A[labels_right[i]] + = 1
  34. Print (A)
  35. Print (B)
  36. Print (C)
  37. #计算准确率, recall rate, F value
  38. For key in B:
  39. p = float (A[key])/float (B[key])
  40. R = Float (A[key])/float (C[key])
  41. f = p * R * 2/(P + r)
  42. print ("%s:\tp:%f\t%fr:\t%f"% (key,p,r,f))

Java version scenario: Githup: [HTML]View PlainCopy
    1. Https://github.com/ivanhk/fastText_java


See the use of the next SH script, their own simple a text method, just use, the back will take Xgboost to compare, look at the effect, the effect can be written service to go online: [Java]View PlainCopy
  1. Package test;
  2. Import java.util.List;
  3. Import Fasttext. Fasttext;
  4. Import Fasttext. Main;
  5. Import Fasttext. Pair;
  6. Public class Test {
  7. public static void Main (string[] args) throws Exception {
  8. string[] Text = {
  9. "supervised",
  10. "-input",
  11. "/users/shuubiasahi/documents/python/fasttext/news_fasttext_train.txt",
  12. "-output", "/users/shuubiasahi/documents/faste.model", "-dim" ,
  13. " Ten", "-LR", "0.1", "-wordngrams", "2", "-mincount", "1",
  14. "-bucket", "10000000", "-epoch", "5", "-thread", "4"};
  15. Main OP = new Main ();
  16. Op.train (text);
  17. Fasttext Fasttext = new Fasttext ();
  18. string[] Test = { "enrolled", "Science", " student","student","student"};
  19. Fasttext.loadmodel ("/users/shuubiasahi/documents/faste.model.bin");
  20. List<pair<float, string>> list = fasttext.predict (test, 6); //Get the maximum possible six prediction probabilities
  21. for (pair<float, string> parir:list) {
  22. System.out.println ("key is:" + parir.getkey () + "value is:"
  23. + Parir.getvalue ());
  24. }
  25. System.out.println (Math.exp (List.get (0). GetKey ())); //Get maximum predictive probability
  26. }
  27. }


Here the set bucket does not apply to set too large, over the General Assembly generated Oom, and the model is saved too large, the above settings model saved there are 1 G,-wordngrams can be set to 2 ratio set to 1 can improve the accuracy of the model classification, the results of the situation:

Key is:0.0 Value is:__label__edu

Key is:-17.75125 Value Is:__label__affairs

Key is:-17.75125 Value Is:__label__economic

Key is:-17.75125 Value Is:__label__ent

Key is:-17.75125 Value Is:__label__fashion

Key is:-17.75125 Value Is:__label__game

1.0


Note that Fasttext is required for the input format, the label label uses "__label__" + the actual label form, over

Contact me if you have any questions

May 2016 26 My model is on the line and it works.

Fasttext Basic use of Java, Python as an example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.