RUNOOB python练习题8 numpy矩阵的索引及遍历

如何使用python导入mat格式的数据并整理 使用scipy导入mat文件 阅读详情

用来练手的python 练习题,原链接 : python练习实例8

题干: 输出 9*9 乘法口诀表。

import numpy as np

table = np.zeros((9,9))
for i in range(table.shape[0]):
  for j in range(table.shape[1]):
    table[i][j] = (i+1) * (j+1)

# 查询九九乘法表
def affichage_table(a,b):
  print("%d, %d 的乘积是 : %d"%(a,b,table[a-1][b-1]))

# 完整输出乘法表
def affichage_all():
  for i in range(table.shape[0]):
    print("\n")
    for j in range(table.shape[1]):
      print("%d * %d = %d"%(i+1,j+1,table[i][j]))

查询输出:

affichage_table(8,9)

在这里插入图片描述

完整输出:

affichage_all()

在这里插入图片描述… … …
小tips : 这个例题需要注意的点就一个,就是Python中的矩阵,向量的索引都是从0开始的。

Python遍历numpy数组的实例 下面小编就为大家分享一篇Python遍历numpy数组的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 立即下载

相关推荐

10-142 6-4 查询厂商“D“生产的PC和便携式电脑的平均价格

分数 10全屏浏览题目切换布局作者 lhm单位 河北农业大学本题目要求编写SQL语句,查询厂商D生产的PC和便携式电脑的平均价格。

dqd66的博客 2757

python遍历矩阵中所有元素_不用循环,python numpy 数组如何对每个元素进行操作?...

