python numpy相關操作

2021-10-05 02:40:24 字數 2102 閱讀 1349

array = np.array([[1, 2, 3],

[2, 3, 4]])

print(array)

print(array.shape)

print(array.size)

a = np.array(([2, 3, 4], [1, 2, 3]), dtype=np.int)

print(a, a.dtype, sep=』\n』)

a = np.zeros((3, 4))

a1 = np.ones((3, 4))

a2 = np.empty((3, 4))

a3 = np.arange(10, 20, 2)

a4 = np.arange(12).reshape(3, 4)

a5 = np.linspace(1, 10, 4)

print(a, a1, a2, a3, a4, a5, sep=』\n』)

d = np.array([[1, 2, 3],

[2, 3, 4]])

e = np.arange(6).reshape(3, 2)

print(d, e, sep=』\n』)

c = d*e

f = np.dot(d, e) # – = a.dot(b)

print(f, sep=』\n』)

a = np.random.random((2, 4))

print(a, np.sum(a, axis=1), np.max(a))

a = np.arange(0, 12, 1).reshape(3, 4)

print(a, np.mean(a, axis=1), a.max(axis=1), a.cumsum(), np.diff(a), sep=』\n』)

print(np.nonzero(a))

print(np.sort(a))

print(np.transpose(a)) # 裝置

print((a.t).dot(a)) # a的轉置乘a

print(np.clip(a, 5, 9)) # 大於9的都是9,小於5的都是5

a = np.arange(3, 15).reshape(3, 4)

print(a)

print(a[1, 3])

print(a[1:3, 1:3])

print(a.flatten()) # --將a平鋪開來

for row in a: # --迭代行

print(row)

for column in a.t: # – 迭代列

print(column)

for item in a.flat: # --將a變為乙個迭代項

print(item)

a = np.array([1, 1, 1])

b = np.array([2, 2, 2])

c = np.vstack((a, b)) # --上下合併

d = np.hstack((a, b)) # --左右合併

print(c, d)

print(a[:, np.newaxis]) # 縱向的輸出

e = np.array([1, 1, 1])[:, np.newaxis]

f = np.array([2, 2, 2])[:, np.newaxis]

g = np.hstack((e,f))

print(g)

h = np.concatenate((e, f), axis=0)

print(h)

a = np.arange(12).reshape(3, 4)

print(a)

print(np.split(a, 2, axis=1))

print(np.array_split(a, 3, axis=1)) # --不等分割

print(np.vsplit(a, 3)) # --橫向分割,縱向分割

print(np.hsplit(a, 2))

a = np.arange(4).reshape((2, 2))

b = a.copy(a)

c = a

d = b

a[0] = 11

print(a, b, c, d)

print(b is a)

python numpy基本操作

import numpy as np a np.array 1 2,3 4 b np.array 5 6,7 8 c np.array 1,2,3,4 4,5,6,7 7,8,9,10 print c array 1,2,3,4 4,5,6,7 7,8,9,10 a.shape 4,b.shape ...

python Numpy基本操作

python 幾天不用就覺得生疏,因此記錄一下 便於下次使用 import numpy as np 生成方式 np.array np.arange np.linspace 隨機數 np.random.random 2,3 np.random.randint 10,size np.random.ran...

python numpy切片操作

python numpy中的切片與索引 import numpy as np import random a np.round random.random for i in range 10000 2 reshape 2500,4 print a print a 2 end n我是第二行 n pri...