Applied Statistics - 应用统计学习 - numpy array交换两行 ? How to Swap Two Rows in a NumPy Array (With Example)
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 - 应用统计学习 - numpy array交换两行 ? How to Swap Two Rows in a NumPy Array (With Example)的更多相关文章
- [译]针对科学数据处理的统计学习教程(scikit-learn教程2)
翻译:Tacey Wong 统计学习: 随着科学实验数据的迅速增长,机器学习成了一种越来越重要的技术.问题从构建一个预测函数将不同的观察数据联系起来,到将观测数据分类,或者从未标记数据中学习到一些结构 ...
- scikit-learning教程(二)统计学习科学数据处理的教程
统计学习:scikit学习中的设置和估计对象 数据集 Scikit学习处理来自以2D数组表示的一个或多个数据集的学习信息.它们可以被理解为多维观察的列表.我们说这些阵列的第一个轴是样本轴,而第二个轴是 ...
- NumPy 字节交换
NumPy 字节交换 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.字节顺序,是跨越多字节的程序对象的存储规则. 大端模式:指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地 ...
- 【StatLearn】统计学习中knn算法实验(2)
接着统计学习中knn算法实验(1)的内容 Problem: Explore the data before classification using summary statistics or vis ...
- NumPy字节交换
NumPy - 字节交换 我们已经知道,存储在计算机内存中的数据取决于 CPU 使用的架构. 它可以是小端(最小有效位存储在最小地址中)或大端(最小有效字节存储在最大地址中). numpy.ndarr ...
- 16、NumPy ——字节交换
NumPy 字节交换 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.字节顺序,是跨越多字节的程序对象的存储规则. 大端模式:指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地 ...
- 统计学习导论:基于R应用——第二章习题
目前在看统计学习导论:基于R应用,觉得这本书非常适合入门,打算把课后习题全部做一遍,记录在此博客中. 第二章习题 1. (a) 当样本量n非常大,预测变量数p很小时,这样容易欠拟合,所以一个光滑度更高 ...
- BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治
BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治 Description 在得知了自己农 ...
- 4.机器学习——统计学习三要素与最大似然估计、最大后验概率估计及L1、L2正则化
1.前言 之前我一直对于“最大似然估计”犯迷糊,今天在看了陶轻松.忆臻.nebulaf91等人的博客以及李航老师的<统计学习方法>后,豁然开朗,于是在此记下一些心得体会. “最大似然估计” ...
- R语言统计学习-1简介
一. 统计学习概述 统计学习是指一组用于理解数据和建模的工具集.这些工具可分为有监督或无监督.1.监督学习:用于根据一个或多个输入预测或估计输出.常用于商业.医学.天体物理学和公共政策等领域.2.无监 ...
随机推荐
- kali 2020使用SSH进行远程登录
修改sshd_config文件,命令为: vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉,并且将NO修改为YES //kali中默认是 ...
- idea格式化代码快捷键
Ctrl+Alt+L Ctrl+Shift+Alt+L
- 【记录】MATLAB|Python NumPy|两种语言的数组/矩阵中元素修改方式的总结和对比
文章目录 二维矩阵 操作 1. 将数组大于0的数全部加1 2. 删除元素 ①删除单个元素 ②删除一列元素 3. 添加一行或多行 ①添加一行 ②添加多行 4. 获取行/列数 5. 格式化输出数组 结构数 ...
- NewtonJsonConvert的比较好搭配使用
(1)与关键字冲突,解决办法加@ var a = new { @class=1, }; var d = JsonConvert.SerializeObject(a); Console.WriteLin ...
- pyqt点击右上角关闭界面但子线程仍在运行
现象: 通过右上角的叉关闭图形界面后,程序运行的子线程却不会被自动关闭,依然留存在系统中 原因: 子线程没有正确关闭 解决方法: 1.将子线程设置成守护线程 self.your_thread = th ...
- 解决VMware虚拟机安装centos无法联网问题
网上的教程几乎试了个遍,最后使用该方法成功解决. 用终端进入目录:/etc/sysconfig/network-scripts 执行ls命令查看以"ifcfg-en"开头的文件,例 ...
- 【公众号搬运】React-Native开发鸿蒙NEXT(7)-上线
.markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...
- #ifndef 、 #define 、#endif使用解释
在C语言程序代码里,看到了这么一段代码: #ifndef __WIFI_CONNECT_H_ #define __WIFI_CONNECT_H_ int WifiConnect(const char ...
- Spring Boot注解之@ComponentScan用法和实现原理
注解@ComponentScan的作用 @Component注解及其衍生注解@RestController.@Controller.@Service和@Repository都是组件注册注解.@Co ...
- 【2020.11.14提高组模拟】无尽之前 (game) 题解
[2020.11.14提高组模拟]无尽之前 (game) 题解 有趣的题面 题目背景 雏见泽,一个和平的,或者说本应和平的小村庄,却因连续四年的怪死事件而蒙上了阴 影. 无一例外,每年的事件都发生在棉 ...