首页 > 其他分享 >numpy的用法-03

numpy的用法-03

时间:2023-02-06 19:32:21浏览次数:42  
标签:03 data random arange 用法 np print array numpy


#coding=utf-8

import numpy as np
import numpy as pi

a = np.arange(15).reshape(3,5) #arange����0-14������ reshape���3*5�ľ���
print(a)

print(a.shape) #输出行和列的长度

print(a.ndim)#the number of axes (dimensions) of the array

print(a.dtype.name) #数据类型

#the total number of elements of the array
print(a.size)

print(np.zeros((3,4))) #zeros参数为元组,生成3*4的0矩阵

print(np.ones( (2,3,4), dtype=np.int32 ))

#To create sequences of numbers
#arange(起始位置,结束位置(不包含),间隔)
print(np.arange(10,20,5)) #array([10, 15, 20, 25])
np.arange( 0, 2, 0.3 )

print(np.random.random((2,3))) #随机生成2*3矩阵,且值为-1~1

print(np.linspace( 0, 2*6, 100 )) #把0~12分割为100份

np.sin(np.linspace( 0, 2*6, 100 ))

#the product operator * operates elementwise in NumPy arrays
a = np.array( [20,30,40,50] )
b = np.arange( 4 )
#print a
#print b
#b
c = a-b
#print c
b**2
#print b**2
print a<35


#The matrix product can be performed using the dot function or method
A = np.array( [[1,1],
[0,1]] )
B = np.array( [[2,0],
[3,4]] )
print A
print B
#print A*B #对应位置的乘法
print A.dot(B) #代表矩阵的规则相乘
print np.dot(A, B)


B = np.arange(3)
print B
#print np.exp(B)
print np.sqrt(B)


#Return the floor of the input
a = np.floor(10*np.random.random((3,4)))
print(a)
print(a.ravel()) #变成一维的向量
a.shape = (6, 2)
print(a)
print(a.T) # 矩阵的转置
print(a.resize((2,6))) #修改矩阵的行列
print(a)
#If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
a.reshape(3,-1) #把a分层3行,-1代表自动计算



a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print('---')
print(b)
print('---')
print(np.hstack((a,b))) #横向合并
np.vstack((a,b)) #纵向合并


a = np.floor(10*np.random.random((2,12)))
print(np.hsplit(a, 3)) #按列分成三部分
print(np.hsplit(a,(3,4))) # Split a after the third and the fourth column
a = np.floor(10*np.random.random((12,2)))
print(a)
np.vsplit(a,3) #按行分成三部分


#Simple assignments make no copy of array objects or of their data.
a = np.arange(12)
b = a
# a and b are two names for the same ndarray object
print(b is a)
b.shape = 3,4
print(a.shape)
print(id(a))
print(id(b))

#The view method creates a new array object that looks at the same data.
c = a.view()
print(c is a)
c.shape = 2,6
print(a.shape)
c[0,4] = 1234
print(c)
print(a)

#The copy method makes a complete copy of the array and its data.
d = a.copy()
d is a
d[0,0] = 9999
print(d)
print(a)


data = np.sin(np.arange(20)).reshape(5,4)
print(data)
ind = data.argmax(axis=0) # argmax取出每列中最大的索引的下标
print(ind)
data_max = data[ind, range(data.shape[1])] #xrange与range相同,而是一个生成器。输出最大值的向量
print(data_max)
all(data_max == data.max(axis=0)) # max 返回每一列的最大值


a = np.array([
[4, 3, 5],
[1, 2, 1]
])
b = np.sort(a,axis=1)
print(b)
print(a.sort(axis=1))


a = np.array([4, 3, 1, 2])
j = np.argsort(a) #argsort 每个数的索引从小到大的排序
print(j)
print(a[j])


标签:03,data,random,arange,用法,np,print,array,numpy
From: https://blog.51cto.com/u_15955675/6040313

相关文章