使用Scikit-Learn Knn.ndarray并不是c contigul。

发布于 2025-02-12 10:06:48 字数 760 浏览 1 评论 0原文

为此,我要调用预测函数,我有以下代码,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖凝香 2025-02-19 10:06:48

数据和追溯信息的量仍然不够。但是我会猜测。

让我们制作一个简单的数据框架:

In [31]: df = pd.DataFrame(np.ones((3,4)))

In [32]: df
Out[32]: 
     0    1    2    3
0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0

预测可能会使用一些预期c-contiguul数据的编译代码。如果给出了一个数据框,它可能首先将其转换为数组,例如使用np.Array(df)或有效:

In [35]: df.values
Out[35]: 
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])

In [36]: df.values.flags
Out[36]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

但是,如果您执行转换的连续性更改。这在数组中众所周知,看起来Pandas是兼容的:

In [37]: df.T.values.flags
Out[37]: 
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

Pandas transpose允许我们指定复制 - 请参阅其文档:

In [38]: df.transpose(copy=True).values.flags
Out[38]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

因此在您的代码中使用可能(?? ?)解决问题:

 features = convert_to_df(features).transpose(copy=True)

我不能过足够的压力,您应该在问题中包含足够的信息。

The amount of data and traceback information is still not enough. But I'll make a guess.

Let's make a simple dataframe:

In [31]: df = pd.DataFrame(np.ones((3,4)))

In [32]: df
Out[32]: 
     0    1    2    3
0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0

predict probably uses some compiled code that expect c-contiguous data. If given a dataframe it probably first converts it to an array, such as with np.array(df) or effectively:

In [35]: df.values
Out[35]: 
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])

In [36]: df.values.flags
Out[36]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

But if you do a transpose the contiguity changes. This is well known for arrays, and it looks like pandas is compatible:

In [37]: df.T.values.flags
Out[37]: 
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

pandas transpose allows us to specify copy - see its docs:

In [38]: df.transpose(copy=True).values.flags
Out[38]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

So using in your code might(???) solve the problem:

 features = convert_to_df(features).transpose(copy=True)

I can't stress enough that you should include enough information in your question.

网白 2025-02-19 10:06:48

即使所有列独立于> C_CONTIGUL:正确
整个df c_contigouul标志显示为false

# this solution worked for me
X, y = np.ascontiguousarray(df[required_cols]), df['labeled']

时,我转换了所需的列

Even if all the columns independently are > C_CONTIGUOUS : True
The entire df C_CONTIGUOUS flag shows as False

# this solution worked for me
X, y = np.ascontiguousarray(df[required_cols]), df['labeled']

while assigining I converted the required columns

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文