【Python学习】Numpy测试题(1)

2024-04-29 06:38
文章标签 python 学习 numpy 测试题

本文主要是介绍【Python学习】Numpy测试题(1),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目原文

1.Import numpy as np and see the version?

import numpy as np
print(np.__version__)

__version__就像是一种内置的宏。


2.How to create a 1D array?

import numpy as np
array = np.array([i for i in range(10)])
array1 = np.arange(10)
array2 = np.arange(0,10,1)
print(array, '\n', array1, '\n', array2)

一般来说array是指定数组元素的初始化方式,但是arange是一种自动初始化一个有规律的序列。arange(start, stop, step)在一个左闭右开的区间上按照步长进行初始化。


3.How to create a boolean array?

Method-1:

import numpy as np
bool_arr = np.full((3,3), True, dtype=bool)
print(bool_arr)

函数numpy.full(shape, value, dtype)是对一个指定shape的numpy数组按照value进行填充。

Method-2:

import numpy as np
arr = np.ones((3,3),dtype=bool)
print(arr)

函数numpy.ones(shape,dtype)是按照shape指定的numpy数组使用dtype指定类型的默认初始值进行构造。


4.How to extract items that satisfy a given condition from 1D array?

import numpy as np
arr = np.arange(10)
arr_ = arr[arr %2 == 1]
print(arr_)

numpy[condition]可以在numpy数组中根据condition进行筛选。


5.How to replace items that satisfy a condition with another value in numpy array?

import numpy as np
arr = np.arange(10)
arr[arr%2==1] = -1
print(arr)

6.How to replace items that satisfy a condition without affecting the original array?

import numpy as np
arr = np.arange(10)
arr_ = np.where(arr % 2 == 1, -1, arr)
print(arr, '\n', arr_)

函数numpy.where(condition,[,x,y])类似于条件表达式,如果condition是满足的(True),则执行x,否则执行y。


7.How to reshape an array?

import numpy as np
arr = np.arange(10)
arr_ = np.reshape(arr, (2,-1))
# arr_ = arr.reshape(2, -1)
print(arr_)

参数-1表示自动计算维度。


8.How to stack two arrays vertically?


9.How to stack two array horizontally?

10.How to generate custom sequences in numpy without hardcoding?

11.How to get the common items between two python numpy arrays?

12.How to remove from one array those items that exist in another?

13.How to get the positions where elements of two arrays match?

14.How to extract all numbers between a given range from a numpy array?

15.How to make a python function that handles scalars to work on numpy arrays?

16.How to swap two columns in a 2d numpy array?

17.How to swap rows in a 2d numpy array?

18.How to reverse the rows of a 2D array?

19.How to reverse the columns of 2 2D array?

20.How to create a 2D array containing random floats between 5 and 10?

这篇关于【Python学习】Numpy测试题(1)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/945358

相关文章

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

Python操作PDF文档的主流库使用指南

《Python操作PDF文档的主流库使用指南》PDF因其跨平台、格式固定的特性成为文档交换的标准,然而,由于其复杂的内部结构,程序化操作PDF一直是个挑战,本文主要为大家整理了Python操作PD... 目录一、 基础操作1.PyPDF2 (及其继任者 pypdf)2.PyMuPDF / fitz3.Fre

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

python中列表应用和扩展性实用详解

《python中列表应用和扩展性实用详解》文章介绍了Python列表的核心特性:有序数据集合,用[]定义,元素类型可不同,支持迭代、循环、切片,可执行增删改查、排序、推导式及嵌套操作,是常用的数据处理... 目录1、列表定义2、格式3、列表是可迭代对象4、列表的常见操作总结1、列表定义是处理一组有序项目的

python运用requests模拟浏览器发送请求过程

《python运用requests模拟浏览器发送请求过程》模拟浏览器请求可选用requests处理静态内容,selenium应对动态页面,playwright支持高级自动化,设置代理和超时参数,根据需... 目录使用requests库模拟浏览器请求使用selenium自动化浏览器操作使用playwright

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

Python极速搭建局域网文件共享服务器完整指南

《Python极速搭建局域网文件共享服务器完整指南》在办公室或家庭局域网中快速共享文件时,许多人会选择第三方工具或云存储服务,但这些方案往往存在隐私泄露风险或需要复杂配置,下面我们就来看看如何使用Py... 目录一、android基础版:HTTP文件共享的魔法命令1. 一行代码启动HTTP服务器2. 关键参

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Python获取浏览器Cookies的四种方式小结

《Python获取浏览器Cookies的四种方式小结》在进行Web应用程序测试和开发时,获取浏览器Cookies是一项重要任务,本文我们介绍四种用Python获取浏览器Cookies的方式,具有一定的... 目录什么是 Cookie?1.使用Selenium库获取浏览器Cookies2.使用浏览器开发者工具

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库