在对SynthText的gt.mat中的wordBB中的数据进行处理时,我遇到一个问题:确保每一个值都在[0.,1.]内。如果用循环,就没有体现出python的逼格。于是找了三种方法(仅展示使每一个元素都小于1的方法)>>>import numpy as np>>> a = np.random.randn(5,5)>>>aarray([[-0....

weixin_32511509的博客 4413

Android14到Android16源码移植中的Bluetooth模块规范适配指南

本文详细解析了从Android14到Android16源码移植过程中Bluetooth模块的规范适配挑战,重点介绍了Android.bp配置差异及常见编译报错解决方案。针对代码规范检查、final字段声明、静态方法优化等核心问题提供实用修复方法,帮助开发者高效完成版本升级适配工作。

fire9的博客 260

Numpy实践_索引与切片

Numpy实践_索引与切片

weixin_42504788的博客 615

别再搞混了!用示波器实测SVPWM:相电压、线电压、端电压波形到底长啥样?(附TI/ST官方笔记解读)

本文详细解析了SVPWM波形实测中的相电压、线电压和端电压的区别与测量技巧,结合TI/ST官方笔记,揭示了马鞍形波形的形成原理及常见误区。通过示波器实测案例,帮助电力电子工程师准确理解并应用这些关键概念,提升电机控制系统的调试效率。

weixin_30632883的博客 443

海康视觉脚本开发全流程解析

本文详细解析了海康机器视觉平台(VM)中脚本模块的开发全流程。从创建第一个脚本模块入手,深入讲解了输入参数、输出参数与内部变量的定义与区别,并提供了C#脚本编写、公用函数调用、代码结构组织以及编译调试的实用指南。旨在帮助开发者掌握海康视觉脚本开发的核心技巧,高效实现复杂的自定义视觉逻辑处理。

beer8的博客 612

Python数据分析 1-3 NumPy数组的索引、切片和遍历

numpy的基本知识 全全全

m0_65523585的博客 953

关于光耦隔离与SP3485E芯片一起使用无法拉高485使能引脚问题

最近在做的一个项目,定制了一块STM32F407ZG的工控板,工控板载4个485接口全部都是只能接收数据,不能对外发送数据。1.接线不正确:接线不正确导致485收发出现问题。 2.程序端口初始化问题:控制光耦隔离的输入脚IO的初始化和置位状态有问题。 3.GPIO复用问题:控制对应串口的GPIO不是正点原子默认的GPIO,而是经过复用的其他IO,复用问题。 4.程序设置与485E芯片收发模式不对应:对于使能引脚的电平要根据光耦的输出情况确定输入IO与RE的关系,可能模式没有对应。 5.光耦隔离或者SP485

weixin_45862642的博客 2987

RUNOOB python练习题44

python 矩阵加法 numpy库矩阵加法 及 合并矩阵的vstack hstack方法

Cy_coding的博客 340

ABAP 7.40 新语法介绍系列之二 - ABAP 类型转换操作符 CONV

这个lv_input_s变量的唯一使命,就是接收来自 char10 类型的 lv_input_c 变量的值,然后传递到 print 方法里去。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1427

24、JavaFX MP3 音频播放器开发详解

本文详细介绍了如何使用JavaFX开发一个功能丰富的MP3音频播放器,涵盖文件拖放加载、音频播放控制、音频频谱可视化、进度定位滑块等核心功能,并提供了完整的代码实现和解析。适合希望学习JavaFX多媒体应用开发的开发者参考。

wind6的博客 172

【Numpy学习笔记】

对于Numpy的基本用法整理 包含具体代码示例

qq_48450154的博客 314

uniapp [安卓苹果App端] - 详细实现手机蓝牙连接打印机及打印票据小票/标签/面单/热敏小票订单/快递单等通用兼容适配解决方案,Uniapp对接蓝牙打印机支持各种主流品牌及打印指令,强悍插件

uni-app,安卓Android,苹果ios,安卓app端,苹果App端,uniapp苹果app,蓝牙打印机,uniappAPP实现通过蓝牙连接打印机打印,uniapp蓝牙连接打印机,安卓蓝牙打印插件,蓝牙打印机,蓝牙ble低功率及高功率打印机小票、标签、面单打印,手机PDA蓝牙打印插件,连接蓝牙打印机,蓝牙连接,打印文本、图片、条码、二维码,兼容斑马打印机、佳博打印机,TSC打印机,免费蓝牙打印机组件,蓝牙打印条码,蓝牙打印机连接,热敏蓝牙打印机,便携式打印机,打印机搜索不到,连接失败,发送打印命令

🏆 前端领域优质创作者 1万+

[Python3] NumPy基础

import numpy as np 一、创建数组 # 1.从python列表创建数组 # 整型数组 np.array([1, 4, 2, 5, 3]) # 明确数据类型 np.array([1, 2, 3, 4], dtype='float32') # 嵌套列表构成的多维数组 np.array([range(i, i + 3) for i in [2, 4, 6]]) # 2.从头创建数组 ...

進撃の算法 1015

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

第二章 numpy之索引、切片与迭代+练习题

副本与视图 在 Numpy 中,尤其是在做数组运算或数组操作时,返回结果不是数组的 副本 就是 视图。 在 Numpy 中,所有赋值运算不会为数组和数组中的任何元素创建副本。 numpy.ndarray.copy() 函数创建一个副本。 对副本数据进行修改,不会影响到原始数据,它们物理内存不在同一位置。 import numpy as np x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) y = x y[0] = -1 print(x) # [-1 2 3 4 5

qq_45396577的博客 740

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

06. numpy-数组的索引切片和遍历

和Python里的列表类似,NumPy里的ndarray也支持索引和下标操作数据。 索引 可以直接使用下标来获取和修改数组里的数据。 na1 = np.array([8, 4, 9, 1, 7]) print(na1[0]) # 一维数组可以直接使用下标获取指定位置上的元素 print(na1) na1[0] = 10 # 可以通过下标来修改元素 print(na1) na2 = np.random.randint(3, 10, size=(3, 5, 2)) # 多维数组有两种方式获取指定

NY_0907的博客 1357

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

python-如何遍历数组所有元素并获取其索引

python-如何遍历数组所有元素并获取其索引

鱼七 9863

python同时遍历数组的索引和值

ls = [1,2,3,4] for index,value in enumerate(ls): print index,value 输出: 0 1 1 2 2 3 3 4 参考: https://docs.python.org/2/library/functions.html#enumerate

小龙在线 5万+

Numpy 数组基础操作--索引、组合、分割、复制、遍历、转换、序列化(四)

Numpy 数组基础操作 数组访问

我喜欢智能化 6万+

numpy - 获取array中数组元素的索引位置

原 numpy - 获取array中数组元素的索引 &lt;div class="article-info-box"&gt; &lt;div class="article-bar-top d-flex"&gt; &lt;span class="...

yuan0061的博客 9万+

numpy - 获取array中数组元素的索引

1. 函数原型 2. 应用 参考文献 http://blog.csdn.net/vernice/article/details/50990919

呵呵 2万+

numpy索引

import numpy as np b = np.arange(0, 8, 2) print(b) print(b[2]) # 输出第三个数 a = np.array([[5, 2, 4, 6], [3, 4, 4, 5], [2, 2, 4, 5]]) print(a[1]) # 输出第二行 print(a[1][1]) # 输出第二行第二列第元素 print(a[1, 1]) # 同...

磕盐xy 1023

numpy索引(取出矩阵中的元素)

import numpy as np a = np.arange(3, 13).reshape((2, 5)) print(a) print(a[1]) # 取出矩阵中的第一行 print(a[0][2]) # 取出矩阵中第0行第二列的数 print(a[0, 2]) # 取出矩阵中第0行第二列的数 print(a[1, 1:3]) # 取出矩阵中第一行 第1列到第3列(前闭后开)的数据 print(a[:, 2]) # :表示所有 # 按行遍历矩阵 for row in a: print

weixin_45827387的博客 3235

python︱sklearn一些小技巧的记录(训练集划分/pipelline/交叉验证等)

sklearn里面包含内容太多,所以一些实用小技巧还是挺好用的。 1、LabelEncoder 简单来说 LabelEncoder 是对不连续的数字或者文本进行编号 from sklearn.preprocessing import LabelEncoder le = LabelEncoder() le.fit([1,5,67,100]) le.transform([1,1,10...

素质云笔记 2万+
上一篇: 赋值语句 变量的地址相关 : RUNOOB python练习题7
下一篇: RUNOOB python练习题9 如何在代码中加入砸瓦鲁多
Cy_coding
博客等级 码龄7年 49粉丝 88原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址