python:DataFrame object no attribute argsort

在python里面这样写

order = y_test.argsort(axis=0)

本意是为了得到矩阵列大小的下标,却报错。
不妨先打印y_tset看看
如下:
Sales
136 9.5
59 18.4
51 10.7
16 12.5
44 8.5
94 11.5
31 11.9
162 14.9
38 10.1
28 18.9
193 19.6
… …
177 11.7
176 20.2
97 15.5
174 11.5
73 11.0
69 22.3
172 7.6
左边是原来的下标,右边是价格Sales,一个n行两列的数据,order = y_test.argsort(axis=0)的本意是得到Sales从小到大的下标。我用的是python3.8不支持。所以就需要自己来变动,废话不多说,代码如下:

    # # 将y_test转为数组
    yTest = np.array(y_test)
    # # 得到y_test的一维数组 取矩阵的第0列数据
    preOrder = yTest[:, 0]
    # # 得到y_test的从小到大的下标
    order = np.argsort(preOrder)

这样就达到了目的。
亲测可用,注意如果报错试着打印yTest看看
有时候 yTest = np.array(y_test) 就已经是一维数组那么只需要

    yTest = np.array(y_test)
    order = np.argsort(yTest)

可以根据实际情况来看。

你可能感兴趣的:(python以及机器学习)