numpy作业

import numpy as np
c = np.arange(1, 13).reshape(6, 2)
print(c)

[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]]
np.vsplit(c,3)
[[1, 2],
        [3, 4]]), array([[5, 6],
        [7, 8]]), array([[ 9, 10],
        [11, 12]]
import numpy as np
c = np.arange(1, 13).reshape(6, 2)
d = c.T
print(d)


[[ 1  3  5  7  9 11]
 [ 2  4  6  8 10 12]]
np.hsplit(d,3)
[[ 1  3  5  7  9 11]
 [ 2  4  6  8 10 12]]
import numpy as np
a = np.array((3,7,8,9))
b = np.array((10,13,15,20))
e = np.dstack((a, b))
e

array([[[ 3,  10],
        [ 7,  13],
        [ 8,  15],
        [ 9, 20]]])
np.dsplit(e,2)
[array([[[3],
         [7],
         [8],
         [9]]]),
 array([[[ 10],
         [ 13],
         [ 15],
         [20]]])]
inistate =np.array([1,2,3,4])
pre_inistate = inistate[0:3]
pre_inistate
array([0.18257419, 0.36514837, 0.54772256])
import numpy as np
a = np.array([1,1,1,1])
b = np.array([[1],[1],[1],[1]])
a+b
array([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
c = np.array([[1,1,1,1]])
c+b
array([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
W = np.array([[1,1,1],[2,2,2]])
W[:,1]
array([1, 2])
W[:,1] = np.array([5,5])
W
array([[1, 5, 1],
[2, 5, 2]])
import numpy as np
matrix = [
    [1,2,3,4],
    [5,6,7,8],
    [9,10,11,12]
]
p1 = np.delete(matrix, 1, 0)
print('>>>>p1>>>>\n',p1)
>>>>p1>>>>
 [[ 1  2  3  4]
 [ 9 10 11 12]]
p2 = np.delete(matrix, 1, 1) 
print(’>>>>p2>>>>\n’,p2)
p2>>>>
[[ 1 3 4]
[ 5 7 8]
[ 9 11 12]]
p3 = np.delete(matrix, 1) 
print(’>>>>p3>>>>\n’,p3)
p3>>>>
[ 1 3 4 5 6 7 8 9 10 11 12]
p4 = np.delete(matrix, [0,1], 1) 
print(’>>>>p4>>>>\n’,p4)
p4>>>>
[[ 3 4]
[ 7 8]
[11 12]]
import numpy as np 
matrix = [
    [1,2,3,4], [5,6,7,8], [9,10,11,12] 
]
q1 = np.insert(matrix, 1, [1,1,1,1], 0)
print('>>>>q1>>>>\n',q1)
>>>>q1>>>>
 [[ 1  2  3  4]
 [ 1  1  1  1]
 [ 5  6  7  8]
 [ 9 10 11 12]]
q2 = np.insert(matrix, 0, [1,1,1], 1)
print(’>>>>q2>>>>\n’,q2)
[[ 1 1 2 3 4]
[ 1 5 6 7 8]
[ 1 9 10 11 12]]
q3 = np.insert(matrix, 3, [1,1,1,1], 0)
print(’>>>>q3>>>>\n’,q3)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 1 1 1 1]]
import numpy as np
c = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
m1 = np.append(c,[[2,2,2,2]],axis=0)
print('m1----\n',m1)
m1----
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [ 2  2  2  2]]
m2 = np.append(matrix,[[1],[1],[1]],axis=1)
print(’>>>>m2>>>>\n’,m2)
m3 = np.append(matrix,[1,1,1,1])
print(’>>>>m3>>>>\n’,m3)
m2>>>>
[[ 1 2 3 4 1]
[ 5 6 7 8 1]
[ 9 10 11 12 1]]

m3>>>>
[ 1 2 3 4 5 6 7 8 9 10 11 12 1 1 1 1]
import numpy as np
a1 = np.random.choice(7,5) 
a1
array([5, 4, 4, 3, 2])
a2 = np.random.choice([0,1,2,3,4,5,6],5) # 从给定list中随机选择5个数组成一维数组
a2
a3 = np.random.choice(np.array([0,1,2,3,4,5,6]),5) # 将list换成array数组依然可以运行,效果一致
a3
array([5, 6, 2, 2, 5])
a4 = np.random.choice([0,1,2,3,4,5,6],5,replace=False) # 上述均有重复,将replace设置为False,即可按要求没有重复的选取
array([5, 4, 6, 3, 1])
a5 = np.random.choice(np.array([0,1,2,3,4,5,6]),5,p=[0.1,0.1,0.1,0.1,0.1,0.1,0.4])
a5
array([3, 2, 6, 3, 3])
import numpy as np
a = np.array([[1,1,1],[2,2,2],[5,3,6]])
print(a)
[[1 1 1]
 [2 2 2]
 [5 3 6]]
b1 = np.argmax(a) # 将数组a拉平,最大值索引为9(初始索引为0)
print(b1)
8
b2 = np.argmax(a, axis=0) # 按列选取最大值的索引
print(b2)
[2 2 2]
b3 = np.argmax(a, axis=1) # 按行选取最大值的索引
print(b3)
[0 0 2]
import numpy as np
y1 = np.linspace(-5.0,5.0) # 默认生成50个数
print(y1)
[-5.         -4.79591837 -4.59183673 -4.3877551  -4.18367347 -3.97959184
 -3.7755102  -3.57142857 -3.36734694 -3.16326531 -2.95918367 -2.75510204
 -2.55102041 -2.34693878 -2.14285714 -1.93877551 -1.73469388 -1.53061224
 -1.32653061 -1.12244898 -0.91836735 -0.71428571 -0.51020408 -0.30612245
 -0.10204082  0.10204082  0.30612245  0.51020408  0.71428571  0.91836735
  1.12244898  1.32653061  1.53061224  1.73469388  1.93877551  2.14285714
  2.34693878  2.55102041  2.75510204  2.95918367  3.16326531  3.36734694
  3.57142857  3.7755102   3.97959184  4.18367347  4.3877551   4.59183673
  4.79591837  5.        ]

Process finished with exit code 0
y2 = np.linspace(1,10,10) # 生成10个数据,包括首尾
y2
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
y3 = np.linspace(1,10,10,endpoint=False) # 不包括尾部数据
y3
array([1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1])
y4= np.linspace(1, 10, 6, retstep=True) # 将步长与结果的数组放入一个list、
y4
(array([ 1. , 2.8, 4.6, 6.4, 8.2, 10. ]), 1.8)
import numpy as np
x = np.array([[1,2,3],[4,5,6],[1,2,3]])
x.flatten()
array([1, 2, 3, 4, 5, 6, 1, 2, 3])
x.ravel()
array([1, 2, 3, 4, 5, 6, 1, 2, 3])
x.ravel(‘F’)
array([1, 4, 1, 2, 5, 2, 3, 6, 3])
x.flatten()[1] = 20
x
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3]])
x.ravel()[1] = 20
x
array([[ 1, 20, 3],
[ 4, 5, 6],
[ 1, 2, 3]])
x.reshape(1,-1
array([[1, 2, 3, 4, 5, 6, 1, 2, 3]])
x[:,None]
array([[1],
[2],
[3],
[6],
[7],
[8]])
x[np.newaxis, :] 
array([[1, 2, 3, 6, 7, 8]])
import numpy as np
x = np.array([[1,2,3],[-3,2,4],[5,-2,9]])
print(x
[[ 1  2  3]
 [-3  2  4]
 [ 5 -2  9]]
y1 = np.maximum(0,x) # 把小于0的元素置0,比改变x的值
y1
[[1, 2, 3],
[0, 2, 4],
[5, 0, 9]]
y2 = np.minimum(0,x) # 把大于0的元素置0,不改变x的值
y2
[[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]]
x1 = x.copy()
x1
[[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]]
x1[x1 < 0] = 0 # 把小于0的元素置0,改变x1的值
x1
[[1, 2, 3],
[0, 2, 4],
[5, 0, 9]
x2 = x.copy()
x2[x2 > 0] = 0 # 把大于0的元素置0,改变x2的值
x2
[[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]]
import numpy as np
x = np.array([[1,2,3],[-3,2,4],[5,-2,9]])
print(x)
[[ 1  2  3]
 [-3  2  4]
 [ 5 -2  9]]
x1 = x.copy() # copy(),开辟新地址
x1[x1 > 0] = 0
x1
[[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]]
x # x不变
[[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]]
x2 = x 
[[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]]
x2[x2>0] = 0
x2
[[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]]
x = np.array([[1,2,3],[-3,2,4],[5,-2,9]])
x3 = x[2] # 取x的第3行
x3
[ 5, -2, 9]
x3[2] = 100 # 将x3第3个元素置100
x # x中对应的元素置也被置成100了
[[ 1, 2, 3],
[ -3, 2, 4],
[ 5, -2, 100]]
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
np.zeros_like(x)
print(x)
[[1 2 3]
 [4 5 6]]
import numpy as np
n = np.random.rand(3,4)
print(n)
[[0.14819249 0.11701437 0.16454841 0.51308638]
 [0.01351841 0.94086185 0.2064842  0.16641379]
 [0.40760857 0.58683672 0.34788573 0.87982704]]
import numpy as np
x = np.random.randn(2,3)
print(x
[[-1.83192177 -0.3535477   0.07111869]
 [ 1.23083173  0.45540563 -0.60378926]]
y = np.multiply(0.1,np.random.randn(2,3))+0.5 # 一般正太分布
y
[[0.49305173, 0.36802044, 0.48699281],
[0.45197275, 0.53837051, 0.60022348]]
import numpy as np
z = np.random.randint(2,9,(2,3))
print(
[[4 4 2]
 [8 6 4]]
m = np.random.randint(9,size = (2,3))
m
[[4, 0, 2],
[5, 2, 8]]
import numpy as np
A = np.arange(95,99).reshape(2,2)
print(A)
[[95 96]
 [97 98]]
import numpy as np
b = np.array([[[1,2],[3,4]],[[3,4],[7,8]],[[4,5],[1,2]]])
print(b)
[[[1 2]
  [3 4]]

 [[3 4]
  [7 8]]

 [[4 5]
  [1 2]]]

Process finished with exit code 0
np.pad(b, ((0,0),(1,1),(1,1)), ‘constant’, constant_values = 0)
[[0, 0, 0, 0],
[0, 1, 2, 0],
[0, 3, 4, 0],
[0, 0, 0, 0]]
import numpy as np
x = np.empty([3,2], dtype = int)
print (x)
[[1952671088 1752440947]
 [1886593125 1768779105]
 [1914726515 1952804965]]
import numpy as np
c = np.array([[1,2],[3,4]])
c
import numpy as np
c = np.array([[1,2],[3,4]])
print(c)
[[1 2]
 [3 4]]
c.astype(np.float32)
([[1., 2.],
[3., 4.]], dtype=float32)
import numpy as np
x = np.array([1,3,5])
y = np.array([4,6])
XX,YY = np.meshgrid(x,y)
print(XX)
[[1 3 5]
 [1 3 5]]
YY
[[4, 4, 4],
[6, 6, 6]]
import numpy as np
x = np.array([[3,4,5],[1,3,4]])
y = np.array([[1,1,1],[2,2,2]])
np.hstack((x,y))
print(x,y)
[[3 4 5]
 [1 3 4]] [[1 1 1]
 [2 2 2]]
import numpy as np
a = np.array([0.125,0.568,5.688])
np.round(a) # 四舍五入取整, np.around 和 round 用法一致
[0., 1., 6.])
np.round(a,decimals = 2) # 四舍五入保留2位小数
[0.12, 0.57, 5.69]
np.floor(a) # 向下取整
[0., 0., 5.]
np.ceil(a) # 向上取整
[1., 1., 6.]
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
a = np.array([[1,2,3,6],[4,5,6,6]])
a1 = a.reshape((1,2,4))
print(a1)
[[[1, 2, 3, 6],
[4, 5, 6, 6]]]
b = np.array([[3,4,5,6],[1,2,3,4],[4,5,5,5]])
b
[[3, 4, 5, 6],
[1, 2, 3, 4],
[4, 5, 5, 5]]
b1 = b.reshape((1,3,4)).transpose((1,0,2))
b1
[[3, 4, 5, 6]]
import numpy as np
a = np.array([2,2,3,4,5,5,6,7])

print(a[0::2])
[2 3 5 6]
a[::-1]
[7, 6, 5, 5, 4, 3, 2, 2]
import numpy as np
a = np.array([2,2,3,4,5,5,6,7])
s = slice(0,7,2)
print(a[s])
[2 3 5 6]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值