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
- http://blog.csdn.net/lxg0807/article/details/52960072
[Python]View PlainCopy
- Import logging
- Import Fasttext
- Logging.basicconfig (format='% (asctime) s:% (levelname) s:% (message) s ', Level=logging.info)
- #classifier = fasttext.supervised ("Fasttext/news_fasttext_train.txt", "Fasttext/news_fasttext.model", Label_prefix = "__label__")
- #load训练好的模型
- Classifier = Fasttext.load_model (' Fasttext/news_fasttext.model.bin ', label_prefix=' __label__ ')
- result = Classifier.test ("Fasttext/news_fasttext_test.txt")
- Print (result.precision)
- Print (Result.recall)
- Labels_right = []
- texts = []
- With open ("Fasttext/news_fasttext_test.txt") as fr:
- lines = Fr.readlines ()
- For line in lines:
- Labels_right.append (Line.split ("\ t") [1].rstrip (). Replace ("__label__", "" "))
- Texts.append (Line.split ("T") [0])
- # Print Labels
- # Print Texts
- # break
- Labels_predict = [e[0] for e in classifier.predict (texts)] #预测输出结果为二维形式
- # Print Labels_predict
- Text_labels = List (set (Labels_right))
- Text_predict_labels = List (set (LABELS_PREDICT))
- Print (Text_predict_labels)
- Print (Text_labels)
- A = Dict.fromkeys (Text_labels,0) #预测正确的各个类的数目
- B = Dict.fromkeys (Text_labels,0) #测试数据集中各个类的数目
- C = Dict.fromkeys (Text_predict_labels,0) #预测结果中各个类的数目
- For I in range (0,len (labels_right)):
- B[labels_right[i]] + = 1
- C[labels_predict[i]] + = 1
- if labels_right[i] = = Labels_predict[i]:
- A[labels_right[i]] + = 1
- Print (A)
- Print (B)
- Print (C)
- #计算准确率, recall rate, F value
- For key in B:
- p = float (A[key])/float (B[key])
- R = Float (A[key])/float (C[key])
- f = p * R * 2/(P + r)
- print ("%s:\tp:%f\t%fr:\t%f"% (key,p,r,f))
Java version scenario: Githup:
[HTML]View PlainCopy
- 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
- Package test;
- Import java.util.List;
- Import Fasttext. Fasttext;
- Import Fasttext. Main;
- Import Fasttext. Pair;
- Public class Test {
- public static void Main (string[] args) throws Exception {
- string[] Text = {
- "supervised",
- "-input",
- "/users/shuubiasahi/documents/python/fasttext/news_fasttext_train.txt",
- "-output", "/users/shuubiasahi/documents/faste.model", "-dim" ,
- " Ten", "-LR", "0.1", "-wordngrams", "2", "-mincount", "1",
- "-bucket", "10000000", "-epoch", "5", "-thread", "4"};
- Main OP = new Main ();
- Op.train (text);
- Fasttext Fasttext = new Fasttext ();
- string[] Test = { "enrolled", "Science", " student","student","student"};
- Fasttext.loadmodel ("/users/shuubiasahi/documents/faste.model.bin");
- List<pair<float, string>> list = fasttext.predict (test, 6); //Get the maximum possible six prediction probabilities
- for (pair<float, string> parir:list) {
- System.out.println ("key is:" + parir.getkey () + "value is:"
- + Parir.getvalue ());
- }
- System.out.println (Math.exp (List.get (0). GetKey ())); //Get maximum predictive probability
- }
- }
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