旅行好きなソフトエンジニアの備忘録

プログラミングや技術関連のメモを始めました

【Python】How to convert a numeric to a categorical (text) array? - 101 Numpy Exercises

Q:

以下のようにiris_2dの3番目のカラムをビニングしなさい

  • 3より小さい --> 'small'
  • 3-5 --> 'medium'
  • 5以上 --> 'large'
# Input
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')


A:

# Input
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')

# Bin petallength 
petal_length_bin = np.digitize(iris[:, 2].astype('float'), [0, 3, 5, 10])

# Map it to respective category
label_map = {1: 'small', 2: 'medium', 3: 'large', 4: np.nan}
petal_length_cat = [label_map[x] for x in petal_length_bin]

# View
petal_length_cat[:4]
<#> ['small', 'small', 'small', 'small']


www.machinelearningplus.com

【Python】How to drop rows that contain a missing value from a numpy array? - 101 Numpy Exercises

Q:

iris_2dからnanを含まない行だけ取り出しなさい

# Input
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])


A:

# Input
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan

# Solution
# No direct numpy function for this.
any_nan_in_row = np.array([~np.any(np.isnan(row)) for row in iris_2d])
iris_2d[any_nan_in_row][:5]
#> array([[ 4.9,  3. ,  1.4,  0.2],
#>        [ 4.7,  3.2,  1.3,  0.2],
#>        [ 4.6,  3.1,  1.5,  0.2],
#>        [ 5. ,  3.6,  1.4,  0.2],
#>        [ 5.4,  3.9,  1.7,  0.4]])


www.machinelearningplus.com

【Python】How to find the percentile scores of a numpy array? - 101 Numpy Exercises

Q:

irisデータの"sepallength"の5~95パーセンタイルを見つけなさい

A:

# Input
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])

# Solution
np.percentile(sepallength, q=[5, 95])
#> array([ 4.6  ,  7.255])


www.machinelearningplus.com

【C#】リストから指定した値に一番近い値を持つインデックスを取得する

例えば以下のようなことがやりたく、普段for文を回してサーチしていました。

var list = new List<double>() { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
double value = 3.1;
int index = FindClosestIndex(list, value);
// 3.1に一番近い値は3.0。3.0のインデックスは2なので2と表示される
Console.WriteLine(index);


このやり方はStackOverflowにあって、下記で出来ました。Aggregateは分かりづらいので放っておいたのですが、こんな使い方も出来るのですね。

int FindClosestIndex(List<double> list, double value)
{
    double closest = list.Aggregate((x, y) => Math.Abs(x - value) < Math.Abs(y - value) ? x : y);
    return list.IndexOf(closest);
}


stackoverflow.com

【Python】How to extract a particular column from 1D array of tuples? - 101 Numpy Exercises

Q:

irisデータのspeciesカラムを取り出しなさい

A:

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_1d = np.genfromtxt(url, delimiter=',', dtype=None)

species = np.array([row[4] for row in iris_1d])
species[:5]
#> array([b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',
#>        b'Iris-setosa'],
#>        dtype='|S18')


www.machinelearningplus.com

【Python】How to print only 3 decimal places in python numpy array? - 101 Numpy Exercises

Q:

小数点を三桁のみ表示するようにしなさい

A:

# Create the random array
rand_arr = np.random.random([5,3])

# Limit to 3 decimal places
np.set_printoptions(precision=3)
rand_arr[:4]
#> array([[ 0.443,  0.109,  0.97 ],
#>        [ 0.388,  0.447,  0.191],
#>        [ 0.891,  0.474,  0.212],
#>        [ 0.609,  0.518,  0.403]])


www.machinelearningplus.com

【Python】How to swap two columns in a 2d numpy array? - 101 Numpy Exercises

Q:

変数arrのカラム0とカラム1を入れ替えなさい

arr = np.arange(9).reshape(3,3)
#> array([[0, 1, 2],
#>        [3, 4, 5],
#>        [6, 7, 8]])

A:

arr = arr[:, [1, 0, 2]]
#> array([[1, 0, 2],
#>        [4, 3, 5],
#>        [7, 6, 8]])


www.machinelearningplus.com