Python之numpy常用命令
本文主要内容:
array数组创建
更新中。。。如何从numpy数组的现有列创建新列?
问题:在iris_2d中为卷创建一个新列,其中volume是(pi x petallength x sepal_length ^ 2)/ 3
给定:
1 | # Input |
答案:
1 | # Input |
- 如何按列对2D数组进行排序
问题:根据sepallength列对虹膜数据集进行排序。
给定:答案:1
2
3url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter=',', dtype='object')
names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')1
2
3
4
5
6
7# Sort by column position 0: SepalLength
print(iris[iris[:,0].argsort()][:5])
# > [[b'4.3' b'3.0' b'1.1' b'0.1' b'Iris-setosa']
# > [b'4.4' b'3.2' b'1.3' b'0.2' b'Iris-setosa']
# > [b'4.4' b'3.0' b'1.3' b'0.2' b'Iris-setosa']
# > [b'4.4' b'2.9' b'1.4' b'0.2' b'Iris-setosa']
# > [b'4.5' b'2.3' b'1.3' b'0.3' b'Iris-setosa']