|
1 | 1 | import Matrix from '../../../lib/util/matrix.js' |
2 | 2 | import MaxAbsScaler from '../../../lib/model/maxabs.js' |
3 | 3 |
|
4 | | -test('fit', () => { |
| 4 | +test('mat mat', () => { |
5 | 5 | const model = new MaxAbsScaler() |
6 | 6 | const x = Matrix.randn(50, 2, 1, 0.2) |
7 | 7 | const xabsmax = Matrix.map(x, Math.abs).max(0).value |
8 | 8 | model.fit(x.toArray()) |
9 | | - const y = model.predict(x.toArray()) |
10 | | - |
11 | | - const min = Array(2).fill(Infinity) |
12 | | - const max = Array(2).fill(-Infinity) |
13 | | - const absmax = Array(2).fill(0) |
14 | | - for (let i = 0; i < x.rows; i++) { |
15 | | - for (let k = 0; k < x.cols; k++) { |
16 | | - expect(y[i][k]).toBeCloseTo(x.at(i, k) / xabsmax[k]) |
17 | | - min[k] = Math.min(min[k], y[i][k]) |
18 | | - max[k] = Math.max(max[k], y[i][k]) |
19 | | - absmax[k] = Math.max(max[k], Math.abs(y[i][k])) |
| 9 | + |
| 10 | + const x1 = Matrix.randn(50, 2, 0, 0.2) |
| 11 | + const y = model.predict(x1.toArray()) |
| 12 | + |
| 13 | + for (let i = 0; i < x1.rows; i++) { |
| 14 | + for (let j = 0; j < x1.cols; j++) { |
| 15 | + expect(y[i][j]).toBeCloseTo(x1.at(i, j) / xabsmax[j]) |
20 | 16 | } |
21 | 17 | } |
22 | | - for (let k = 0; k < min.length; k++) { |
23 | | - expect(min[k]).toBeGreaterThanOrEqual(-1) |
| 18 | +}) |
| 19 | + |
| 20 | +test('mat arr', () => { |
| 21 | + const model = new MaxAbsScaler() |
| 22 | + const x = Matrix.randn(50, 2, 1, 0.2) |
| 23 | + const xabsmax = Matrix.map(x, Math.abs).max(0).value |
| 24 | + model.fit(x.toArray()) |
| 25 | + |
| 26 | + const x1 = Matrix.randn(50, 1, 0, 0.2) |
| 27 | + const y = model.predict(x1.value) |
| 28 | + |
| 29 | + for (let i = 0; i < x1.rows; i++) { |
| 30 | + expect(y[i]).toBeCloseTo(x1.at(i, 0) / xabsmax[0]) |
24 | 31 | } |
25 | | - for (let k = 0; k < max.length; k++) { |
26 | | - expect(max[k]).toBeLessThanOrEqual(1) |
| 32 | +}) |
| 33 | + |
| 34 | +test('mat 0', () => { |
| 35 | + const model = new MaxAbsScaler() |
| 36 | + const x = Matrix.zeros(50, 2) |
| 37 | + model.fit(x.toArray()) |
| 38 | + |
| 39 | + const x1 = Matrix.randn(50, 2, 0, 0.2) |
| 40 | + const y = model.predict(x1.toArray()) |
| 41 | + |
| 42 | + for (let i = 0; i < x1.rows; i++) { |
| 43 | + for (let j = 0; j < x1.cols; j++) { |
| 44 | + expect(y[i][j]).toBeCloseTo(x1.at(i, j)) |
| 45 | + } |
27 | 46 | } |
28 | | - for (let k = 0; k < absmax.length; k++) { |
29 | | - expect(absmax[k]).toBeCloseTo(1) |
| 47 | +}) |
| 48 | + |
| 49 | +test('arr mat', () => { |
| 50 | + const model = new MaxAbsScaler() |
| 51 | + const x = Matrix.randn(50, 1, 1, 0.2) |
| 52 | + const xabsmax = Matrix.map(x, Math.abs).max() |
| 53 | + model.fit(x.value) |
| 54 | + |
| 55 | + const x1 = Matrix.randn(50, 2, 0, 0.2) |
| 56 | + const y = model.predict(x1.toArray()) |
| 57 | + |
| 58 | + for (let i = 0; i < x1.rows; i++) { |
| 59 | + for (let j = 0; j < x1.cols; j++) { |
| 60 | + expect(y[i][j]).toBeCloseTo(x1.at(i, j) / xabsmax) |
| 61 | + } |
| 62 | + } |
| 63 | +}) |
| 64 | + |
| 65 | +test('arr arr', () => { |
| 66 | + const model = new MaxAbsScaler() |
| 67 | + const x = Matrix.randn(50, 1, 1, 0.2) |
| 68 | + const xabsmax = Matrix.map(x, Math.abs).max() |
| 69 | + model.fit(x.value) |
| 70 | + |
| 71 | + const x1 = Matrix.randn(50, 1, 0, 0.2) |
| 72 | + const y = model.predict(x1.value) |
| 73 | + |
| 74 | + for (let i = 0; i < x1.rows; i++) { |
| 75 | + expect(y[i]).toBeCloseTo(x1.at(i, 0) / xabsmax) |
| 76 | + } |
| 77 | +}) |
| 78 | + |
| 79 | +test('arr 0', () => { |
| 80 | + const model = new MaxAbsScaler() |
| 81 | + const x = Matrix.zeros(50, 1, 1, 0.2) |
| 82 | + model.fit(x.value) |
| 83 | + |
| 84 | + const x1 = Matrix.randn(50, 1, 0, 0.2) |
| 85 | + const y = model.predict(x1.value) |
| 86 | + |
| 87 | + for (let i = 0; i < x1.rows; i++) { |
| 88 | + expect(y[i]).toBeCloseTo(x1.at(i, 0)) |
30 | 89 | } |
31 | 90 | }) |
0 commit comments