numpy基础练习

10道numpy练习题
导入相关库
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt

%matplotlib inline

1、创建一个长度为10的一维全为0的ndarray对象,然后让第5个元素等于1s1=np.zeros(shape=10)
s1[4]=1
s1

array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])
2、创建一个元素为从10到49的ndarray对象
a=np.arange(10,50)
a

array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49])
3、将第2题的所有元素位置反转
a[::-1]

array([49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
15, 14, 13, 12, 11, 10])
4、使用np.random.random创建一个10*10的ndarray对象,并打印出最大最小元素
a4=np.random.random(size=(10,10))
a4

array([[0.63608138, 0.8703843 , 0.82927002, 0.14332637, 0.28179683,
0.05328882, 0.59934698, 0.30226559, 0.79040768, 0.33678025],
[0.17225144, 0.73290557, 0.09521554, 0.57432315, 0.14666323,
0.90349124, 0.84862075, 0.18668117, 0.5611663 , 0.31720321],
[0.93244247, 0.12933431, 0.3821767 , 0.07605885, 0.72894208,
0.12826146, 0.01857141, 0.73384976, 0.52111772, 0.66629845],
[0.41900082, 0.00972746, 0.37705011, 0.45183342, 0.78942404,
0.20614969, 0.72096089, 0.29801699, 0.98566501, 0.69204734],
[0.71359142, 0.12839176, 0.24714057, 0.01127062, 0.95868589,
0.00125053, 0.82275878, 0.91791597, 0.16823268, 0.98480915],
[0.06257263, 0.43786902, 0.09402993, 0.01439648, 0.56451624,
0.69918841, 0.75174126, 0.91292163, 0.89573275, 0.77146426],
[0.02895765, 0.19851769, 0.28963204, 0.77540277, 0.89243123,
0.13448186, 0.40515985, 0.44136095, 0.10545047, 0.23111201],
[0.4739347 , 0.97842197, 0.10331755, 0.44685197, 0.9112558 ,
0.56769973, 0.89934064, 0.38677289, 0.68001136, 0.00547094],
[0.98769248, 0.32163025, 0.04157509, 0.45654179, 0.67683493,
0.26414036, 0.3353933 , 0.35998095, 0.07503577, 0.46785704],
[0.0601489 , 0.34874263, 0.82676758, 0.7081031 , 0.93907723,
0.45275705, 0.08144091, 0.95527085, 0.72842101, 0.26077336]])
zmin,zmax=a4.min(),a4.max()
zmin,zmax

(0.0012505310439516748, 0.9876924843768792)
5、创建一个10*10的ndarray对象,且矩阵边界全为1,里面全为0x=np.ones([2,3],dtype = int)
x

array([[1, 1, 1],
[1, 1, 1]])
nd=np.zeros(shape = (10,10))
nd[[0,9]]=1
nd[:,[0,9]]=1
nd

array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
6、创建一个每一行都是从0到4的55矩阵
arr = [0,1,2,3,4]
nd= np.array(arr
5)
nd.reshape(5,5)

array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
7、创建一个范围在(0,1)之间的长度为12的等差数列
np.linspace(0,1,12)

array([0. , 0.09090909, 0.18181818, 0.27272727, 0.36363636,
0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,
0.90909091, 1. ])
8、创建一个长度为10的随机数组并排序
a=np.random.random(10)
np.sort(a)

array([0.15915969, 0.28512151, 0.45348959, 0.54258381, 0.71467532,
0.71716631, 0.89789373, 0.91088578, 0.92363071, 0.96634431])
9、创建一个长度为10的随机数组并将最大值替换为-100
nd=np.random.randint(0,10,size=10)
nd

array([5, 2, 8, 2, 5, 0, 6, 1, 3, 9])
nd_max=nd.argmax()
nd[nd_max]
arr=np.argwhere(nd==nd[nd_max]).reshape(-1)
nd[arr]=-100
nd

array([ 5, 2, 8, 2, 5, 0, 6, 1, 3, -100])
10、如何根据第3列来对一个5*5矩阵排序?
nd=np.random.randint(0,100,size=(5,5))
nd

array([[ 4, 25, 64, 60, 55],
[91, 16, 18, 88, 95],
[62, 11, 9, 16, 39],
[ 4, 29, 84, 76, 96],
[69, 63, 19, 61, 6]])
nd[:,2]

array([11, 24, 12, 57, 61])
np.argsort(nd[:,2])

array([0, 2, 1, 3, 4], dtype=int64)
nd[np.argsort(nd[:,2])]

array([[58, 48, 11, 95, 64],
[91, 59, 12, 85, 8],
[ 1, 55, 24, 64, 91],
[86, 3, 57, 73, 90],
[81, 70, 61, 4, 42]])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值