使用Scikit-Learn Knn.ndarray并不是c contigul。
为此,我要调用预测函数,我有以下代码,
def convert_to_df(obj):
obj_dic = obj.dict()
df = pd.DataFrame(obj_dic.values(), index=obj_dic.keys())
df.reset_index(drop=True, inplace=True)
return df
@app.get("/get_rating")
def get_rating(features: Features):
features = convert_to_df(features).T # shape (1, 26)
return {'rating': Predictor().predict(features)}
但是我会收到以下错误:
文件“ Stringsource”,第658行,in view.memoryview.memoryview_cwrapper文件“ stringsource”,第349行, 在View.MemoryView.Memoryview。 c contigul
如何解决这个问题?
谢谢
编辑
预测器是Scikit学习的KNN模型培训师
def predict(self, features) -> int:
return self.model.predict(features)
I am triying to call predict function in order to this I have the following code
def convert_to_df(obj):
obj_dic = obj.dict()
df = pd.DataFrame(obj_dic.values(), index=obj_dic.keys())
df.reset_index(drop=True, inplace=True)
return df
@app.get("/get_rating")
def get_rating(features: Features):
features = convert_to_df(features).T # shape (1, 26)
return {'rating': Predictor().predict(features)}
but I am getting the following error:
File "stringsource", line 658, in
View.MemoryView.memoryview_cwrapper File "stringsource", line 349,
in View.MemoryView.memoryview.cinit ValueError: ndarray is not
C-contiguous
How can I solve this?
Thanks
EDIT
Predictor is a knn model trainer with scikit learn
def predict(self, features) -> int:
return self.model.predict(features)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据和追溯信息的量仍然不够。但是我会猜测。
让我们制作一个简单的数据框架:
预测
可能会使用一些预期c-contiguul
数据的编译代码。如果给出了一个数据框,它可能首先将其转换为数组,例如使用np.Array(df)
或有效:但是,如果您执行转换的连续性更改。这在数组中众所周知,看起来Pandas是兼容的:
Pandas
transpose
允许我们指定复制 - 请参阅其文档:因此在您的代码中使用可能(?? ?)解决问题:
我不能过足够的压力,您应该在问题中包含足够的信息。
The amount of data and traceback information is still not enough. But I'll make a guess.
Let's make a simple dataframe:
predict
probably uses some compiled code that expectc-contiguous
data. If given a dataframe it probably first converts it to an array, such as withnp.array(df)
or effectively:But if you do a transpose the contiguity changes. This is well known for arrays, and it looks like pandas is compatible:
pandas
transpose
allows us to specify copy - see its docs:So using in your code might(???) solve the problem:
I can't stress enough that you should include enough information in your question.
即使所有列独立于> C_CONTIGUL:正确
整个df
c_contigouul
标志显示为false
时,我转换了所需的列
Even if all the columns independently are > C_CONTIGUOUS : True
The entire df
C_CONTIGUOUS
flag shows asFalse
while assigining I converted the required columns