NumPy 中的 resize函数

首页 / Numpy入门教程 / NumPy 中的 resize函数

此函数返回具有指定大小的新数组,该函数采用以下参数。

numpy.resize(arr, shape)
Sr.No. 描述
1

arr

输入数组要调整大小

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-resize.html

来源:LearnFk无涯教程网

2

shape

输出数组的新维度

import numpy as np 
a = np.array([[1,2,3],[4,5,6]]) 

print 'First array:' 
print a 
print '\n'

print 'The shape of first array:' 
print a.shape 
print '\n'  
b = np.resize(a, (3,2)) 

print 'Second array:' 
print b 
print '\n'  

print 'The shape of second array:' 
print b.shape 
print '\n'  
# Observe that first row of a is repeated in b since size is bigger 

print 'Resize the second array:' 
b = np.resize(a,(3,3)) 
print b

上面的程序将产生以下输出-

First array:
[[1 2 3]
 [4 5 6]]

The shape of first array:
(2, 3)

Second array:
[[1 2]
 [3 4]
 [5 6]]

The shape of second array:
(3, 2)

Resize the second array:
[[1 2 3]
 [4 5 6]
 [1 2 3]]

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

高并发系统设计40问 -〔唐扬〕

JavaScript核心原理解析 -〔周爱民〕

OAuth 2.0实战课 -〔王新栋〕

操作系统实战45讲 -〔彭东〕

超级访谈:对话张雪峰 -〔张雪峰〕

深入C语言和程序运行原理 -〔于航〕

林外 · 专利写作第一课 -〔林外〕

说透元宇宙 -〔方军〕

零基础GPT应用入门课 -〔林健(键盘)〕

好记忆不如烂笔头。留下您的足迹吧 :)