1. Introduction of Da Hang:
Be sure to declare the variable type of the variables stored in the array when generating the array; otherwise, really, this error will not be found for a few days! It's too hidden!
The official website has quietly hinted you:
In general, it is caused by the lack of solid basic knowledge. One thing that is overlooked is:
List in Python is Python's built-in data type. The data classes in list need not be the same , and the types in array must all be the same . The data type in the list saves the address where the data is stored , which is simply a pointer, not data, so it is too troublesome to save a list, for example list1 = [1,2,3, ' a ' ] requires 4 Pointer and four data, increase storage and consume CPU.
2. Da Hang code:
>>> a = [0,3,2] >>> b = [0,8,2] >>> c = np.array(list(zip(a,b))) >>> c = np.mean([[0,0],[1,1]]) >>> c 0.5 >>> c = np.mean([[0,0],[1,1]],axis = 0) >>> c Array ([ 0.5, 0.5 ]) # see this little partner, no problem ah? What's going on, little brother! # Let's take a closer look >>> c = np.array (list (zip (a, b))) >>> c [0] = np.mean ([[0,0], [1,1] ], axis = 0) >>> c [0] array([0, 0]) # What's going on? How to find the average value of each column direction is 0, shouldn't it be array [0.5,0.5]
The problem is here, I gave c an attribute of an array in the "grey row" before, and it started to store
array([[0, 0],
[3, 8],
[2, 2]])
Such a data, because the elements in it are all int, so the system thinks that the array you want is int, but in the end? You want it to store the float type, as the "good heart" of the system, it will definitely be converted to int. It was precisely this "good intention" that caused the program to go wrong.
Method: Add data type
>>> c = np.array(list(zip(a,b)),dtype = np.float32) >>> c[0] = np.mean([[0,0],[1,1]],axis = 0) >>> c[0] array ([ 0.5, 0.5], dtype = float32) # That's right!
3. Data types in numpy:
type of data
|
description
|
bool_
|
Boolean (True or False), stored as a byte
|
int_
|
Default integer type (same as Clong; usually int64 or int32)
|
INTC
|
Same as Cint (usually int32 or int64)
|
INTP
|
Integer for indexing (same as Cssize_t; usually int32 or int64)
|
INT8
|
Bytes (-128 to 127)
|
INT16
|
Integer (-32768 to 32767)
|
INT32
|
Integer (-2147483648 to 2147483647)
|
Int64
|
Integer (-9223372036854775808 to 9223372036854775807)
|
UINT8
|
Unsigned integer (0 to 255)
|
UINT16
|
Unsigned integer (0 to 65535)
|
UINT32
|
Unsigned integer (0 to 4294967295)
|
UINT64
|
Unsigned integer (0 to 18446744073709551615)
|
float_
|
Short for float64.
|
float16
|
Half precision floating point: sign bit, 5 exponent, 10 mantissa
|
FLOAT32
|
Single-precision floating-point number: sign bit, 8-bit exponent, 23-bit mantissa
|
float64
|
Double precision floating point: sign bit, 11 exponent, 52 mantissa
|
complex_
|
Abbreviation of complex128.
|
complex64
|
Complex numbers, composed of two 32-bit floating-point numbers (real and imaginary)
|
complex128
|
Complex numbers, composed of two 64-bit floating-point numbers (real and imaginary)
|
4. Reference:
https://www.numpy.org.cn/ (Official website link)
https://www.cnblogs.com/hackpig/p/8183470.html (the difference between list and array)
https://www.cnblogs.com/chenhuabin/p/11412818.html (data type in numpy) <------------------------- It is highly recommended that the students who learn Matrix take a look