[英]How do 'numpy.ndarray' object do not 'numpy.ndarray' object?
When you call DataFrame.to_numpy(), pandas will find the NumPy dtype that can hold all of the dtypes in the DataFrame.当您调用 DataFrame.to_numpy() 时,pandas 将找到可以容纳 DataFrame 中所有 dtype 的 NumPy dtype。 But how to perform the reverse operation?
但是如何进行反向操作呢?
I have an 'numpy.ndarray' object 'pred'.我有一个“numpy.ndarray”对象“pred”。 It looks like this:
它看起来像这样:
[[0.00599913 0.00506044 0.00508315 ... 0.00540191 0.00542058 0.00542058]]
[[0.00599913 0.00506044 0.00508315 ... 0.00540191 0.00542058 0.00542058]]
I am trying to do like this:我正在尝试这样做:
pred = np.uint8(pred)
print("Model predict:\n", pred.T)
But I get:但我得到:
[[0 0 0 ... 0 0 0]]
[[0 0 0 ... 0 0 0]]
Why, after the conversion, I do not get something like this:为什么,转换后,我没有得到这样的东西:
0 0 0 0 0 0 ... 0 0 0 0 0 0
0 0 0 0 0 0 ... 0 0 0 0 0 0
And how to write the pred to a file?以及如何将 pred 写入文件?
pred.to_csv('pred.csv', header=None, index=False)
pred = pd.read_csv('pred.csv', sep=',', header=None)
Gives an error message:给出错误信息:
AttributeError Traceback (most recent call last)
<ipython-input-68-b223b39b5db1> in <module>()
----> 1 pred.to_csv('pred.csv', header=None, index=False)
2 pred = pd.read_csv('pred.csv', sep=',', header=None)
AttributeError: 'numpy.ndarray' object has no attribute 'to_csv'
Please help me figure this out.请帮我解决这个问题。
您可以使用一行代码将 ndarray 转换为 pandas df,然后转换为 csv 文件来解决该问题。
pd.DataFrame(X_train_res).to_csv("x_train_smote_oversample.csv")
pred
is an ndarray
. pred
是一个ndarray
。 It does not have a to_csv
method.它没有
to_csv
方法。 That's something a pandas
DataFrame
has.这是东西
pandas
DataFrame
了。
But lets look at the first stuff.但让我们看看第一件事。
Copying your array display, adding commas, lets me make a list:复制您的数组显示,添加逗号,让我列出一个列表:
In [1]: alist = [[0.00599913, 0.00506044, 0.00508315, 0.00540191, 0.00542058, 0.
...: 00542058]]
In [2]: alist
Out[2]: [[0.00599913, 0.00506044, 0.00508315, 0.00540191, 0.00542058, 0.00542058]]
and make an array from that:并从中制作一个数组:
In [3]: arr = np.array(alist)
In [8]: print(arr)
[[0.00599913 0.00506044 0.00508315 0.00540191 0.00542058 0.00542058]]
or the repr
display that ipython
gives as the default:或
ipython
默认提供的repr
显示:
In [4]: arr
Out[4]:
array([[0.00599913, 0.00506044, 0.00508315, 0.00540191, 0.00542058,
0.00542058]])
Because of the double brackets, this is a 2d array.由于双括号,这是一个二维数组。 Its transpose will have shape (6,1).
它的转置将具有形状 (6,1)。
In [5]: arr.shape
Out[5]: (1, 6)
Conversion to uint8
works as expected (I prefer the astype
version).转换为
uint8
可以按预期工作(我更喜欢astype
版本)。 But但
In [6]: np.uint8(arr)
Out[6]: array([[0, 0, 0, 0, 0, 0]], dtype=uint8)
In [7]: arr.astype('uint8')
Out[7]: array([[0, 0, 0, 0, 0, 0]], dtype=uint8)
The converted shape is as before (1,6).转换后的形状如前 (1,6)。
The conversion is nearly meaningless.转换几乎没有意义。 The values are all small between 1 and 0. Converting to small (1 byte) unsigned integers predictably produces all 0s.
这些值在 1 和 0 之间都很小。转换为小的(1 字节)无符号整数可预测地产生全 0。
import numpy as np
import pandas as pd
x = [1,2,3,4,5,6,7]
x = np.array(x)
y = pd.Series(x)
print(y)
y.to_csv('a.csv')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.