对numpy.append()里的axis的用法详解
作者:我爱阿鑫 发布时间:2022-03-20 18:19:59
标签:numpy,append,axis
如下所示:
def append(arr, values, axis=None):
"""
Append values to the end of an array.
Parameters
----------
arr : array_like
Values are appended to a copy of this array.
values : array_like
These values are appended to a copy of `arr`. It must be of the
correct shape (the same shape as `arr`, excluding `axis`). If
`axis` is not specified, `values` can be any shape and will be
flattened before use.
axis : int, optional
The axis along which `values` are appended. If `axis` is not
given, both `arr` and `values` are flattened before use.
Returns
-------
append : ndarray
A copy of `arr` with `values` appended to `axis`. Note that
`append` does not occur in-place: a new array is allocated and
filled. If `axis` is None, `out` is a flattened array.
numpy.append(arr, values, axis=None):
简答来说,就是arr和values会重新组合成一个新的数组,做为返回值。而axis是一个可选的值
当axis无定义时,是横向加成,返回总是为一维数组!
Examples
--------
>>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
当axis有定义的时候,分别为0和1的时候。(注意加载的时候,数组要设置好,行数或者列数要相同。不然会有error:all the input array dimensions except for the concatenation axis must match exactly)
当axis为0时,数组是加在下面(列数要相同):
import numpy as np
aa= np.zeros((1,8))
bb=np.ones((3,8))
c = np.append(aa,bb,axis = 0)
print(c)
[[ 0. 0. 0. 0. 0. 0. 0. 0.]
[ 1. 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1. 1.]]
当axis为1时,数组是加在右边(行数要相同):
import numpy as np
aa= np.zeros((3,8))
bb=np.ones((3,1))
c = np.append(aa,bb,axis = 1)
print(c)
[[ 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
来源:https://blog.csdn.net/qq_35019361/article/details/79055991


猜你喜欢
- 前期准备首先,使用Python内置的Turtle绘图库需要在程序前添加以下代码:import turtle也可以写成这样:from turt
- Python 3.x 起始版本是Python 3.0,目前的最新版本是 3.3.3Python之父Guido van Rossum谈到了Py
- //定义编码header( 'Content-Type:text/html;charset=utf-8 ');//Atomh
- 写在前面:从昨晚的梦里回忆起数据管理的作业:实现一个自己的选题----毕业生信息管理系统,实现学生个人信息基本的增删改查,我想了想前段时间刚
- 前言在日常开发工作中,我经常会遇到需要统计总数的场景,比如:统计订单总数、统计用户总数等。一般我们会使用MySQL 的count函数进行统计
- torch.argmax()函数解析1. 官网链接torch.argmax(),如下图所示:2. torch.argmax(input)函数
- 前言JS 中 GBK 编码转字符串是非常简单的,直接调用 TextDecoder 即可:const gbkBuf = n
- 写在之前命名空间,又名 namesapce,是在很多的编程语言中都会出现的术语,估计很多人都知道这个词,但是让你真的来说这是个什么,估计就歇
- 本文介绍了python OpenCV学习笔记实现二维直方图,分享给大家,具体如下:官方文档 – https://docs.opencv.or
- 一、前言容器使用沙箱机制,互相隔离,优势在于让各个部署在容器的里的应用互不影响,独立运行,提供更高的安全性。本文主要介绍python应用(d
- 实例如下所示:#########start 获取文件路径、文件名、后缀名############def jwkj_get_filePath_
- oracle数据库的权限系统分为系统权限与对象权限。系统权限( database system privilege )可以让用户执行特定的命
- 一、UNION和UNION ALL的作用和语法UNION 用于合并两个或多个 SELECT 语句的结果集,并消去表中任何重复行。UNION
- 目录什么是时间序列?如何在Python中绘制时间序列数据?时间序列的要素是什么?如何分解时间序列?经典分解法如何获得季节性调整值?STL分解
- 本文主要分享了关于在python中实现一个简单的文件浏览器的代码示例,代码及展示如下。#!/usr/bin/env python# -*-
- 代码如下:<% '=================================================
- 阅读上一篇:你是真正的用户体验设计者吗? Ⅰwrite2vin 的 原文路宛兮写的简介:本文介绍了: 1.关于用户体验的几种观点; 2.关于
- 大致介绍在python爬虫爬取某些网站的验证码的时候可能会遇到验证码识别的问题,现在的验证码大多分为四类:1、计算验证码 &nbs
- 前言:线性回归模型属于经典的统计学模型,该模型的应用场景是根据已知的变量(即自变量)来预测某个连续的数值变量(即因变量)。例如餐厅根据媒体的
- 一.python读取txt文件最简单的open函数:# -*- coding: utf-8 -*-with open("test.