>

LoeiJe

:D 获取中...

何以解忧?唯有暴富

numpy学习笔记

numpy学习笔记
numpy 简介

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 2019-10-23
# numpy 学习笔记
# icenaive
# 参考: https://github.com/lijin-THU/notes-python
# 仅供个人学习使用
#
# 1. numpy 简介

# from numpy import *
import numpy
import matplotlib.pyplot as plt

# 列表每个元素加1 不支持
a = [1, 2, 3, 4]
# a + 1

# 转换为array
# array 支持每个元素加1 与另一个array对应元素相加 对应元素相乘 对应元素乘方
a = array(a)
print(a)

# a + 1
print(a + 1)

b = array([2, 4, 5, 6])
print(a + b)
print(a * b)
print(a ** b)

# 提取数组中元素 切片

print(a[0])
print(a[-1])

# 前两个
print(a[:2])
# 后两个
print(a[-2:])

# 反向索引
print(a[:-3])

print(a[:2] + a[-2:])

# 修改数组形状
print(a.shape)
a.shape = 2, 2
print(a.shape)

# 多维数组 对应元素操作 不是矩阵
print(a, a + a, a * a)

# 画图
# linspace 用来生成一组等间隔的数据:
a = linspace(0, 2*pi, 21)
print(a)

# 三角函数
b = sin(a)
print(b)

#
# plt.figure()
# plt.plot(a, b)
# plt.show()

# 从数组中选取元素
# 选取正数
# b > 0
print(b > 0)
points = b > 0
# plt.plot(a[points], b[points], 'ro')
# plt.show()