On the problem of creating empty array with Python numpy

Time:2022-1-6

1、 Problem Description:

There is a two-dimensional array with shape (308, 2) and a single number that needs to be saved to the CSV file. This single number needs to be saved to the first row of column 3.

2、 Specific implementation:

First of all, we need to splice a two-dimensional array (308, 2) and a number. Direct splicing cannot be realized, because two Darries with different row numbers and column numbers cannot be spliced (here, according to my current understanding, it is impossible to splice directly. If you can, please comment).

Then the first solution I think of is to build a (308,1) two-dimensional array, and then set the first element of the two-dimensional array to that number, then splice and save it.

In order to display the data completely, take only 3 rows of data as an example:

?
1
2
3
4
5
6
7
8
9
10
>>> a = np.ones((3,2))
>>> b = 0.2
>>> _b = np.empty((3,1))
>>> _b[0, 0] = b
>>> c = np.c_[a, _b]
>>> print(c)
[[1.00000000e+000 1.00000000e+000 2.00000000e-001]
 [1.00000000e+000 1.00000000e+000 2.12199579e-313]
 [1.00000000e+000 1.00000000e+000 2.54639495e-313]]
>>>

But in this way, when I save the results to the file, all the rows in column 3 except the first row have data. I don’t want it to display data.
That is, the empty function just creates an uninitialized array. In fact, the values in it are garbage values.

So how to realize that there is no data visually? In fact, it is OK to use an empty string

So I passed NP When dtype is set to STR by ones, an array with empty string elements is generated (the specific reason is not clear). At this time, if the element in the first line is directly set to a certain value, it will automatically change to ‘0’. Only after splicing and then assigning a value to it,I don’t quite understand this place, but the result is correct.

3、 Full code:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
y_true = np.ones((3, 1), dtype=np.int)
y_pred = np.ones((3, 1), dtype=np.int)
y = np.c_[y_true, y_pred]
 
accuracy = np.zeros(shape=(y_true.shape[0], 1), dtype=np.str)
 
#At this time, if you set accuracy [0, 0] = '0.89', the final accuracy [0, 0] is' 0 ', and the specific reason is not clear
 
res = np.c_[y, accuracy]  #Splice it first
res[0, 2] = '0.89'  #Then set it again
 
res = pd.dataframe(res, columns=['y_true', 'y_pred', 'accuracy'])
res.to_csv('1.csv'#Save to file

On the problem of creating empty array with Python numpy

When reading from the file, read it directly, and the blank space is assigned Nan

?
1
2
3
a = pd.read_csv('1.csv', usecols=(1, 2, 3))
a = a.values
print(a, type(a), a.dtype)

On the problem of creating empty array with Python numpy

About NP Nan should pay attention to the following:

  • np. Nan is not an empty object.
  • “= = NP. Nan” cannot be used to judge the operation of Nan in the list. Only NP Isnan().
  • np. The data type of Nan is float.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np
 
np.nan == np.nan
out[3]: false
 
aa = np.array([1,2,3,np.nan,np.nan,4,5,np.nan])
aa
out[5]: array([  1.,   2.,   3.,  nan,  nan,   4.,   5.,  nan])
 
aa[aa==np.nan] = 100  #Wrong way
aa
out[7]: array([  1.,   2.,   3.,  nan,  nan,   4.,   5.,  nan])
 
aa[np.isnan(aa)] = 100  #The correct way to operate on Nan
aa
out[9]: array([   1.,    2.,    3.100.100.,    4.,    5.100.])
 
type(np.nan)
out[10]: float

For reference:http://www.zzvips.com/article/201492.html

This is the end of this article on the creation of empty arrays by Python numpy. For more information about creating empty arrays by numpy, please search the previous articles of developeppaper or continue to browse the relevant articles below. I hope you will support developeppaper in the future!

Recommended Today

MySQL related

Indexes Bottom structure Disadvantages of hash table index: Using hash storage requires adding all files to memory, which consumes more memory space If all queries are equivalent queries, the hash is indeed fast, but in the actual scene, more data is found, and not all queries are equivalent queries, so the hash table is not […]