首页 > 其他分享 >Applied Statistics - 应用统计学习 - numpy array交换两行 ? How to Swap Two Rows in a NumPy Array (With Example)

Applied Statistics - 应用统计学习 - numpy array交换两行 ? How to Swap Two Rows in a NumPy Array (With Example)

时间:2023-12-30 11:23:40浏览次数:40  
标签:Applied Statistics some rows swap following array NumPy

https://www.statology.org/qualitative-vs-quantitative-variables/

https://www.statology.org/numpy-swap-rows/
How to Swap Two Rows in a NumPy Array (With Example)
You can use the following basic syntax to swap two rows in a NumPy array:
some_array[[0, 3], :] = some_array[[3, 0], :]
OR
some_array[[0, 3]] = some_array[[3, 0]]

This particular example will swap the first and fourth rows in the NumPy array called some_array.
All other rows will remain in their original positions.

The following example shows how to use this syntax in practice.

Example: Swap Two Rows in NumPy Array
Suppose we have the following NumPy array:

import numpy as np

#create NumPy array
some_array = np.array([[1, 1, 2], [3, 3, 7], [4, 3, 1], [9, 9, 5], [6, 7, 7]])

#view NumPy array
print(some_array)

[[1 1 2]
 [3 3 7]
 [4 3 1]
 [9 9 5]
 [6 7 7]]

We can use the following syntax to swap the first and fourth rows in the NumPy array:

#swap rows 1 and 4
some_array[[0, 3]] = some_array[[3, 0]]

#view updated NumPy array
print(some_array)

[[9 9 5]
 [3 3 7]
 [4 3 1]
 [1 1 2]
 [6 7 7]]

Notice that the first and fourth rows have been swapped.
All other rows remained in their original positions.

Note that some_array[[0, 3]] is shorthand for some_array[[0, 3], :], so we could also use the following syntax to get the same results:

#swap rows 1 and 4
some_array[[0, 3], :] = some_array[[3, 0], :]

#view updated NumPy array
print(some_array)

[[9 9 5]
 [3 3 7]
 [4 3 1]
 [1 1 2]
 [6 7 7]]
Notice that the first and fourth rows have been swapped.

This result matches the result from using the shorthand notation in the previous example.

Feel free to use whichever notation you prefer to swap two rows in a given NumPy array.

Additional Resources

The following tutorials explain how to perform other common tasks in NumPy:

标签:Applied,Statistics,some,rows,swap,following,array,NumPy
From: https://www.cnblogs.com/abaelhe/p/17936166.html

相关文章

  • numpy、scipy、pandas、matplotlib的读书报告
    numpy、scipy、pandas、matplotlib的读书报告一、numpy的读书报告1Numpy概述1.1概念Numpy提供了两种基本的对象:ndarray和ufunc。Ndarray是存储单一数据类型的多维数组,而ufunc则是能够对数组进行处理的函数。1.2功能l  创建n维数组(矩阵)l  对数组进行函数运算,使用函......
  • 使用Numpy实现手写数字识别
    1概要  用Python语言在只使用Numpy库的前提下,完成一个全连接网络的搭建和训练。2实现代码参考:https://github.com/binisnull/ann2.1环境设置  创建Python3.8.16的虚拟环境,激活并执行python-mpipinstallnumpy==1.18.5tensorflow-gpu==2.3.0Pillowmatplot......
  • numpy、scipy、pandas、matplotlib的读书报告07
    numpy库常用的创建数组(ndarray类型)使用实例:importnumpyasnpa=np.ones((4,5))#创建一个4行5列全是1的数组print(a)输出为:[[1.1.1.1.1.][1.1.1.1.1.][1.1.1.1.1.][1.1.1.1.1.]] matplotlib.pyplot为matplotlib的子库引用如下:importma......
  • numpy 读书报告
    numpy库常见函数的介绍<1>. numpy创建数组1.从已有数据中创建数组a. 将列表转换成ndarray:importnumpyasnpls1=[10,42,0,-17,30]nd1=np.array(ls1)print(nd1)print(type(nd1))运行结果:[10420-1730]<class'numpy.ndarray'>b.......
  • numpy、scipy、pandas、matplotlib的读书报告
    Numpy:基础的数学计算模块,以矩阵为主,纯数学。SciPy:基于Numpy,提供方法(函数库)直接计算结果,封装了一些高阶抽象和物理模型。比方说做个傅立叶变换,这是纯数学的,用Numpy;做个滤波器,这属于信号处理模型了,在Scipy里找。Pandas:提供了一套名为DataFrame的数据结构,适合统计分析中的......
  • numpy、scipy、pandas、matplotlib的读书报告:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪
    1、基本函数用法Numpy:基础的数学计算模块,来存储和处理大型矩阵,比Python自身的嵌套列表(nestedliststructure)结构要高效的多,本身是由C语言开发。这个是很基础的扩展,其余的扩展都是以此为基础。数据结构为ndarray,一般有三种方式来创建。Scipy:方便、易于使用、专为科学和工程......
  • 和鲸-numpy+pandas使用基础 关卡1
    STEP1:按照下列要求创建数据框已知10位同学的学号以及语数英三科成绩如下:(都是数值型数据)Id:[202001,202002,202003,202004,202005,202006,202007,202008,202009,202010]Chinese:[98,67,84,88,78,90,93,75,82,87]Math:[92,80,73,76,88,78,90,82,7......
  • Django 报错(You have 18 unapplied migration(s))
    pythonmanage.pyrunserver0.0.0.0:8000时报错:Youhave18unappliedmigration(s).Yourprojectmaynotworkproperlyuntilyouapplythemigrationsforapp(s):admin,auth,contenttypes,sessions.Run'pythonmanage.pymigrate'toapplythem.说明之前有......
  • numpy、scipy、pandas、matplotlib的读书报告
    numpy、scipy、pandas、matplotlib的读书报告:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬......
  • 1.NumPy 介绍
    1.NumPy介绍1.1NumPy演变史在NumPy之前,有两个Python数组包:Numeric包Numeric包开发于20世纪90年代中期,在Python中提供了数组对象和数组感知函数。它由C语言编写,并与线性代数的标准快速实现相链接。它最早的用途之一是引导C++应用程序,用于劳伦斯利弗莫尔国......