numpy练习
作者:互联网
import numpy as np
array1 = np.array([1,2,3,4])
print(array1)
array2 = np.array((1,2,3,4))
print(array2)
array3 = np.array([[1,2,3,4],
[5,6,7,8]])
print(array3.ndim)
array4 = np.arange(5)
print(array4)
array5 = np.arange(50,55, dtype = float)
print(array5)
array6 = np.array([np.arange(5),
np.arange(5,10),
np.arange(50,55,dtype = float)])
print(array6)
array7 = np.genfromtxt(r'data.txt', delimiter=',', dtype=int)
print(array7)
print(array7.ndim)
print(array7.shape)
print(array7.size)
print(array7.dtype)
print(array7[1])
print('----------------')
print(array7[0:3][0:1])
print(array7[0:3:2])
print('----------------')
example = np.array([
np.arange(6),
np.arange(6,12),
np.arange(12,18),
np.arange(18,24),
])
print(example)
result = example.reshape(3, 8)
print(result)
print(example)
result2 = example.reshape(-1,2)
print(result2)
print('----------------')
result3 = example.resize(3,8)
print(example)
print('----------------')
result4 = example.flatten()
print(result4)
print('----------------')
result5 = example.ravel()
print(result5)
print(example)
print('--------求一维数组--------')
print(array1.max())
print(array1.min())
print(array1.mean())
print(array1.std())
print(array1.sum())
print('--------求二维数组--------')
# axis = 0, 按列,1,按行
print(example)
print(example.max(axis=0))
print(example.min(axis=0))
print(example.mean(axis=0))
print(example.std(axis=0))
print(example.sum(axis=0))
print('------------------')
print(array1 * 2)
print(array1 + 2)
print('------------------')
print(np.linspace(1,10,3))
print('------------------')
q = np.random.randint(0,10,100)
print(q)
t = np.random.random(100)
print(t)
标签:array1,练习,array7,arange,np,print,numpy,example 来源: https://www.cnblogs.com/bigcoolcool/p/16067885.html