numy练习题

导入相关库

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
 
%matplotlib inline

创建一个长度为10的一维全为0的ndarray对象,然后让第5个元素等于1

s1=np.zeros(shape=10)
s1

s1[4]=1
s1

创建一个元素为从10到49的ndarray对象

np.random.randint(10,50,size=10)

np.linspace(10,49,10)

a=np.arange(10,50)
a

使用np.random.random创建一个10*10的ndarray对象,并打印出最大最小元素

a[::-1]

a4=np.random.random(size=(10,10))
a4

zmin,zmax=a4.min(),a4.max()
zmin,zmax

创建一个10*10的ndarray对象,且矩阵边界全为1,里面全为0

import numpy as np
nd=np.zeros(shape = (10,10),dtype = np.int8)
nd[[0,9]] = 1

nd[:,[0,9]] = 1
nd

a5=np.ones((10,10))
a5

a5[1:-1,1:-1]=0
a5

在这里插入图片描述

创建一个每一行都是从0到4的5*5矩阵

l=[0,1,2,3,4]
nd=np.array(l*5)
nd.reshape(5,5)

nd6_1=np.arange(0,5,1)
nd6_1

nd6=np.arange(0,25).reshape(5,5)
nd6

nd6[0:5]=nd6_1
nd6

在这里插入图片描述

创建一个范围在(0,1)之间的长度为12的等差数列

np.linspace(0,1,12)

创建一个长度为10的随机数组并排序

a8=np.random.random(10)
a8

在这里插入图片描述

np.sort(a8)

a8.argsort()

a8[a8.argsort()]

创建一个长度为10的随机数组并将最大值替换为0

nd=np.random.randint(0,10,size=10)
display(nd)
index_max=nd.argmax()

nd[index_max]

all_index_max=np.argwhere(nd==nd[index_max]).reshape(-1)
all_index_max

nd[all_index_max]=-100
nd

根据第3列来对一个5*5矩阵排序

n10=np.random.randint(0,100,size=(5,5))
n10

给定数组[1, 2, 3, 4, 5],如何得到在这个数组的每个元素之间插入3个0后的新数组

nd1=np.arange(1,6)
nd2=np.zeros(shape=17,dtype=int)
nd2[::4]=nd1
nd2

n12=np.array([1,2,3,4,5]).reshape(5,1)
n12

n12_1=np.zeros((5,3))
n12_1

n12=np.concatenate([n12,n12_1],axis=1)
n12

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值