diff --git a/src/components/Predicter/index.jsx b/src/components/Predicter/index.jsx new file mode 100644 index 0000000..d691f28 --- /dev/null +++ b/src/components/Predicter/index.jsx @@ -0,0 +1,38 @@ +import React, { useState } from 'react'; +import { Button, Textarea, Box, ButtonGroup } from '@chakra-ui/react'; +import { GiWaterDivinerStick } from 'react-icons/gi'; +import { predict } from '../../utils/predict'; + +/** + * @param {Object} props + * @param {Object} props.tree + * @param {Object[]} props.testSet + * @param {Function} props.onChange + */ +function Predicter({ tree, onChange, testSet }) { + const [allow, setAllow] = useState(true); + + function handlePredict() { + if (allow) setAllow(false); + let result = []; + //for (let index = 0; index < testSet.length; index++) { + for (let index = 0; index < 1; index++) { + result.push(predict(tree, testSet[index])); + } + console.log(result); + } + return ( + + + + + + + + + ); +} + +export default Predicter; diff --git a/src/components/Tree.jsx b/src/components/Tree.jsx index c2d65a0..3d48116 100644 --- a/src/components/Tree.jsx +++ b/src/components/Tree.jsx @@ -1,11 +1,12 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react'; -import { Box, Button, HStack, Spinner } from '@chakra-ui/react'; +import { Box, Button, HStack, Spinner, Stack } from '@chakra-ui/react'; import { GrTechnology, GrDocumentUpload } from 'react-icons/gr'; import { GiWaterDivinerStick } from 'react-icons/gi'; import Node from './Node'; import { useLoadingContext } from '../contexts/LoadingContext'; import { executeAlgorithm } from '../utils/algorithm-executor'; import TestSetFileReader from './TestSetFileReader'; +import Predicter from './Predicter'; /** * @typedef {import('../utils/decision-tree.js').DecisionTreeBuilder} DecisionTreeBuilder @@ -69,7 +70,7 @@ const Tree = ({ options }) => { return (
- + - - + + {isLoading && }

Tree nodes:

{!root ? (

No tree to show

) : ( - {}} requestChildChange={requestChildChange} side={true} /> + + {}} requestChildChange={requestChildChange} side={true} /> + {/* {}} requestChildChange={requestChildChange} side={true} /> */} + )}
diff --git a/src/utils/predict.js b/src/utils/predict.js new file mode 100644 index 0000000..9c4e858 --- /dev/null +++ b/src/utils/predict.js @@ -0,0 +1,70 @@ +export function predict(tree, item) { + var attr1, attr2, value, predicate, pivot, match; + + // Traversing tree from the root to leaf + while (true) { + if (tree.category) { + // only leafs contains predicted category + return tree.category; + } + console.log( + tree.predicateName, + tree.weight, + tree.predicateName === '>=', + tree.predicateName === '==', + tree.predicateName === '<' + ); + if (tree.weight) { + attr1 = tree.attr2; + attr2 = tree.pivot; + value = item[attr1]; + pivot = item[attr2]; + + predicate = predicates['w']; + match = predicate(value, pivot, tree.weight); + + console.log('predict - waga', match); + } + if (tree.predicateName === '>=' || tree.predicateName === '==') { + attr1 = tree.attr2; + value = item[attr1]; + pivot = tree.pivot; + predicate = predicates[tree.predicateName]; + match = predicate(value, pivot); + + console.log('predict - c45', match); + } + if (tree.predicateName === '<') { + attr1 = tree.attr2; + attr2 = tree.pivot; + value = item[attr1]; + pivot = item[attr2]; + + predicate = predicates[tree.predicateName]; + match = predicate(value, pivot); + + console.log('predict - tsp', match); + } + + // move to one of subtrees + if (match) { + tree = tree.match; + } else { + tree = tree.notMatch; + } + } +} +var predicates = { + '==': function (a, b) { + return a === b; + }, + '>=': function (a, b) { + return a >= b; + }, + '<': function (a, b) { + return a < b; + }, + w: function (a, b, w) { + return a < w * b; + }, +};