MCPcopy Index your code
hub / github.com/clips/pattern / export

Method export

pattern/vector/__init__.py:844–875  ·  view source on GitHub ↗

Exports the model as a file for other machine learning applications, e.g., Orange or Weka.

(self, path, format=ORANGE, **kwargs)

Source from the content-addressed store, hash-verified

842 cPickle.dump(self, open(path, "wb"), 1) # 1 = binary
843
844 def export(self, path, format=ORANGE, **kwargs):
845 """ Exports the model as a file for other machine learning applications,
846 e.g., Orange or Weka.
847 """
848 # The Document.vector space is exported without cache or LSA concept space.
849 keys = sorted(self.vector.keys())
850 s = []
851 # Orange tab format:
852 if format == ORANGE:
853 s.append("\t".join(keys + ["m#name", "c#type"]))
854 for document in self.documents:
855 v = document.vector
856 v = [v.get(k, 0) for k in keys]
857 v = "\t".join(x==0 and "0" or "%.4f" % x for x in v)
858 v = "%s\t%s\t%s" % (v, document.name or "", document.type or "")
859 s.append(v)
860 # Weka ARFF format:
861 if format == WEKA:
862 s.append("@RELATION %s" % kwargs.get("name", hash(self)))
863 s.append("\n".join("@ATTRIBUTE %s NUMERIC" % k for k in keys))
864 s.append("@ATTRIBUTE class {%s}" % ",".join(set(d.type or "" for d in self.documents)))
865 s.append("@DATA")
866 for document in self.documents:
867 v = document.vector
868 v = [v.get(k, 0) for k in keys]
869 v = ",".join(x==0 and "0" or "%.4f" % x for x in v)
870 v = "%s,%s" % (v, document.type or "")
871 s.append(v)
872 s = "\n".join(s)
873 f = open(path, "w", encoding="utf-8")
874 f.write(decode_utf8(s))
875 f.close()
876
877 def _update(self):
878 # Ensures that all document vectors are recalculated

Callers 1

test_model_exportMethod · 0.45

Calls 6

decode_utf8Function · 0.85
writeMethod · 0.80
keysMethod · 0.45
appendMethod · 0.45
getMethod · 0.45
closeMethod · 0.45

Tested by 1

test_model_exportMethod · 0.36