|
| 1 | +import { extractFeatures, Sample, splitSamplesIntoTrainingValidationTestForBinaryClassification } from '@ronas-it/tfjs-node-helpers'; |
| 2 | +import { AgeFeatureExtractor } from '../feature-extractors/age'; |
| 3 | +import { AnnualSalaryFeatureExtractor } from '../feature-extractors/annual-salary'; |
| 4 | +import { GenderFeatureExtractor } from '../feature-extractors/gender'; |
| 5 | +import { OwnsTheCarFeatureExtractor } from '../feature-extractors/owns-the-car'; |
| 6 | +import dataset from '../../assets/data.json'; |
| 7 | + |
| 8 | +export class TrainingDataService { |
| 9 | + private simulatedDelayMs: number; |
| 10 | + private trainingSamples: Array<Sample>; |
| 11 | + private validationSamples: Array<Sample>; |
| 12 | + private testingSamples: Array<Sample>; |
| 13 | + |
| 14 | + constructor({ simulatedDelayMs }: { simulatedDelayMs: number }) { |
| 15 | + this.simulatedDelayMs = simulatedDelayMs; |
| 16 | + } |
| 17 | + |
| 18 | + public async initialize(): Promise<void> { |
| 19 | + const samples = await extractFeatures({ |
| 20 | + data: dataset, |
| 21 | + inputFeatureExtractors: [ |
| 22 | + new AgeFeatureExtractor(), |
| 23 | + new AnnualSalaryFeatureExtractor(), |
| 24 | + new GenderFeatureExtractor() |
| 25 | + ], |
| 26 | + outputFeatureExtractor: new OwnsTheCarFeatureExtractor() |
| 27 | + }); |
| 28 | + |
| 29 | + const { trainingSamples, validationSamples, testingSamples } = splitSamplesIntoTrainingValidationTestForBinaryClassification(samples); |
| 30 | + |
| 31 | + this.trainingSamples = trainingSamples; |
| 32 | + this.validationSamples = validationSamples; |
| 33 | + this.testingSamples = testingSamples; |
| 34 | + } |
| 35 | + |
| 36 | + public async getTrainingSamples(skip: number, take: number): Promise<Array<Sample>> { |
| 37 | + await this.simulateDelay(); |
| 38 | + |
| 39 | + return this.trainingSamples.slice(skip, skip + take); |
| 40 | + } |
| 41 | + |
| 42 | + public async getValidationSamples(skip: number, take: number): Promise<Array<Sample>> { |
| 43 | + await this.simulateDelay(); |
| 44 | + |
| 45 | + return this.validationSamples.slice(skip, skip + take); |
| 46 | + } |
| 47 | + |
| 48 | + public async getTestingSamples(skip: number, take: number): Promise<Array<Sample>> { |
| 49 | + await this.simulateDelay(); |
| 50 | + |
| 51 | + return this.testingSamples.slice(skip, skip + take); |
| 52 | + } |
| 53 | + |
| 54 | + public async getValidationSamplesCount(): Promise<number> { |
| 55 | + await this.simulateDelay(); |
| 56 | + |
| 57 | + return this.validationSamples.length; |
| 58 | + } |
| 59 | + |
| 60 | + public async getTestingSamplesCount(): Promise<number> { |
| 61 | + await this.simulateDelay(); |
| 62 | + |
| 63 | + return this.testingSamples.length; |
| 64 | + } |
| 65 | + |
| 66 | + private async simulateDelay(): Promise<void> { |
| 67 | + if (this.simulatedDelayMs > 0) { |
| 68 | + await new Promise((resolve) => setTimeout(resolve, this.simulatedDelayMs)); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments