辛霄龙的作业1

import numpy as np
c=np.arange(1,13).reshape(6,2)
c
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10],
       [11, 12]])
np.vsplit(c,3)
[array([[1, 2],
        [3, 4]]),
 array([[5, 6],
        [7, 8]]),
 array([[ 9, 10],
        [11, 12]])]
d=c.T#水平拆分
d
array([[ 1,  3,  5,  7,  9, 11],
       [ 2,  4,  6,  8, 10, 12]])
np.hsplit(d,3)
[array([[1, 3],
        [2, 4]]),
 array([[5, 7],
        [6, 8]]),
 array([[ 9, 11],
        [10, 12]])]
a=np.array([[1,2,3,4],[11,12,13,45]])
b=np.array([[9,8,7,6],21,[22,23,62]])
e=np.dstack((a.b))
e
1 aarray([[[ 1,  9],
        [ 2,  8],
        [ 3,  7],
        [ 4,  6]],

       [[11, 21],
        [12, 22],
        [13, 23],
        [45, 62]]])

np.dsplit(e,2)
[array([[[ 1],
         [ 2],
         [ 3],
         [ 4]],
 
        [[11],
         [12],
         [13],
         [45]]]),
 array([[[ 9],
         [ 8],
         [ 7],
         [ 6]],
 
        [[21],
         [22],
         [23],
         [62]]])]

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([[2,2,2,2,]])
b+c
array([[3, 3, 3, 3],
       [3, 3, 3, 3],
       [3, 3, 3, 3],
       [3, 3, 3, 3]])
W=np.array([[1,1,1,],[2,2,2]])
W[:,1]
array([1, 2])
W[1]
array([2, 2, 2])
W[:,1]=np.array([5,5])
W
array([[1, 5, 1],
       [2, 5, 2]])
import numpy as np#delete函数
matrix=[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
p1 = np.delete(matrix, 1, 0) # 第0维度(行)第1行被删除(初始行为0行)
print(p1)
[[ 1  2  3  4]
 [ 9 10 11 12]]
 
p2 = np.delete(matrix, 1, 1) # 第1维度(列)第1行被删除
print(p2)
[[ 1  3  4]
 [ 5  7  8]
 [ 9 11 12]]
 
p3 = np.delete(matrix, 1) # 拉平后删除第1个元素(初始为第0个)
print(p3)
[ 1  3  4  5  6  7  8  9 10 11 12]
 
p4 = np.delete(matrix, [0,1], 1) # 第1维度(列)第0、1行被删除
print(p4)
[[ 3  4]
 [ 7  8]
 [11 12]]
import numpy as np#insert()函数
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
 
q1 = np.insert(matrix, 1, [1,1,1,1], 0) # 第0维度(行)第1行添加[1,1,1,1]
print(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) # 第1维度(列)第0列添加1,1,1
print(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) # 第0维度(行)第3行添加[1,1,1,1]
print(q3)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [ 1  1  1  1]]
import numpy as np#append()函数
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
m1 = np.append(matrix,[[1,1,1,1]],axis=0)
 
print(m1)
m2 = np.append(matrix,[[1],[1],[1]],axis=1)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [ 1  1  1  1]]
 
print(m2)
m3 = np.append(matrix,[[1],[1],[1]])
[[ 1  2  3  4  1]
 [ 5  6  7  8  1]
 [ 9 10 11 12  1]]
 
print(m3)
[ 1  2  3  4  5  6  7  8  9 10 11 12  1  1  1]
import numpy as np
a1 = np.random.choice(7,5) # 从0~7中随机选择5个数组成一维数组
a1
array([4, 2, 2, 0, 4])
 
a2 = np.random.choice([0,1,2,3,4,5,6],5) # 从给定list中随机选择5个数组成一维数组
a2
array([4, 1, 0, 3, 5])
 
a3 = np.random.choice(np.array([0,1,2,3,4,5,6]),5) # 将list换成array数组依然可以运行,效果一致
a3
array([3, 1, 1, 1, 3])
 
a4 = np.random.choice([0,1,2,3,4,5,6],5,replace=False) # 将replace设置为False,即可按要求没有重复的选取
a4
array([2, 1, 3, 4, 6])
 
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.2,0.3])
a5
array([5, 6, 5, 6, 2])
import numpy as np
a = np.array([[1,1,2],[9,2,2],[0,6,6]])
a
array([[1, 1, 2],
       [9, 2, 2],
       [0, 6, 6]])
 
