菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
242
0

numpy

原创
05/13 14:22
阅读数 26782

1.numpy常用函数

1.random

import numpy as np
np.random.random((3,3))

array([[ 0.29870525,  0.46127543,  0.11524519],
       [ 0.83500081,  0.62489366,  0.48149446],
       [ 0.00748799,  0.32096626,  0.91338608]])

2.arange 和 reshape

a=np.arange(2,20,2).reshape(3,3)
print(a)
b=np.arange(1,5,0.7).reshape(2,3)
print(b)

[[ 2  4  6]
 [ 8 10 12]
 [14 16 18]]
[[ 1.   1.7  2.4]
 [ 3.1  3.8  4.5]]

3.ndim dtype  size

print (a.ndim)
# the type of the value
print (a.dtype.name)
# the total number of elements of the array
print (a.size)

2
int32
9

4.linspace

np_linespace=np.linspace(1,50,100)
print(np_linespace)

[  1.           1.49494949   1.98989899   2.48484848   2.97979798
   3.47474747   3.96969697   4.46464646   4.95959596   5.45454545
   5.94949495   6.44444444   6.93939394   7.43434343   7.92929293
   8.42424242   8.91919192   9.41414141   9.90909091  10.4040404
  10.8989899   11.39393939  11.88888889  12.38383838  12.87878788
  13.37373737  13.86868687  14.36363636  14.85858586  15.35353535
  15.84848485  16.34343434  16.83838384  17.33333333  17.82828283
  18.32323232  18.81818182  19.31313131  19.80808081  20.3030303
  20.7979798   21.29292929  21.78787879  22.28282828  22.77777778
  23.27272727  23.76767677  24.26262626  24.75757576  25.25252525
  25.74747475  26.24242424  26.73737374  27.23232323  27.72727273
  28.22222222  28.71717172  29.21212121  29.70707071  30.2020202
  30.6969697   31.19191919  31.68686869  32.18181818  32.67676768
  33.17171717  33.66666667  34.16161616  34.65656566  35.15151515
  35.64646465  36.14141414  36.63636364  37.13131313  37.62626263
  38.12121212  38.61616162  39.11111111  39.60606061  40.1010101
  40.5959596   41.09090909  41.58585859  42.08080808  42.57575758
  43.07070707  43.56565657  44.06060606  44.55555556  45.05050505
  45.54545455  46.04040404  46.53535354  47.03030303  47.52525253
  48.02020202  48.51515152  49.01010101  49.50505051  50.        ]

5.dot

第一个数=第一行*第一列

np_dot1=np.array([[1,2],[3,4]])
print(np_dot1)
np_dot2=np.array([[1,1],[0,1]])
print(np_dot2)
print(np.dot(np_dot1,np_dot2))
#[[1*1+2*0 1*1+2*1]
#[3*1+4*0 3*1+4*1]]

[[1 2]
 [3 4]]
[[1 1]
 [0 1]]
[[1 3]
 [3 7]]
import numpy as np

x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])

v = np.array([9,10])
w = np.array([11, 12])

# Inner product of vectors; both produce 219
print (v.dot(w))
print (np.dot(v, w))

# Matrix / vector product; both produce the rank 1 array [29 67]
print (x.dot(v))
print (np.dot(x, v))

print(v.dot(x))

# Matrix / matrix product; both produce the rank 2 array
# [[19 22]
# [43 50]]
print (x.dot(y))
print (np.dot(x, y))
-------------------------------------------------------

219
219
[29 67]
[29 67]
[39 58]
[[19 22]
[43 50]]
[[19 22]
[43 50]]

  问题1.x.dot(v)是怎么算的? v.dot(x)是怎么算的

    答:  

 2.常用操作

1.e的n次方

np_e=np.array([[1,2],
         [3,4]])
print(np.exp(np_e))
#e的n次方

[[  2.71828183   7.3890561 ]
 [ 20.08553692  54.59815003]]

2.floor

np_floor=10*np.random.random((3,4))
print(np.floor(np_floor))

[[ 6.  4.  1.  4.]
 [ 9.  4.  1.  4.]
 [ 7.  6.  6.  0.]]

3.ravel  

print(np.ravel(np_floor))

[ 6.6650322   4.55285191  1.08112183  4.78423965  9.07002475  4.88373098
  1.84273175  4.94375497  7.53521955  6.4163809   6.26857371  0.06157525]

4.reshape(n,-1)

print(np_floor.reshape(3,-1))
#if a dimension is given as -1 in a reshaping operation ,the other dimentions are automatically calculated

[[ 6.6650322   4.55285191  1.08112183  4.78423965]
 [ 9.07002475  4.88373098  1.84273175  4.94375497]
 [ 7.53521955  6.4163809   6.26857371  0.06157525]]

5.hstack(()) 

np_hs1=np.floor(10*np.random.random((2,2)))
print(np_hs1)
np_hs2=np.floor(10*np.random.random((2,2)))
print(np_hs2)
print(np.hstack((np_hs1,np_hs2)))

[[ 4.  5.]
 [ 9.  1.]]
[[ 9.  9.]
 [ 9.  9.]]
[[ 4.  5.  9.  9.]
 [ 9.  1.  9.  9.]]

6.vstack

np_hs1=np.floor(10*np.random.random((2,2)))
print(np_hs1)
np_hs2=np.floor(10*np.random.random((2,2)))
print(np_hs2)
print(np.vstack((np_hs1,np_hs2)))

[[ 0.  5.]
 [ 3.  9.]]
[[ 7.  6.]
 [ 4.  2.]]
[[ 0.  5.]
 [ 3.  9.]
 [ 7.  6.]
 [ 4.  2.]]

7.view 浅复制, copy 深复制

 

 

  

发表评论

0/200
242 点赞
0 评论
收藏