array([1, 2, 3, 4])
numpy 索引和切片
1 # 基本索引
2 ar = np.arange(20)
3 # print(ar)
4 # print(ar[4])
5 # print(ar[:3])
6 # 一維的
7 ar = np.arange(16).reshape(4,4)
8 print(ar)
9 # print(ar[0]) # 切出一行 切片爲下一個維度的一個元素
10 # print(ar[0][-1]) # 二次索引,獲得一維中的一個值
11 print(ar[:3]) # 切出多行,
12 print(ar[1,1]) # 相似於二次索引
13 print(ar[:2, 1:])
14 # 二維的
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
5
[[1 2 3]
[5 6 7]]
1 # 布爾型索引
2 ar = np.arange(12).reshape(3,4)
3 i = np.array([True, False, True])
4 j = np.array([False, True, False, True])
5 # print(ar)
6 # print(i)
7 # print(j)
8 # print(ar[i])
9 # print(ar[i,:]) # 選行
10 # print(ar[:, j]) # 選列
11 # 基本的布爾型索引
12 ai = ar > 5
13 print(ar)
14 print(ai)
15 print(ar[ar % 2 != 0]) # 選取全部的奇數
16 print(ar[i,j])
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[False False False False]
[False False True True]
[ True True True True]]
[ 1 3 5 7 9 11]
[ 1 11]
numpy 隨機數
numpy.random數組
1 # 生成
2 samples = np.random.normal(size=(4,4)) # 符合標準正態分佈的4*4樣本值
3 print(samples)
[[ 0.85235645 0.85512049 -0.29868382 1.03635971]
[-1.42338011 -1.20987794 1.74704175 0.08565753]
[ 1.11191625 0.5263941 0.21444175 0.08820261]
[ 0.60592032 0.93473874 -0.28916297 -1.41480416]]
1 # numpy.random.rand() : [0, 1) 之間的隨機樣本或N維浮點數組 ---均勻分佈
2 import matplotlib.pyplot as plt
3
4 a = np.random.rand()
5 b = np.random.rand(4)
6 c = np.random.rand(3,4)
7 s1 = np.random.rand(1000)
8 s2 = np.random.rand(1000)
9 plt.scatter(s1, s2)
10 plt.show()
![](http://static.javashuo.com/static/loading.gif)
1 # numpy.random.randn() :生成一個浮點數或者N維的浮點數組---正態分佈
2 s1 = np.random.randn(1000)
3 s2 = np.random.randn(1000)
4 plt.scatter(s1, s2)
5 plt.show()
![](http://static.javashuo.com/static/loading.gif)
1 # numpy.random.randint(low, high=None, size=None, dtype='l'): 生成一個整數或者N維的整數數組
2 # 若high不爲None時,取[low,high)之間隨機整數,不然取值[0,low)之間隨機整數,且high必須大於low
3 # dtype參數:只能是int類型
4 np.random.randint(2)
5 np.random.randint(2, 6, size=5)
6 np.random.randint(1,100, size=[3,5])
array([[30, 84, 1, 94, 18],
[95, 57, 92, 38, 82],
[95, 25, 33, 74, 57]])
numpy的輸入輸出
讀寫數組數據,文本數據dom
1 # 二進制文件
2 # 存數據
3 import numpy as np
4 ar = np.random.rand(5,5)
5 np.save('test.npy',ar)
6 np.load('test.npy')
array([[0.61427194, 0.69416288, 0.1939631 , 0.60784977, 0.25270645],
[0.27015826, 0.21855706, 0.86313053, 0.81490094, 0.72308043],
[0.95732625, 0.08912547, 0.58596282, 0.75812357, 0.43485775],
[0.41967473, 0.42947078, 0.98010856, 0.26209422, 0.69600965],
[0.95892746, 0.48951498, 0.98279041, 0.44956069, 0.41290226]])
1 # 文本數據
2 # 存
3 np.savetxt('test.txt', ar, delimiter=',')
1 # 讀取
2 np.loadtxt('test.txt', delimiter=',')
array([[0.61427194, 0.69416288, 0.1939631 , 0.60784977, 0.25270645],
[0.27015826, 0.21855706, 0.86313053, 0.81490094, 0.72308043],
[0.95732625, 0.08912547, 0.58596282, 0.75812357, 0.43485775],
[0.41967473, 0.42947078, 0.98010856, 0.26209422, 0.69600965],
[0.95892746, 0.48951498, 0.98279041, 0.44956069, 0.41290226]])總結思惟導圖:https://img2018.cnblogs.com/blog/1816772/201910/1816772-20191023000450837-1220857330.png