2023-7-20 myluzh
Python
0x01 逻辑运算
import numpy as np
# 生成40-100的10行5列数据
arr1 = np.random.randint(40, 100, (10, 5))
"""
#查看下arr1
arr1 Out[3]:
array([[95, 65, 74, 76, 64],
[95, 62, 74, 81, 59],
[48, 93, 87, 40, 63],
[99, 68, 57, 95, 51],
[75, 78, 43, 50, 49],
[75, 74, 86, 50, 98],
[62, 61, 53, 40, 73],
[58, 63, 99, 76, 85],
[52, 44, 65, 77, 51],
[90, 74, 93, 78, 46]])
"""
# 取6行开始取,每行取0-5列
arr2 = arr1[6:, 0:5]
"""
# 查看下arr2
arr2 Out[4]:
array([[62, 61...
阅读全文>>
标签: python ndarray
评论(0)
(439)
2023-7-13 myluzh
Python
0x01 ndarray属性
属性名字
属性解释
ndarray.shape
数组维度的元组
ndarray.ndim
数组维数
ndarray.size
数组中的元素数量
ndarray.itemsize
个数组元素的长度 (字节)
ndarray.dtype
数组元素的类型
0x02 ndarray形状
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.array([1,2,3,4])
c = np.array([[[1,2,3], [4,5,6]],[[1,2,3],...
阅读全文>>
标签: python numpy ndarray
评论(0)
(318)
2023-7-13 myluzh
Python
0x01 ndarray与list运行时长对比
import random
import numpy as np
import timeit
a = []
for i in range(100000000):
a.append(random.random())
def sum_list():
return sum(a)
def sum_numpy():
b = np.array(a)
return np.sum(b)
time_list = timeit.timeit(sum_list, number=1)
time_numpy = timeit.timeit(sum_numpy, number=1)
print("List sum time:", time_list)
print("Numpy sum time:", time_numpy)
List sum time: 6.816154757
Numpy sum time: 4.999972694
0x02 ndarray的优势
(1)ndarray在存储数据的时候,数据...
阅读全文>>
标签: numpy ndarray
评论(0)
(301)