numpy的用法-03

#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])

























  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值