二、numpy
import numpy
as np
newlist
= [1,2,3]
print('列表',newlist
)
print(type(newlist
))
arr1
=np
.array
(newlist
)
print('数组',arr1
)
print(type(arr1
))
arr1
= np
.random
.rand
(3,4)
arr1
= np
.random
.uniform
(5,10,size
=(4,4,5))
arr1
= np
.random
.randint
(-1,20,(4,4))
arr2
= np
.random
.randint
(-1,552147483648,(4,4),'int64')
arr1
=np
.array
([[1,2,3],[4,5,6]],np
.int32
)
arr1
=np
.zeros
((3,4))
arr1
=np
.ones
((3,4))
arr2
=np
.arange
(1,13,1)
arr1
=arr2
.reshape
(3,4)
print(arr1
)
print('维度的个数',arr1
.ndim
)
print('维度的大小',arr1
.shape
)
print('数据类型',arr1
.dtype
)
arr2
= np
.array
([1,2,3,4,'asd123'])
注意点:
1、np
.array
(list,type)
数组的属性
import numpy
as np
arr2
= np
.array
([1,2,3,4,'asd123'])
print('数组维度的个数',arr2
.ndim
)
print('数组的维度大小',arr2
.shape
)
print('数据类型',arr2
.dtype
)
调整数组的维度大小
def reshape(self
, shape
, *shapes
, order
='C'):
"""
a
.reshape
(shape
, order
='C')
Returns an array containing the same data
with a new shape
.
使用:
arr2
= np
.array
([3,4,5,6,7,8]).reshape
(2,3)
print(arr2
)
结果:二维数组,含有两个一维数组,每个一维数组有三个元素
[[3 4 5]
[6 7 8]]
注意点:
1、旧的数组的元素个数与新数组的元素个数必须相同
2、reshape这个方法存在返回值,返回新数组
转载请注明原文地址: https://lol.8miu.com/read-38713.html