【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使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操