numpy学习笔记

In [4]:
 
 
 
 
 
 
 
 
 
import numpy as np
my_list = [1,2,3]
x = np.array(my_list)
print('列表',my_list)
print('Array:',x)
 
 
 
列表 [1, 2, 3]
Array: [1 2 3]
In [5]:
 
 
 
 
 
 
 
 
 
 
np.array([1,2,3])-np.array([4,5,6])
 
 
Out[5]:
array([-3, -3, -3])
In [9]:
 
 
 
 
 
 
 
 
 
 
my_list = [[1,2,3],[4,5,6]]
x = np.array(my_list)
print(x)
print("x's shape is : ",x.shape)
 
 
 
[[1 2 3]
 [4 5 6]]
x's shape is :  (2, 3)
In [10]:
 
 
 
 
 
 
 
 
 
 list  = np.arange(0,30,2)
 print(list)

 
 
 
[ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28]
In [12]:
 
 
 
 
 
 
 
 
new_array = list.reshape(3,5)
print(new_array)
 
 
 
[[ 0  2  4  6  8]
 [10 12 14 16 18]
 [20 22 24 26 28]]
In [18]:
 
 
 
 
 
x
 
 
print('ones:\n',np.ones((3,2)))
print('zeros:\n',np.zeros((3,2)))
print('eye:\n',np.eye((3)))
print('diag:\n',np.diag((1,2,3)))
print('diag:\n',np.diag(my_list))
 
 
 
ones:
 [[ 1.  1.]
 [ 1.  1.]
 [ 1.  1.]]
zeros:
 [[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]
eye:
 [[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
diag:
 [[1 0 0]
 [0 2 0]
 [0 0 3]]
diag:
 [1 5]
In [20]:
 
 
 
 
 
 
 
 
 
x=np.arange(16).reshape((4,4))
print(x)
 
 
 
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
In [21]:
 
 
 
 
 
x
 
 
 
print('diag:\n',np.diag(x,1))
print('diag:\n',np.diag(x,-1))
 
 
 
diag:
 [ 1  6 11]
diag:
 [ 4  9 14]
In [22]:
 
 
 
 
 
 
 
 
print('diag:\n',np.diag(x,2))
print('diag:\n',np.diag(x,-2))
 
 
 
diag:
 [2 7]
diag:
 [ 8 13]
In [23]:
 
 
 
 
 
x
 
 
print("*操作:\n",np.array([1,2,3]*3))
print("*操作:\n",np.repeat([1,2,3],3))
 
 
 
*操作:
 [1 2 3 1 2 3 1 2 3]
*操作:
 [1 1 1 2 2 2 3 3 3]
In [25]:
 
 
 
 
 
 
 
 
 
p1 = np.ones((3,3))
p2 = np.arange(9).reshape(3,3)
print("纵向+\n",np.vstack((p1,p2)))
print("横向+\n",np.hstack((p1,p2)))
 
 
 
纵向+
 [[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 0.  1.  2.]
 [ 3.  4.  5.]
 [ 6.  7.  8.]]
横向+
 [[ 1.  1.  1.  0.  1.  2.]
 [ 1.  1.  1.  3.  4.  5.]
 [ 1.  1.  1.  6.  7.  8.]]
In [27]:
 
 
 
 
 
 
 
 
 
 
  print('p1 + p2 \n',p1+p2)
  print('p1 * p2 \n',p1*p2)
  print('p2^2 \n',p2**2)
  print('p1 . p2 \n',p1.dot(p2))

 
 
 
p1 + p2 
 [[ 1.  2.  3.]
 [ 4.  5.  6.]
 [ 7.  8.  9.]]
p1 * p2 
 [[ 0.  1.  2.]
 [ 3.  4.  5.]
 [ 6.  7.  8.]]
p2^2 
 [[ 0  1  4]
 [ 9 16 25]
 [36 49 64]]
p1 . p2 
 [[  9.  12.  15.]
 [  9.  12.  15.]
 [  9.  12.  15.]]
In [ ]:
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2017-10-13 13:53  在路上-UP  阅读(161)  评论(0编辑  收藏  举报