Platform
Solutions
Resources
Pricing
Sign in
Sign up
fheyen
PhD student at the Visualization Research Center (VISUS), University of Stuttgart
Workspace
Fork
Public
By
fheyen
Edited
Jun 6, 2023
2 stars
Insert cell
Insert cell
rfc
=
import
(
"https://cdn.skypack.dev/random-forest-classifier@latest"
)
Insert cell
rf
=
new
rfc
.
RandomForestClassifier
(
{
n_estimators
:
10
}
)
Insert cell
data
=
[
{
length
:
5.1
,
width
:
3.5
,
petal_length
:
1.4
,
petal_width
:
0.2
,
species
:
"setosa"
}
,
{
length
:
6.5
,
width
:
3
,
petal_length
:
5.2
,
petal_width
:
2
,
species
:
"virginica"
}
,
{
length
:
6.6
,
width
:
3
,
petal_length
:
4.4
,
petal_width
:
1.4
,
species
:
"versicolor"
}
]
Insert cell
testdata
=
[
{
length
:
6.3
,
width
:
2.5
,
petal_length
:
5
,
petal_width
:
1.9
//"species":"virginica"
}
,
{
length
:
4.7
,
width
:
3.2
,
petal_length
:
1.3
,
petal_width
:
0.2
//"species":"setosa"
}
]
Insert cell
rf
.
fit
(
data
,
null
,
"species"
,
function
(
err
,
trees
)
{
//console.log(JSON.stringify(trees, null, 4));
var
pred
=
rf
.
predict
(
testdata
,
trees
)
;
// pred = ["virginica", "setosa"]
console
.
log
(
{
rf
,
trees
,
pred
}
)
;
for
(
const
tree
of
rf
.
trees
)
{
const
render
=
renderTreeNode
(
tree
.
model
)
;
console
.
log
(
render
)
;
}
}
)
Insert cell
function
renderTreeNode
(
node
,
depth
=
0
)
{
if
(
node
.
type
===
"result"
)
{
// class name
return
`\n${
" "
.
repeat
(
depth
*
2
)
} - ${
depth
} ${
node
.
type
} ${
node
.
val
}`
;
}
else
{
// feature value to cut
let
resultString
=
`\n${
" "
.
repeat
(
depth
*
2
)
} - ${
depth
} ${
node
.
type
} ${
node
.
name
} ${
node
.
cut
}`
;
resultString
+=
renderTreeNode
(
node
.
vals
[
0
]
.
child
,
depth
+
1
)
;
resultString
+=
renderTreeNode
(
node
.
vals
[
1
]
.
child
,
depth
+
1
)
;
return
resultString
;
}
}
Insert cell
One platform
to build and deploy the best data apps
Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
rfc
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
rf
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
data
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
testdata
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
renderTreeNode
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML