Python之numpy常用命令

本文主要内容:

  1. array数组创建
    更新中。。。

  2. 如何从numpy数组的现有列创建新列?

问题:在iris_2d中为卷创建一个新列,其中volume是(pi x petallength x sepal_length ^ 2)/ 3

给定:

1
2
3
4
# Input
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')
names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')

答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Input
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')

# Solution
# Compute volume
sepallength = iris_2d[:, 0].astype('float')
petallength = iris_2d[:, 2].astype('float')
volume = (np.pi * petallength * (sepallength**2))/3

# Introduce new dimension to match iris_2d's
volume = volume[:, np.newaxis]

# Add the new column
out = np.hstack([iris_2d, volume])

# View
out[:4]
# > array([[b'5.1', b'3.5', b'1.4', b'0.2', b'Iris-setosa', 38.13265162927291],
# > [b'4.9', b'3.0', b'1.4', b'0.2', b'Iris-setosa', 35.200498485922445],
# > [b'4.7', b'3.2', b'1.3', b'0.2', b'Iris-setosa', 30.0723720777127],
# > [b'4.6', b'3.1', b'1.5', b'0.2', b'Iris-setosa', 33.238050274980004]], dtype=object)
  1. 如何按列对2D数组进行排序
    问题:根据sepallength列对虹膜数据集进行排序。
    给定:
    1
    2
    3
    url = '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']