b1 = np.argmax(a) # 将数组a拉平,最大值索引为9(初始索引为0)
b1
3
b2 = np.argmax(a, axis=0) # 按列选取最大值的索引
b2
array([1, 2, 2], dtype=int64)
b3 = np.argmax(a, axis=1) # 按行选取最大值的索引
b3
array([2, 0, 1], dtype=int64)

import numpy as np
y1 = np.linspace(0.0,20.0) # 默认生成50个数据
y1
array([ 0.        ,  0.40816327,  0.81632653,  1.2244898 ,  1.63265306,
        2.04081633,  2.44897959,  2.85714286,  3.26530612,  3.67346939,
        4.08163265,  4.48979592,  4.89795918,  5.30612245,  5.71428571,
        6.12244898,  6.53061224,  6.93877551,  7.34693878,  7.75510204,
        8.16326531,  8.57142857,  8.97959184,  9.3877551 ,  9.79591837,
       10.20408163, 10.6122449 , 11.02040816, 11.42857143, 11.83673469,
       12.24489796, 12.65306122, 13.06122449, 13.46938776, 13.87755102,
       14.28571429, 14.69387755, 15.10204082, 15.51020408, 15.91836735,
       16.32653061, 16.73469388, 17.14285714, 17.55102041, 17.95918367,
       18.36734694, 18.7755102 , 19.18367347, 19.59183673, 20.        ])
 
