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

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

2018-03-01から1ヶ月間の記事一覧

【Python】How to generate one-hot encodings for an array in numpy? - 101 Numpy Exercises

Q: One-hot encodingを計算しなさい (Kerasのnp_utils.to_categoricalを使えば良いのですが、Keras使わない時のためのメモ) Input: arr = np.random.randint(1,4, size=6) arr #> array([2, 3, 2, 2, 2, 1]) Output: #> array([[ 0., 1., 0.], #> [ 0., 0., …

【Python】How to find the most frequent value in a numpy array? - 101 Numpy Exercises

Q: irisのpetal lengthで最も出現頻度が高い値を見つけなさい A: # Input: url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris = np.genfromtxt(url, delimiter=',', dtype='object') # Solution: vals, counts = np.u…

【Python】How to sort a 2D array by a column? - 101 Numpy Exercises

Q: irisデータセットをsepallengthカラムの値でソートしなさい A: url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris = np.genfromtxt(url, delimiter=',', dtype='object') names = ('sepallength', 'sepalwidth', '…

【Python】How to get the second largest value of an array when grouped by another array? - 101 Numpy Exercises

Q: setosaで二番目に長いpetallengthは何? # 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',…

【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, delimi…

【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:…

【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]) # Solut…

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

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な</double>…

【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]) specie…

【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]…

【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

【Python】How to replace items that satisfy a condition without affecting the original array? - 101 Numpy Exercises

Q: 0~9の奇数を元の変数に影響を与えずに-1に変更する A: import numpy as np arr = np.arange(0, 10) arr_ = np.where(arr%2 == 1, -1, arr) www.machinelearningplus.com