提示:对于黑马人工智能基础课程所做的笔记
import numpy as np
1. hello-numpy
1.1 ndarray与Python原生list运算效率对比
import random
import time
import numpy as np
a = []
for i in range(100000000):
a.append(random.random())
%time sum1=sum(a)
b=np.array(a)
%time sum2=np.sum(b)
Wall time: 420 ms
Wall time: 111 ms
2. ndarray的属性
b.shape
(100000000,)
b.ndim
1
b.size
100000000
b.itemsize
8
b.dtype
dtype('float64')
创建数据时指定类型
b = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
b
array([[1., 2., 3.],
[4., 5., 6.]], dtype=float32)
arr = np.array(["pythonI", "hello", "I"], dtype=np.string_)
arr
array([b'pythonI', b'hello', b'I'], dtype='|S7')
|