1. 数组
1.1 数学操作
# -*- coding: utf-8 -*-
import numpy as np
a = [1, 2, 3, 4] # a为列表,若此时执行 a + 1 将报错
a = np.array(a)
a # array([1, 2, 3, 4])
a + 1 # array([2, 3, 4, 5])
b = array([2, 3, 4, 5])
a + b # array([3, 5, 7, 9])
a * b # array([2, 6, 12, 20])
a ** b # array([1, 8, 81, 1024])
1.2 提取数组中的元素
a[0] # 1
a[:2] # array([1, 2]) # 提取前两个元素
a[-2:] # array([3, 4]) #提取最后两个元素
1.3 修改数组形状,多维数组
a.shape # (4L,) 查看array形状
a.shape = 2,2 # 修改array形状
a # array([[1, 2], [3, 4]])
a * a # array([[1, 4], [9, 16]]) 对应元素乘积
1.4 画图
a = linspace(0, 2*pi, 21)
a
>>array([ 0. , 0.314, 0.628, 0.942, 1.257, 1.571, 1.885, 2.199,
2.513, 2.827, 3.142, 3.456, 3.77 , 4.084, 4.398, 4.712,
5.027, 5.341, 5.655, 5.969, 6.283])
b = sin(a)
b
>>array([ 0.000e+00, 3.090e-01, 5.878e-01, 8.090e-01, 9.511e-01,
1.000e+00, 9.511e-01, 8.090e-01, 5.878e-01, 3.090e-01,
1.225e-16, -3.090e-01, -5.878e-01, -8.090e-01, -9.511e-01,
-1.000e+00, -9.511e-01, -8.090e-01, -5.878e-01, -3.090e-01,
-2.449e-16])
网友评论