Section 1 numpy的使用

import numpy 
vector = numpy.array([ 1,2,3,4 ] )  #创建一行四列矩阵
print(vector.shape) #打印vector的尺寸


matrix = numpy.array([[1,2,3,4],[4,5,6,7]]) #创建一个两行四列的矩阵
print(matrix.shape) #打印矩阵信息


numbers = numpy.array([1,2,3,4]) 
print(numbers.dtype)    #打印array内元素的类型


numbers = numpy.array([1,2,3,'4'])  #将其中一个数字改为字符型
print(numbers.dtype) #打印结果就显示是字符型


number = numpy.array([5,10,15,20]) 
print(number[0,3])  #打印结果为5,10,15,不会打印20


matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])


print(matrix[:,1])    #打印第二列
print(matrix[0,:])    #打印第一行
print(matrix[1,:])    #打印第二行
print(matrix[:,0:2]) #打印前两列


vector = numpy.array([5,10,15,20])
vector ==10        #输出布尔变量, 如果等于10就返回true,不等于就false
equal_to_ten = (vector ==10)
print equal_to_ten  #返回布尔向量数组


print(vector[equal_to_ten])  #返回vector里可以返回true的值


matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])


second_column_25 = (matrix[:,1]==25) #查看哪一列中,包括25这个数,返回布尔变量
#返回布尔变量
print second_column_25                        #查看布尔变量
print(matrix[second_column_25, :])       #查看具体值


 vector = numpy.array([5,10,15,20]) 
 equal_to_ten_and_five = (vector ==10) &(vector == 5 ) #返回布尔变量,
 print equal_to_ten_and_five 
 equal_to_ten_or_five = (vector ==10) | (vector == 5 ) #返回布尔变量
 print equal_to_ten_or_five
 
  vector = numpy.array(["1","2","3","4"]) 
  print(vector.dtype)      #返回S1,表示这是字符串变量
  
 vector = vector.astype(float) #将字符变量改为浮点变量
 print(vector.dtype)                 #打印字符类型,显示float64,表示类型变了
 
 vector = numpy.array([5,10,20,15])  #
 vector.min() #求vector里的最小值
 
 martix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
martix.sum(axis=0)   #求martix每一列的和
martix.sum(axis=1)   #求martix每一行的和


import numpy as np
print(np.arange(15)) #打印0,1,2,3,4


a=np.arange(15).reshape(3,5) #一个3*5的矩阵,元素值为0~14
print(a)
a.shape #a的形状


a.ndim #a的维度
a.size   #a的大小
a.dtype #a的类型
np.zeros(3,4) #创建一个3*4的矩阵,矩阵元素全部为0
np.ones((3,4)) #创建一个3*4的矩阵,矩阵元素全部为1
np.ones((2,3,4), dtype = np.int32) #
np.arange(10,30,5) #10,15,20,25


np.arrange(12).reshape(3,4) 


np.random.random((2,3))

np.linspace(0,2*pi,100) 

 a = np.array([20,30,40,50]) #[20,30,40,50]
 b = np.arrange(4)               #[0 1 2 3]
 c=a-b                                  #[20 29 38 47]
 
 c=c-1 #[19,28,37,46]
 b**2                #b内的元素都平方[0 1 4 9]
 
print(a<35)                         #true true flase flase


A = np.array([[1,1],[0,1]])  
B = np.array([[2,0],[3,4]]) 
 
 print(A*B) #[2 0] [0 4]
 print(A.dot(B))


 B = np.arrange(3)  #0,1,2
 print(np.exp(B))  #[1   2.718  7.3890]
print(np.sqrt(B)) 
 
 a = np.floor(10*np.random.random((3,4))) 
 print(a)
 
 print(a.revel())   #将元素平摊
 a.shape[6,2]
 
 print(a.T) #求a的转置
 a.shape(3,-1) #3行,列自己算
 
 
 np.hstack((a,b)) 进行行拼接
 np.vstack((a,b))  进行列拼接
 
 a=np.arange(12) 从0到11
b=a   #从此b和a一模一样
print(b is a ) #返回true
c = a.view() #浅复制
print(c is a ) #return false


d = a.copy()  #d 和a没有关系


 a = np.arange(0,40,10) 
 b = np.tile(a,(2,2))   #变成[0 10 20 30   0 10 20 30]
[0 10 20 30  0 10 20 30]
 
 b = np.sort(a,axis=1) #进行行排序
 b = np.sort(a,axis=0) #进行列排序
 
 b = np.argsort(a)       #进行排序
 




 
 
 

猜你喜欢

转载自blog.csdn.net/qq_41858768/article/details/79604032