概述
# coding: utf-8
# In[1]:
import numpy
# In[43]:
numbers=numpy.array([1,2,3,4.0])
numbers.dtype
# In[48]:
print(numbers.astype(int))
print(numbers.min())
print(numbers.max())
# In[34]:
vector=numpy.array([5,10,15,20])
vector == 10
equal_to_ten = (vector==10)
equal_to_ten_and_five = (vector==5)&(vector==10)
equal_to_ten_or_five = (vector==5)|(vector==10)
print(vector[0:3])
print(equal_to_ten)
print(vector[equal_to_ten])
print(equal_to_ten_and_five)
print(equal_to_ten_or_five)
# In[26]:
matrix=numpy.array( [ [5,10,15],[20,25,30],[35,40,50] ] )
column25 =(matrix[:,1]==25)
print(column25)
print(matrix[column25,:] )
# In[49]:
print(matrix[:,0:2])
print(matrix[1:3,0:2])
print(matrix.sum(axis=0))
print(matrix.sum(axis=1))
# In[54]:
a = numpy.arange(15)
b = a.reshape(3,5)
print(b)
print(b.shape)
print(b.ndim)
print(b.dtype.name)
print(b.size)
# In[55]:
numpy.zeros((3,4))
# In[56]:
numpy.ones((2,3,4),)
# In[57]:
numpy.random.random((2,3))
# In[60]:
from numpy import pi
numpy.linspace(0,2*pi,20)
# In[63]:
a = numpy.array([10,20,30,40])
b = numpy.arange(4)
print(a)
print(b)
c = b-a
print(c)
c = c-1
print(b*2)
print(b**2)
print(a<35)
# In[67]:
A = numpy.array( [ [1,1],[0,1] ] )
B = numpy.array( [ [2,0],[3,4] ] )
print(A*B)#对应位置相城
print('-'*50)
print(A.dot(B))#矩阵相乘
print('-'*50)
print(numpy.dot(A,B))
# In[69]:
C = numpy.arange(3)
print(numpy.exp(C))#e的C矩阵元素次方
print(numpy.sqrt(C))
# In[76]:
D = numpy.floor(10*numpy.random.random((3,4)))#floor向下取整
print('D=')
print(D)
print('-'*25)
E = D.ravel()#与reshape相反,flatten the array
print('E=')
print(E)
print('-'*25)
F = E.reshape(2,6)
print('F=')
print(F)
G = D.T #转置
print('G=')
print(G)
# In[81]:
H = numpy.floor(10*numpy.random.random((2,2)))
I = numpy.floor(10*numpy.random.random((2,2)))
print(H)
print('-'*25)
print(I)
print('_'*25)
print(numpy.hstack((H,I)))#矩阵横向拼接horizon stack(拼接)
print(numpy.vstack((H,I)))#矩阵横向拼接vertical stack(拼接)
# In[87]:
J= numpy.floor(10*numpy.random.random((6,9)))
print(J)
print('_'*25)
print(numpy.hsplit(J,3))#横向竖切,平均切分三份
print(numpy.hsplit(J,(3,4)))#在第三列和第四列切两刀,定点切分
print(numpy.vsplit(J,3))#竖向横切,平均切分三份
print(numpy.vsplit(J,(3,4)))#在第三行和第四行切两刀,定点切分
# In[101]:
#赋值的3种方式
a = numpy.arange(12)
b = a#引用同一个地址
print(b is a)
b.shape = 3,4
print(a.shape)
print(id(a))#查看内存地址
print(id(b))
# In[102]:
c = a.view()#浅复制
print(c is a)#二者指向位置不同
c.shape = 2,6
print(a.shape)
c[1,3]=1234
print(a)
print(id(a))
print(id(c))
# In[106]:
d = a.copy()#深复制
d is a
d[0,0] = 9999
print(d)
print(a)
print(id(a))
print(id(d))
# In[112]:
data = numpy.sin(numpy.arange(20)).reshape(5,4)
print(data)
index = data.argmax(axis=0)
print(index)
data_max = data[index,range(data.shape[1])]
print(data_max)
#help(range)
# In[114]:
a = numpy.arange(0,50,10)
print(a)
b = numpy.tile(a,(3,2))#操作扩展
print(b)
# In[115]:
a = numpy.array( [[4,3,5],[1,2,1]] )
print(a)
b = numpy.sort(a,axis=1)
print(b)
a.sort(axis=1)
a = numpy.array([4,3,1,2])
j = numpy.argsort(a)#将元素从小到大排序后的index输出
print('-'*25)
print(j)
print('-'*25)
print(a[j])
最后
以上就是风趣星星为你收集整理的python---numpy矩阵运算库常用函数的全部内容,希望文章能够帮你解决python---numpy矩阵运算库常用函数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复