Repository navigation
Expand file tree
/
Copy pathtrainAndPredict.js
More file actions
46 lines (33 loc) · 1.74 KB
/
Copy pathtrainAndPredict.js
File metadata and controls
46 lines (33 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const tf = require('@tensorflow/tfjs');
async function trainAndPredict(timeAndPriceData, newTimestamp) {
// Extract timestamps and prices from the data
const timestamps = timeAndPriceData.map(([timestamp, price]) => timestamp);
const prices = timeAndPriceData.map(([timestamp, price]) => price);
// Normalize and scale the data
const minTimestamp = Math.min(...timestamps);
const maxTimestamp = Math.max(...timestamps);
const minPrice = Math.min(...prices);
const maxPrice = Math.max(...prices);
const normalizedTimestamps = timestamps.map(ts => (ts - minTimestamp) / (maxTimestamp - minTimestamp));
const normalizedPrices = prices.map(price => (price - minPrice) / (maxPrice - minPrice));
// Convert data to TensorFlow tensors
const X = tf.tensor1d(normalizedTimestamps);
const y = tf.tensor1d(normalizedPrices);
// Create a simple linear regression model
const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [1] }));
model.add(tf.layers.dense({ units: 1, inputShape: [1] }));
model.add(tf.layers.dense({ units: 1, inputShape: [1] }));
// Compile the model
model.compile({ optimizer: 'sgd', loss: 'meanSquaredError' });
// Train the model
await model.fit(X, y, { epochs: 100 });
// Normalize the new timestamp for prediction
const normalizedNewTimestamp = (newTimestamp - minTimestamp) / (maxTimestamp - minTimestamp);
// Predict the normalized price for the new timestamp
const normalizedPredictedPrice = model.predict(tf.tensor1d([normalizedNewTimestamp]));
// Scale the predicted price back to the original range
const predictedPrice = normalizedPredictedPrice.mul(maxPrice - minPrice).add(minPrice);
return predictedPrice.dataSync()[0];
}
module.exports = trainAndPredict;