在javascript中实现一个规则算法[关闭]
Posted
技术标签:
【中文标题】在javascript中实现一个规则算法[关闭]【英文标题】:Implementing OneRule algorithmn in javascript [closed] 【发布时间】: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 来说是新的。试图跟随文章。我附上了有关我的问题的更多信息。 【参考方案1】:如果我正确理解必须如何比较频率表(最低错误率、最高准确度),您可以在必要时使用 Maps 来处理非字符串类型。
虽然您的示例具有布尔值(0 或 1)的目标值,但通常它们可能来自更大的域,例如“call”、“fold”、“raise”、“check”。
您的模板代码创建了一个类,但老实说,我没有看到这样做的好处,因为您实际上只能对其执行一项操作。当然,如果除了单规则预测之外,您还有其他行动,那么一个类可能是有意义的。在这里,我将只提供一个获取数据的函数,并返回所选预测器的编号和与之相关的规则表:
function oneR(data)
if (!data && !data.length) return console.log("Cannot predict!");
const predictorCount = data[0].length - 1;
// get unique list of classes (target values):
let classes = [...new Set(data.map(row => row[predictorCount]))];
let bestAccuracy = -1;
let bestFreq, bestPredictor;
// For each predictor,
for (let i = 0; i < predictorCount; i++)
// create frequency table for this predictor: Map of Map of counts
let freq = new Map(data.map(row => [row[i], new Map(classes.map(targetValue => [targetValue, 0]))]));
// For each value of that predictor, collect the frequencies
for (let row of data)
// Count how often each value of target (class) appears
let targetValue = row[predictorCount];
let predictorValueFreq = freq.get(row[i]);
let count = predictorValueFreq.get(targetValue);
predictorValueFreq.set(targetValue, count+1);
// Find the most frequent class for each predictor value
let accuracy = 0;
for (let [predictorValue, predictorValueFreq] of freq)
let maxCount = 0;
let chosenTargetValue;
for (let [targetValue, count] of predictorValueFreq)
if (count > maxCount)
// Make the rule assign that class to this value of the predictor
maxCount = count;
chosenTargetValue = targetValue;
freq.set(predictorValue, chosenTargetValue);
accuracy += maxCount;
// If this accuracy is best, then retain this frequency table
if (accuracy > bestAccuracy)
bestAccuracy = accuracy;
bestPredictor = i;
bestFreq = freq;
// Return the best frequency table and the predictor for which it applies
return
predictor: bestPredictor, // zero-based column number
rule: [...bestFreq.entries()]
let data = [
["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]
];
let result = oneR(data);
console.log(result);
【讨论】:
以上是关于在javascript中实现一个规则算法[关闭]的主要内容,如果未能解决你的问题,请参考以下文章