unique_arr = np.unique(arr, axis=0)会打乱顺序
时间: 2024-06-17 07:06:20 浏览: 11
使用`unique_arr = np.unique(arr, axis=0)`将不会打乱原始数组的顺序,`np.unique()`函数将返回一个新的数组,其中包含原始数组中所有不重复的行,并且这些行的顺序与原始数组中的顺序相同。如果原始数组是有序的,则返回的不同行也是有序的。因此,使用`np.unique()`不会改变原始数组的顺序。
如果你需要对数组进行排序,可以使用`np.sort()`函数来对数组进行排序,例如:
```
sorted_arr = np.sort(unique_arr, axis=0)
```
相关问题
unique_arr, indices = np.unique(arr, axis=0, return_index=True) 这是什么意思
这行代码使用 NumPy 库中的 `unique` 函数对二维数组 `arr` 进行处理,返回两个值:去重后的数组 `unique_arr` 和保留原数组中第一次出现的元素的下标数组 `indices`。
其中,`axis=0` 表示按照行的方向进行去重,`return_index=True` 表示返回下标数组。因此,`unique_arr` 是一个去重后的二维数组,`indices` 是一个一维数组,包含了去重后的数组中每个元素在原数组中第一次出现的下标。
需要注意的是,如果 `arr` 数组中有多个相同的行,`unique` 函数会将它们全部视为重复元素,只保留第一个出现的元素,并返回它们在 `unique_arr` 中的下标。
错误改正:import xlrd import numpy as np wb=xlrd.open("历年总人口.xls") sheet=wb.sheet_by_index(0) col_0=sheet.col_values(0) col_1=sheet.col_values(1) col_2=sheet.col_values(2) col_4=sheet.col_values(4) year=col_0[38:] total=col_1[38:] man=col_2[38:] woman=col_4[38:] year=[int(c) for c in year] total=[int(c) for c in total] man=[int(c) for c in man] woman=[int(c) for c in woman] arr=np.array(year).reshape(m,1) arr=np.insert(arr,1,values=total,axis=1) arr=np.insert(arr,1,values=man,axis=1) arr=np.insert(arr,1,values=woman,axis=1) file='历年总人口.csv' np.savetxt(file,arr,fmt='%i',delimiter=',',comments='',header='年份,年末总人口,男性人口,女性人口') x=np.loadtxt(file,dtype=np.int,,delimiter=',',skiprows=1) print(x)
import xlrd
import numpy as np
wb = xlrd.open_workbook("历年总人口.xls")
sheet = wb.sheet_by_index(0)
col_0 = sheet.col_values(0)[38:]
col_1 = sheet.col_values(1)[38:]
col_2 = sheet.col_values(2)[38:]
col_4 = sheet.col_values(4)[38:]
year = [int(c) for c in col_0]
total = [int(c) for c in col_1]
man = [int(c) for c in col_2]
woman = [int(c) for c in col_4]
m = len(year)
arr = np.array(year).reshape(m,1)
arr = np.insert(arr,1,values=total,axis=1)
arr = np.insert(arr,1,values=man,axis=1)
arr = np.insert(arr,1,values=woman,axis=1)
file='历年总人口.csv'
np.savetxt(file,arr,fmt='%i',delimiter=',',comments='',header='年份,年末总人口,男性人口,女性人口')
x = np.loadtxt(file,dtype=np.int,delimiter=',',skiprows=1)
print(x)
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)