1 " "" 2 月8月26日午後8時57分24秒2019年に作成 3 4 @author:huoqs 5 6 KNNアルゴリズム 7 """ 8 インポートNPとしてnumpyの 9 インポートPLTとしてmatplotlib.pyplot 10 11 DEF generate_data(num_samples、num_features = 2 ): 12 DATA_SIZE = (num_samples、num_features) 13件の データ= np.random.randint(0、100 、DATA_SIZE) 14 15 label_size =(num_samples、1 ) 16枚の ラベル= np.random.randint(0、2 、label_size ) 17 位のfloat32でなければならない 18 のリターンラベル、data.astype(np.float32) 19 20 DEF :plot_data(all_blue、all_red) 21 plt.scatter(all_blue [:, 0]、all_blue [:, 1]、C = ' Bを'マーカー= ' S 'S = 180 ) 22 plt.scatter(all_red [:, 0]、[:, 1]、C = all_red ' 、R 'マーカー= ' ^ '、S = 180 ) 23 PLT。 xlabel(' X ' ) 24 plt.ylabel(' Y' ) 25 26 plt.style.use(' ggplot ' ) 27 28 np.random.seed(42 ) 29 30 train_data、ラベル= generate_data(11 ) 31 32 #プリント(train_data、ラベル) 33 34青= train_data [ラベル.ravel()== 0] 35赤= train_data [labels.ravel()== 1 ] 36 37 plot_data(青、赤)
知識ポイント:
1、np.random.randint関数、配列パラメータを生成する:低、高、サイズ、タイプ
https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.random.randint.html
2、ndarray.ravel()、平坦なアレイは、アレイは、一次元になります
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.ravel.html