Tensorflow.js数据格式化/整形



我在txt文件中有大约1397个样本数据:

(x, y)
907 1 < first point (x,y)
907 1 < second point 
908 3
910 4
911 6
913 8
914 10
916 12
917 13
919 15
920 17
922 18 ...
...

我想得到第一个16点,我的第17点作为这16点的输出预测数据,以此类推…

例如,我想要前16个点,下一个点是输出数据。而且,我想在接下来的1000个样本点上继续这样做。

我应该如何在Tensorflow.js中格式化它们?

tldr:我在一个.txt文件中有1000多个样本数据(x,y(点。我想得到第一个16分作为我的输入,下一个点作为我的预测,我想为下一个1000分做这件事。我应该如何在tensorflowjs中格式化它们?

步骤:

  1. 导入数据
  2. 将其解析为对象数组
  3. 为模型准备数据

1.导入数据

由于没有关于你是否在浏览器或nodejs中工作的信息,我将跳过细节,但按照的思路进行

  • 浏览器:fetch("url/to/text/file.txt")
  • NodeJS:readFileSync("path/to/file.text")

2.分析数据

例如,将数据导入一个名为text的变量后

const originalText = ... // However you get your data
const dataPoints = originalText
.split("n") // returns an array where each line is a value
.map(line => {
return line.split(" ") // returns an array where 0 is the x and 1 is the y
}) 

3.准备数据

const history = 16 // You said you wanted 16 previous points of data, 
let xs = []
let ys = []

for (let i = history; i < dataPoints.length; i++){
let currentY = dataPoints[i][1]  // "i" is current point, "1" is because y is at position 1 in the array
let currentXs = []
// Loop back to previous data points to get all the Xs you want
for(let j = history - 1; i >= 0; i==){
currentXs.push(dataPoints[j][0]) // "0" because x is at position 0 in the array
}
xs.push(currentXs)
ys.push(currentY)
}

最新更新