【发布时间】:2020-06-06 14:13:19
【问题描述】:
OneR,“One Rule”的缩写,是一种简单而准确的分类算法,它为数据中的每个预测变量生成一个规则,然后选择总误差最小的规则作为其“一个规则”。
我尝试在 GitHub 上查找代码示例,但只找到一个,使用 R 语言开发。我如何在 Javascript 中实现这个算法?
我尝试了什么? 我正在尝试按照此示例文章实施: https://www.saedsayad.com/oner.htm
class OneR {
/**
* Pass dataset which will be an array of values.
* Last value is classifcator's value.
* All other values are predictors.
*
* Example
*
* The meaning of sequence values:
* |Outlook|Temp|Humidity|Windy|Play Golf|
*
* Representation of a sequence:
* ['rainy', 'hot', 'high', 0, 0]
*
* True and False are represented as zeros or ones
*/
constructor(data = []) {
this.data = data;
this.frequences = {};
}
predict() {
if (this.data && this.data.length > 0) {
const firstRow = this.data[0];
const predictorCount = firstRow.length - 1;
let classifcator;
// For each predictor,
for (let i = 0; i < predictorCount; i++) {
// For each value of that predictor, make a rule as follos;
for (let y = 0; y < this.data.length; y++) {
// Count how often each value of target (class) appears
classifcator = this.data[y][predictorCount];
console.log(classifcator);
// Find the most frequent class
// Make the rule assign that class to this value of the predictor
}
// Calculate the total error of the rules of each predictor
}
// Choose the predictor with the smallest total error
} else {
console.log("Cannot predict!");
}
}
}
module.exports = {
OneR
};
我已从 csv 加载数据
rainy,hot,high,0,0
rainy,hot,high,1,0
overcast,hot,high,0,1
sunny,mild,high,0,1
sunny,cool,normal,0,1
sunny,cool,normal,1,0
overcast,cool,normal,1,1
rainy,mild,high,0,0
rainy,cool,normal,0,1
sunny,mild,normal,0,1
rainy,mild,normal,1,1
overcast,mild,high,1,1
overcast,hot,normal,0,1
sunny,mild,high,1,0
【问题讨论】:
-
嗨,欢迎来到堆栈溢出。您尝试过什么来解决这个问题,遇到了什么问题?
-
嗨,谢谢。我刚刚迷失了实现算法,对这个 ml-stuff 来说是新的。试图跟随文章。我附上了有关我的问题的更多信息。
标签: javascript algorithm machine-learning artificial-intelligence classification