pip install nupic from nupic.encoders import RandomDistributedScalarEncoder from nupic.algorithms import anomaly_likelihood from nupic.algorithms import htm_prediction from nupic.algorithms import spatial_pooler from nupic.algorithms import temporal_memory from nupic.algorithms import anomaly from nupic.data.plotter import Plotter inputData = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2] sp = spatial_pooler.SpatialPooler() enc = RandomDistributedScalarEncoder() spatialInput = enc.encode(inputData) sp.compute(spatialInput, learn=True) tm = temporal_memory.TemporalMemory() anomaly_likelihood = anomaly_likelihood.AnomalyLikelihood() for step in range(len(inputData)): spatialInput = enc.encode(inputData[step]) predictedActiveCells = htm_prediction.Prediction.getMostProbableCells() predictive = len(predictedActiveCells) > 0 tm.compute(predictive, activeColumns) anomaly_score = anomaly_likelihood.anomaly anomaly_likelihood.anomaly = anomaly_score if anomaly_score > 0.5: print("Anomaly detected at step", step) predictedValue = tm.infer() print("Predicted value at step", step, ":", predictedValue) steps = list(range(len(inputData))) Plotter.plot(data={"Input Data": inputData}, steps=steps) Plotter.plot(data={"Predicted Value": predictedValues}, steps=steps)


上一篇:
下一篇:
切换中文