如何使用Orange对数据进行分层?(How to stratify data using Orange?)
寻找橙色专家的帮助。
我有一个大约600万行的数据集。 为简单起见,我们只考虑两列。 一个是正十进制数,并作为连续值导入。 另一个是离散值(0或1),其中1到0的比率为30:1。
我正在使用分类树(我将其标记为“学习者”)来获取分类器。 我正在尝试对我的数据集进行交叉验证,同时调整压倒性的30:1样本偏差。 我已经尝试了几种变体来做到这一点,但无论我是否对数据进行分层,都会继续得到相同的结果。
下面是我的代码,我已经注释掉了我尝试的各种行(使用True和False值进行分层):
import Orange import os import time import operator start = time.time() print "Starting" print "" mydata = Orange.data.Table("testData.csv") # This is used only for the test_with_indices method below indicesCV = Orange.data.sample.SubsetIndicesCV(mydata) # I only want the highest level classifier so max_depth=1 learner = Orange.classification.tree.TreeLearner(max_depth=1) # These are the lines I've tried: #res = Orange.evaluation.testing.cross_validation([learner], mydata, folds=5, stratified=True) #res = Orange.evaluation.testing.proportion_test([learner], mydata, 0.8, 100, store_classifiers=1) res = Orange.evaluation.testing.proportion_test([learner], mydata, learning_proportion=0.8, times=10, stratification=True, store_classifiers=1) #res = Orange.evaluation.testing.test_with_indices([learner], mydata, indicesCV) f = open('results.txt', 'a') divString = "\n##### RESULTS (" + time.strftime("%Y-%m-%d %H:%M:%S") + ") #####" f.write(divString) f.write("\nAccuracy: %.2f" % Orange.evaluation.scoring.CA(res)[0]) f.write("\nPrecision: %.2f" % Orange.evaluation.scoring.Precision(res)[0]) f.write("\nRecall: %.2f" % Orange.evaluation.scoring.Recall(res)[0]) f.write("\nF1: %.2f\n" % Orange.evaluation.scoring.F1(res)[0]) tree = learner(mydata) f.write(tree.to_string(leaf_str="%V (%M out of %N)")) print tree.to_string(leaf_str="%V (%M out of %N)") end = time.time() print "Ending" timeStr = "Execution time: " + str((end - start) / 60) + " minutes" f.write(timeStr) f.close()
注意:似乎存在语法错误(分层与分层),但程序按原样运行,没有例外。 此外,我知道文档显示像stratified = StratifiedIfPossible之类的东西,但由于某种原因,只有布尔值对我有效。
Looking for some help from the Orange experts out there.
I have a data set of about 6 million lines. For simplicity's sake, we'll consider only two columns. One is of positive decimal numbers and is imported as a continuous value. The other is of discrete values (either 0 or 1) where there is a ratio of 30:1 for 1's to 0's.
I am using a classification tree (which I label as 'learner') to get the classifier. I'm then trying to do a cross-validation on my data set while adjusting for the overwhelming 30:1 sample bias. I've tried several variations to do this but continue to get the same result regardless of whether I stratify the data or not.
Below is my code and I've commented out the various lines I've tried (using both True and False values for stratification):
import Orange import os import time import operator start = time.time() print "Starting" print "" mydata = Orange.data.Table("testData.csv") # This is used only for the test_with_indices method below indicesCV = Orange.data.sample.SubsetIndicesCV(mydata) # I only want the highest level classifier so max_depth=1 learner = Orange.classification.tree.TreeLearner(max_depth=1) # These are the lines I've tried: #res = Orange.evaluation.testing.cross_validation([learner], mydata, folds=5, stratified=True) #res = Orange.evaluation.testing.proportion_test([learner], mydata, 0.8, 100, store_classifiers=1) res = Orange.evaluation.testing.proportion_test([learner], mydata, learning_proportion=0.8, times=10, stratification=True, store_classifiers=1) #res = Orange.evaluation.testing.test_with_indices([learner], mydata, indicesCV) f = open('results.txt', 'a') divString = "\n##### RESULTS (" + time.strftime("%Y-%m-%d %H:%M:%S") + ") #####" f.write(divString) f.write("\nAccuracy: %.2f" % Orange.evaluation.scoring.CA(res)[0]) f.write("\nPrecision: %.2f" % Orange.evaluation.scoring.Precision(res)[0]) f.write("\nRecall: %.2f" % Orange.evaluation.scoring.Recall(res)[0]) f.write("\nF1: %.2f\n" % Orange.evaluation.scoring.F1(res)[0]) tree = learner(mydata) f.write(tree.to_string(leaf_str="%V (%M out of %N)")) print tree.to_string(leaf_str="%V (%M out of %N)") end = time.time() print "Ending" timeStr = "Execution time: " + str((end - start) / 60) + " minutes" f.write(timeStr) f.close()
Note: There may seem like there are syntax errors (stratified vs. stratification) but the program runs as-is without exceptions. Also, I know the documentation shows stuff like stratified=StratifiedIfPossible but for some reason, only boolean values work for me.
原文:https://stackoverflow.com/questions/29973059
最满意答案
%s
用于以null结尾的字符串。magic
只是一个2字节的数组,而不是一个字符串。printf("magic number = %c%c\n", bmp_header_p->magic[0], bmp_header_p->magic[1]);
%s
is for null-terminated strings.magic
is just an array of 2 bytes, not a string.printf("magic number = %c%c\n", bmp_header_p->magic[0], bmp_header_p->magic[1]);
相关问答
更多-
您必须分配整个数组: e.values = malloc(sizeof(int) * (arg_num - 2)) 重要提示:记得在完成内存后free呼叫,否则会出现内存泄漏。 你有另一个问题,与你所询问的问题无关。 你做 struct entry e; e.length++; 当定义结构对象e ,它是未初始化的 ,其所有成员将具有不确定的值。 除了初始化之外,以任何方式使用此类未初始化数据都将导致未定义的行为 。 当你使用e.length++时,你会使用这些未初始化的值。 当你展示它时,这种增加在代码 ...
-
动态内存分配,如何在处理结构时正确使用指针?(Dynamic Memory Allocate, how to use pointer correctly when dealing with Structure?)[2023-04-20]
假如说 if (ptr=NULL) 是一个错字(因为随后“它输入值后它崩溃了”不可能发生),这里有一个错误: scanf_s("%d%s%f", &ptr->eno, ptr->ename, &ptr->esal); 缺少%s所需的大小参数 scanf_s("%d%s%f", &ptr->eno, ptr->ename, 20, &ptr->esal); 也许是代替硬编码 scanf_s("%d%s%f", &ptr->eno, ptr->ename, (unsigned)sizeof ptr->en ... -
struct Code *c = malloc(sizeof (struct Code)+as * sizeof (struct Codes)); 是不正确的。 struct Code的ci是一个指针数组,但是你为一个结构数组分配了空间。 要解决此问题,请更改为sizeof(struct Codes *) ,或者最好使用解除引用指向要为其分配空间的类型的指针的模式: struct Code *c = malloc( sizeof *c + as * sizeof c->ci[0] ); 另外, for ( ...
-
是的,你可以泄漏记忆。 如果std::copy抛出一个异常,你会有内存泄漏。 如果元素分配抛出,可能会发生这种情况。 这一切都取决于T和rhs的状态。 具体而言,如果对于任何非负整数n < rhs._size ,则会泄漏内存, n < rhs._size抛出以下内容: *(_data + n) = *(rhs._data + n); Yes, you can leak memory. You'll have a memory leak if std::copy throws an exception. ...
-
分配struct Data的最佳方式是: struct Data *tmp; tmp = malloc(sizeof *tmp); if (tmp == NULL) { /* malloc failed, abort or take corrective action */ } 不建议为指针类型定义typedef(至少由我来说); 使用struct Data *明确地让读者更清楚你正在处理一个指针。 为结构类型定义typedef也是不必要的。 typedef只是为现有类型声明一个新名称; 你的类型 ...
-
我如何正确地指向这段内存以将其视为我的结构?(How do I point to this piece of memory correctly to treat it as my struct?)[2021-09-11]
%s用于以null结尾的字符串。 magic只是一个2字节的数组,而不是一个字符串。 printf("magic number = %c%c\n", bmp_header_p->magic[0], bmp_header_p->magic[1]); %s is for null-terminated strings. magic is just an array of 2 bytes, not a string. printf("magic number = %c%c\n", bmp_header_p->m ... -
如何分配结构的内存?(How to allocate memory of a struct?)[2022-06-21]
sendQ是Sender的数组成员,它不是指针。 不需要使用malloc分配内存。 您可以使用: initSend(Sender* sender) { sender->winSize = -1; memset(sender->sendQ, 0, sizeof(sender->sendQ)); } sendQ is an array member of Sender, it is not a pointer. There is no need for allocating memory usin ... -
在C中,指针的特定类型如何处理指向的内存空间?(In C, how does the specific type of pointer treat the memory space which point to?)[2023-04-29]
首先, void*可以隐式转换为任何其他指针类型(提示: malloc返回的转换为多余的值)。 然后记忆意味着什么,你是如何解释其内容,赋予它一个意义。 因此,您基本上分配了sizeof(A) + SOME_LENGTH个字节的内存,然后告诉编译器您希望将从分配的内存开始的特定地址视为A* 。 没有什么可以阻止你这样做,只要从地址开始保留的内存是>= sizeof(A) ,它就会起作用。 唯一的问题是你如何释放内存。 派生地址charPointer + sizeof(A)不是标记为malloc从操作系统返回 ... -
当您使用不带-c标志的gcc时,它会做两件事:它将源文件编译为对象和文件,然后将目标文件链接到最终的可执行文件(或库)文件中。 所以在这个意义上你可以把它想象成两个工具,实际上对于链接步骤,gcc确实调用了单独的工具ld。 现在当gcc看到函数原型时会发生什么? 它将有关函数签名的信息存储在其内部数据结构中,因此它知道如何对函数进行类型检查以及如何为函数调用生成代码(根据类型,它可能必须为隐式转换插入代码并生成调用variadic函数时代码看起来不同)。 原型不会导致生成任何实际代码。 当gcc看到实际的函 ...
-
如果您的AccountNodeP是指向您的结构的指针,则sizeof(AccountNodeP)将返回指针的大小而不是结构的大小。 您可能希望改为使用sizeof(struct AccountNode) 。 PS并且不要忘记malloc的回归! If your AccountNodeP is a pointer to your structure, the sizeof(AccountNodeP) will return the size of the pointer and not of the stru ...