本文整理汇总了Python中titus.genpy.PFAEngine.fromJson方法的典型用法代码示例。如果您正苦于以下问题:Python PFAEngine.fromJson方法的具体用法?Python PFAEngine.fromJson怎么用?Python PFAEngine.fromJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类titus.genpy.PFAEngine
的用法示例。
在下文中一共展示了PFAEngine.fromJson方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testCartMustBuildNumericalCategorical
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testCartMustBuildNumericalCategorical(self):
random.seed(12345)
numpy.seterr(divide="ignore", invalid="ignore")
dataset = Dataset.fromIterable(((x, y, c) for (x, y, z, a, b, c) in TestProducerCart.data()), 100000, ("x", "y", "c"))
tree = TreeNode.fromWholeDataset(dataset, "c")
tree.splitMaxDepth(2)
doc = tree.pfaDocument({"type": "record", "name": "Datum", "fields": [{"name": "x", "type": "double"}, {"name": "y", "type": "double"}]}, "TreeNode")
# look(doc, maxDepth=8)
self.assertEqual(doc["cells"]["tree"]["init"]["field"], "x")
self.assertAlmostEqual(doc["cells"]["tree"]["init"]["value"], 4.00, places=2)
self.assertEqual(doc["cells"]["tree"]["init"]["pass"]["TreeNode"]["field"], "y")
self.assertAlmostEqual(doc["cells"]["tree"]["init"]["pass"]["TreeNode"]["value"], 6.00, places=2)
self.assertEqual(doc["cells"]["tree"]["init"]["pass"]["TreeNode"]["pass"]["string"], "C3")
self.assertEqual(doc["cells"]["tree"]["init"]["pass"]["TreeNode"]["fail"]["string"], "C6")
self.assertEqual(doc["cells"]["tree"]["init"]["fail"]["TreeNode"]["field"], "y")
self.assertAlmostEqual(doc["cells"]["tree"]["init"]["fail"]["TreeNode"]["value"], 2.00, places=2)
self.assertEqual(doc["cells"]["tree"]["init"]["fail"]["TreeNode"]["pass"]["string"], "C0")
self.assertEqual(doc["cells"]["tree"]["init"]["fail"]["TreeNode"]["fail"]["string"], "C0")
engine, = PFAEngine.fromJson(doc)
self.assertEqual(engine.action({"x": 2.0, "y": 3.0}), "C3")
self.assertEqual(engine.action({"x": 2.0, "y": 8.0}), "C6")
self.assertEqual(engine.action({"x": 7.0, "y": 1.0}), "C0")
self.assertEqual(engine.action({"x": 7.0, "y": 5.0}), "C0")
doc = tree.pfaDocument(
{"type": "record", "name": "Datum", "fields": [{"name": "x", "type": "double"}, {"name": "y", "type": "double"}]},
"TreeNode",
nodeScores=True, datasetSize=True, predictandDistribution=True, predictandUnique=True, entropy=True, gain=True)
# look(doc, maxDepth=8)
engine, = PFAEngine.fromJson(doc)
示例2: testCartMustBuildCategoricalCategorical
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testCartMustBuildCategoricalCategorical(self):
random.seed(12345)
numpy.seterr(divide="ignore", invalid="ignore")
dataset = Dataset.fromIterable(((a, b, c) for (x, y, z, a, b, c) in TestProducerCart.data()), 100000, ("a", "b", "c"))
tree = TreeNode.fromWholeDataset(dataset, "c")
tree.splitMaxDepth(2)
doc = tree.pfaDocument({"type": "record", "name": "Datum", "fields": [{"name": "a", "type": "string"}, {"name": "b", "type": "string"}]}, "TreeNode")
# look(doc, maxDepth=8)
self.assertEqual(doc["cells"]["tree"]["init"]["field"], "a")
self.assertEqual(doc["cells"]["tree"]["init"]["value"], ["A0", "A1", "A2", "A3"])
self.assertEqual(doc["cells"]["tree"]["init"]["pass"]["TreeNode"]["field"], "b")
self.assertEqual(doc["cells"]["tree"]["init"]["pass"]["TreeNode"]["value"], ["B6", "B8"])
self.assertEqual(doc["cells"]["tree"]["init"]["pass"]["TreeNode"]["pass"]["string"], "C6")
self.assertEqual(doc["cells"]["tree"]["init"]["pass"]["TreeNode"]["fail"]["string"], "C3")
self.assertEqual(doc["cells"]["tree"]["init"]["fail"]["TreeNode"]["field"], "b")
self.assertEqual(doc["cells"]["tree"]["init"]["fail"]["TreeNode"]["value"], ["B0"])
self.assertEqual(doc["cells"]["tree"]["init"]["fail"]["TreeNode"]["pass"]["string"], "C0")
self.assertEqual(doc["cells"]["tree"]["init"]["fail"]["TreeNode"]["fail"]["string"], "C0")
engine, = PFAEngine.fromJson(doc)
self.assertEqual(engine.action({"a": "A1", "b": "B6"}), "C6")
self.assertEqual(engine.action({"a": "A1", "b": "B2"}), "C3")
self.assertEqual(engine.action({"a": "A5", "b": "B0"}), "C0")
self.assertEqual(engine.action({"a": "A5", "b": "B4"}), "C0")
doc = tree.pfaDocument(
{"type": "record", "name": "Datum", "fields": [{"name": "a", "type": "string"}, {"name": "b", "type": "string"}]},
"TreeNode",
nodeScores=True, datasetSize=True, predictandDistribution=True, predictandUnique=True, entropy=True, gain=True)
# look(doc, maxDepth=8)
engine, = PFAEngine.fromJson(doc)
示例3: testTop5List
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testTop5List(self):
pfaDocument = titus.prettypfa.jsonNode('''
input: <<INPUT>>
output: array(string)
cells:
mostPlanets(array(Star)) = []
action:
// update the list of stars, keeping only the 5 with the most planets
var currentList =
mostPlanets to fcn(old: array(Star) -> array(Star))
stat.sample.topN(input, old, 5, u.morePlanets);
// map this top 5 list of stars to their names
a.map(currentList, fcn(x: Star -> string) x.name)
fcns:
// our comparison function
morePlanets = fcn(x: Star, y: Star -> boolean) a.len(x.planets) < a.len(y.planets)
'''.replace("<<INPUT>>", open("test/prettypfa/exoplanetsSchema.ppfa").read()), check=False, lineNumbers=False)
engine, = PFAEngine.fromJson(pfaDocument)
self.assertEqual(self.runEngine(engine), ["KOI-351", "HD 40307", "GJ 667C", "Kepler-11", "HD 10180"])
示例4: testSimpleKMeansWithStrings
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testSimpleKMeansWithStrings(self):
# define the workflow, leaving clusters as an empty array for now
pfaDocument = titus.prettypfa.jsonNode('''
input: <<INPUT>>
output: string
cells:
clusters(array(record(id: string, center: array(double)))) = []
action:
// ifnotnull runs the first block if all four expressions are not null
// input.mag has type union(double, null) while mag has type double, etc.
ifnotnull(mag: input.mag,
dist: input.dist,
mass: input.mass,
radius: input.radius)
model.cluster.closest(new(array(double), mag, dist, mass, radius),
clusters,
metric.simpleEuclidean)["id"]
else
"MISSING"
'''.replace("<<INPUT>>", open("test/prettypfa/exoplanetsSchema.ppfa").read()))
# fill in the clusters with the k-means result
if self.kmeansResult is None: self.doKmeans()
pfaDocument["cells"]["clusters"]["init"] = self.kmeansResult.pfaValue(self.clusterNames)
# build a scoring engine and test it
engine, = PFAEngine.fromJson(pfaDocument)
self.runEngine(engine)
示例5: testTree
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testTree(self):
engine, = PFAEngine.fromJson(open("test/hipparcos_numerical_10.pfa"))
data = []
for line in open("test/hipparcos_numerical.csv"):
ra, dec, dist, mag, absmag, x, y, z, vx, vy, vz, spectrum = line.split(",")
data.append({"ra": float(ra), "dec": float(dec), "dist": float(dist), "mag": float(mag), "absmag": float(absmag), "x": float(x), "y": float(y), "z": float(z), "vx": float(vx), "vy": float(vy), "vz": float(vz)})
i = 0
startTime = time.time()
for datum in data:
engine.action(datum)
i += 1
if i % 5000 == 0:
print "{0}, {1}".format(time.time() - startTime, i)
示例6: testKMeansTransform
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testKMeansTransform(self):
random.seed(12345)
numpy.seterr(divide="ignore", invalid="ignore")
dataset = numpy.empty((100000, 3), dtype=numpy.dtype(float))
for i, (x, y, z) in enumerate(TestProducerKMeans.data([1, 1, 1], [3, 2, 5], [8, 2, 7], [5, 8, 5], [1, 1, 9])):
if i >= dataset.shape[0]:
break
dataset[i,:] = [x * 10.0, y * 20.0, z * 30.0]
trans = Transformation("x/10.0", "y/20.0", "z/30.0")
kmeans = KMeans(5, trans.transform(dataset, ["x", "y", "z"]))
kmeans.optimize(whileall(moving(), maxIterations(1000)))
centers = kmeans.centers()
self.assertArrayAlmostEqual(centers[0], [1.00, 1.01, 1.00], places=1)
self.assertArrayAlmostEqual(centers[1], [1.01, 1.00, 9.01], places=1)
self.assertArrayAlmostEqual(centers[2], [3.01, 2.01, 5.00], places=1)
self.assertArrayAlmostEqual(centers[3], [4.99, 8.00, 4.99], places=1)
self.assertArrayAlmostEqual(centers[4], [8.02, 2.00, 7.01], places=1)
doc = kmeans.pfaDocument("Cluster",
["one", "two", "three", "four", "five"],
preprocess=trans.new(AvroArray(AvroDouble()),
x="input[0]", y="input[1]", z="input[2]"))
# look(doc, maxDepth=10)
self.assertArrayAlmostEqual(doc["cells"]["clusters"]["init"][0]["center"], [1.00, 1.01, 1.00], places=2)
self.assertArrayAlmostEqual(doc["cells"]["clusters"]["init"][1]["center"], [1.01, 1.00, 9.01], places=2)
self.assertArrayAlmostEqual(doc["cells"]["clusters"]["init"][2]["center"], [3.01, 2.01, 5.00], places=2)
self.assertArrayAlmostEqual(doc["cells"]["clusters"]["init"][3]["center"], [4.99, 8.00, 4.99], places=2)
self.assertArrayAlmostEqual(doc["cells"]["clusters"]["init"][4]["center"], [8.02, 2.00, 7.01], places=2)
engine, = PFAEngine.fromJson(doc)
self.assertEqual(engine.action([1.00 * 10, 1.01 * 20, 1.00 * 30]), "one")
self.assertEqual(engine.action([1.01 * 10, 1.00 * 20, 9.01 * 30]), "two")
self.assertEqual(engine.action([3.01 * 10, 2.01 * 20, 5.00 * 30]), "three")
self.assertEqual(engine.action([4.99 * 10, 8.00 * 20, 4.99 * 30]), "four")
self.assertEqual(engine.action([8.02 * 10, 2.00 * 20, 7.01 * 30]), "five")
示例7: testSimpleKMeansEmitExample
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testSimpleKMeansEmitExample(self):
# the emit method allows us to ignore the "else" clause in ifnotnull
pfaDocument = titus.prettypfa.jsonNode('''
input: <<INPUT>>
output: string
method: emit
cells:
clusters(array(record(id: string, center: array(double)))) = []
action:
ifnotnull(mag: input.mag, dist: input.dist, mass: input.mass, radius: input.radius)
emit(model.cluster.closest(new(array(double), mag, dist, mass, radius),
clusters,
metric.simpleEuclidean)["id"])
'''.replace("<<INPUT>>", open("test/prettypfa/exoplanetsSchema.ppfa").read()))
if self.kmeansResult is None: self.doKmeans()
pfaDocument["cells"]["clusters"]["init"] = self.kmeansResult.pfaValue(self.clusterNames)
engine, = PFAEngine.fromJson(pfaDocument)
self.runEngine(engine)
示例8: testSimpleKMeansWithEnums
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testSimpleKMeansWithEnums(self):
# same as the above using enums rather than strings and compacted a bit
pfaDocument = titus.prettypfa.jsonNode('''
input: <<INPUT>>
output: enum([cluster0, cluster1, cluster2, cluster3, cluster4, MISSING], ClusterId)
cells:
clusters(array(record(id: ClusterId, center: array(double)))) = []
action:
ifnotnull(mag: input.mag, dist: input.dist, mass: input.mass, radius: input.radius)
model.cluster.closest(new(array(double), mag, dist, mass, radius),
clusters,
metric.simpleEuclidean)["id"]
else
[email protected]
'''.replace("<<INPUT>>", open("test/prettypfa/exoplanetsSchema.ppfa").read()))
if self.kmeansResult is None: self.doKmeans()
pfaDocument["cells"]["clusters"]["init"] = self.kmeansResult.pfaValue(self.clusterNames)
engine, = PFAEngine.fromJson(pfaDocument)
self.runEngine(engine)
示例9: testPopulationOfClosestCluster
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testPopulationOfClosestCluster(self):
# now that the ifnotnull clause has become three lines long, notice that it needs to be
# surrounded by curly brackets and expressions must be separated by semicolons
# (the last semicolon is optional: they're delimiters, not line terminators)
pfaDocument = titus.prettypfa.jsonNode('''
input: <<INPUT>>
output: union(int, null)
cells:
clusters(array(record(id: string, center: array(double), population: int))) = []
action:
ifnotnull(mag: input.mag, dist: input.dist, mass: input.mass, radius: input.radius)
model.cluster.closest(new(array(double), mag, dist, mass, radius),
clusters,
metric.simpleEuclidean)["population"]
else
null
'''.replace("<<INPUT>>", open("test/prettypfa/exoplanetsSchema.ppfa").read()))
if self.kmeansResult is None: self.doKmeans()
pfaDocument["cells"]["clusters"]["init"] = self.kmeansResult.pfaValue(self.clusterNames, populations=True)
engine, = PFAEngine.fromJson(pfaDocument)
self.runEngine(engine)
示例10: testHistogram2d
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testHistogram2d(self):
pfaDocument = titus.prettypfa.jsonNode('''
input: <<INPUT>>
output: Histogram
cells:
histogram(record(Histogram,
xnumbins: int,
xlow: double,
xhigh: double,
ynumbins: int,
ylow: double,
yhigh: double,
values: array(array(double)))) = {
xnumbins: 10, xlow: 0.0, xhigh: 3.0,
ynumbins: 10, ylow: 0.0, yhigh: 3.0,
values: [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]}
method: emit
action:
ifnotnull(mass: input.mass, radius: input.radius)
emit(histogram to fcn(old: Histogram -> Histogram)
stat.sample.fillHistogram2d(mass, radius, 1.0, old))
'''.replace("<<INPUT>>", open("test/prettypfa/exoplanetsSchema.ppfa").read()), check=False, lineNumbers=False)
engine, = PFAEngine.fromJson(pfaDocument)
self.assertEqual(self.runEngine(engine), {"values": [[6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [3.0, 33.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 8.0, 118.0, 28.0, 6.0, 0.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 33.0, 184.0, 72.0, 25.0, 8.0, 4.0, 0.0, 1.0], [0.0, 0.0, 1.0, 12.0, 45.0, 34.0, 20.0, 3.0, 4.0, 1.0], [0.0, 0.0, 0.0, 1.0, 1.0, 4.0, 4.0, 4.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], "xhigh": 3.0, "yhigh": 3.0, "ynumbins": 10, "xnumbins": 10, "ylow": 0.0, "xlow": 0.0})
示例11: testKMeans
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testKMeans(self):
random.seed(12345)
numpy.seterr(divide="ignore", invalid="ignore")
dataset = numpy.empty((100000, 3), dtype=numpy.dtype(float))
for i, x in enumerate(TestProducerKMeans.data([1, 1, 1], [3, 2, 5], [8, 2, 7], [5, 8, 5], [1, 1, 9])):
if i >= dataset.shape[0]:
break
dataset[i,:] = x
kmeans = KMeans(5, dataset)
kmeans.optimize(whileall(moving(), maxIterations(1000)))
centers = kmeans.centers()
self.assertArrayAlmostEqual(centers[0], [1.00, 1.01, 1.00], places=2)
self.assertArrayAlmostEqual(centers[1], [1.01, 1.00, 9.01], places=2)
self.assertArrayAlmostEqual(centers[2], [3.01, 2.01, 5.00], places=2)
self.assertArrayAlmostEqual(centers[3], [4.99, 8.00, 4.99], places=2)
self.assertArrayAlmostEqual(centers[4], [8.02, 2.00, 7.01], places=2)
doc = kmeans.pfaDocument("Cluster", ["one", "two", "three", "four", "five"])
# look(doc, maxDepth=8)
self.assertArrayAlmostEqual(doc["cells"]["clusters"]["init"][0]["center"], [1.00, 1.01, 1.00], places=2)
self.assertArrayAlmostEqual(doc["cells"]["clusters"]["init"][1]["center"], [1.01, 1.00, 9.01], places=2)
self.assertArrayAlmostEqual(doc["cells"]["clusters"]["init"][2]["center"], [3.01, 2.01, 5.00], places=2)
self.assertArrayAlmostEqual(doc["cells"]["clusters"]["init"][3]["center"], [4.99, 8.00, 4.99], places=2)
self.assertArrayAlmostEqual(doc["cells"]["clusters"]["init"][4]["center"], [8.02, 2.00, 7.01], places=2)
engine, = PFAEngine.fromJson(doc)
self.assertEqual(engine.action([1.00, 1.01, 1.00]), "one")
self.assertEqual(engine.action([1.01, 1.00, 9.01]), "two")
self.assertEqual(engine.action([3.01, 2.01, 5.00]), "three")
self.assertEqual(engine.action([4.99, 8.00, 4.99]), "four")
self.assertEqual(engine.action([8.02, 2.00, 7.01]), "five")
示例12: errors
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
inputFile, = sys.argv[1:]
# Failures that I'm giving up on:
#
# prob.dist.binomialQF({"p": 0.99999, "prob": 1e-05, "size": 1}) should be 1, is 0 (rounding in count)
# {"p": 0.9, "prob": 0.1, "size": 1} should be 1, is 0 (same reason)
# prob.dist.hypergeometricPDF \
# prob.dist.hypergeometricCDF } many errors! and the QF has a long or infinite loop
# prob.dist.hypergeometricQF /
# prob.dist.negativeBinomialPDF({"x": 17, "prob": 0.9, "size": 100}) should be 0.00245, is 0.02715
# {"x": 100, "prob": 0.1, "size": 17} should be 0.00245, is 0.00462
# {"x": 100, "prob": 0.5, "size": 100} should be 5.7e42, is 0.02817
# prob.dist.negativeBinomialQF has many errors (though not as many as the hypergeometric)
for counter, example in enumerate(getExamples(open(inputFile))):
engine, = PFAEngine.fromJson(example["engine"])
if example["function"] in ("prob.dist.binomialQF", "prob.dist.hypergeometricPDF", "prob.dist.hypergeometricCDF", "prob.dist.hypergeometricQF", "prob.dist.negativeBinomialPDF", "prob.dist.negativeBinomialQF"):
continue
functionWritten = False
def maybeWriteFunction(functionWritten):
if not functionWritten:
print "%4d %-20s%s" % (counter + 1, example["function"], json.dumps(example["engine"]))
return True
for trial in example["trials"]:
trialWritten = False
try:
result = {"success": convertOut(engine.action(trial["sample"]), engine.outputType.jsonNode(set()), dobase64=True)}
except PFARuntimeException as err:
示例13: testNormalized
# 需要导入模块: from titus.genpy import PFAEngine [as 别名]
# 或者: from titus.genpy.PFAEngine import fromJson [as 别名]
def testNormalized(self):
# for k-means on normalized data, we have to explicitly normalize,
# re-compute the clusters, and put the same transformation into PFA
# get a dataset for the k-means generator
dataset = []
for record in DataFileReader(open("test/prettypfa/exoplanets.avro", "r"), DatumReader()):
mag, dist, mass, radius = record.get("mag"), record.get("dist"), record.get("mass"), record.get("radius")
if mag is not None and dist is not None and mass is not None and radius is not None:
dataset.append([mag, dist, mass, radius])
dataset = numpy.array(dataset)
# compute the normalization (1st to 99th percentile instead of strict min/max)
maglow, maghigh = numpy.percentile(dataset[:,0], [1, 99])
distlow, disthigh = numpy.percentile(dataset[:,1], [1, 99])
masslow, masshigh = numpy.percentile(dataset[:,2], [1, 99])
radiuslow, radiushigh = numpy.percentile(dataset[:,3], [1, 99])
# transform the data
normalized = numpy.empty_like(dataset)
normalized[:,0] = (dataset[:,0] - maglow) / (maghigh - maglow)
normalized[:,1] = (dataset[:,1] - distlow) / (disthigh - distlow)
normalized[:,2] = (dataset[:,2] - masslow) / (masshigh - masslow)
normalized[:,3] = (dataset[:,3] - radiuslow) / (radiushigh - radiuslow)
# set up and run the k-means generator
kmeansResult = KMeans(len(self.clusterNames), normalized)
kmeansResult.optimize(whileall(moving(), maxIterations(1000)))
# put the transformation into PFA by string replacement
# this re.subs will replace French quotes (<< >>) with Python variable values
inputSchema = open("test/prettypfa/exoplanetsSchema.ppfa").read()
namesToSubstitute = locals()
pfaDocument = titus.prettypfa.jsonNode(
re.sub("<<[A-Za-z0-9]+>>",
lambda x: str(namesToSubstitute[x.group().lstrip("<<").rstrip(">>")]),
'''
input: <<inputSchema>>
output: string
cells:
clusters(array(record(id: string, center: array(double)))) = []
action:
ifnotnull(mag: input.mag, dist: input.dist, mass: input.mass, radius: input.radius) {
var normmag = (mag - <<maglow>>) / (<<maghigh>> - <<maglow>>);
var normdist = (dist - <<distlow>>) / (<<disthigh>> - <<distlow>>);
var normmass = (mass - <<masslow>>) / (<<masshigh>> - <<masslow>>);
var normradius = (radius - <<radiuslow>>) / (<<radiushigh>> - <<radiuslow>>);
model.cluster.closest(new(array(double), normmag, normdist, normmass, normradius),
clusters,
metric.simpleEuclidean)["id"]
}
else
"MISSING"
'''))
# now put the clusters in and run the scoring engine
pfaDocument["cells"]["clusters"]["init"] = kmeansResult.pfaValue(self.clusterNames)
engine, = PFAEngine.fromJson(pfaDocument)
self.runEngine(engine)