Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loaded Model - TypeError: Cannot read properties of undefined (reading 'transform') #7

Open
alasdairbarrie opened this issue May 31, 2022 · 1 comment

Comments

@alasdairbarrie
Copy link

TypeError: Cannot read properties of undefined (reading 'transform')
    at RandomForestClassifier.predict (/Users/alasdairbarrie/tradingBots/fantomSimulator/node_modules/random-forest/src/base.js:66:25)
    at module.exports (/Users/alasdairbarrie/tradingBots/fantomSimulator/fun/randomForestClassifier.js:218:32)
    at module.exports (/Users/alasdairbarrie/tradingBots/fantomSimulator/fun/strategyBuilder.js:312:19)
    at module.exports (/Users/alasdairbarrie/tradingBots/fantomSimulator/fun/simulator.js:28:28)
    at signals (/Users/alasdairbarrie/tradingBots/fantomSimulator/index.js:39:11)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
TypeError: Cannot read properties of undefined (reading 'length')
TypeError: Cannot read properties of undefined (reading 'length')

My Data:
I am using BTCUSDT price data for this. I have added technical indicator features and removed the OHLCV.

I am using Danfo.js (js version of Pandas) to convert my data into a dataframe so its easier to manipulate it. It is then extracted back into data arrays before being used in the model.

The same dataset that was used to train the saved model is now being using on the loaded model...just a different section of the time series.

My Code:

    /* ------------------------------ Define the train and test samples ----------------------------- */
    let XTrain_Cols=[], XTest_Cols=[], yTrain_Cols=[], yTest_Cols=[];
    XTrain_Cols = traindf.loc({ columns: ["ema0","ema1","rsi2","stochk0","stochd0","williamsR0","xBarHigh","xBarLow","historicalReturn1","historicalReturn5","distanceFromEma0","distanceFromEma1"]})['$data']; //
    XTest_Cols = testdf.loc({ columns: ["ema0","ema1","rsi2","stochk0","stochd0","williamsR0","xBarHigh","xBarLow","historicalReturn1","historicalReturn5","distanceFromEma0","distanceFromEma1"]})['$data']; //
    yTrain_Cols = traindf['Prediction']['$data'];
    yTest_Cols = testdf['Prediction']['$data'];
    

    /* -------------------------- Define new random forest classifier model ------------------------- */
    const rfClassifier = new RandomForestClassifier({nEstimators: 400, maxDepth: 20, maxFeatures: 'auto', minSamplesLeaf: 10, minInfoGain: 0});

    /* -------------------------------- Load a previously saved model ------------------------------- */
    var modelName = `./models/rf-BTCUSDT-tf60m-acc0.8603871361073007-epoch1653977716111-ema90-ema140-rsi2-stochk9-stochd3-willr10-pb5-histret1-histret5-forret3-target0.02.model`
    const modelLoaded = new Uint8Array(fs.readFileSync(modelName));
    rfClassifier.load(modelLoaded);

    /* --------------------------------------- Run predictions -------------------------------------- */
    var yPred = rfClassifier.predict(XTest_Cols);

Behaviour:
Everything works completely fine when I save and load the model in the same run. It's when I dont train the model first and just try to load in the saved model I get this error.

@chrisgdt
Copy link

chrisgdt commented Nov 16, 2022

You can add these lines as temporary fix :

const {Encoder} = require("random-forest/src/util");

// ...

/* -------------------------------- Load a previously saved model ------------------------------- */
var modelName = `./models/rf-BTCUSDT-tf60m-acc0.8603871361073007-epoch1653977716111-ema90-ema140-rsi2-stochk9-stochd3-willr10-pb5-histret1-histret5-forret3-target0.02.model`
const modelLoaded = new Uint8Array(fs.readFileSync(modelName));
rfClassifier.load(modelLoaded);
rfClassifier.Xencoder = new Encoder();
rfClassifier.yencoder = new Encoder();

// ...

Currently, Xencoder and yencoder are only initialized in train function, so if you never call it these fields will be undefined. I think the issue can be resolved if these fields are also initialized in constructor (and not in train anymore ?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants