用本地指针分段错误?(Segmentation fault with local pointer?)
这是一个函数调用(因此它只是整个程序的一部分)。 它会产生分段错误。 我猜这是由“指针是一个局部变量”引起的?
int fileExists(const char *fname){ int i = 0; fseek(fs, sizeof(NODE)*i, SEEK_SET); NODE* pointer; fread(pointer, sizeof(NODE), 1, fs); return 1; }
更新:
typedef struct node { char fname[MAX_NAME]; short fstart; } NODE; int findStart (const char *fname){ fs = fopen("Directory", "w+"); NODE* pointer = malloc(sizeof(NODE)); int i; for(i=0;i<numberNodes;i++){ fseek(fs, sizeof(NODE)*i, SEEK_SET); fread(pointer, sizeof(NODE), 1, fs); if(strcmp(pointer->fname, fname)==0) return pointer->fstart; } return 0; }
因此,如果我想运行目录文件中的节点并找到正确节点的“fstart”,我是否可以实现这一点而无需释放分配给“指针”的内存?
This is a function call (so it is just a part of the whole program). It will generate a segmentation fault. I guess it is caused by "pointer being a local variable"?
int fileExists(const char *fname){ int i = 0; fseek(fs, sizeof(NODE)*i, SEEK_SET); NODE* pointer; fread(pointer, sizeof(NODE), 1, fs); return 1; }
Updated:
typedef struct node { char fname[MAX_NAME]; short fstart; } NODE; int findStart (const char *fname){ fs = fopen("Directory", "w+"); NODE* pointer = malloc(sizeof(NODE)); int i; for(i=0;i<numberNodes;i++){ fseek(fs, sizeof(NODE)*i, SEEK_SET); fread(pointer, sizeof(NODE), 1, fs); if(strcmp(pointer->fname, fname)==0) return pointer->fstart; } return 0; }
So if I want to run through the nodes in the directory file and find the "fstart" of the right node, can I achieve that without the need of free-ing the memory allocated to "pointer"?
原文:https://stackoverflow.com/questions/8147619
最满意答案
所以我发现没有可以使用的predict()方法。 相反,我们需要使用transform()方法进行预测。 只需删除标签列并创建一个新的数据框。 例如,就我而言,我做了,
pred = sqlContext.createDataFrame([("What are liver enzymes ?" ,)], ["sentence"]) prediction = model.transform(pred)
然后我们可以使用select()方法找到预测结果。 至少现在,这个解决方案为我成功地工作。 请让我知道是否有任何改正或比这更好的方法。
So I figured it out that there is no predict() method that can be used. So instead, we need to use the transform() method to make predictions. Just remove the label column and create a new dataframe. For example, in my case, I did,
pred = sqlContext.createDataFrame([("What are liver enzymes ?" ,)], ["sentence"]) prediction = model.transform(pred)
And then we can find the prediction using the select() method. Atleast for now, this solution worked successfully for me. Please do let me know if there is any correction or a better approach than this.
相关问答
更多-
sklearn RandomForestClassifier活动路径或结束节点(sklearn RandomForestClassifier active paths or ended nodes)[2022-12-30]
您可以使用随机林的apply方法(请参阅docs )来获取林中每棵树最终所在叶子的索引。这是调用决策树估计器的tree_属性的apply方法。 您可以在源中找到有关底层树对象的更多信息。 values属性为每个节点提供了例如最终进入该叶子的训练点的标签。 You can use the apply method (see docs) of the random forest, to get for each tree in the forest the index of the leaf it ended ... -
问题是较新版本的spark在ml的linalg模块中有一个Vector类,你不需要从mllib.linalg获取它。 此外,较新的版本不接受以ml为单位的spark.mllib.linalg.VectorUDT。 这是适合您的代码: from pyspark import SparkContext from pyspark.ml.regression import LinearRegression from pyspark.ml.linalg import Vectors import numpy as n ...
-
您可以尝试保留列车的索引并进行测试,然后以这种方式将它们组合在一起: from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split data = df[['Feature1', 'Feature2', 'Feature3']] labels = df['Target'] indices = df.index.values # use the indices ...
-
我会说它主要是基于意见的,虽然它看起来不必要地冗长,而且Python Transformers不能很好地与Pipeline API的其余部分集成。 值得指出的是,使用SQLTransformer可以轻松实现此处的所有功能。 例如: from pyspark.ml.feature import SQLTransformer def column_selector(columns): return SQLTransformer( statement="SELECT {} FROM __ ...
-
让我们首先修复导入以消除歧义 import org.apache.spark.ml.classification.RandomForestClassifier import org.apache.spark.ml.feature.{StringIndexer, VectorIndexer} import org.apache.spark.ml.{Pipeline, PipelineStage} import org.apache.spark.ml.linalg.Vectors 我将使用您使用的相同数据: ...
-
所以我发现没有可以使用的predict()方法。 相反,我们需要使用transform()方法进行预测。 只需删除标签列并创建一个新的数据框。 例如,就我而言,我做了, pred = sqlContext.createDataFrame([("What are liver enzymes ?" ,)], ["sentence"]) prediction = model.transform(pred) 然后我们可以使用select()方法找到预测结果。 至少现在,这个解决方案为我成功地工作。 请让我知道是 ...
-
在scikit-learn问题跟踪器上发布问题后,我得到的反馈是问题出在我使用的“预测”功能中。 应该是“pred_test = clr.predict_proba(X_test)[:,1]”而不是“pred_test = clr.predict(X_test)”,因为分类问题是二进制:0或1。 实施更改后,结果对于WEKA和scikit的随机森林来说是相同的:) After posting the question at scikit-learn issue tracker, I got feedback ...
-
实际上它有trees属性: import org.apache.spark.ml.attribute.NominalAttribute import org.apache.spark.ml.classification.{ RandomForestClassificationModel, RandomForestClassifier, DecisionTreeClassificationModel } val meta = NominalAttribute .defaultAttr .w ...
-
看起来,与文档相反,列出了: 在变换中保留元数据; 如果已存在要素的元数据,请勿重新计算。 在TODO中,元数据已经保留。 from pyspark.sql.functions import col from pyspark.ml import Pipeline from pyspark.ml.feature import * df = spark.range(10) stages = [StringIndexer(inputCol="id", outputCol="idx"), VectorAsse ...
-
基于正在解决的不同任务,这些之间存在重大的概念差异: 回归 :连续(实值)目标变量。 分类 :离散目标变量(类)。 对于一般分类方法,可以不定义probability of observation being class X术语probability of observation being class X ,因为例如某些分类方法不处理概率。 然而,对于随机森林(以及一些其他分类方法),分类被简化为类概率分解的回归。 然后将预测类作为计算“概率”的argmax。 在您的情况下,您输入相同的输入,您得到相同的 ...