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个元素等于1

s1=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.52667323, 0.09511843, 0.52333792, 0.04762413, 0.58586139,
        0.14900331, 0.56960978, 0.50757788, 0.66571403, 0.20067781],
       [0.02377154, 0.43143442, 0.19548033, 0.64346217, 0.57787913,
        0.94163686, 0.28904847, 0.18082473, 0.90689384, 0.2883719 ],
       [0.72238338, 0.50486696, 0.53750581, 0.8859637 , 0.35649331,
        0.74489172, 0.82165757, 0.42719481, 0.92563668, 0.03053803],
       [0.93534451, 0.16592594, 0.06932313, 0.61799815, 0.87924111,
        0.89727827, 0.12065471, 0.65256364, 0.70560278, 0.75687252],
       [0.62135682, 0.96339491, 0.5977129 , 0.53646374, 0.72026989,
        0.26019546, 0.47781239, 0.3244741 , 0.83904571, 0.21677118],
       [0.75726593, 0.59887832, 0.67065446, 0.20171953, 0.70837563,
        0.6867683 , 0.90187339, 0.47141811, 0.46834463, 0.10395074],
       [0.93200155, 0.14605271, 0.09333598, 0.2853065 , 0.66001182,
        0.73361787, 0.57908998, 0.28209687, 0.86031825, 0.6590266 ],
       [0.60712126, 0.69602905, 0.24708437, 0.28685261, 0.49169714,
        0.27471922, 0.93513632, 0.16485538, 0.88133209, 0.31291952],
       [0.99718404, 0.52393742, 0.34003144, 0.99637819, 0.43615456,
        0.20141012, 0.49028605, 0.98032541, 0.2926153 , 0.98080751],
       [0.84463118, 0.76228566, 0.75315541, 0.46838515, 0.52054057,
        0.92107125, 0.37127466, 0.65637769, 0.11616873, 0.02590352]])
zmin,zmax=a4.min(),a4.max()
zmin,zmax
(0.023771537636103957, 0.9971840431335078)

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

x=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的5*5矩阵

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.25238437, 0.53308367, 0.63047905, 0.68531869, 0.74069569,
       0.75111386, 0.82946716, 0.85533339, 0.98054272, 0.99938397])

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

nd=np.random.randint(0,10,size=10)
nd
array([7, 5, 9, 3, 1, 8, 0, 0, 9, 3])
nd_max=nd.argmax()
nd[nd_max]
arr=np.argwhere(nd==nd[nd_max]).reshape(-1)
nd[arr]=-100
nd
array([   7,    5, -100,    3,    1,    8,    0,    0, -100,    3])

10、如何根据第3列来对一个5*5矩阵排序?

nd=np.random.randint(0,100,size=(5,5))
nd
array([[53,  1, 68, 65, 73],
       [90, 33, 87, 91, 29],
       [77, 32, 97, 91, 35],
       [ 5, 36, 95, 90, 78],
       [51, 46, 88,  3, 82]])
nd[:,2]
array([68, 87, 97, 95, 88])
np.argsort(nd[:,2])
array([0, 1, 4, 3, 2], dtype=int64)
nd[np.argsort(nd[:,2])]
array([[53,  1, 68, 65, 73],
       [90, 33, 87, 91, 29],
       [51, 46, 88,  3, 82],
       [ 5, 36, 95, 90, 78],
       [77, 32, 97, 91, 35]])

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值