From fd5bc8bfd2465ed9ebc376fc4ee7d226270c117b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hubert=20Soko=C5=82owski?= Date: Fri, 14 May 2021 22:56:13 +0200 Subject: [PATCH] second tree for test data --- src/components/ConfusionMatrix/index.jsx | 3 +- src/components/Node/Joint.jsx | 2 +- src/components/Node/ModalPopup.jsx | 2 +- src/components/Node/index.jsx | 2 +- src/components/Tree.jsx | 41 ++++++++++++++-- src/services/playground3.js | 62 ++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 src/services/playground3.js diff --git a/src/components/ConfusionMatrix/index.jsx b/src/components/ConfusionMatrix/index.jsx index de56b71..d0fc525 100644 --- a/src/components/ConfusionMatrix/index.jsx +++ b/src/components/ConfusionMatrix/index.jsx @@ -53,8 +53,9 @@ function ConfusionMatrix({ tree, onChange, data, allClasses, categoryAttr, disab console.log('use effect confusion matrix'); let CM = buildArray(allClasses.length); - if (!disabled && tree !== null) { + if (!disabled && tree !== null && data !== null) { let prediction, clazz; + //console.log(data); for (let index = 0; index < data.length; index++) { //for (let index = 100; index < 102; index++) { prediction = predict(tree, data[index]); diff --git a/src/components/Node/Joint.jsx b/src/components/Node/Joint.jsx index 64f6576..ae803d5 100644 --- a/src/components/Node/Joint.jsx +++ b/src/components/Node/Joint.jsx @@ -44,7 +44,7 @@ function Joint({ children, attr2, predicateName, pivot, weight, requestFoldToLea {predicateName} - {weight ? weight?.toFixed(3) + ' * ' : ''} + {weight ? parseFloat(weight).toFixed(3) + ' * ' : ''} {pivot} diff --git a/src/components/Node/ModalPopup.jsx b/src/components/Node/ModalPopup.jsx index 360c6de..b1c409a 100644 --- a/src/components/Node/ModalPopup.jsx +++ b/src/components/Node/ModalPopup.jsx @@ -33,7 +33,7 @@ function ModalPopup({ attr2, predicateName, pivot, weight, isOpen, nodeSet, onOp - {attr2} {predicateName} {weight ? weight?.toFixed(3) + ' * ' : ''} {pivot} + {attr2} {predicateName} {weight ? parseFloat(weight).toFixed(3) + ' * ' : ''} {pivot} diff --git a/src/components/Node/index.jsx b/src/components/Node/index.jsx index c637843..a9652f8 100644 --- a/src/components/Node/index.jsx +++ b/src/components/Node/index.jsx @@ -134,7 +134,7 @@ const Node = props => {
- + {/* */}
{category ? ( diff --git a/src/components/Tree.jsx b/src/components/Tree.jsx index e30fbc3..4b3d404 100644 --- a/src/components/Tree.jsx +++ b/src/components/Tree.jsx @@ -22,6 +22,7 @@ import { executeAlgorithm } from '../utils/algorithm-executor'; import TestSetFileReader from './TestSetFileReader'; import ConfusionMatrix from './ConfusionMatrix'; import { getSizeTree } from '../utils/size-checker'; +import { testTree } from '../services/playground3'; /** * @typedef {import('../utils/decision-tree.js').DecisionTreeBuilder} DecisionTreeBuilder @@ -45,14 +46,16 @@ const Tree = ({ options }) => { const [accuracyTraining, setAccuracyTraining] = useState(0); const [accuracyTest, setAccuracyTest] = useState(0); const [root, setRoot] = useState(null); + const [secondRoot, setSecondRoot] = useState(null); const [sizeTree, setSizeTree] = useState({ joints: 0, leafs: 0 }); - const [testSet, setTestSet] = useState([]); + const [testSet, setTestSet] = useState(null); const [showTestTree, setShowTestTree] = useState(false); const { isLoading, setIsLoading } = useLoadingContext(); useEffect(() => { setRoot(null); setIsLoading(true); + setTestSet(null); let terminated = false; executeAlgorithm(options) .then(value => { @@ -60,6 +63,7 @@ const Tree = ({ options }) => { return; } setRoot(value); + updateTestTree(value); }) .catch(e => console.error(e)) .finally(() => { @@ -82,13 +86,33 @@ const Tree = ({ options }) => { //setAccuracy(Math.random() * 10); }, [root]); - const requestChildChange = newRoot => setRoot(newRoot); + const requestChildChange = newRoot => { + setRoot(newRoot); + updateTestTree(newRoot); + }; function handleGetTestSet({ data }) { setTestSet(data); } + + function updateTestTree(newRoot) { + console.log(options.categoryAttr); + let tmpRoot = JSON.parse(JSON.stringify(newRoot)); + if (testSet == null) { + testTree(tmpRoot, options.trainingSet, options.categoryAttr); + console.log(tmpRoot); + setSecondRoot(tmpRoot); + } else { + testTree(tmpRoot, testSet, options.categoryAttr); + console.log(tmpRoot); + setSecondRoot(tmpRoot); + } + } + function handleShowTestTree(e) { + console.log(e); setShowTestTree(e.target.checked); + //updateTestTree(root); } return ( @@ -193,7 +217,7 @@ const Tree = ({ options }) => { Show test tree - + handleShowTestTree(e)} /> @@ -211,7 +235,16 @@ const Tree = ({ options }) => { Test tree - {}} requestChildChange={requestChildChange} side={true} /> + {showTestTree ? ( + {}} + //requestChildChange={requestChildChange} + side={true} + /> + ) : ( +
+ )}
)} diff --git a/src/services/playground3.js b/src/services/playground3.js new file mode 100644 index 0000000..c35b3a6 --- /dev/null +++ b/src/services/playground3.js @@ -0,0 +1,62 @@ +export function testTree(tree, newData, categoryAttr) { + let predicate; + console.log(categoryAttr); + if (tree.category) { + tree.trainingSet2 = newData; + let _positiveCounter = 0, + _quality = 0; + for (let element of newData) { + if (element[categoryAttr] === tree.category) _positiveCounter++; + } + let _negativeCounter = newData.length - _positiveCounter; + _quality = _positiveCounter / newData.length; + _quality = _quality * 100; + + tree.quality = _quality.toFixed(2); + tree.matchedCount = _positiveCounter; + tree.notMatchedCount = _negativeCounter; + return; + } else { + tree.nodeSet = newData; + + predicate = predicates[tree.predicateName]; + + let matchedData = [], + notMatchedData = []; + + newData.forEach(x => { + let match; + if (tree.predicateName === '==' || tree.predicateName === '>=') { + match = predicate(x[tree.attr2], tree.pivot); + console.log('c45', match); + } else if (tree.weight) { + match = predicate(x[tree.attr2], x[tree.pivot], tree.weight); + console.log('tspw', match); + } else { + match = predicate(x[tree.attr2], x[tree.pivot]); + console.log('tsp', match); + } + + match ? matchedData.push(x) : notMatchedData.push(x); + }); + + testTree(tree.match, matchedData, categoryAttr); + testTree(tree.notMatch, notMatchedData, categoryAttr); + } +} + +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) { + //console.log(a < w * b, a, w * b); + return a < w * b; + }, +};