本文整理匯總了Python中nupic.frameworks.opf.predictionmetricsmanager.MetricsManager類的典型用法代碼示例。如果您正苦於以下問題:Python MetricsManager類的具體用法?Python MetricsManager怎麽用?Python MetricsManager使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了MetricsManager類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: runHotgym
def runHotgym():
model = createModel()
model.enableInference({'predictedField': 'consumption'})
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
with open (_INPUT_FILE_PATH) as fin:
reader = csv.reader(fin)
headers = reader.next()
reader.next()
reader.next()
for i, record in enumerate(reader, start=1):
modelInput = dict(zip(headers, record))
modelInput["consumption"] = float(modelInput["consumption"])
modelInput["timestamp"] = datetime.datetime.strptime(
modelInput["timestamp"], "%m/%d/%y %H:%M")
result = model.run(modelInput)
result.metrics = metricsManager.update(result)
isLast = i == _NUM_RECORDS
if i % 100 == 0 or isLast:
_LOGGER.info("After %i records, 1-step altMAPE=%f", i,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=consumption"])
if isLast:
break
示例2: runIoThroughNupic
def runIoThroughNupic(inputData, model, name, plot):
inputFile = open(inputData, "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
if len(plot) == 0:
for field in SWARM_DESCRIPTION["includedFields"]:
plot.append(field["fieldName"])
output = nupic_output.NuPICFileOutput(name, plot)
else:
output = nupic_output.NuPICPlotOutput(name, plot)
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
counter = 0
for row in csvReader:
counter += 1
data = {}
fldCounter = 0
for field in SWARM_DESCRIPTION["includedFields"]:
data[field["fieldName"]] = translate_data(field["fieldType"], row[fldCounter])
fldCounter += 1
result = model.run(data)
result.metrics = metricsManager.update(result)
if options.verbose is not None and counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step altMAPE=%f" % (counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field="+PREDICTED_FIELD]))
if plot:
result = shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
vals = []
for field in plot:
vals.append(data[field])
output.write(vals, prediction)
inputFile.close()
output.close()
示例3: __init__
def __init__(self, model, inputs):
super(HotGym, self).__init__(model)
self.model = model
self.inputs = inputs
self.metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
self.counter = 0
self.timestampStr = ""
self.consumptionStr = ""
self.shifter = InferenceShifter()
self.dates = deque(maxlen=WINDOW)
self.convertedDates = deque(maxlen=WINDOW)
self.actualValues = deque([0.0] * WINDOW, maxlen=WINDOW)
self.predictedValues = deque([0.0] * WINDOW, maxlen=WINDOW)
self.linesInitialized = False
self.actualLines = None
self.predictedLines = None
self.shouldScheduleDraw = True
fig = plt.figure(figsize=(6.1, 11))
plotCount = 1
gs = gridspec.GridSpec(plotCount, 1)
self.graph = fig.add_subplot(gs[0, 0])
plt.xlabel('Date')
plt.ylabel('Consumption (kW)')
plt.tight_layout()
示例4: HotGym
class HotGym(CLASanityModel):
def __init__(self, model, inputs):
super(HotGym, self).__init__(model)
self.model = model
self.inputs = inputs
self.metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(), model.getInferenceType())
self.counter = 0
self.timestampStr = ""
self.consumptionStr = ""
def step(self):
self.counter += 1
self.timestampStr, self.consumptionStr = self.inputs.next()
timestamp = datetime.datetime.strptime(self.timestampStr, "%m/%d/%y %H:%M")
consumption = float(self.consumptionStr)
result = self.model.run({"timestamp": timestamp, "kw_energy_consumption": consumption})
result.metrics = self.metricsManager.update(result)
if self.counter % 100 == 0:
print "Read %i lines..." % self.counter
print (
"After %i records, 1-step altMAPE=%f"
% (
self.counter,
result.metrics[
"multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=kw_energy_consumption"
],
)
)
def getInputDisplayText(self):
return (["time", self.timestampStr], ["power consumption (kW)", self.consumptionStr])
示例5: runCPU
def runCPU():
"""Poll CPU usage, make predictions, and plot the results. Runs forever."""
# Create the model for predicting CPU usage.
model = ModelFactory.create(model_params.MODEL_PARAMS)
model.enableInference({'predictedField': 'cpu'})
# Create a metrics manager for computing an error metric.
metricsManager = MetricsManager(METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
# The shifter will align prediction and actual values.
shifter = InferenceShifter()
# Keep the last WINDOW predicted and actual values for plotting.
actHistory = deque([0.0] * WINDOW, maxlen=60)
predHistory = deque([0.0] * WINDOW, maxlen=60)
# Initialize the plot lines that we will update with each new record.
actline, = plot(range(WINDOW), actHistory)
predline, = plot(range(WINDOW), predHistory)
# Set the y-axis range.
actline.axes.set_ylim(0, 100)
predline.axes.set_ylim(0, 100)
while True:
s = time.time()
# Get the CPU usage.
cpu = psutil.cpu_percent()
# Run the input through the model and shift the resulting prediction.
modelInput = {'cpu': cpu}
result = shifter.shift(model.run(modelInput))
# Compute an error metric (not currently used).
result.metrics = metricsManager.update(result)
# Update the trailing predicted and actual value deques.
inference = result.inferences['multiStepBestPredictions'][5]
if inference is not None:
actHistory.append(result.rawInput['cpu'])
predHistory.append(inference)
# Redraw the chart with the new data.
actline.set_ydata(actHistory) # update the data
predline.set_ydata(predHistory) # update the data
draw()
# Make sure we wait a total of 2 seconds per iteration.
time.sleep(SECONDS_PER_STEP - (time.time() - s))
示例6: __init__
def __init__(self, model, inputs):
super(HotGym, self).__init__(model)
self.model = model
self.inputs = inputs
self.metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(), model.getInferenceType())
self.counter = 0
self.timestampStr = ""
self.consumptionStr = ""
示例7: runIoThroughNupic
def runIoThroughNupic(inputData, model, gymName, plot):
inputFile = open(inputData, "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
if plot:
output = nupic_output.NuPICPlotOutput([gymName])
else:
output = nupic_output.NuPICFileOutput([gymName])
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
counter = 0
for row in csvReader:
counter += 1
timestamp = datetime.datetime.strptime(row[0], DATE_FORMAT)
consumption = float(row[1])
result = model.run({
"timestamp": timestamp,
"kw_energy_consumption": consumption
})
result.metrics = metricsManager.update(result)
if counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step altMAPE=%f" % (counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=kw_energy_consumption"]))
if plot:
result = shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
output.write([timestamp], [consumption], [prediction])
if plot and counter % 20 == 0:
output.refreshGUI()
inputFile.close()
output.close()
示例8: runHotgym
def runHotgym():
model = createModel()
model.enableInference({'predictedField': 'Volume'})
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
with open (findDataset(_DATA_PATH)) as fin:
reader = csv.reader(fin)
headers = reader.next()
for i, record in enumerate(reader, start=1):
modelInput = dict(zip(headers, record))
modelInput["Volume"] = float(modelInput["Volume"])
result = model.run(modelInput)
result.metrics = metricsManager.update(result)
isLast = i == _NUM_RECORDS
if i % 100 == 0 or isLast:
print result.metrics
if isLast:
break
示例9: runIoThroughNupic
def runIoThroughNupic(inputData, model, modelName, plot):
inputFile = open(inputData, "rb")
csvReader = csv.reader(inputFile)
# skip header rows
headers = csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
if plot:
output = nupic_anomaly_output.NuPICPlotOutput(modelName)
else:
output = nupic_anomaly_output.NuPICFileOutput(modelName)
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
counter = 0
for row in csvReader:
counter += 1
timestamp = datetime.datetime.strptime(row[0], DATE_FORMAT)
consumption = float(row[1])
result = model.run({
"Time": timestamp,
PREDICTED_FIELD: consumption
})
result.metrics = metricsManager.update(result)
if counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step altMAPE=%f", counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=%s" % PREDICTED_FIELD])
if plot:
result = shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
anomalyScore = result.inferences["anomalyScore"]
output.write(timestamp, consumption, prediction, anomalyScore)
inputFile.close()
output.close()
示例10: runIoThroughNupic
def runIoThroughNupic( inputData, model, textName, word_list ):
inputFile = open(inputData, "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
output = nupic_output.NuPICFileOutput([textName])
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
model.enableLearning()
counter = 0
for row in csvReader:
counter += 1
reset_flag = bool( row[0] )
word_num = int( row[1] )
if reset_flag:
print( 'resetting model' )
model.resetSequenceStates()
result = model.run({
"word_num": word_num
})
result.metrics = metricsManager.update(result)
if counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step altMAPE=%f", counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=word_num"])
model.finishLearning()
model.save( MODEL_DIR )
inputFile.close()
output.close()
示例11: runIoThroughNupic
def runIoThroughNupic(inputData, model, dataName, plot):
inputFile = open(inputData, "rU")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvReader.next()
csvReader.next()
shifter = InferenceShifter()
if plot:
output = nupic_output.NuPICPlotOutput([dataName])
else:
output = nupic_output.NuPICFileOutput([dataName])
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
counter = 0
for row in csvReader:
counter += 1
y = float(row[0])
result = model.run({
"y": y
})
result.metrics = metricsManager.update(result)
if counter % 100 == 0:
print "Read %i lines..." % counter
print ("After %i records, 1-step nrmse=%f", counter,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='nrmse':steps=1:window=1000000:"
"field=y"])
if plot:
result = shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
output.write([0], [y], [prediction])
inputFile.close()
output.close()
示例12: runHotgym
def runHotgym():
model = createModel()
model.enableInference({'predictedField': 'consumption'})
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
with open (findDataset(_DATA_PATH)) as fin:
reader = csv.reader(fin)
headers = reader.next()
print headers[0], headers[1]
record = reader.next()
print record
record = reader.next()
print record
record1 = reader.next()
timestamp1 = time.mktime(datetime.datetime.strptime(record1[0], "%m/%d/%y %H:%M").timetuple())
print timestamp1
record2 = reader.next()
timestamp2 = time.mktime(datetime.datetime.strptime(record2[0], "%m/%d/%y %H:%M").timetuple())
print record2[0], record2[1]
print timestamp2-timestamp1
for i, record in enumerate(reader, start=1):
modelInput = dict(zip(headers, record))
modelInput["consumption"] = float(modelInput["consumption"])
modelInput["timestamp"] = datetime.datetime.strptime(
modelInput["timestamp"], "%m/%d/%y %H:%M")
result = model.run(modelInput)
#print result
#import sys; sys.exit()
result.metrics = metricsManager.update(result)
isLast = i == _NUM_RECORDS
if i % 10 == 0 or isLast:
_LOGGER.info("After %i records, 1-step altMAPE=%f", i,
result.metrics["multiStepBestPredictions:multiStep:"
"errorMetric='altMAPE':steps=1:window=1000:"
"field=consumption"])
if isLast:
break
示例13: init_metrics
def init_metrics(self):
metric_list = []
# create a MetricSpec for each level of inference
for i in self.steps:
metric_list.append(
MetricSpec(field='value',
metric='multiStep',
inferenceElement='multiStepBestPredictions',
params={'errorMetric': 'altMAPE',
'window': 384,
'steps': i
}
)
)
# convert specs to tuple and initialize metrics manager
metric_specs = tuple(metric_list)
self.metrics_manager = MetricsManager(metric_specs,
self.model.getFieldInfo(),
self.model.getInferenceType())
示例14: __init__
def __init__(self, modelConfig, inferenceArgs, metricSpecs, sourceSpec,
sinkSpec=None):
"""Initialization.
Args:
modelConfig: The model config dict.
metricSpecs: A sequence of MetricSpec instances.
sourceSpec: Path to the source CSV file.
sinkSpec: Path to the sink CSV file.
"""
self.model = ModelFactory.create(modelConfig)
self.model.enableInference(inferenceArgs)
self.metricsManager = MetricsManager(metricSpecs, self.model.getFieldInfo(),
self.model.getInferenceType())
self.sink = None
if sinkSpec is not None:
# TODO: make this work - sinkSpec not yet supported.
raise NotImplementedError('The sinkSpec is not yet implemented.')
#self.sink = BasicPredictionLogger(
# self.model.getFieldInfo(), sinkSpec, 'myOutput',
# self.model.getInferenceType())
#self.sink.setLoggedMetrics(
# self.metricsManager.getMetricLabels())
self.datasetReader = BasicDatasetReader(sourceSpec)
示例15: printTPRegionParams
print model._spLearningEnabled
printTPRegionParams(model._getTPRegion())
inputData = "%s/%s.csv" % (DATA_DIR, dataSet.replace(" ", "_"))
sensor = model._getSensorRegion()
encoderList = sensor.getSelf().encoder.getEncoderList()
if sensor.getSelf().disabledEncoder is not None:
classifier_encoder = sensor.getSelf().disabledEncoder.getEncoderList()
classifier_encoder = classifier_encoder[0]
else:
classifier_encoder = None
_METRIC_SPECS = getMetricSpecs(predictedField, stepsAhead=_options.stepsAhead)
metric = metrics.getModule(_METRIC_SPECS[0])
metricsManager = MetricsManager(_METRIC_SPECS, model.getFieldInfo(),
model.getInferenceType())
if plot:
plotCount = 1
plotHeight = max(plotCount * 3, 6)
fig = plt.figure(figsize=(14, plotHeight))
gs = gridspec.GridSpec(plotCount, 1)
plt.title(predictedField)
plt.ylabel('Data')
plt.xlabel('Timed')
plt.tight_layout()
plt.ion()
print "Load dataset: ", dataSet
df = pd.read_csv(inputData, header=0, skiprows=[1, 2])