y2 = np.linspace(1,10,5) # 生成5个数据,包括首尾
y2
array([ 1.  ,  3.25,  5.5 ,  7.75, 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('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 = np.array([1,2,3,6,7,8]) 
x[None,:] # 转成行向量(二维矩阵)# 注意操作的是数组,即原x是数组
array([[1, 2, 3, 6, 7, 8]])
x[:,None] # 转成列向量(二维矩阵)***
array([[1],
[2],
[3],
[6],
[7],
[8]])
x[np.newaxis, :] # np.newaxis与None用法一致
array([[1, 2, 3, 6, 7, 8]])
x = np.array([[1,2,3],[2,3,4]])
np.prod(x)
144
np.prod(x,axis=1)
array([ 6, 24])
np.prod(x,axis=0)
array([ 2, 6, 12])
import numpy as np
x = np.array([[1,2,3],[-3,2,4],[5,-2,9]])
x
array([[ 1,  2,  3],
       [-3,  2,  4],
       [ 5, -2,  9]])
y1 = np.maximum(0,x) # 把小于0的元素置0,比改变x的值
y1
array([[1, 2, 3],
[0, 2, 4],
[5, 0, 9]])
y2 = np.minimum(0,x) # 把大于0的元素置0,不改变x的值
y2
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x1 = x.copy()
x1
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x1[x1 < 0] = 0 # 把小于0的元素置0,改变x1的值
x1
array([[1, 2, 3],
[0, 2, 4],
[5, 0, 9]])
x2 = x.copy()
x2[x2 > 0] = 0 # 把大于0的元素置0,改变x2的值
x2
array([[ 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]])
x
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
 
x1 = x.copy() # copy(),开辟新地址
x1[x1 > 0] = 0
x1
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x # x不变
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x2 = x # 直接等于,未开辟新地址,x2与x相关联
x2
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x2[x2>0] = 0
x2
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x # x也改变
array([[ 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
array([ 5, -2, 9])
x3[2] = 100 # 将x3第3个元素置100
x # x中对应的元素置也被置成100了
array([[ 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) # 生成一个和x大小相同的全零矩阵
array([[0, 0, 0],
[0, 0, 0]])
import numpy as np
n = np.random.rand(3,4)
n
array([[0.2475704 , 0.88462247, 0.13423477, 0.1450988 ],
       [0.22450036, 0.20503036, 0.11874109, 0.84063727],
       [0.2036903 , 0.356566  , 0.21515832, 0.52965046]])
import numpy as np
x = np.random.randn(2,3)
x
array([[ 0.0663273 ,  1.00451172, -1.34876747],
       [ 0.51042144,  1.06405736,  1.00671665]])
y = np.multiply(0.1,np.random.randn(2,3))+0.5 # 一般正太分布
y
array([[0.38764744, 0.47289113, 0.55155268],
       [0.55454147, 0.69613112, 0.51384308]])
import numpy as np
z = np.random.randint(2,9,(2,3))
z
array([[2, 6, 8],
       [3, 4, 5]])
m = np.random.randint(9,size = (2,3))
m
array([[0, 8, 1],
       [7, 3, 5]])
x = ‘You are right’
type(x)
str
 
assert type(x)==str, ‘x is not str’
x = [1,2,3]
type(x)
list
 
assert type(x)==str, ‘x is not str’
x = [1,2,3]
type(x)
list
 
assert type(x)==str, ‘x is not str’
Traceback (most recent call last):
File “”, line 1, in
  File "<ipython-input-97-9486af65fcbf>", line 1
    assert type(x)==str, ‘x is not str’

A = np.arange(95,99).reshape(2,2)
A
array([[95, 96], [97, 98]])
np.pad(A,((3,2),(2,3)),'constant',constant_values = (0,0))
array([[ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0, 95, 96,  0,  0,  0],
       [ 0,  0, 97, 98,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0]])
b = np.array([[[1,2],[3,4]],[[3,4],[7,8]],[[4,5],[1,2]]])
 
b
array([[[1, 2],
        [3, 4]],
 
       [[3, 4],
        [7, 8]],
 
       [[4, 5],
        [1, 2]]])
np.pad(b, ((0,0),(1,1),(1,1)), 'constant', constant_values = 0)
array([[[0, 0, 0, 0],
        [0, 1, 2, 0],
        [0, 3, 4, 0],
        [0, 0, 0, 0]],
 
       [[0, 0, 0, 0],
        [0, 3, 4, 0],
        [0, 7, 8, 0],
        [0, 0, 0, 0]],
 
       [[0, 0, 0, 0],
        [0, 4, 5, 0],
        [0, 1, 2, 0],
        [0, 0, 0, 0]]])
import numpy as np
c = np.array([[1,2],[3,4]])
c
array([[1, 2],
[3, 4]])
c.astype(np.float32)
array([[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)
XX
array([[1, 3, 5],
[1, 3, 5]])
YY
array([[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)) # 水平堆叠
array([[3, 4, 5, 1, 1, 1],
       [1, 3, 4, 2, 2, 2]])
np.vstack((x,y)) # 竖直堆叠
array([[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 用法一致
array([0., 1., 6.])
np.round(a,decimals = 2) # 四舍五入保留2位小数
array([0.12, 0.57, 5.69])
np.floor(a) # 向下取整
array([0., 0., 5.])
np.ceil(a) # 向上取整
array([1., 1., 6.])
import numpy as np
c = np.array([1,2,5,4])
c[:,np.newaxis]
array([[1],
       [2],
       [5],
       [4]])
c[np.newaxis,:]
array([[1, 2, 5, 4]])
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))
a1
array([[[1, 2, 3, 6],
[4, 5, 6, 6]]])
b = np.array([[3,4,5,6],[1,2,3,4],[4,5,5,5]])
b
array([[3, 4, 5, 6],
[1, 2, 3, 4],
[4, 5, 5, 5]])
b1 = b.reshape((1,3,4)).transpose((1,0,2))
b1
array([[[1, 2, 3, 4]],
 
       [[3, 4, 7, 8]],
 
       [[4, 5, 1, 2]]])
a1
array([[[1, 2, 3, 6],
[4, 5, 6, 6]]])
a1+b1
array([[[ 4, 6, 8, 12],
[ 7, 9, 11, 12]],
[[ 2,  4,  6, 10],
[ 5,  7,  9, 10]],
 
[[ 5,  7,  8, 11],
[ 8, 10, 11, 11]]])

c = np.array([[[1,2,5],[3,4,6]],[[4,5,6],[7,8,9]]])
c
array([[[1, 2, 5],
        [3, 4, 6]],
 
       [[4, 5, 6],
        [7, 8, 9]]])
c.transpose(1,0,2) # 将c的维度按照 第1维度,第0维度,第2维度的排序排成 第0,1,2维度
array([[[1, 2, 5],
        [4, 5, 6]],
 
       [[3, 4, 6],
        [7, 8, 9]]])
c.transpose(1,2,0) # 将c的维度按照 第1维度,第2维度,第0维度的排序排成 第0,1,2维度
array([[[1, 4],
        [2, 5],
        [5, 6]],
 
       [[3, 7],
        [4, 8],
        [6, 9]]])
import numpy as np
a = np.array([2,2,3,4,5,5,6,7])
a[0:7:2]
array([2, 3, 5, 6])
import numpy as np
a = np.array([2,2,3,4,5,5,6,7])
a[0::2]
array([2, 3, 5, 6])
a[::-1]
array([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)
a[s]
array([2, 3, 5, 6])

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值