├── 2.图像风格迁移 ├── Code_Keras.py ├── Code_Tensorflow.py ├── content.jpg ├── flower.jpg ├── style1.jpg ├── style2.jpg ├── style3.jpg ├── style4.jpg └── style5.jpg ├── Bagging.py ├── DL ├── 1.词云 │ ├── Hiragino.ttf │ ├── Woedcloud_chinese.png │ ├── Word_1.py │ ├── Wordcloud_2.png │ ├── Wordcloud_2_chinese.py │ ├── Wordcloud_2_chinese2.py │ ├── Wordcloud_2_chinese3.py │ ├── Wordcloud_2_chinese4.py │ ├── Wordcloud_2_chinese5.py │ ├── Wordcloud_chinese3.png │ ├── Wordcloud_chinese4.png │ ├── Wordcloud_chinese5.png │ ├── black_mask.png │ ├── color_mask.png │ ├── constitution.txt │ ├── wordcloud.png │ └── xyj.txt ├── 2.图像风格迁移 │ ├── Code_Keras.py │ ├── Code_Tensorflow.py │ ├── content.jpg │ ├── flower.jpg │ ├── style1.jpg │ ├── style2.jpg │ ├── style3.jpg │ ├── style4.jpg │ └── style5.jpg └── README.md ├── ML ├── 1-Logistic Regression │ ├── test.py │ ├── test_data.txt │ ├── train.py │ └── train_data.txt ├── 2-Softmax Regression │ ├── test.py │ ├── train.py │ ├── train_data.txt │ └── 逻辑回归于Softmax回归的关系.txt ├── 3.Factorization Machine │ ├── Test │ │ ├── Test_FM.py │ │ ├── __init__.py │ │ ├── predict_result │ │ └── test_data.txt │ └── Train │ │ ├── Train.py │ │ ├── __init__.py │ │ ├── train_data.txt │ │ └── weights_FM ├── 4.SVM-SMO │ ├── SVM_Train.py │ ├── __pycache__ │ │ ├── SVM_Train.cpython-36.pyc │ │ └── svm.cpython-36.pyc │ ├── heart_scale │ ├── model_file │ ├── svm.py │ ├── svm_test.py │ └── svm_test_data └── 5.Random Forest │ ├── Random_Forest_Test.py │ ├── Random_Forest_Train.py │ ├── Tree.py │ ├── __pycache__ │ ├── Random_Forest_Train.cpython-36.pyc │ └── Tree.cpython-36.pyc │ ├── data.txt │ ├── feature_file │ ├── final_result │ ├── result_file │ └── test_data.txt ├── README.md └── 机器学习——基础知识.md /2.图像风格迁移/Code_Keras.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019/2/21 20:23 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Code_Keras.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | from __future__ import print_function 10 | from keras.preprocessing.image import load_img, img_to_array 11 | from scipy.misc import imsave 12 | import numpy as np 13 | from scipy.optimize import fmin_l_bfgs_b 14 | import time 15 | import argparse 16 | 17 | from keras.applications import vgg19 18 | from keras import backend as K 19 | 20 | parse = argparse.ArgumentParser(description='Neural style transfer with Keras.') 21 | parse.add_argument('base_image_path', metavar='base', type=str, help='path to the image to transform.') 22 | parse.add_argument('style_reference_image_path', metavar='ref', type=str, help='path to the style reference image.') 23 | parse.add_argument('result_prefix', metavar='res_prefix', type=str, help='prefix for the saved results.') 24 | parse.add_argument('--iter', type=int, defult=10, required=False, help='Number of the iteration to run.') 25 | parse.add_argument('--content_weight', type=float, defult=0.025, required=False, help='content weight.') 26 | parse.add_argument('--style_weight', type=float, defult=1.0, required=False, help='style weght.') 27 | parse.add_argument('--tv_weight', type=float, defult=1.0, required=False, help='Total Variation weight.') 28 | 29 | args = parse.parse_args() 30 | base_image_path = args.base_image_path 31 | style_reference_image_path = args.style_reference_image_path 32 | result_prefix = args.result_prefix 33 | iterations = args.iter 34 | 35 | total_variation_weight = args.tv_weight 36 | style_weight = args.style_weight 37 | content_weight = args.content_weight 38 | 39 | width, height = load_img(base_image_path).size 40 | img_nrows = 400 41 | img_ncols = int(width * img_nrows / height) 42 | 43 | 44 | def preprocess_image(image_path): 45 | img = load_img(image_path, target_size=(img_nrows, img_ncols)) 46 | img = img_to_array(img) 47 | img = np.expand_dims(img, axis=0) 48 | img = vgg19.preprocess_input(img) 49 | return img 50 | 51 | 52 | def deprocess_img(x): 53 | if K.image_data_format() == 'channels_first': 54 | x = x.reshape((3, img_nrows, img_ncols)) 55 | x = x.transpose((1, 2, 0)) 56 | else: 57 | x = x.reshape((img_nrows, img_ncols, 3)) 58 | x[:, :, 0] += 103.939 59 | x[:, :, 1] += 116.779 60 | x[:, :, 2] += 123.68 61 | 62 | x = x[:, :, ::-1] 63 | x = np.clip(x, 0, 255).astype('uint8') 64 | return x 65 | 66 | base_image = K.variable(preprocess_image(base_image_path)) 67 | style_reference_image = K.variable(preprocess_image(style_reference_image_path)) 68 | 69 | if K.image_data_format() == 'channels_first': 70 | combination_image = K.placeholder((1, 3, img_nrows, img_ncols)) 71 | else: 72 | combination_image = K.placeholder((1, img_nrows, img_ncols, 3)) 73 | 74 | input_tensor = K.concatenate([base_image, style_reference_image, combination_image], axis=0) 75 | model = vgg19.VGG19(input_tensor=input_tensor, weight='imagenet', include_top=False) 76 | print('*********模型加载完成*********') 77 | 78 | outputs_dict = dict([layer.name, layer.output]) 79 | 80 | 81 | def gram_matrix(x): 82 | assert K.ndim(x) == 3 83 | if K.image_data_format() == 'channels_first': 84 | features = K.batch_flatten(x) 85 | else: 86 | features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1))) 87 | gram = K.dot(features, K.transpose(features)) 88 | return gram 89 | 90 | 91 | def style_loss(style, combination): 92 | assert K.ndim(style) == 3 93 | assert K.ndim(combination) == 3 94 | S = gram_matrix(style) 95 | C = gram_matrix(combination) 96 | channels = 3 97 | size = img_nrows * img_ncols 98 | return K.sum(K.square(S - C)) / (4.0 * (channels ** 2) * (size ** 2)) 99 | 100 | 101 | def content_loss(base, combination): 102 | return K.sum(K.square(combination - base)) 103 | 104 | 105 | def total_variation_loss(x): 106 | assert K.ndim(x) == 4 107 | if K.image_data_format() == 'channels_first': 108 | a = K.square(x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, 1:, :img_ncols - 1]) 109 | b = K.square(x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, :img_nrows - 1, 1:]) 110 | else: 111 | a = K.square(x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, 1:, :img_ncols - 1, :]) 112 | b = K.square(x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, :img_nrows - 1, 1:, :]) 113 | return K.sum(K.pow(a + b, 1.25)) 114 | 115 | loss = K.variable(0.) 116 | layer_features = outputs_dict['block5_conv2'] 117 | base_image_features = layer_features[0, :, :, :] 118 | combination_features = layer_features[2, :, :, :] 119 | loss += content_weight * content_loss(base_image_features, combination_features) 120 | 121 | feature_layers = ['block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1', 'block5_conv1'] 122 | 123 | for layer_name in feature_layers: 124 | layer_features = outputs_dict[layer_name] 125 | style_reference_features = layer_features[1, :, :, :] 126 | combination_features = layer_features[2, :, :, :] 127 | sl = style_loss(style_reference_features, combination_features) 128 | loss += (style_weight / len(feature_layers)) * sl 129 | loss += total_variation_weight * total_variation_loss(combination_image) 130 | 131 | grads = K.gradients(loss, combination_image) 132 | outputs = [loss] 133 | 134 | if isinstance(grads, (list, tuple)): 135 | outputs += grads 136 | else: 137 | outputs.append(grads) 138 | 139 | f_outputs = K.function([combination_image], outputs) 140 | 141 | 142 | def eval_loss_and_grads(x): 143 | if K.image_data_format() == 'channels_first': 144 | x = x.reshape((1, 3, img_nrows, img_ncols)) 145 | else: 146 | x = x.reshape((1, img_nrows, img_ncols, 3)) 147 | outs = f_outputs([x]) 148 | loss_values = outs[0] 149 | if len(outs[1:]) == 1: 150 | grad_values = outs[1].flatten().astype('float64') 151 | else: 152 | grad_values = np.array(outs[1:].flatten().astype('float64')) 153 | return loss_values, grad_values 154 | 155 | 156 | class Evalautor(object): 157 | def __init__(self): 158 | self.loss_value = None 159 | self.grads_values = None 160 | 161 | def loss(self, x): 162 | assert self.loss_value is None 163 | loss_value, grad_values = eval_loss_and_grads(x) 164 | self.loss_value = loss_value 165 | self.grad_values = grad_values 166 | return self.loss_value 167 | 168 | def grads(self, x): 169 | assert self.loss_value is not None 170 | grad_values = np.copy(self.grad_values) 171 | self.loss_value = None 172 | self.grad_values = None 173 | return grad_values 174 | 175 | evaluator = Evalautor() 176 | 177 | x = preprocess_image(base_image_path) 178 | 179 | for i in range(iterations): 180 | print('********* 开始迭代 *********') 181 | start = time.time() 182 | x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(), fprime=evaluator.grads, maxfun=20) 183 | print('当前的误差为:', min_val) 184 | img = deprocess_img(x.copy()) 185 | fname = result_prefix + '_at_iteration_%d.png' % i 186 | imsave(fname, img) 187 | end = time.time() 188 | print('图片保存为:', fname) 189 | print('第 %d 迭代次完成,用时为 %d s', (i, end-start)) 190 | -------------------------------------------------------------------------------- /2.图像风格迁移/Code_Tensorflow.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019/2/21 18:03 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Code_Tensorflow.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import tensorflow as tf 10 | import numpy as np 11 | import scipy.io 12 | import scipy.misc 13 | import os 14 | import time 15 | 16 | 17 | def the_current_time(): 18 | print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(time.time())))) 19 | 20 | 21 | CONTENT_IMG = 'flower.jpg' 22 | STYLE_IMG = 'style5.jpg' 23 | OUTPUT_DIR = 'Tensorflow_Picture2/' 24 | 25 | # 如果没有相应的文件夹就自动创建文件夹 26 | if not os.path.exists(OUTPUT_DIR): 27 | os.mkdir(OUTPUT_DIR) 28 | 29 | IMAGE_W = 270 30 | IMAGE_H = 480 31 | COLOR_C = 3 32 | 33 | NOSIE_RATIO = 0.7 34 | BETE = 5 35 | ALPHA = 100 36 | VGG_MODEL = 'imagenet-vgg-verydeep-19.mat' 37 | MEAN_VALUES = np.array([123.68, 116.779, 103.939]).reshape((1, 1, 1, 3)) 38 | 39 | 40 | def load_vgg_model(path): 41 | vgg = scipy.io.loadmat(path) 42 | vgg_layers = vgg['layers'] 43 | 44 | def _weights(layer, expected_layer_name): 45 | W = vgg_layers[0][layer][0][0][2][0][0] 46 | b = vgg_layers[0][layer][0][0][2][0][1] 47 | layer_name = vgg_layers[0][layer][0][0][0][0] 48 | assert layer_name == expected_layer_name 49 | return W, b 50 | 51 | def _conv2d_relu(prev_layer, layer, layer_name): 52 | W, b = _weights(layer, layer_name) 53 | W = tf.constant(W) 54 | b = tf.constant(np.reshape(b, (b.size))) 55 | return tf.nn.relu(tf.nn.conv2d(prev_layer, filter=W, strides=[1, 1, 1, 1], padding='SAME') + b) 56 | 57 | def _avgpool(prev_layer): 58 | return tf.nn.avg_pool(prev_layer, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 59 | 60 | graph = {} 61 | graph['input'] = tf.Variable(np.zeros((1, IMAGE_H, IMAGE_W, COLOR_C)), dtype='float32') 62 | graph['conv1_1'] = _conv2d_relu(graph['input'], 0, 'conv1_1') 63 | graph['conv1_2'] = _conv2d_relu(graph['conv1_1'], 2, 'conv1_2') 64 | graph['avgpool1'] = _avgpool(graph['conv1_2']) 65 | graph['conv2_1'] = _conv2d_relu(graph['avgpool1'], 5, 'conv2_1') 66 | graph['conv2_2'] = _conv2d_relu(graph['conv2_1'], 7, 'conv2_2') 67 | graph['avgpool2'] = _avgpool(graph['conv2_2']) 68 | graph['conv3_1'] = _conv2d_relu(graph['avgpool2'], 10, 'conv3_1') 69 | graph['conv3_2'] = _conv2d_relu(graph['conv3_1'], 12, 'conv3_2') 70 | graph['conv3_3'] = _conv2d_relu(graph['conv3_2'], 14, 'conv3_3') 71 | graph['conv3_4'] = _conv2d_relu(graph['conv3_3'], 16, 'conv3_4') 72 | graph['avgpool3'] = _avgpool(graph['conv3_4']) 73 | graph['conv4_1'] = _conv2d_relu(graph['avgpool3'], 19, 'conv4_1') 74 | graph['conv4_2'] = _conv2d_relu(graph['conv4_1'], 21, 'conv4_2') 75 | graph['conv4_3'] = _conv2d_relu(graph['conv4_2'], 23, 'conv4_3') 76 | graph['conv4_4'] = _conv2d_relu(graph['conv4_3'], 25, 'conv4_4') 77 | graph['avgpool4'] = _avgpool(graph['conv4_4']) 78 | graph['conv5_1'] = _conv2d_relu(graph['avgpool4'], 28, 'conv5_1') 79 | graph['conv5_2'] = _conv2d_relu(graph['conv5_1'], 30, 'conv5_2') 80 | graph['conv5_3'] = _conv2d_relu(graph['conv5_2'], 32, 'conv5_3') 81 | graph['conv5_4'] = _conv2d_relu(graph['conv5_3'], 34, 'conv5_4') 82 | graph['avgpool5'] = _avgpool(graph['conv5_4']) 83 | return graph 84 | 85 | 86 | def content_loss_func(sess, model): 87 | def _content_loss(p, x): 88 | N = p.shape[3] 89 | M = p.shape[1] * p.shape[2] 90 | return (1/(4 * N * M)) * tf.reduce_sum(tf.pow(x-p, 2)) 91 | return _content_loss(sess.run(model['conv4_2']), model['conv4_2']) 92 | 93 | STYLE_LAYERS = [('conv1_1', 0.5), ('conv2_1', 1.0), ('conv3_1', 1.5), ('conv4_1', 3.0), ('conv5_1', 4.0)] 94 | 95 | 96 | def style_loss_func(sess, model): 97 | def _gram_matrix(F, N, M): 98 | Ft = tf.reshape(F, (M, N)) 99 | return tf.matmul(tf.transpose(Ft), Ft) 100 | 101 | def _style_loss(a, x): 102 | N = a.shape[3] 103 | M = a.shape[1] * a.shape[2] 104 | A = _gram_matrix(a, N, M) 105 | G = _gram_matrix(x, N, M) 106 | return (1 / (4 * N ** 2 * M ** 2)) * tf.reduce_sum(tf.pow(G - A, 2)) 107 | return sum([_style_loss(sess.run(model[layer_name]), model[layer_name]) * w for layer_name, w in STYLE_LAYERS]) 108 | 109 | 110 | def generate_noise_image(content_image, noise_ratio=NOSIE_RATIO): 111 | noise_image = np.random.uniform(-20, 20, (1, IMAGE_H, IMAGE_W, COLOR_C)).astype('float32') 112 | input_image = noise_image * noise_ratio + content_image * (1 - noise_ratio) 113 | return input_image 114 | 115 | 116 | def load_image(path): 117 | image = scipy.misc.imread(path) 118 | image = scipy.misc.imresize(image, (IMAGE_H, IMAGE_W)) 119 | image = np.reshape(image, ((1, ) + image.shape)) 120 | image = image - MEAN_VALUES 121 | return image 122 | 123 | 124 | def save_image(path, image): 125 | image = image + MEAN_VALUES 126 | image = image[0] 127 | image = np.clip(image, 0, 255).astype('uint8') 128 | scipy.misc.imsave(path, image) 129 | 130 | the_current_time() 131 | 132 | 133 | with tf.Session() as sess: 134 | content_image = load_image(CONTENT_IMG) 135 | style_image = load_image(STYLE_IMG) 136 | model = load_vgg_model(VGG_MODEL) 137 | 138 | input_image = generate_noise_image(content_image) 139 | sess.run(tf.global_variables_initializer()) 140 | 141 | sess.run(model['input'].assign(content_image)) 142 | content_loss = content_loss_func(sess, model) 143 | 144 | sess.run(model['input'].assign(style_image)) 145 | style_loss = style_loss_func(sess, model) 146 | 147 | total_loss = BETE * content_loss + ALPHA * style_loss 148 | optimizer = tf.train.AdamOptimizer(2.0) 149 | train = optimizer.minimize(total_loss) 150 | 151 | sess.run(tf.global_variables_initializer()) 152 | sess.run(model['input'].assign(input_image)) 153 | 154 | ITERATIONS = 2000 155 | for i in range(ITERATIONS): 156 | sess.run(train) 157 | if i % 100 == 0: 158 | output_image = sess.run(model['input']) 159 | the_current_time() 160 | print('Iteration %d' % i) 161 | print('Cost: ', sess.run(total_loss)) 162 | 163 | save_image(os.path.join(OUTPUT_DIR, 'output_%d.jpg' % i), output_image) 164 | -------------------------------------------------------------------------------- /2.图像风格迁移/content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/2.图像风格迁移/content.jpg -------------------------------------------------------------------------------- /2.图像风格迁移/flower.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/2.图像风格迁移/flower.jpg -------------------------------------------------------------------------------- /2.图像风格迁移/style1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/2.图像风格迁移/style1.jpg -------------------------------------------------------------------------------- /2.图像风格迁移/style2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/2.图像风格迁移/style2.jpg -------------------------------------------------------------------------------- /2.图像风格迁移/style3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/2.图像风格迁移/style3.jpg -------------------------------------------------------------------------------- /2.图像风格迁移/style4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/2.图像风格迁移/style4.jpg -------------------------------------------------------------------------------- /2.图像风格迁移/style5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/2.图像风格迁移/style5.jpg -------------------------------------------------------------------------------- /Bagging.py: -------------------------------------------------------------------------------- 1 | from random import randrange, seed, random 2 | import numpy as np 3 | 4 | 5 | # 导入数据集ving进行出路形成list,方便下面的函数进行操作计算 6 | def loadData(filename): 7 | dataset = [] 8 | df = open(filename, 'r') 9 | for line in df.readlines(): 10 | if not line: 11 | continue 12 | lineArr = [] 13 | for feature in line.split(','): 14 | str_line = feature.strip() 15 | if str_line.isdigit(): 16 | lineArr.append(float(str_line)) 17 | else: 18 | lineArr.append(str_line) 19 | dataset.append(lineArr) 20 | return dataset 21 | 22 | 23 | # 分割数据集 24 | def Cross_Validation_split(dataset, n_folds): 25 | """ 26 | param --> dataset: 输入的数据集或者说待处理的数据集 27 | param --> n_folds: 数据集被分割后的块数 28 | return --> dataset_split: split后的数据集 29 | """ 30 | 31 | 32 | dataset_split = [] 33 | dataset_copy = dataset.copy() 34 | dataset_num = len(dataset_copy) # 获取数据集的长度,此处为208 35 | fold_size = int(dataset_num) / n_folds # 得到每个split后fold的大小 36 | for i in range(n_folds): 37 | fold = [] 38 | while len(fold) < fold_size: 39 | index = np.random.randint(dataset_num) # 随机生成一个范围内的随机数,作为寻找数据的索引值 40 | # fold.append(dataset_copy.pop(index)) # 无放回的方式 41 | fold.append(dataset_copy[index]) # 有放回的方式 42 | dataset_split.append(fold) 43 | return dataset_split 44 | 45 | 46 | # 根据特征和特征值分割数据集 47 | def test_split(index, value, dataset): 48 | """ 49 | :param index: 待决策数据(特征)的索引值 50 | :param value: 特征值---row[index] 51 | :param dataset: 数据集 52 | :return: 53 | """ 54 | left, right = list(), list() 55 | for row in dataset: 56 | if float(row[index]) < float(value): 57 | # left.append(row[index]) 58 | left.append(row) 59 | else: 60 | right.append(row) 61 | # right.append(row[index]) 62 | return left, right 63 | 64 | 65 | # 计算 group 的基尼系数,判断算法的优良 66 | def gini_index(groups, class_values): 67 | """ 68 | 69 | :param groups: 分组后的数据集 70 | :param class_values: 71 | :return: 72 | """ 73 | gini = 0.0 74 | D = len(groups[0]) + len(groups[1]) 75 | for class_value in class_values: 76 | for group in groups: 77 | size = len(group) 78 | if size == 0: 79 | continue 80 | # count()方法用于统计某个元素在列表中出现的次数,row[-1]--->[M,R], 81 | # class_value为dataset中的[M,R] 82 | # 故proportion为 class_value=[M,R] 在 group=float(size) 中的占比,即为:p(k) 83 | # proportion = [row[-1] for row in group].count(class_value) / float(size) 84 | proportion = [row[-1] for row in group].count(class_value) / float(size) 85 | gini += float(size)/D * (proportion * (1.0 - proportion)) # 基尼系数:Gini(p) = ∑p(k)*[1-p(k)] k=1--->K 86 | return gini 87 | 88 | 89 | # 通过计算 gini 系数来寻找分割数据集的最优特征: 90 | # 最优的特征 index、特征值 row[index]、以及分割完的数据 groups(left, right) 91 | def get_split(dataset, n_features): 92 | """ 93 | :param dataset: 数据集 94 | :param n_features: 找取的特征的个数 95 | :return: {'index': b_index, 'value': b_value, 'groups': b_groups}字典 96 | b_index =》为最优特征的索引 97 | b_value =》最优特征的特征值 98 | b_groups =》最优的分组 99 | """ 100 | # 获取数据集所属类别[M,R] 101 | class_value = list(set(row[-1] for row in dataset)) 102 | b_index, b_value, b_score, b_groups = 999, 999, 999, None 103 | features = list() # 特征索引 list 104 | while len(features) < n_features: 105 | index = np.random.randint(len(dataset[0]) - 1) # 随机获取数据集中的一个特征的索引 106 | if index not in features: 107 | features.append(index) 108 | # 根据给出的n_features的个数进行 split 数据集 group。 109 | for index in features: 110 | for row in dataset: 111 | groups = test_split(index, row[index], dataset) # 根据不同特征索引,特征值划分数据集 dataset,得到 group 112 | gini = gini_index(groups, class_value) # 计算分割后的数据集 group 的 Gini 值 113 | if gini < b_score: 114 | b_index, b_value, b_score, b_groups = index, row[index], gini, groups 115 | return {'index': b_index, 'value': b_value, 'groups': b_groups} 116 | 117 | 118 | # 输出group中出现次数较多的标签 119 | def to_terminal(group): 120 | """ 121 | :param group: 分组后的数据集 122 | :return: group中出现次数较多的标签 M or R 123 | """ 124 | outcomes = [row[-1] for row in group] 125 | # max() 方法返回给定参数的最大值,参数可以为序列。 126 | # set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。 127 | return max(set(outcomes), key=outcomes.count) # key=outcomes.count表示用count()函数对outcomes中的两个元素计数 128 | 129 | 130 | # 创建随机森林中的子分割器进行递归分类,直到分类结束。 131 | def split(node, max_depth, min_size, n_features, depth): 132 | left, right = node['groups'] 133 | del (node['groups']) 134 | if not left or not right: 135 | node['left'] = node['right'] = to_terminal(left + right) 136 | return 137 | # max_depth 表示递归次数,若分类还未结束,则选取数据中分类标签较多的作为结果,使分类提前结束,防止过拟合 138 | if depth >= max_depth: 139 | node['left'], node['right'] = to_terminal(left), to_terminal(right) 140 | return 141 | if len(left) <= min_size: 142 | node['left'] = to_terminal(left) 143 | else: 144 | # get_split(left, n_features) 的 return 值 ===》{'index': b_index, 'value': b_value, 'groups': b_groups} 145 | node['left'] = get_split(left, n_features) # left 为左支数数据集 146 | split(node['left'], max_depth, min_size, n_features, depth+1) # 递归,depth+1计算递归层数 147 | if len(right) < min_size: 148 | node['right'] = to_terminal(right) 149 | else: 150 | node['right'] = get_split(right, n_features) 151 | split(node['right'], max_depth, min_size, n_features, depth+1) 152 | 153 | 154 | # 构建数 155 | def build_tree(dataset, max_depth, min_size, n_features): 156 | root = get_split(dataset, n_features) # 根据初始数据集,划分 root 157 | split(root, max_depth, min_size, n_features, 1) # 持续跟新划分树 root 158 | return root 159 | 160 | 161 | # 预测模型分类结果 162 | def predict(node, row): 163 | """ 164 | :param node: 决策树 165 | :param row: 遍历的dataset内容 166 | :return: True or False(bool类型数据,布尔数据) 167 | """ 168 | # 返回 True or False 169 | if float(row[node['index']]) < float(node['value']): 170 | if isinstance(node['left'], dict): # 判断node['left']是否为字典 171 | return predict(node['left'], row) # 形成递归 172 | else: 173 | return node['left'] 174 | # 返回 True or False 175 | else: 176 | if isinstance(node['right'], dict): 177 | return predict(node['right'], row) 178 | else: 179 | return node['right'] 180 | 181 | 182 | # 随机森林的分类预测 183 | def bagginig_predict(trees, row): 184 | """ 185 | bagging 预测 186 | :param trees: 决策树集合 187 | :param row: 测试数据集的每一行数据 188 | :return 189 | 返回随机森林中,决策树结果出现次数最多的那个树 190 | """ 191 | predictions = [predict(tree, row) for tree in trees] 192 | return max(predictions, key=predictions.count) 193 | 194 | 195 | # 训练数据随机化 196 | def subsample(dataset, ratio): 197 | sample = [] 198 | n_sample = round(len(dataset) * ratio) # round -->四舍五入 199 | while len(sample) < n_sample: 200 | index = randrange(len(dataset)) 201 | sample.append(dataset[index]) 202 | return sample # sample: 随机抽样的训练样本组合 203 | 204 | 205 | # 随机森林的建立 206 | def random_forest(train, test, max_depth, min_size, sample_size, n_features, n_trees): 207 | """ 208 | :param train: 训练数据集 209 | :param test: 测试数据集 210 | :param max_depth: 决策树深度 不能太深 容易过拟合 211 | :param min_size: 叶子节点的大小 212 | :param sample_size: 训练数据集的样本比例 213 | :param n_features: 选取的特征的个数 214 | :param n_trees: 决策树的个数 215 | :return: 每一行的预测结果 bagging 预测最后的分类结果 216 | """ 217 | trees = [] 218 | for i in range(n_trees): 219 | sample = subsample(train, sample_size) # 获取随机的训练数据样本 220 | tree = build_tree(sample, max_depth, min_size, n_features) # 根据随机数据集样本构建不同的决策树 221 | trees.append(tree) # 生成决策树集合 list 222 | prediction = [bagginig_predict(trees, row) for row in test] # 计算决策树中的预测结果,输出最好的那棵决策树 223 | return prediction 224 | 225 | 226 | # 计算精确度:导入实际值和预测值 227 | def accuracy_metric(actual, predicted): # 导入实际值和预测值,计算精确度 228 | correct = 0 229 | for i in range(len(actual)): 230 | if actual[i] == predicted[i]: 231 | correct += 1 232 | return correct / float(len(actual)) * 100.0 233 | 234 | 235 | # 评估算法性能,返回模型得分 236 | def evaluate_algorithm(dataset, algorithm, n_folds, *args): 237 | """evaluate_algorithm(评估算法性能,返回模型得分) 238 | 239 | Args: 240 | dataset 原始数据集 241 | algorithm 使用的算法 242 | n_folds 数据的份数 243 | *args 其他的参数 244 | Returns: 245 | scores 模型得分 246 | """ 247 | 248 | # 将数据集进行抽重抽样 n_folds 份,得到 folds 数据集合,数据可以重复重复抽取,每一次 list 的元素是无重复的 249 | folds = Cross_Validation_split(dataset, n_folds) 250 | scores = list() 251 | # 每次循环从 folds 从取出一个 fold 作为测试集,其余作为训练集,遍历整个 folds ,实现交叉验证 252 | for fold in folds: 253 | train_set = list(folds) 254 | train_set.remove(fold) # remove() 函数用于移除列表中某个值的第一个匹配项 255 | # 将多个 fold 列表组合成一个 train_set 列表, 类似 union all 256 | """ 257 | In [20]: l1=[[1, 2, 'a'], [11, 22, 'b']] 258 | In [21]: l2=[[3, 4, 'c'], [33, 44, 'd']] 259 | In [22]: l=[] 260 | In [23]: l.append(l1) 261 | In [24]: l.append(l2) 262 | In [25]: l 263 | Out[25]: [[[1, 2, 'a'], [11, 22, 'b']], [[3, 4, 'c'], [33, 44, 'd']]] 264 | In [26]: sum(l, []) 265 | Out[26]: [[1, 2, 'a'], [11, 22, 'b'], [3, 4, 'c'], [33, 44, 'd']] 266 | """ 267 | train_set = sum(train_set, []) # 就是在train_set上再加一个[],即[train_set] 268 | test_set = list() 269 | # fold 表示从原始数据集 dataset 提取出来的测试集(去除row[-1]的值) 270 | for row in fold: 271 | row_copy = list(row) 272 | row_copy[-1] = None 273 | test_set.append(row_copy) 274 | predicted = algorithm(train_set, test_set, *args) 275 | actual = [row[-1] for row in fold] 276 | 277 | # 计算随机森林的预测结果的正确率 278 | accuracy = accuracy_metric(actual, predicted) 279 | scores.append(accuracy) 280 | return scores 281 | 282 | 283 | def main(): 284 | dataset = loadData('./sonar-all-data.txt') 285 | n_folds = 5 # 分成5份数据,进行交叉验证 286 | max_depth = 20 # 调参(自己修改) #决策树深度不能太深,不然容易导致过拟合 287 | min_size = 1 # 决策树的叶子节点最少的元素数量 288 | sample_size = 1.0 # 做决策树时候的样本的比例 289 | n_features = 15 # 调参(自己修改) #准确性与多样性之间的权衡 290 | for n_trees in [1, 10, 20, 30, 40, 50]: 291 | scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, 292 | n_features) 293 | # 每一次执行本文件时都能产生同一个随机数 294 | # import numpy as np 295 | seed(1) 296 | print('random=', random()) 297 | print('Trees: %d' % n_trees) 298 | print('Scores: %s' % scores) 299 | print('Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores)))) 300 | 301 | 302 | if __name__ == '__main__': 303 | main() 304 | 305 | # import numpy as np 306 | # dataset = loadData('./sonar-all-data.txt') 307 | # outcomes = [row[-1] for row in dataset] 308 | # print(max(set(outcomes), key=outcomes.count)) 309 | # # class_values = list(set(row[-1] for row in dataset)) 310 | # # print(class_values) 311 | # for row in dataset: 312 | # # print(row[-1]) 313 | # for i in range(10): 314 | # pass 315 | # # print(row[i]) 316 | 317 | # print(type(dataset)) 318 | 319 | # print(np.mat(dataset).shape) 320 | # print(len(dataset)) 321 | # dataset_split = Cross_Validation_split(dataset, 6) # 分成6块 322 | # print(np.array(dataset_split).T.shape) 323 | # print(np.array(dataset_split)[0].shape) 324 | # print(np.array(dataset_split).shape) 325 | 326 | # for row in dataset: 327 | # print(row[randrange(len(dataset[0])-1)]) 328 | # # print(randrange(len(dataset[0])-1)) 329 | # for n_trees in [1, 10, 20]: # 理论上树是越多越好 330 | # scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features) 331 | # # 每一次执行本文件时都能产生同一个随机数 332 | # seed(1) 333 | # print('random=', random()) 334 | # print('Trees: %d' % n_trees) 335 | # print('Scores: %s' % scores) 336 | # print('Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores)))) 337 | -------------------------------------------------------------------------------- /DL/1.词云/Hiragino.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/1.词云/Hiragino.ttf -------------------------------------------------------------------------------- /DL/1.词云/Woedcloud_chinese.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/1.词云/Woedcloud_chinese.png -------------------------------------------------------------------------------- /DL/1.词云/Word_1.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019/2/15 22:10 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Word_1.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | from wordcloud import WordCloud 10 | import matplotlib.pyplot as plt 11 | 12 | # 1.打开文本 13 | file = open('constitution.txt').read() 14 | 15 | # 2.生成图像 16 | wc = WordCloud().generate(file) 17 | 18 | # 3.显示词云 19 | plt.imshow(wc, interpolation='bilinear') 20 | plt.axis('off') 21 | plt.show() 22 | 23 | # 4.保存到文件 24 | wc.to_file('wordcloud.png') 25 | -------------------------------------------------------------------------------- /DL/1.词云/Wordcloud_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/1.词云/Wordcloud_2.png -------------------------------------------------------------------------------- /DL/1.词云/Wordcloud_2_chinese.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019/2/15 22:26 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Wordcloud_2_chinese.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | 10 | from wordcloud import WordCloud 11 | import matplotlib.pyplot as plt 12 | # 1.读取文件 13 | file = open('constitution.txt').read() 14 | # 2.生成图像 15 | wc = WordCloud(font_path='Hiragino.ttf', width=800, height=600, mode='RGBA', background_color=None).generate(file) 16 | # 3.显示词云 17 | plt.imshow(wc, interpolation='bilinear') 18 | plt.axis('off') 19 | plt.show() 20 | # 4.保存文件 21 | wc.to_file('Wordcloud_2.png') 22 | -------------------------------------------------------------------------------- /DL/1.词云/Wordcloud_2_chinese2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019/2/15 22:35 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Wordcloud_2_chinese2.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | 10 | from wordcloud import WordCloud 11 | import matplotlib.pyplot as plt 12 | import jieba 13 | # 1.读取文件 14 | file = open('xyj.txt', 'rb').read() 15 | 16 | # 2.用 jieba 对中文进行分词 17 | text = ' '.join(jieba.cut(file)) 18 | print(text[:100]) 19 | 20 | # 3.生成对象 21 | wc = WordCloud(font_path='Hiragino.ttf', width=800, height=600, mode='RGBA', background_color=None).generate(text) 22 | 23 | # 4.显示词云 24 | plt.imshow(wc, interpolation='bilinear') 25 | plt.axis('off') 26 | plt.show() 27 | 28 | # 5.保存文件 29 | wc.to_file('Woedcloud_chinese.png') 30 | -------------------------------------------------------------------------------- /DL/1.词云/Wordcloud_2_chinese3.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019/2/15 22:50 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Wordcloud_2_chinese3.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | 10 | from wordcloud import WordCloud 11 | from PIL import Image 12 | import matplotlib.pyplot as plt 13 | import numpy as np 14 | import jieba 15 | 16 | # 1.读取文件 17 | file = open('xyj.txt', 'rb').read() 18 | 19 | # 2.中文分词 20 | text = ' '.join(jieba.cut(file)) 21 | print(text[:200]) 22 | 23 | # 3.生成对象及 mask 24 | mask = np.array(Image.open('black_mask.png')) 25 | wc = WordCloud(mask=mask, font_path='Hiragino.ttf', mode='RGBA', background_color=None).generate(text) 26 | 27 | # 4.显示词云 28 | plt.imshow(wc, interpolation='bilinear') 29 | plt.axis('off') 30 | plt.show() 31 | 32 | # 5.保存到文件 33 | wc.to_file('Wordcloud_chinese3.png') 34 | -------------------------------------------------------------------------------- /DL/1.词云/Wordcloud_2_chinese4.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019/2/15 23:02 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Wordcloud_2_chinese4.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | from wordcloud import WordCloud, ImageColorGenerator 10 | from PIL import Image 11 | import numpy as np 12 | import matplotlib.pyplot as plt 13 | import jieba 14 | 15 | # 1.读取文件 16 | file = open('xyj.txt', 'rb').read() 17 | 18 | # 2.中文分词 19 | text = ' '.join(jieba.cut(file)) 20 | print(text[:300]) 21 | 22 | # 3.生成 mask 及词云对象 23 | # mask = np.array(Image.open('color_mask.png'), dtype=np.float) 24 | mask = np.array(Image.open('color_mask.png')) 25 | wc = WordCloud(mask=mask, font_path='Hiragino.ttf', width=800, height=600, mode='RGBA', background_color=None).generate(text) 26 | 27 | color = ImageColorGenerator(mask) 28 | wc.recolor(color_func=color) 29 | # 4.显示词云 30 | plt.imshow(wc, interpolation='bilinear') 31 | plt.axis('off') 32 | plt.show() 33 | # 5.保存图像 34 | wc.to_file('Wordcloud_chinese4.png') 35 | -------------------------------------------------------------------------------- /DL/1.词云/Wordcloud_2_chinese5.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019/2/15 23:20 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Wordcloud_2_chinese5.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | from wordcloud import WordCloud, ImageColorGenerator 10 | from PIL import Image 11 | import numpy as np 12 | import matplotlib.pyplot as plt 13 | import jieba 14 | import random 15 | 16 | # 1.读取文件 17 | file = open('xyj.txt', 'rb').read() 18 | 19 | # 2.中文分词 20 | text = ' '.join(jieba.cut(file)) 21 | print(text[:300]) 22 | 23 | 24 | # 颜色函数 25 | def random_color(word, font_size, position, orientation, font_path, random_state): 26 | s = 'hsl(0, %d%%, %d%%)' % (random.randint(60, 80), random.randint(60, 80)) 27 | print(s) 28 | return s 29 | # 3.生成 mask 及词云对象 30 | mask = np.array(Image.open('color_mask.png')) 31 | wc = WordCloud(color_func=random_color, mask=mask, font_path='Hiragino.ttf', width=1000, height=800, mode='RGBA', background_color=None).generate(text) 32 | 33 | # 4.显示词云 34 | plt.imshow(wc, interpolation='bilinear') 35 | plt.axis('off') 36 | plt.show() 37 | # 5.保存图像 38 | wc.to_file('Wordcloud_chinese5.png') 39 | -------------------------------------------------------------------------------- /DL/1.词云/Wordcloud_chinese3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/1.词云/Wordcloud_chinese3.png -------------------------------------------------------------------------------- /DL/1.词云/Wordcloud_chinese4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/1.词云/Wordcloud_chinese4.png -------------------------------------------------------------------------------- /DL/1.词云/Wordcloud_chinese5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/1.词云/Wordcloud_chinese5.png -------------------------------------------------------------------------------- /DL/1.词云/black_mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/1.词云/black_mask.png -------------------------------------------------------------------------------- /DL/1.词云/color_mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/1.词云/color_mask.png -------------------------------------------------------------------------------- /DL/1.词云/constitution.txt: -------------------------------------------------------------------------------- 1 | We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America. 2 | 3 | Article. I. 4 | 5 | Section. 1. 6 | 7 | All legislative Powers herein granted shall be vested in a Congress of the United States, which shall consist of a Senate and House of Representatives. 8 | 9 | Section. 2. 10 | 11 | The House of Representatives shall be composed of Members chosen every second Year by the People of the several States, and the Electors in each State shall have the Qualifications requisite for Electors of the most numerous Branch of the State Legislature. 12 | 13 | No Person shall be a Representative who shall not have attained to the Age of twenty five Years, and been seven Years a Citizen of the United States, and who shall not, when elected, be an Inhabitant of that State in which he shall be chosen. 14 | 15 | Representatives and direct Taxes shall be apportioned among the several States which may be included within this Union, according to their respective Numbers, which shall be determined by adding to the whole Number of free Persons, including those bound to Service for a Term of Years, and excluding Indians not taxed, three fifths of all other Persons. The actual Enumeration shall be made within three Years after the first Meeting of the Congress of the United States, and within every subsequent Term of ten Years, in such Manner as they shall by Law direct. The Number of Representatives shall not exceed one for every thirty Thousand, but each State shall have at Least one Representative; and until such enumeration shall be made, the State of New Hampshire shall be entitled to chuse three, Massachusetts eight, Rhode-Island and Providence Plantations one, Connecticut five, New-York six, New Jersey four, Pennsylvania eight, Delaware one, Maryland six, Virginia ten, North Carolina five, South Carolina five, and Georgia three. 16 | 17 | When vacancies happen in the Representation from any State, the Executive Authority thereof shall issue Writs of Election to fill such Vacancies. 18 | 19 | The House of Representatives shall chuse their Speaker and other Officers; and shall have the sole Power of Impeachment. 20 | 21 | Section. 3. 22 | 23 | The Senate of the United States shall be composed of two Senators from each State, chosen by the Legislature thereof for six Years; and each Senator shall have one Vote. 24 | 25 | Immediately after they shall be assembled in Consequence of the first Election, they shall be divided as equally as may be into three Classes. The Seats of the Senators of the first Class shall be vacated at the Expiration of the second Year, of the second Class at the Expiration of the fourth Year, and of the third Class at the Expiration of the sixth Year, so that one third may be chosen every second Year; and if Vacancies happen by Resignation, or otherwise, during the Recess of the Legislature of any State, the Executive thereof may make temporary Appointments until the next Meeting of the Legislature, which shall then fill such Vacancies. 26 | 27 | No Person shall be a Senator who shall not have attained to the Age of thirty Years, and been nine Years a Citizen of the United States, and who shall not, when elected, be an Inhabitant of that State for which he shall be chosen. 28 | 29 | The Vice President of the United States shall be President of the Senate, but shall have no Vote, unless they be equally divided. 30 | 31 | The Senate shall chuse their other Officers, and also a President pro tempore, in the Absence of the Vice President, or when he shall exercise the Office of President of the United States. 32 | 33 | The Senate shall have the sole Power to try all Impeachments. When sitting for that Purpose, they shall be on Oath or Affirmation. When the President of the United States is tried, the Chief Justice shall preside: And no Person shall be convicted without the Concurrence of two thirds of the Members present. 34 | 35 | Judgment in Cases of Impeachment shall not extend further than to removal from Office, and disqualification to hold and enjoy any Office of honor, Trust or Profit under the United States: but the Party convicted shall nevertheless be liable and subject to Indictment, Trial, Judgment and Punishment, according to Law. 36 | 37 | Section. 4. 38 | 39 | The Times, Places and Manner of holding Elections for Senators and Representatives, shall be prescribed in each State by the Legislature thereof; but the Congress may at any time by Law make or alter such Regulations, except as to the Places of chusing Senators. 40 | 41 | The Congress shall assemble at least once in every Year, and such Meeting shall be on the first Monday in December, unless they shall by Law appoint a different Day. 42 | 43 | Section. 5. 44 | 45 | Each House shall be the Judge of the Elections, Returns and Qualifications of its own Members, and a Majority of each shall constitute a Quorum to do Business; but a smaller Number may adjourn from day to day, and may be authorized to compel the Attendance of absent Members, in such Manner, and under such Penalties as each House may provide. 46 | 47 | Each House may determine the Rules of its Proceedings, punish its Members for disorderly Behaviour, and, with the Concurrence of two thirds, expel a Member. 48 | 49 | Each House shall keep a Journal of its Proceedings, and from time to time publish the same, excepting such Parts as may in their Judgment require Secrecy; and the Yeas and Nays of the Members of either House on any question shall, at the Desire of one fifth of those Present, be entered on the Journal. 50 | 51 | Neither House, during the Session of Congress, shall, without the Consent of the other, adjourn for more than three days, nor to any other Place than that in which the two Houses shall be sitting. 52 | 53 | Section. 6. 54 | 55 | The Senators and Representatives shall receive a Compensation for their Services, to be ascertained by Law, and paid out of the Treasury of the United States. They shall in all Cases, except Treason, Felony and Breach of the Peace, be privileged from Arrest during their Attendance at the Session of their respective Houses, and in going to and returning from the same; and for any Speech or Debate in either House, they shall not be questioned in any other Place. 56 | 57 | No Senator or Representative shall, during the Time for which he was elected, be appointed to any civil Office under the Authority of the United States, which shall have been created, or the Emoluments whereof shall have been encreased during such time; and no Person holding any Office under the United States, shall be a Member of either House during his Continuance in Office. 58 | 59 | Section. 7. 60 | 61 | All Bills for raising Revenue shall originate in the House of Representatives; but the Senate may propose or concur with Amendments as on other Bills. 62 | 63 | Every Bill which shall have passed the House of Representatives and the Senate, shall, before it become a Law, be presented to the President of the United States: If he approve he shall sign it, but if not he shall return it, with his Objections to that House in which it shall have originated, who shall enter the Objections at large on their Journal, and proceed to reconsider it. If after such Reconsideration two thirds of that House shall agree to pass the Bill, it shall be sent, together with the Objections, to the other House, by which it shall likewise be reconsidered, and if approved by two thirds of that House, it shall become a Law. But in all such Cases the Votes of both Houses shall be determined by yeas and Nays, and the Names of the Persons voting for and against the Bill shall be entered on the Journal of each House respectively. If any Bill shall not be returned by the President within ten Days (Sundays excepted) after it shall have been presented to him, the Same shall be a Law, in like Manner as if he had signed it, unless the Congress by their Adjournment prevent its Return, in which Case it shall not be a Law. 64 | 65 | Every Order, Resolution, or Vote to which the Concurrence of the Senate and House of Representatives may be necessary (except on a question of Adjournment) shall be presented to the President of the United States; and before the Same shall take Effect, shall be approved by him, or being disapproved by him, shall be repassed by two thirds of the Senate and House of Representatives, according to the Rules and Limitations prescribed in the Case of a Bill. 66 | 67 | Section. 8. 68 | 69 | The Congress shall have Power To lay and collect Taxes, Duties, Imposts and Excises, to pay the Debts and provide for the common Defence and general Welfare of the United States; but all Duties, Imposts and Excises shall be uniform throughout the United States; 70 | 71 | To borrow Money on the credit of the United States; 72 | 73 | To regulate Commerce with foreign Nations, and among the several States, and with the Indian Tribes; 74 | 75 | To establish an uniform Rule of Naturalization, and uniform Laws on the subject of Bankruptcies throughout the United States; 76 | 77 | To coin Money, regulate the Value thereof, and of foreign Coin, and fix the Standard of Weights and Measures; 78 | 79 | To provide for the Punishment of counterfeiting the Securities and current Coin of the United States; 80 | 81 | To establish Post Offices and post Roads; 82 | 83 | To promote the Progress of Science and useful Arts, by securing for limited Times to Authors and Inventors the exclusive Right to their respective Writings and Discoveries; 84 | 85 | To constitute Tribunals inferior to the supreme Court; 86 | 87 | To define and punish Piracies and Felonies committed on the high Seas, and Offences against the Law of Nations; 88 | 89 | To declare War, grant Letters of Marque and Reprisal, and make Rules concerning Captures on Land and Water; 90 | 91 | To raise and support Armies, but no Appropriation of Money to that Use shall be for a longer Term than two Years; 92 | 93 | To provide and maintain a Navy; 94 | 95 | To make Rules for the Government and Regulation of the land and naval Forces; 96 | 97 | To provide for calling forth the Militia to execute the Laws of the Union, suppress Insurrections and repel Invasions; 98 | 99 | To provide for organizing, arming, and disciplining, the Militia, and for governing such Part of them as may be employed in the Service of the United States, reserving to the States respectively, the Appointment of the Officers, and the Authority of training the Militia according to the discipline prescribed by Congress; 100 | 101 | To exercise exclusive Legislation in all Cases whatsoever, over such District (not exceeding ten Miles square) as may, by Cession of particular States, and the Acceptance of Congress, become the Seat of the Government of the United States, and to exercise like Authority over all Places purchased by the Consent of the Legislature of the State in which the Same shall be, for the Erection of Forts, Magazines, Arsenals, dock-Yards, and other needful Buildings;--And 102 | 103 | To make all Laws which shall be necessary and proper for carrying into Execution the foregoing Powers, and all other Powers vested by this Constitution in the Government of the United States, or in any Department or Officer thereof. 104 | 105 | Section. 9. 106 | 107 | The Migration or Importation of such Persons as any of the States now existing shall think proper to admit, shall not be prohibited by the Congress prior to the Year one thousand eight hundred and eight, but a Tax or duty may be imposed on such Importation, not exceeding ten dollars for each Person. 108 | 109 | The Privilege of the Writ of Habeas Corpus shall not be suspended, unless when in Cases of Rebellion or Invasion the public Safety may require it. 110 | 111 | No Bill of Attainder or ex post facto Law shall be passed. 112 | 113 | No Capitation, or other direct, Tax shall be laid, unless in Proportion to the Census or enumeration herein before directed to be taken. 114 | 115 | No Tax or Duty shall be laid on Articles exported from any State. 116 | 117 | No Preference shall be given by any Regulation of Commerce or Revenue to the Ports of one State over those of another; nor shall Vessels bound to, or from, one State, be obliged to enter, clear, or pay Duties in another. 118 | 119 | No Money shall be drawn from the Treasury, but in Consequence of Appropriations made by Law; and a regular Statement and Account of the Receipts and Expenditures of all public Money shall be published from time to time. 120 | 121 | No Title of Nobility shall be granted by the United States: And no Person holding any Office of Profit or Trust under them, shall, without the Consent of the Congress, accept of any present, Emolument, Office, or Title, of any kind whatever, from any King, Prince, or foreign State. 122 | 123 | Section. 10. 124 | 125 | No State shall enter into any Treaty, Alliance, or Confederation; grant Letters of Marque and Reprisal; coin Money; emit Bills of Credit; make any Thing but gold and silver Coin a Tender in Payment of Debts; pass any Bill of Attainder, ex post facto Law, or Law impairing the Obligation of Contracts, or grant any Title of Nobility. 126 | 127 | No State shall, without the Consent of the Congress, lay any Imposts or Duties on Imports or Exports, except what may be absolutely necessary for executing it's inspection Laws: and the net Produce of all Duties and Imposts, laid by any State on Imports or Exports, shall be for the Use of the Treasury of the United States; and all such Laws shall be subject to the Revision and Controul of the Congress. 128 | 129 | No State shall, without the Consent of Congress, lay any Duty of Tonnage, keep Troops, or Ships of War in time of Peace, enter into any Agreement or Compact with another State, or with a foreign Power, or engage in War, unless actually invaded, or in such imminent Danger as will not admit of delay. 130 | 131 | Article. II. 132 | 133 | Section. 1. 134 | 135 | The executive Power shall be vested in a President of the United States of America. He shall hold his Office during the Term of four Years, and, together with the Vice President, chosen for the same Term, be elected, as follows: 136 | 137 | Each State shall appoint, in such Manner as the Legislature thereof may direct, a Number of Electors, equal to the whole Number of Senators and Representatives to which the State may be entitled in the Congress: but no Senator or Representative, or Person holding an Office of Trust or Profit under the United States, shall be appointed an Elector. 138 | 139 | The Electors shall meet in their respective States, and vote by Ballot for two Persons, of whom one at least shall not be an Inhabitant of the same State with themselves. And they shall make a List of all the Persons voted for, and of the Number of Votes for each; which List they shall sign and certify, and transmit sealed to the Seat of the Government of the United States, directed to the President of the Senate. The President of the Senate shall, in the Presence of the Senate and House of Representatives, open all the Certificates, and the Votes shall then be counted. The Person having the greatest Number of Votes shall be the President, if such Number be a Majority of the whole Number of Electors appointed; and if there be more than one who have such Majority, and have an equal Number of Votes, then the House of Representatives shall immediately chuse by Ballot one of them for President; and if no Person have a Majority, then from the five highest on the List the said House shall in like Manner chuse the President. But in chusing the President, the Votes shall be taken by States, the Representation from each State having one Vote; A quorum for this purpose shall consist of a Member or Members from two thirds of the States, and a Majority of all the States shall be necessary to a Choice. In every Case, after the Choice of the President, the Person having the greatest Number of Votes of the Electors shall be the Vice President. But if there should remain two or more who have equal Votes, the Senate shall chuse from them by Ballot the Vice President. 140 | 141 | The Congress may determine the Time of chusing the Electors, and the Day on which they shall give their Votes; which Day shall be the same throughout the United States. 142 | 143 | No Person except a natural born Citizen, or a Citizen of the United States, at the time of the Adoption of this Constitution, shall be eligible to the Office of President; neither shall any Person be eligible to that Office who shall not have attained to the Age of thirty five Years, and been fourteen Years a Resident within the United States. 144 | 145 | In Case of the Removal of the President from Office, or of his Death, Resignation, or Inability to discharge the Powers and Duties of the said Office, the Same shall devolve on the Vice President, and the Congress may by Law provide for the Case of Removal, Death, Resignation or Inability, both of the President and Vice President, declaring what Officer shall then act as President, and such Officer shall act accordingly, until the Disability be removed, or a President shall be elected. 146 | 147 | The President shall, at stated Times, receive for his Services, a Compensation, which shall neither be increased nor diminished during the Period for which he shall have been elected, and he shall not receive within that Period any other Emolument from the United States, or any of them. 148 | 149 | Before he enter on the Execution of his Office, he shall take the following Oath or Affirmation:--"I do solemnly swear (or affirm) that I will faithfully execute the Office of President of the United States, and will to the best of my Ability, preserve, protect and defend the Constitution of the United States." 150 | 151 | Section. 2. 152 | 153 | The President shall be Commander in Chief of the Army and Navy of the United States, and of the Militia of the several States, when called into the actual Service of the United States; he may require the Opinion, in writing, of the principal Officer in each of the executive Departments, upon any Subject relating to the Duties of their respective Offices, and he shall have Power to grant Reprieves and Pardons for Offences against the United States, except in Cases of Impeachment. 154 | 155 | He shall have Power, by and with the Advice and Consent of the Senate, to make Treaties, provided two thirds of the Senators present concur; and he shall nominate, and by and with the Advice and Consent of the Senate, shall appoint Ambassadors, other public Ministers and Consuls, Judges of the supreme Court, and all other Officers of the United States, whose Appointments are not herein otherwise provided for, and which shall be established by Law: but the Congress may by Law vest the Appointment of such inferior Officers, as they think proper, in the President alone, in the Courts of Law, or in the Heads of Departments. 156 | 157 | The President shall have Power to fill up all Vacancies that may happen during the Recess of the Senate, by granting Commissions which shall expire at the End of their next Session. 158 | 159 | Section. 3. 160 | 161 | He shall from time to time give to the Congress Information of the State of the Union, and recommend to their Consideration such Measures as he shall judge necessary and expedient; he may, on extraordinary Occasions, convene both Houses, or either of them, and in Case of Disagreement between them, with Respect to the Time of Adjournment, he may adjourn them to such Time as he shall think proper; he shall receive Ambassadors and other public Ministers; he shall take Care that the Laws be faithfully executed, and shall Commission all the Officers of the United States. 162 | 163 | Section. 4. 164 | 165 | The President, Vice President and all civil Officers of the United States, shall be removed from Office on Impeachment for, and Conviction of, Treason, Bribery, or other high Crimes and Misdemeanors. 166 | 167 | Article III. 168 | 169 | Section. 1. 170 | 171 | The judicial Power of the United States shall be vested in one supreme Court, and in such inferior Courts as the Congress may from time to time ordain and establish. The Judges, both of the supreme and inferior Courts, shall hold their Offices during good Behaviour, and shall, at stated Times, receive for their Services a Compensation, which shall not be diminished during their Continuance in Office. 172 | 173 | Section. 2. 174 | 175 | The judicial Power shall extend to all Cases, in Law and Equity, arising under this Constitution, the Laws of the United States, and Treaties made, or which shall be made, under their Authority;--to all Cases affecting Ambassadors, other public Ministers and Consuls;--to all Cases of admiralty and maritime Jurisdiction;--to Controversies to which the United States shall be a Party;--to Controversies between two or more States;-- between a State and Citizens of another State,--between Citizens of different States,--between Citizens of the same State claiming Lands under Grants of different States, and between a State, or the Citizens thereof, and foreign States, Citizens or Subjects. 176 | 177 | In all Cases affecting Ambassadors, other public Ministers and Consuls, and those in which a State shall be Party, the supreme Court shall have original Jurisdiction. In all the other Cases before mentioned, the supreme Court shall have appellate Jurisdiction, both as to Law and Fact, with such Exceptions, and under such Regulations as the Congress shall make. 178 | 179 | The Trial of all Crimes, except in Cases of Impeachment, shall be by Jury; and such Trial shall be held in the State where the said Crimes shall have been committed; but when not committed within any State, the Trial shall be at such Place or Places as the Congress may by Law have directed. 180 | 181 | Section. 3. 182 | 183 | Treason against the United States, shall consist only in levying War against them, or in adhering to their Enemies, giving them Aid and Comfort. No Person shall be convicted of Treason unless on the Testimony of two Witnesses to the same overt Act, or on Confession in open Court. 184 | 185 | The Congress shall have Power to declare the Punishment of Treason, but no Attainder of Treason shall work Corruption of Blood, or Forfeiture except during the Life of the Person attainted. 186 | 187 | Article. IV. 188 | 189 | Section. 1. 190 | 191 | Full Faith and Credit shall be given in each State to the public Acts, Records, and judicial Proceedings of every other State. And the Congress may by general Laws prescribe the Manner in which such Acts, Records and Proceedings shall be proved, and the Effect thereof. 192 | 193 | Section. 2. 194 | 195 | The Citizens of each State shall be entitled to all Privileges and Immunities of Citizens in the several States. 196 | 197 | A Person charged in any State with Treason, Felony, or other Crime, who shall flee from Justice, and be found in another State, shall on Demand of the executive Authority of the State from which he fled, be delivered up, to be removed to the State having Jurisdiction of the Crime. 198 | 199 | No Person held to Service or Labour in one State, under the Laws thereof, escaping into another, shall, in Consequence of any Law or Regulation therein, be discharged from such Service or Labour, but shall be delivered up on Claim of the Party to whom such Service or Labour may be due. 200 | 201 | Section. 3. 202 | 203 | New States may be admitted by the Congress into this Union; but no new State shall be formed or erected within the Jurisdiction of any other State; nor any State be formed by the Junction of two or more States, or Parts of States, without the Consent of the Legislatures of the States concerned as well as of the Congress. 204 | 205 | The Congress shall have Power to dispose of and make all needful Rules and Regulations respecting the Territory or other Property belonging to the United States; and nothing in this Constitution shall be so construed as to Prejudice any Claims of the United States, or of any particular State. 206 | 207 | Section. 4. 208 | 209 | The United States shall guarantee to every State in this Union a Republican Form of Government, and shall protect each of them against Invasion; and on Application of the Legislature, or of the Executive (when the Legislature cannot be convened), against domestic Violence. 210 | 211 | Article. V. 212 | 213 | The Congress, whenever two thirds of both Houses shall deem it necessary, shall propose Amendments to this Constitution, or, on the Application of the Legislatures of two thirds of the several States, shall call a Convention for proposing Amendments, which, in either Case, shall be valid to all Intents and Purposes, as Part of this Constitution, when ratified by the Legislatures of three fourths of the several States, or by Conventions in three fourths thereof, as the one or the other Mode of Ratification may be proposed by the Congress; Provided that no Amendment which may be made prior to the Year One thousand eight hundred and eight shall in any Manner affect the first and fourth Clauses in the Ninth Section of the first Article; and that no State, without its Consent, shall be deprived of its equal Suffrage in the Senate. 214 | 215 | Article. VI. 216 | 217 | All Debts contracted and Engagements entered into, before the Adoption of this Constitution, shall be as valid against the United States under this Constitution, as under the Confederation. 218 | 219 | This Constitution, and the Laws of the United States which shall be made in Pursuance thereof; and all Treaties made, or which shall be made, under the Authority of the United States, shall be the supreme Law of the Land; and the Judges in every State shall be bound thereby, any Thing in the Constitution or Laws of any State to the Contrary notwithstanding. 220 | 221 | The Senators and Representatives before mentioned, and the Members of the several State Legislatures, and all executive and judicial Officers, both of the United States and of the several States, shall be bound by Oath or Affirmation, to support this Constitution; but no religious Test shall ever be required as a Qualification to any Office or public Trust under the United States. 222 | 223 | Article. VII. 224 | 225 | The Ratification of the Conventions of nine States, shall be sufficient for the Establishment of this Constitution between the States so ratifying the Same. 226 | 227 | The Word, "the," being interlined between the seventh and eighth Lines of the first Page, the Word "Thirty" being partly written on an Erazure in the fifteenth Line of the first Page, The Words "is tried" being interlined between the thirty second and thirty third Lines of the first Page and the Word "the" being interlined between the forty third and forty fourth Lines of the second Page. 228 | 229 | Attest William Jackson Secretary 230 | 231 | done in Convention by the Unanimous Consent of the States present the Seventeenth Day of September in the Year of our Lord one thousand seven hundred and Eighty seven and of the Independance of the United States of America the Twelfth In witness whereof We have hereunto subscribed our Names, 232 | -------------------------------------------------------------------------------- /DL/1.词云/wordcloud.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/1.词云/wordcloud.png -------------------------------------------------------------------------------- /DL/2.图像风格迁移/Code_Keras.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019/2/21 20:23 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Code_Keras.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | from __future__ import print_function 10 | from keras.preprocessing.image import load_img, img_to_array 11 | from scipy.misc import imsave 12 | import numpy as np 13 | from scipy.optimize import fmin_l_bfgs_b 14 | import time 15 | import argparse 16 | 17 | from keras.applications import vgg19 18 | from keras import backend as K 19 | 20 | parse = argparse.ArgumentParser(description='Neural style transfer with Keras.') 21 | parse.add_argument('base_image_path', metavar='base', type=str, help='path to the image to transform.') 22 | parse.add_argument('style_reference_image_path', metavar='ref', type=str, help='path to the style reference image.') 23 | parse.add_argument('result_prefix', metavar='res_prefix', type=str, help='prefix for the saved results.') 24 | parse.add_argument('--iter', type=int, defult=10, required=False, help='Number of the iteration to run.') 25 | parse.add_argument('--content_weight', type=float, defult=0.025, required=False, help='content weight.') 26 | parse.add_argument('--style_weight', type=float, defult=1.0, required=False, help='style weght.') 27 | parse.add_argument('--tv_weight', type=float, defult=1.0, required=False, help='Total Variation weight.') 28 | 29 | args = parse.parse_args() 30 | base_image_path = args.base_image_path 31 | style_reference_image_path = args.style_reference_image_path 32 | result_prefix = args.result_prefix 33 | iterations = args.iter 34 | 35 | total_variation_weight = args.tv_weight 36 | style_weight = args.style_weight 37 | content_weight = args.content_weight 38 | 39 | width, height = load_img(base_image_path).size 40 | img_nrows = 400 41 | img_ncols = int(width * img_nrows / height) 42 | 43 | 44 | def preprocess_image(image_path): 45 | img = load_img(image_path, target_size=(img_nrows, img_ncols)) 46 | img = img_to_array(img) 47 | img = np.expand_dims(img, axis=0) 48 | img = vgg19.preprocess_input(img) 49 | return img 50 | 51 | 52 | def deprocess_img(x): 53 | if K.image_data_format() == 'channels_first': 54 | x = x.reshape((3, img_nrows, img_ncols)) 55 | x = x.transpose((1, 2, 0)) 56 | else: 57 | x = x.reshape((img_nrows, img_ncols, 3)) 58 | x[:, :, 0] += 103.939 59 | x[:, :, 1] += 116.779 60 | x[:, :, 2] += 123.68 61 | 62 | x = x[:, :, ::-1] 63 | x = np.clip(x, 0, 255).astype('uint8') 64 | return x 65 | 66 | base_image = K.variable(preprocess_image(base_image_path)) 67 | style_reference_image = K.variable(preprocess_image(style_reference_image_path)) 68 | 69 | if K.image_data_format() == 'channels_first': 70 | combination_image = K.placeholder((1, 3, img_nrows, img_ncols)) 71 | else: 72 | combination_image = K.placeholder((1, img_nrows, img_ncols, 3)) 73 | 74 | input_tensor = K.concatenate([base_image, style_reference_image, combination_image], axis=0) 75 | model = vgg19.VGG19(input_tensor=input_tensor, weight='imagenet', include_top=False) 76 | print('*********模型加载完成*********') 77 | 78 | outputs_dict = dict([layer.name, layer.output]) 79 | 80 | 81 | def gram_matrix(x): 82 | assert K.ndim(x) == 3 83 | if K.image_data_format() == 'channels_first': 84 | features = K.batch_flatten(x) 85 | else: 86 | features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1))) 87 | gram = K.dot(features, K.transpose(features)) 88 | return gram 89 | 90 | 91 | def style_loss(style, combination): 92 | assert K.ndim(style) == 3 93 | assert K.ndim(combination) == 3 94 | S = gram_matrix(style) 95 | C = gram_matrix(combination) 96 | channels = 3 97 | size = img_nrows * img_ncols 98 | return K.sum(K.square(S - C)) / (4.0 * (channels ** 2) * (size ** 2)) 99 | 100 | 101 | def content_loss(base, combination): 102 | return K.sum(K.square(combination - base)) 103 | 104 | 105 | def total_variation_loss(x): 106 | assert K.ndim(x) == 4 107 | if K.image_data_format() == 'channels_first': 108 | a = K.square(x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, 1:, :img_ncols - 1]) 109 | b = K.square(x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, :img_nrows - 1, 1:]) 110 | else: 111 | a = K.square(x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, 1:, :img_ncols - 1, :]) 112 | b = K.square(x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, :img_nrows - 1, 1:, :]) 113 | return K.sum(K.pow(a + b, 1.25)) 114 | 115 | loss = K.variable(0.) 116 | layer_features = outputs_dict['block5_conv2'] 117 | base_image_features = layer_features[0, :, :, :] 118 | combination_features = layer_features[2, :, :, :] 119 | loss += content_weight * content_loss(base_image_features, combination_features) 120 | 121 | feature_layers = ['block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1', 'block5_conv1'] 122 | 123 | for layer_name in feature_layers: 124 | layer_features = outputs_dict[layer_name] 125 | style_reference_features = layer_features[1, :, :, :] 126 | combination_features = layer_features[2, :, :, :] 127 | sl = style_loss(style_reference_features, combination_features) 128 | loss += (style_weight / len(feature_layers)) * sl 129 | loss += total_variation_weight * total_variation_loss(combination_image) 130 | 131 | grads = K.gradients(loss, combination_image) 132 | outputs = [loss] 133 | 134 | if isinstance(grads, (list, tuple)): 135 | outputs += grads 136 | else: 137 | outputs.append(grads) 138 | 139 | f_outputs = K.function([combination_image], outputs) 140 | 141 | 142 | def eval_loss_and_grads(x): 143 | if K.image_data_format() == 'channels_first': 144 | x = x.reshape((1, 3, img_nrows, img_ncols)) 145 | else: 146 | x = x.reshape((1, img_nrows, img_ncols, 3)) 147 | outs = f_outputs([x]) 148 | loss_values = outs[0] 149 | if len(outs[1:]) == 1: 150 | grad_values = outs[1].flatten().astype('float64') 151 | else: 152 | grad_values = np.array(outs[1:].flatten().astype('float64')) 153 | return loss_values, grad_values 154 | 155 | 156 | class Evalautor(object): 157 | def __init__(self): 158 | self.loss_value = None 159 | self.grads_values = None 160 | 161 | def loss(self, x): 162 | assert self.loss_value is None 163 | loss_value, grad_values = eval_loss_and_grads(x) 164 | self.loss_value = loss_value 165 | self.grad_values = grad_values 166 | return self.loss_value 167 | 168 | def grads(self, x): 169 | assert self.loss_value is not None 170 | grad_values = np.copy(self.grad_values) 171 | self.loss_value = None 172 | self.grad_values = None 173 | return grad_values 174 | 175 | evaluator = Evalautor() 176 | 177 | x = preprocess_image(base_image_path) 178 | 179 | for i in range(iterations): 180 | print('********* 开始迭代 *********') 181 | start = time.time() 182 | x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(), fprime=evaluator.grads, maxfun=20) 183 | print('当前的误差为:', min_val) 184 | img = deprocess_img(x.copy()) 185 | fname = result_prefix + '_at_iteration_%d.png' % i 186 | imsave(fname, img) 187 | end = time.time() 188 | print('图片保存为:', fname) 189 | print('第 %d 迭代次完成,用时为 %d s', (i, end-start)) 190 | -------------------------------------------------------------------------------- /DL/2.图像风格迁移/Code_Tensorflow.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019/2/21 18:03 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Code_Tensorflow.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import tensorflow as tf 10 | import numpy as np 11 | import scipy.io 12 | import scipy.misc 13 | import os 14 | import time 15 | 16 | 17 | def the_current_time(): 18 | print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(time.time())))) 19 | 20 | 21 | CONTENT_IMG = 'flower.jpg' 22 | STYLE_IMG = 'style5.jpg' 23 | OUTPUT_DIR = 'Tensorflow_Picture2/' 24 | 25 | # 如果没有相应的文件夹就自动创建文件夹 26 | if not os.path.exists(OUTPUT_DIR): 27 | os.mkdir(OUTPUT_DIR) 28 | 29 | IMAGE_W = 270 30 | IMAGE_H = 480 31 | COLOR_C = 3 32 | 33 | NOSIE_RATIO = 0.7 34 | BETE = 5 35 | ALPHA = 100 36 | VGG_MODEL = 'imagenet-vgg-verydeep-19.mat' 37 | MEAN_VALUES = np.array([123.68, 116.779, 103.939]).reshape((1, 1, 1, 3)) 38 | 39 | 40 | def load_vgg_model(path): 41 | vgg = scipy.io.loadmat(path) 42 | vgg_layers = vgg['layers'] 43 | 44 | def _weights(layer, expected_layer_name): 45 | W = vgg_layers[0][layer][0][0][2][0][0] 46 | b = vgg_layers[0][layer][0][0][2][0][1] 47 | layer_name = vgg_layers[0][layer][0][0][0][0] 48 | assert layer_name == expected_layer_name 49 | return W, b 50 | 51 | def _conv2d_relu(prev_layer, layer, layer_name): 52 | W, b = _weights(layer, layer_name) 53 | W = tf.constant(W) 54 | b = tf.constant(np.reshape(b, (b.size))) 55 | return tf.nn.relu(tf.nn.conv2d(prev_layer, filter=W, strides=[1, 1, 1, 1], padding='SAME') + b) 56 | 57 | def _avgpool(prev_layer): 58 | return tf.nn.avg_pool(prev_layer, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 59 | 60 | graph = {} 61 | graph['input'] = tf.Variable(np.zeros((1, IMAGE_H, IMAGE_W, COLOR_C)), dtype='float32') 62 | graph['conv1_1'] = _conv2d_relu(graph['input'], 0, 'conv1_1') 63 | graph['conv1_2'] = _conv2d_relu(graph['conv1_1'], 2, 'conv1_2') 64 | graph['avgpool1'] = _avgpool(graph['conv1_2']) 65 | graph['conv2_1'] = _conv2d_relu(graph['avgpool1'], 5, 'conv2_1') 66 | graph['conv2_2'] = _conv2d_relu(graph['conv2_1'], 7, 'conv2_2') 67 | graph['avgpool2'] = _avgpool(graph['conv2_2']) 68 | graph['conv3_1'] = _conv2d_relu(graph['avgpool2'], 10, 'conv3_1') 69 | graph['conv3_2'] = _conv2d_relu(graph['conv3_1'], 12, 'conv3_2') 70 | graph['conv3_3'] = _conv2d_relu(graph['conv3_2'], 14, 'conv3_3') 71 | graph['conv3_4'] = _conv2d_relu(graph['conv3_3'], 16, 'conv3_4') 72 | graph['avgpool3'] = _avgpool(graph['conv3_4']) 73 | graph['conv4_1'] = _conv2d_relu(graph['avgpool3'], 19, 'conv4_1') 74 | graph['conv4_2'] = _conv2d_relu(graph['conv4_1'], 21, 'conv4_2') 75 | graph['conv4_3'] = _conv2d_relu(graph['conv4_2'], 23, 'conv4_3') 76 | graph['conv4_4'] = _conv2d_relu(graph['conv4_3'], 25, 'conv4_4') 77 | graph['avgpool4'] = _avgpool(graph['conv4_4']) 78 | graph['conv5_1'] = _conv2d_relu(graph['avgpool4'], 28, 'conv5_1') 79 | graph['conv5_2'] = _conv2d_relu(graph['conv5_1'], 30, 'conv5_2') 80 | graph['conv5_3'] = _conv2d_relu(graph['conv5_2'], 32, 'conv5_3') 81 | graph['conv5_4'] = _conv2d_relu(graph['conv5_3'], 34, 'conv5_4') 82 | graph['avgpool5'] = _avgpool(graph['conv5_4']) 83 | return graph 84 | 85 | 86 | def content_loss_func(sess, model): 87 | def _content_loss(p, x): 88 | N = p.shape[3] 89 | M = p.shape[1] * p.shape[2] 90 | return (1/(4 * N * M)) * tf.reduce_sum(tf.pow(x-p, 2)) 91 | return _content_loss(sess.run(model['conv4_2']), model['conv4_2']) 92 | 93 | STYLE_LAYERS = [('conv1_1', 0.5), ('conv2_1', 1.0), ('conv3_1', 1.5), ('conv4_1', 3.0), ('conv5_1', 4.0)] 94 | 95 | 96 | def style_loss_func(sess, model): 97 | def _gram_matrix(F, N, M): 98 | Ft = tf.reshape(F, (M, N)) 99 | return tf.matmul(tf.transpose(Ft), Ft) 100 | 101 | def _style_loss(a, x): 102 | N = a.shape[3] 103 | M = a.shape[1] * a.shape[2] 104 | A = _gram_matrix(a, N, M) 105 | G = _gram_matrix(x, N, M) 106 | return (1 / (4 * N ** 2 * M ** 2)) * tf.reduce_sum(tf.pow(G - A, 2)) 107 | return sum([_style_loss(sess.run(model[layer_name]), model[layer_name]) * w for layer_name, w in STYLE_LAYERS]) 108 | 109 | 110 | def generate_noise_image(content_image, noise_ratio=NOSIE_RATIO): 111 | noise_image = np.random.uniform(-20, 20, (1, IMAGE_H, IMAGE_W, COLOR_C)).astype('float32') 112 | input_image = noise_image * noise_ratio + content_image * (1 - noise_ratio) 113 | return input_image 114 | 115 | 116 | def load_image(path): 117 | image = scipy.misc.imread(path) 118 | image = scipy.misc.imresize(image, (IMAGE_H, IMAGE_W)) 119 | image = np.reshape(image, ((1, ) + image.shape)) 120 | image = image - MEAN_VALUES 121 | return image 122 | 123 | 124 | def save_image(path, image): 125 | image = image + MEAN_VALUES 126 | image = image[0] 127 | image = np.clip(image, 0, 255).astype('uint8') 128 | scipy.misc.imsave(path, image) 129 | 130 | the_current_time() 131 | 132 | 133 | with tf.Session() as sess: 134 | content_image = load_image(CONTENT_IMG) 135 | style_image = load_image(STYLE_IMG) 136 | model = load_vgg_model(VGG_MODEL) 137 | 138 | input_image = generate_noise_image(content_image) 139 | sess.run(tf.global_variables_initializer()) 140 | 141 | sess.run(model['input'].assign(content_image)) 142 | content_loss = content_loss_func(sess, model) 143 | 144 | sess.run(model['input'].assign(style_image)) 145 | style_loss = style_loss_func(sess, model) 146 | 147 | total_loss = BETE * content_loss + ALPHA * style_loss 148 | optimizer = tf.train.AdamOptimizer(2.0) 149 | train = optimizer.minimize(total_loss) 150 | 151 | sess.run(tf.global_variables_initializer()) 152 | sess.run(model['input'].assign(input_image)) 153 | 154 | ITERATIONS = 2000 155 | for i in range(ITERATIONS): 156 | sess.run(train) 157 | if i % 100 == 0: 158 | output_image = sess.run(model['input']) 159 | the_current_time() 160 | print('Iteration %d' % i) 161 | print('Cost: ', sess.run(total_loss)) 162 | 163 | save_image(os.path.join(OUTPUT_DIR, 'output_%d.jpg' % i), output_image) 164 | -------------------------------------------------------------------------------- /DL/2.图像风格迁移/content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/2.图像风格迁移/content.jpg -------------------------------------------------------------------------------- /DL/2.图像风格迁移/flower.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/2.图像风格迁移/flower.jpg -------------------------------------------------------------------------------- /DL/2.图像风格迁移/style1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/2.图像风格迁移/style1.jpg -------------------------------------------------------------------------------- /DL/2.图像风格迁移/style2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/2.图像风格迁移/style2.jpg -------------------------------------------------------------------------------- /DL/2.图像风格迁移/style3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/2.图像风格迁移/style3.jpg -------------------------------------------------------------------------------- /DL/2.图像风格迁移/style4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/2.图像风格迁移/style4.jpg -------------------------------------------------------------------------------- /DL/2.图像风格迁移/style5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/DL/2.图像风格迁移/style5.jpg -------------------------------------------------------------------------------- /DL/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1234 5 | -------------------------------------------------------------------------------- /ML/1-Logistic Regression/test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-8 19:30 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Test_main.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import numpy as np 10 | 11 | 12 | def load_weights(filename): 13 | weights_pre = open(filename) 14 | w = [] 15 | for line in weights_pre: 16 | lines = line.strip().split('\t') 17 | w_temp = [] 18 | for x in lines: 19 | w_temp.append(float(x)) 20 | w.append(w_temp) 21 | weights_pre.close() 22 | return np.mat(w) 23 | 24 | 25 | def load_data(filename, n): 26 | data = open(filename) 27 | data_test = [] 28 | for line in data.readlines(): 29 | feature_tmp = [] 30 | lines = line.strip().split('\t') 31 | if len(lines) != n-1: 32 | continue 33 | feature_tmp.append(1) 34 | for x in lines: 35 | feature_tmp.append(float(x)) 36 | data_test.append(feature_tmp) 37 | data.close() 38 | return np.mat(data_test) 39 | 40 | 41 | # 定义 Sigmoid 函数 42 | def Sigmoid(matrix): 43 | return 1.0 / (1+np.exp(-matrix)) 44 | 45 | 46 | def predict(data, w): 47 | h = Sigmoid(data * w.T) # 获取预测值 48 | m = np.shape(h)[0] 49 | for i in range(m): 50 | if h[i, 0] < 0.5: 51 | h[i, 0] = 0.0 52 | else: 53 | h[i, 0] = 1.0 54 | return h 55 | 56 | 57 | def save_test(filename, result): 58 | m = np.shape(result)[0] 59 | tmp = [] 60 | for i in range(m): 61 | tmp.append(str(result[i, 0])) 62 | f_result = open(filename, 'w') 63 | f_result.write('\t'.join(tmp)) # tmp list 中每个元素用空格隔开,在写入文件时。 64 | f_result.close() 65 | 66 | 67 | def main(): 68 | # 1、导入模型 69 | print('***************** 导入模型 *****************') 70 | w = load_weights('weights') 71 | n = np.shape(w)[1] 72 | # 2、导入测试数据 73 | print('***************** 导入数据 *****************') 74 | data_test = load_data('./test_data.txt', n) 75 | # 3、对测试数据进行预测 76 | print('***************** 测试数据 *****************') 77 | h = predict(data_test, w) 78 | # 4、保存最终预测结果 79 | print('***************** 保存模型 *****************') 80 | save_test('test_model', h) 81 | 82 | 83 | if __name__ == '__main__': 84 | main() 85 | dataset = load_data('test_data.txt', 3) 86 | print(dataset.shape) 87 | -------------------------------------------------------------------------------- /ML/1-Logistic Regression/test_data.txt: -------------------------------------------------------------------------------- 1 | 7.33251317753861 9.84290929650398 2 | 1.14288134664155 9.31938382343869 3 | 5.69123321602869 7.01397166818584 4 | 2.50648396980344 7.05766788137925 5 | 8.61756151890868 9.98657202473272 6 | 1.41851773509793 9.77704969645800 7 | 8.61450253418651 9.80161361673675 8 | 7.20252421999920 8.45756147719461 9 | 3.79585154363648 9.56147516348640 10 | 7.12986596603599 9.92424540796407 11 | 5.90166629240928 7.01231298989034 12 | 7.64216375281899 9.91037363924651 13 | 6.10861639371996 9.29953378509465 14 | 6.68819221312425 8.59494569110640 15 | 5.89930101159801 7.43009940132321 16 | 6.35441479217648 7.43863129967550 17 | 2.49230686464801 3.79277610650803 18 | 0.874186031122628 8.56545115532597 19 | 6.25345760678235 8.12438477163678 20 | 8.55199843954519 9.56743033736205 21 | 3.94869923690758 6.87606576238586 22 | 6.88965109334102 9.56780033528095 23 | 1.68185344098941 6.26602106875295 24 | 4.01027580639810 8.23519946958995 25 | 6.38428347772265 9.35832990092658 26 | 2.48422569298721 7.91301493123820 27 | 5.89588203576457 7.40064804417771 28 | 1.07097913402539 6.02251810104346 29 | 8.63769562664473 9.76101886404358 30 | 5.26740975881800 7.10280802002263 31 | 6.76140353375088 8.33245855777541 32 | 4.55361346498628 8.66197879154849 33 | 8.01812927282219 9.96002944206410 34 | 4.92493976967423 6.48984272359645 35 | 1.34364605003152 4.31522038864128 36 | 7.56645530385296 8.93098017284231 37 | 7.32856343461935 8.73559997192947 38 | 8.36337260868505 9.58618186062654 39 | 1.76935725388087 4.58485493022287 40 | 5.54440208531975 8.17989804462944 41 | 3.16493556356697 9.01287414297507 42 | 5.26737682037452 8.31928790306938 43 | 8.25474297446829 9.46776651141527 44 | 6.81480206199649 9.46184932462711 45 | 3.42401262277821 7.59017892397541 46 | 0.682688606067573 2.13140854275735 47 | 4.77717797708075 9.06746251589252 48 | 8.40609615806265 9.48324795436671 49 | 5.11941294784973 7.94092419194072 50 | 0.107118625511173 4.10511031080441 51 | 1.45964077373918 8.44883153836142 52 | 2.80093537840324 7.07734644006141 53 | 1.49083856549803 7.01121814413782 54 | 2.36674156086130 7.70541726069665 55 | 6.20293052826007 9.29556250878087 56 | 4.05487438652248 5.46938162981209 57 | 2.06079271845137 9.39862998789417 58 | 1.37140217072301 8.67122777257986 59 | 4.84508191734051 9.98394006421844 60 | 0.703579758778653 5.37622471649251 61 | 0.959874931625260 9.69365580472953 62 | 0.0417080172066070 7.98358222061436 63 | 7.35572898588090 9.78409851002885 64 | 0.759922609598193 5.05416257751295 65 | 2.33883362565589 8.66822288329864 66 | 3.88272444717190 9.54275911938782 67 | 1.63662325472567 4.57910351557924 68 | 1.30985082346245 3.35623833816854 69 | 7.82362986876080 9.50557703028000 70 | 4.94874181652699 6.53599112906454 71 | 7.67728005949704 9.50008478600453 72 | 3.15857142803044 7.15668195476007 73 | 3.61627230376748 5.02525628581462 74 | 2.15924538198292 4.00283995494553 75 | 1.65517009454175 4.41758058093557 76 | 3.75540362175933 5.01582106720932 77 | 8.12444498923753 9.95165814730251 78 | 4.41777683221272 7.65964160679034 79 | 3.03947468839239 9.40426842177465 80 | 3.32322103008194 4.95449449273065 81 | 7.02226861489024 8.79306734474469 82 | 2.17522157322449 5.93183247074687 83 | 0.868090726515497 2.94128556851324 84 | 8.47845531697937 9.97712220268860 85 | 5.17687735570619 6.40542188001300 86 | 2.11301922035166 5.54521551252613 87 | 7.39074636178163 8.41553439986347 88 | 0.387214214920271 2.84268913849686 89 | 5.84203927460807 9.15278983040824 90 | 5.82971366822676 8.25927093139731 91 | 4.92308003057711 7.13115624031492 92 | 6.70223526366741 8.13640943396238 93 | 6.18097890028784 7.69830072034376 94 | 3.31636136841303 7.87215118881414 95 | 7.02204691636239 8.18250988105294 96 | 8.36447373871857 9.85745951718317 97 | 4.38112469162855 7.39430116436659 98 | 4.02105374486826 6.54635130132668 99 | 4.57657789843014 7.83593612424246 100 | 7.35864937490036 9.66324641785085 101 | 6.79886317174323 2.19550400558507 102 | 8.30422412454229 3.89187751988243 103 | 4.15654393219195 2.96399968284951 104 | 8.88348530343686 4.33714944383228 105 | 6.60227577401105 3.28878632645783 106 | 2.86968063459726 0.563234429967053 107 | 5.23831013665832 0.976880305804374 108 | 8.59877913425850 1.47997081966077 109 | 3.03329602875159 0.347099994341680 110 | 3.04897868034898 0.892737314784995 111 | 3.79992057985372 2.58538966294283 112 | 4.87186652196626 0.715584122601641 113 | 9.14392871811904 7.97900095502468 114 | 4.94982975813493 0.438902015446521 115 | 3.32258226320860 0.949285465202363 116 | 6.35406466607753 1.40389865382386 117 | 6.42558780443875 3.85876366464538 118 | 2.99572060615516 0.234332825339264 119 | 3.67008285896494 0.851164479782249 120 | 4.81750083742427 1.93874942698667 121 | 1.76964217381040 0.202017397699835 122 | 8.20913160492765 0.210652826478027 123 | 9.35968725530240 6.10533760636674 124 | 5.39748076423221 2.54405282747684 125 | 3.13555221794369 0.979895632720517 126 | 9.66779685358222 4.73960088840639 127 | 5.69022247723601 1.08622919814201 128 | 5.40007969528150 2.74591412260864 129 | 7.11221986779173 2.41747595922350 130 | 4.30692983690029 3.26718716457572 131 | 1.33964979615597 0.300647133549756 132 | 9.21958144875315 6.54429819711743 133 | 1.88841050790017 0.232649111467001 134 | 4.01821155966517 2.05156276027461 135 | 2.22897823619833 0.886372899104719 136 | 1.96085675446517 0.628167164249429 137 | 5.44756542975343 3.46488351223326 138 | 7.43533370560625 5.81574338379743 139 | 9.01830253897710 2.67942045419741 140 | 7.28871249101315 1.24396912792495 141 | 1.27486851674173 0.204522588292904 142 | 5.50020192031181 2.15974654118566 143 | 9.14250014260627 4.96583927175149 144 | 6.55899750629609 4.77763763389266 145 | 8.24940482076717 4.18088773553725 146 | 2.64630222473423 0.395000602784235 147 | 8.97860739768491 0.228779804972462 148 | 5.40911249661002 0.740409676547689 149 | 9.80812584677043 6.27750259684544 150 | 5.50424461739359 2.12189727534722 151 | 1.53656980821675 0.365925533818477 152 | 1.38188023750668 0.0272836109904681 153 | 5.69484858217855 0.454132824391398 154 | 8.36333698473662 6.01987473987129 155 | 7.50195633130158 0.974418562562929 156 | 6.93644727617476 3.07861153390469 157 | 9.75677099287476 5.68306987800605 158 | 8.20297517817161 3.26869363187115 159 | 4.89152353405116 3.21172805778414 160 | 1.75122833373023 0.100041834145903 161 | 2.56049751807105 0.610057470246340 162 | 8.48241768555163 6.01110793166852 163 | 1.54424061252904 0.217292293635713 164 | 5.74188247457466 1.97641409239304 165 | 6.91173901876337 3.71241461026804 166 | 3.62785671965543 1.13431742828523 167 | 1.13938413072417 0.137162866799778 168 | 2.50451568923190 0.159804157398042 169 | 4.35168766049983 0.664031005121227 170 | 5.40718874214422 1.49621154952786 171 | 9.56467418299954 7.88234406137552 172 | 1.47409297912713 0.349813342676726 173 | 3.42207483758700 1.02413950354846 174 | 5.93083811093360 4.64848345065932 175 | 4.75969693884996 3.69597934891562 176 | 3.71309453840859 1.90214720551986 177 | 6.99704966425983 3.23316818617885 178 | 7.28294968162278 4.18776134130548 179 | 2.60319208960304 0.205231672986662 180 | 9.99172355285225 1.53867332274632 181 | 1.29340738477475 0.164660163515074 182 | 8.93679850406629 5.31110955598668 183 | 2.71389940461959 0.632285848653224 184 | 5.14653343534370 4.07039458510250 185 | 2.40764457003907 1.20427203219359 186 | 6.80288083183079 2.18346279657764 187 | 2.71831325712673 0.735872795240678 188 | 5.33819854928671 0.523237125832879 189 | 6.30556736225553 1.20005397144010 190 | 4.46157211932470 2.01804940846530 191 | 3.26625510225082 0.658212637318821 192 | 6.55381795953901 1.47332188122579 193 | 8.41938640019952 7.29075946387086 194 | 7.57223913040838 2.26004190249210 195 | 6.25662399950607 0.566501191933395 196 | 9.15677335584760 7.17513606222610 197 | 8.35984503433578 1.91891766916066 198 | 6.34920625597898 0.120424501924355 199 | 4.82733388192721 1.19687959104710 200 | 2.45336269880575 0.259812107633635 -------------------------------------------------------------------------------- /ML/1-Logistic Regression/train.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-8 18:27 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Logsitic_Regression.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import numpy as np 10 | 11 | 12 | # 定义 Sigmoid 函数 13 | def Sigmoid(x): 14 | return 1.0 / (1 + np.exp(-x)) 15 | 16 | 17 | # 定义损失函数 18 | def error_rate(h, label): 19 | '''计算当前的损失函数值 20 | input: h(mat):预测值 21 | label(mat):实际值 22 | output: err/m(float):错误率 23 | ''' 24 | m = np.shape(h)[0] 25 | 26 | sum_err = 0.0 27 | for i in range(m): 28 | if h[i, 0] > 0 and (1 - h[i, 0]) > 0: 29 | sum_err -= (label[i, 0] * np.log(h[i, 0]) + (1 - label[i, 0]) * np.log(1 - h[i, 0])) 30 | else: 31 | sum_err -= 0 32 | return sum_err / m 33 | 34 | 35 | # 定义 BGD 梯度下降法 36 | def lr_train_bgd(feature, label, maxCycle, alpha): 37 | n = np.shape(feature)[1] # 特征的个数 38 | w = np.mat(np.ones((n, 1))) # 初始化权重 39 | i = 0 40 | while i <= maxCycle: 41 | i += 1 42 | h = Sigmoid(feature * w) 43 | error = label - h 44 | if i % 100 == 0: 45 | print('\t 迭代次数为=:' + str(i) + ',训练误差率为:' + str(error_rate(h, label))) 46 | w = w + alpha * feature.T * error 47 | return w 48 | 49 | 50 | def load_data(file_name): 51 | '''导入训练数据 52 | input: file_name(string)训练数据的位置 53 | output: feature_data(mat)特征 54 | label_data(mat)标签 55 | ''' 56 | f = open(file_name) # 打开文件 57 | feature_data = [] 58 | label_data = [] 59 | for line in f.readlines(): 60 | feature_tmp = [] 61 | lable_tmp = [] 62 | lines = line.strip().split("\t") 63 | feature_tmp.append(1) # 偏置项 64 | for i in range(len(lines) - 1): 65 | feature_tmp.append(float(lines[i])) 66 | lable_tmp.append(float(lines[-1])) 67 | 68 | feature_data.append(feature_tmp) 69 | label_data.append(lable_tmp) 70 | f.close() # 关闭文件 71 | return np.mat(feature_data), np.mat(label_data) 72 | 73 | 74 | # 保持最终的模型 75 | def save_model(filename, w): 76 | m = np.shape(w)[0] 77 | f_w = open(filename, 'w') 78 | w_array = [] 79 | for i in range(m): 80 | w_array.append(str(w[i, 0])) 81 | f_w.write('\t'.join(w_array)) 82 | f_w.close() 83 | 84 | 85 | def main(): 86 | print('***************** 导入模型 *****************') 87 | features, labels = load_data('data.txt') 88 | print('***************** 训练模型 *****************') 89 | w = lr_train_bgd(features, labels, 1000, 0.01) 90 | print('***************** 保存模型 *****************') 91 | save_model('weights', w) 92 | 93 | 94 | if __name__ == '__main__': 95 | main() 96 | 97 | -------------------------------------------------------------------------------- /ML/1-Logistic Regression/train_data.txt: -------------------------------------------------------------------------------- 1 | 4.45925637575900 8.22541838354701 0 2 | 0.0432761720122110 6.30740040001402 0 3 | 6.99716180262699 9.31339338579386 0 4 | 4.75483224215432 9.26037784240288 0 5 | 8.66190392439652 9.76797698918454 0 6 | 7.17376551727269 8.69456339325210 0 7 | 0.134053879775005 1.96878052943837 0 8 | 2.95850718791494 5.80458392655308 0 9 | 0.162199197495798 2.59575596457315 0 10 | 3.99590517062991 8.83289511075255 0 11 | 6.13130341636350 9.18109248691241 0 12 | 4.13802790208159 9.60748524925563 0 13 | 8.90049077697510 9.99329318936273 0 14 | 4.15371012638716 9.53922527310308 0 15 | 3.47806488316686 7.80788315923614 0 16 | 5.04287107839320 9.38996803836429 0 17 | 2.56324459286653 7.83286351946551 0 18 | 5.42032358874179 8.77024851395462 0 19 | 2.78940677946772 5.84911155908821 0 20 | 1.69377595965126 3.42939148982086 0 21 | 2.57907558635543 5.85738793565177 0 22 | 4.82185528268354 9.96169885562949 0 23 | 0.253602746238408 7.45945161909674 0 24 | 8.14641315834860 9.88547372518201 0 25 | 1.07252390677591 9.64544522650273 0 26 | 3.96810711848012 9.37483872272884 0 27 | 7.78456478657554 9.21622730177576 0 28 | 5.68031802716968 9.55034658708916 0 29 | 0.188925610590359 1.86579459185882 0 30 | 3.39042984564948 5.22734216789434 0 31 | 0.306872061364665 8.10706884864857 0 32 | 2.94520568616781 8.89485671892082 0 33 | 1.56257145202601 7.59114877849288 0 34 | 7.88040432728020 9.72811231772191 0 35 | 2.06663143127242 5.55531983916915 0 36 | 3.26847684673349 5.80133323123041 0 37 | 3.03572486052262 4.55438209687869 0 38 | 4.06403686804154 7.30824174639464 0 39 | 0.261319587511897 6.82874924285753 0 40 | 0.535129082770840 2.96735234780109 0 41 | 6.16211768166533 8.73591445232541 0 42 | 0.0543225029100144 3.63143937000534 0 43 | 3.38950408283417 5.21140264842544 0 44 | 0.670802539239751 5.50842093397427 0 45 | 3.33354941448896 9.00586384747725 0 46 | 4.83782090456725 9.24046925519147 0 47 | 4.14342743933778 5.21158367718785 0 48 | 0.0448488462892471 2.53343482171120 0 49 | 3.29344955007619 8.39195386760030 0 50 | 1.43481273099994 4.54377111225283 0 51 | 5.80444602917862 7.72222238849776 0 52 | 2.89737803415351 4.84582798104369 0 53 | 3.48896826617082 9.42538199279707 0 54 | 7.98990181138566 9.38748992074748 0 55 | 6.07911967934092 7.81580715692381 0 56 | 8.54988937636567 9.83106545896296 0 57 | 1.86253147316586 3.64519173433558 0 58 | 5.09264649345917 7.16456405109382 0 59 | 0.640487340762152 2.96504627163533 0 60 | 0.445682665759531 7.27017831379406 0 61 | 7.03580231161425 9.62162716377990 0 62 | 2.38548233873891 9.31142472376713 0 63 | 5.47187479221741 6.52268275403238 0 64 | 3.09625873884070 7.24687725634908 0 65 | 5.64986319346389 8.14649426712568 0 66 | 7.29995079462111 8.54616550280286 0 67 | 8.77346437600257 9.96234073280813 0 68 | 3.06091044795100 7.72080944560689 0 69 | 2.73380551789568 4.29286766635781 0 70 | 4.69373224420759 9.24765856154850 0 71 | 6.87533960169376 9.88875131106959 0 72 | 3.00192545879843 6.33960532829528 0 73 | 1.35371591887987 4.90531471836317 0 74 | 4.98310945603791 8.19357999963722 0 75 | 1.44378940451651 3.32854209513079 0 76 | 3.58695390614628 9.08726843217394 0 77 | 1.66501084835760 6.33826894701403 0 78 | 1.13683031909428 8.93555055642300 0 79 | 6.89981119465722 9.08506017228389 0 80 | 3.50612434749800 7.19887972236541 0 81 | 7.98213686241980 9.90335214596799 0 82 | 4.48550059813567 7.87469534845766 0 83 | 8.18730647207947 9.65752968521276 0 84 | 6.98206593291099 9.31671668967771 0 85 | 4.22691515046792 6.27588464716913 0 86 | 5.42330764790338 7.08234539249773 0 87 | 1.77758743959901 9.00325034532830 0 88 | 1.13084327057428 7.21101548428525 0 89 | 3.93586389272097 8.03817933454068 0 90 | 6.63734173280782 8.35388742591753 0 91 | 0.386709419537340 8.58317856311490 0 92 | 3.32377361598771 8.07768523594907 0 93 | 8.06147926210387 9.31872691969549 0 94 | 8.98108629344805 9.99686700846425 0 95 | 7.12209318712481 9.35515066520718 0 96 | 4.89305717339869 7.48113528632786 0 97 | 7.40008878777077 9.35257115162952 0 98 | 7.03477188625162 9.97577769141571 0 99 | 7.09428420398105 9.71970901043399 0 100 | 4.37051546270230 9.41555568974276 0 101 | 4.00462374868525 0.616899669845488 1 102 | 5.44921361045783 0.138855328072746 1 103 | 8.44816973291296 1.96823424949569 1 104 | 7.09749399121559 4.84058301823207 1 105 | 7.13659478263630 3.99344574129641 1 106 | 3.13607555660068 1.01976990251471 1 107 | 9.42777338698193 2.03196575362135 1 108 | 2.88202491477532 0.512799144639290 1 109 | 7.98235678340026 2.31403273981939 1 110 | 6.42767231292936 0.998772381680681 1 111 | 1.78712965201534 0.243156456792515 1 112 | 3.07790563815491 1.88920490138202 1 113 | 9.43195911269671 0.268801081716839 1 114 | 6.34282338934720 0.234109280621259 1 115 | 4.82412559000009 1.99459231605986 1 116 | 8.56305479341248 4.72713371076877 1 117 | 3.29684212405129 2.07793293965495 1 118 | 7.90570542236263 3.88560220548909 1 119 | 9.07438180273557 2.97201368564586 1 120 | 4.03957906251732 1.88105978289717 1 121 | 1.78205895950614 0.293661208884570 1 122 | 2.68642960682293 1.36689620711673 1 123 | 7.50923375595256 4.39161107527268 1 124 | 9.22583275513275 6.75308704026472 1 125 | 5.93852172849132 0.0767525003841111 1 126 | 1.85217721700788 0.438665288257335 1 127 | 9.57234374408993 7.22701037061452 1 128 | 9.41511021370755 8.19137839466097 1 129 | 4.35273751059844 1.07589578304157 1 130 | 8.04923110762197 1.49599991038707 1 131 | 9.63236985254770 8.03011417611781 1 132 | 4.29156538673758 1.13530168347901 1 133 | 7.23413465556835 4.46740498567466 1 134 | 8.20309146793921 4.27626275005668 1 135 | 1.87457510864822 0.489332789968566 1 136 | 1.38780964339963 0.293816887566648 1 137 | 6.93682776203853 3.55888673905837 1 138 | 3.04295274833797 1.45808471523332 1 139 | 7.52052171782518 2.24448074758751 1 140 | 1.29475743274558 0.248662041648605 1 141 | 7.93270103161629 3.53322776478622 1 142 | 7.36266950886594 5.03397809680607 1 143 | 3.97602116402406 2.71911655949805 1 144 | 6.19034466536054 1.49434836516839 1 145 | 3.40178374234313 2.13801547822256 1 146 | 4.29491070615890 0.494287306651284 1 147 | 9.51694812792873 5.54313061426752 1 148 | 9.83993111985939 1.71347650375439 1 149 | 8.45868335411795 3.68311342408982 1 150 | 6.31700682494856 0.238683478937646 1 151 | 6.08012336783280 3.21848321902105 1 152 | 5.48289786541560 0.267286639106428 1 153 | 5.63646865791522 0.246636667516577 1 154 | 8.59268728497635 4.51516446747963 1 155 | 7.21916947499839 4.02521298309224 1 156 | 6.61095852079607 5.35205946687567 1 157 | 2.59249592920151 0.756284591960114 1 158 | 8.58163248547907 3.76779427699367 1 159 | 7.86164254606212 1.23425344739733 1 160 | 7.22459851512079 5.69192759004084 1 161 | 2.46391682231140 0.626697111247094 1 162 | 1.37666679614458 0.0850311192606557 1 163 | 6.99535329428774 5.22402041296117 1 164 | 4.10255509353404 0.0845494795236941 1 165 | 9.14754400713588 3.83800038341603 1 166 | 9.19417616894172 2.68752405465039 1 167 | 7.21993188858784 0.108525252131188 1 168 | 5.61112164928016 1.53003323224417 1 169 | 4.02501202704844 2.88388192100925 1 170 | 7.75454869096328 2.25550746659844 1 171 | 6.69549473855791 2.36673839888467 1 172 | 6.18849817990230 4.97836674652459 1 173 | 6.54263406218390 0.834598177291021 1 174 | 8.54443627754918 5.47850873429622 1 175 | 1.97961580331274 0.158062518938500 1 176 | 3.67309260569741 1.13227508530645 1 177 | 5.75572976460165 1.30828876313430 1 178 | 8.45160472860189 5.90751849062474 1 179 | 3.46373193769655 1.51690595784220 1 180 | 1.91395651414783 0.612317246772413 1 181 | 4.12656938065540 2.09322445393649 1 182 | 3.65772017039046 1.80838713249693 1 183 | 4.54395760030265 1.38858754767689 1 184 | 2.86266970800483 1.34954202841685 1 185 | 8.37374823952218 6.83156860507414 1 186 | 5.62707992632729 0.893689431466388 1 187 | 6.42610816769460 1.07534713660481 1 188 | 4.69353317658611 3.15086012845143 1 189 | 4.86173611501328 3.57464916591233 1 190 | 4.35769946480643 2.34450146003832 1 191 | 6.29762879066540 0.534351386077976 1 192 | 4.71155500188011 3.43210544532588 1 193 | 1.49995426186507 0.192006207383273 1 194 | 3.16576104712266 0.493379007110222 1 195 | 4.21408348419092 2.97014277918461 1 196 | 5.52248511695330 3.63263027130760 1 197 | 4.15244831176753 1.44597290703838 1 198 | 9.55986996363196 1.13832040773527 1 199 | 1.63276516895206 0.446783742774178 1 200 | 9.38532498107474 0.913169554364942 1 201 | -------------------------------------------------------------------------------- /ML/2-Softmax Regression/test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-9 14:42 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Test.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import numpy as np 10 | 11 | 12 | def load_data(num, m): 13 | testDataSet = np.mat(np.ones((num, m))) 14 | for i in range(num): 15 | testDataSet[i, 1] = np.random.random() * 6 - 3 16 | testDataSet[i, 2] = np.random.random() * 15 17 | return testDataSet 18 | 19 | 20 | def load_weights(weights_path): 21 | f = open(weights_path) 22 | w = [] 23 | for line in f.readlines(): 24 | w_tmp = [] 25 | lines = line.strip().split('\t') 26 | for x in range(len(lines) - 1): 27 | w_tmp.append(float(lines[x])) 28 | w.append(w_tmp) 29 | f.close() 30 | weights = np.mat(w) 31 | m, n = np.shape(weights) 32 | return weights, m, n 33 | 34 | 35 | def predict(test_data, w): 36 | h = test_data * w 37 | return h.argmax(axis=1) 38 | 39 | 40 | def save_result(filename, result): 41 | f_reslt = open(filename, 'w') 42 | m = np.shape(result)[0] 43 | for i in range(m): 44 | f_reslt.write(str(result[i, 0])+'\n') 45 | f_reslt.close() 46 | return print('写入数据成功') 47 | 48 | 49 | def main(): 50 | w, m, n = load_weights('../Softmax Regression Train/weights_Softmax_Regression') 51 | test_data = load_data(4000, m) 52 | result = predict(test_data, w) 53 | save_result('test_result', result) 54 | 55 | 56 | if __name__ == '__main__': 57 | main() 58 | 59 | -------------------------------------------------------------------------------- /ML/2-Softmax Regression/train.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-9 13:47 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Softmax_Regression_Train.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import numpy as np 10 | 11 | 12 | # 梯度更新函数 GradientAscent 13 | def GradientAscent(feature_data, label_data, k, maxCycle, alpha): 14 | m, n = np.shape(feature_data) 15 | weights = np.mat(np.ones((n, k))) 16 | i = 0 17 | while i <= maxCycle: 18 | error = np.exp(feature_data * weights) 19 | # print('**********************************************') 20 | # print(error, '*****2828****') 21 | # print('**********************************************') 22 | 23 | if i % 1000 == 0: 24 | print("\t 迭代次数 Iter 为: " + str(i) + ", 误差Cost为: " + str(Cost(error, label_data))) 25 | rowsum = -error.sum(axis=1) # axis=1 表示将一个矩阵的每一行向量相加 26 | ''' 27 |  (1)numpy.repeat(a,repeats,axis=None); 28 | (2)object(ndarray).repeat(repeats,axis=None):理解了第一种方法就可以轻松知道第二种方法了。 29 | 参数的意义:axis=None,时候就会flatten当前矩阵,实际上就是变成了一个行向量 30 |               axis=0,沿着y轴复制,实际上增加了行数 31 |               axis=1,沿着x轴复制,实际上增加列数 32 | ''' 33 | rowsum = rowsum.repeat(k, axis=1) 34 | # print('************************* rowsum ===>'+'\n', rowsum) 35 | error = error / rowsum # 得到整体事件中每个事件发生的概率 36 | print('**********************************************') 37 | print(error, '*****2828****') 38 | print('**********************************************') 39 | for x in range(m): 40 | error[x, label_data[x, 0]] += 1 # 更具有普适性 41 | # print('**********************************************') 42 | # print(error, '*****2828****') 43 | # print('**********************************************') 44 | # error[x, label_data[x]] += 1 # label 为一维数据时这样也可以 45 | weights = weights + (alpha / m) * feature_data.T * error 46 | i += 1 47 | return weights 48 | 49 | 50 | # 误差函数 51 | def Cost(error, label_data): 52 | m = np.shape(error)[0] 53 | sum_cost = 0.0 54 | for i in range(m): 55 | if error[i, label_data[i, 0]] / np.sum(error[i, :]) > 0: # 如果事件发生的概率大于 0。I{ y = j } = 1 56 | sum_cost -= np.log(error[i, label_data[i, 0]] / np.sum(error[i, :])) 57 | else: # I{ y = j } = 0 58 | sum_cost -= 0 59 | return sum_cost / m 60 | 61 | 62 | def load_data(filename): 63 | data = open(filename) 64 | feature_data = [] 65 | label_data = [] 66 | for line in data.readlines(): 67 | feature_tmp = [] 68 | feature_tmp.append(1) 69 | lines = line.strip().split('\t') 70 | a = len(lines)-1 71 | for x in range(a): 72 | feature_tmp.append(float(lines[a])) 73 | label_data.append(int(lines[-1])) 74 | feature_data.append(feature_tmp) 75 | data.close() 76 | return np.mat(feature_data), np.mat(label_data).T, len(set(label_data)) 77 | 78 | 79 | def save_model(weights_name, weights): 80 | f_w = open(weights_name, 'w') 81 | m, n = np.shape(weights) 82 | for i in range(m): 83 | w_tmp = [] 84 | for j in range(n): 85 | w_tmp.append(str(weights[i, j])) 86 | f_w.write('\t'.join(w_tmp)+'\n') 87 | f_w.close() 88 | return print('写入数据成功') 89 | 90 | 91 | def main(): 92 | # 1.导入训练数据 93 | feature, label, k = load_data('./train_data.txt') 94 | print(feature.shape) 95 | # 2.训练模型 96 | weights = GradientAscent(feature, label, k, 1, 0.9) 97 | # 3.保存模型 98 | save_model('weights_Softmax_Regression', weights) 99 | 100 | if __name__ == '__main__': 101 | main() 102 | 103 | -------------------------------------------------------------------------------- /ML/2-Softmax Regression/train_data.txt: -------------------------------------------------------------------------------- 1 | -0.017612 14.053064 2 2 | -1.395634 4.662541 3 3 | -0.752157 6.53862 3 4 | -1.322371 7.152853 3 5 | 0.423363 11.054677 2 6 | 0.406704 7.067335 3 7 | 0.667394 12.741452 2 8 | -2.46015 6.866805 3 9 | 0.569411 9.548755 0 10 | -0.026632 10.427743 2 11 | 0.850433 6.920334 3 12 | 1.347183 13.1755 2 13 | 1.176813 3.16702 3 14 | -1.781871 9.097953 2 15 | -0.566606 5.749003 3 16 | 0.931635 1.589505 1 17 | -0.024205 6.151823 3 18 | -0.036453 2.690988 1 19 | -0.196949 0.444165 1 20 | 1.014459 5.754399 3 21 | 1.985298 3.230619 3 22 | -1.693453 -0.55754 1 23 | -0.576525 11.778922 2 24 | -0.346811 -1.67873 1 25 | -2.124484 2.672471 1 26 | 1.217916 9.597015 0 27 | -0.733928 9.098687 0 28 | -3.642001 -1.618087 1 29 | 0.315985 3.523953 3 30 | 1.416614 9.619232 0 31 | -0.386323 3.989286 3 32 | 0.556921 8.294984 0 33 | 1.224863 11.58736 2 34 | -1.347803 -2.406051 1 35 | 1.196604 4.951851 3 36 | 0.275221 9.543647 0 37 | 0.470575 9.332488 0 38 | -1.889567 9.542662 2 39 | -1.527893 12.150579 2 40 | -1.185247 11.309318 2 41 | -0.445678 3.297303 3 42 | 1.042222 6.105155 3 43 | -0.618787 10.320986 2 44 | 1.152083 0.548467 1 45 | 0.828534 2.676045 3 46 | -1.237728 10.549033 2 47 | -0.683565 -2.166125 1 48 | 0.229456 5.921938 3 49 | -0.959885 11.555336 2 50 | 0.492911 10.993324 2 51 | 0.184992 8.721488 0 52 | -0.355715 10.325976 2 53 | -0.397822 8.058397 0 54 | 0.824839 13.730343 2 55 | 1.507278 5.027866 3 56 | 0.099671 6.835839 3 57 | -0.344008 10.717485 2 58 | 1.785928 7.718645 0 59 | -0.918801 11.560217 2 60 | -0.364009 4.7473 3 61 | -0.841722 4.119083 3 62 | 0.490426 1.960539 1 63 | -0.007194 9.075792 0 64 | 0.356107 12.447863 2 65 | 0.342578 12.281162 2 66 | -0.810823 -1.466018 1 67 | 2.530777 6.476801 3 68 | 1.296683 11.607559 2 69 | 0.475487 12.040035 2 70 | -0.783277 11.009725 2 71 | 0.074798 11.02365 2 72 | -1.337472 0.468339 1 73 | -0.102781 13.763651 2 74 | -0.147324 2.874846 3 75 | 0.518389 9.887035 0 76 | 1.015399 7.571882 0 77 | -1.658086 -0.027255 1 78 | 1.319944 2.171228 1 79 | 2.056216 5.019981 3 80 | -0.851633 4.375691 3 81 | -1.510047 6.061992 3 82 | -1.076637 -3.181888 1 83 | 1.821096 10.28399 0 84 | 3.01015 8.401766 0 85 | -1.099458 1.688274 1 86 | -0.834872 -1.733869 1 87 | -0.846637 3.849075 3 88 | 1.400102 12.628781 2 89 | 1.752842 5.468166 3 90 | 0.078557 0.059736 1 91 | 0.089392 -0.7153 1 92 | 1.825662 12.693808 2 93 | 0.197445 9.744638 0 94 | 0.126117 0.922311 1 95 | -0.679797 1.22053 1 96 | 0.677983 2.556666 1 97 | 0.761349 10.693862 0 98 | -2.168791 0.143632 1 99 | 1.38861 9.341997 0 100 | 0.317029 14.739025 2 101 | -2.65887965178 0.658328066452 1 102 | -2.30615885683 11.5036718065 2 103 | -2.83005963556 7.30810428189 3 104 | -2.30319006285 3.18958964564 1 105 | -2.31349250532 4.41749905123 3 106 | -2.71157223048 0.21599278192 1 107 | -2.99935111344 14.5766538514 2 108 | -2.50329272687 12.7274016382 2 109 | -2.14191210185 9.75999136268 2 110 | -2.21409612618 9.25234159289 2 111 | -2.0503599261 1.87312594247 1 112 | -2.99747377006 2.82404034943 1 113 | -2.39019233623 1.88778487771 1 114 | -2.00981101171 13.0015287952 2 115 | -2.06105014551 7.26924117028 3 116 | -2.94028883652 10.8418044558 2 117 | -2.56811396636 1.31240093493 1 118 | -2.89942462914 7.47932555859 3 119 | -2.83349151782 0.292728283929 1 120 | -2.16467022383 4.62184237142 3 121 | 2.02604290795 6.68200376515 3 122 | 2.3755881562 9.3838379637 0 123 | 2.48299208843 9.75753701005 0 124 | 2.65108044441 9.39059526201 0 125 | 2.49422603944 11.856131521 0 126 | 2.47215954581 4.83431641068 3 127 | 2.26731525725 5.64891602081 3 128 | 2.33628075296 10.4603294628 0 129 | 2.4548064459 9.90879879651 0 130 | 2.13147505967 8.99561368732 0 131 | 2.86925733903 4.26531919929 3 132 | 2.05715970133 4.97240425903 3 133 | 2.14839753847 8.91032469409 0 134 | 2.17630437606 5.76122354509 3 135 | 2.86205491781 11.630342945 0 136 | -------------------------------------------------------------------------------- /ML/2-Softmax Regression/逻辑回归于Softmax回归的关系.txt: -------------------------------------------------------------------------------- 1 | 逻辑回归的特点: 2 | 3 | 1、复杂度低; 4 | 2、容易实现。 5 | 应用:实现广告的点击率预测。 6 | 局限:逻辑回归仅限于在二分类问题中使用,面对多分类问题不能做到预测的功能。 7 | 8 | Softmax回归的特点: 9 | 1、可以很好地对多分类问题进行拟合,然后进行预测; 10 | 2、就是逻辑回归的延伸。 11 | 应用:多分类问题 ---> 手写数字识别,图像分类 ... 12 | -------------------------------------------------------------------------------- /ML/3.Factorization Machine/Test/Test_FM.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-11 17:16 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Test_FM.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | 10 | import numpy as np 11 | # from FM_train import getPrediction 12 | 13 | 14 | # 定义 Sigmoid 函数 15 | def sigmoid(x): 16 | return 1 / (1 + np.exp(-x)) 17 | 18 | 19 | # 定义预测结果的函数 20 | def prediction(dataMatrix, w0, w, v): 21 | m = np.shape(dataMatrix)[0] 22 | result = [] 23 | for x in range(m): 24 | inter_1 = dataMatrix[x] * v 25 | inter_2 = np.multiply(dataMatrix[x], dataMatrix[x]) * np.multiply(v, v) 26 | interaction = 0.5 * np.sum(np.multiply(inter_1, inter_1) - inter_2) 27 | p = w0 + dataMatrix[x] * w + interaction 28 | pre = sigmoid(p[0, 0]) 29 | result.append(pre) 30 | return result 31 | 32 | 33 | def loadDataSet(data): 34 | '''导入测试数据集 35 | input: data(string)测试数据 36 | output: dataMat(list)特征 37 | ''' 38 | dataMat = [] 39 | fr = open(data) # 打开文件 40 | for line in fr.readlines(): 41 | lines = line.strip().split("\t") 42 | lineArr = [] 43 | for i in range(len(lines)): 44 | lineArr.append(float(lines[i])) 45 | dataMat.append(lineArr) 46 | fr.close() 47 | return dataMat 48 | 49 | 50 | def loadModel(model_file): 51 | '''导入FM模型 52 | input: model_file(string)FM模型 53 | output: w0, np.mat(w).T, np.mat(v)FM模型的参数 54 | ''' 55 | f = open(model_file) 56 | line_index = 0 57 | w0 = 0.0 58 | w = [] 59 | v = [] 60 | for line in f.readlines(): 61 | lines = line.strip().split("\t") 62 | if line_index == 0: # w0 63 | w0 = float(lines[0].strip()) 64 | elif line_index == 1: # w 65 | for x in lines: 66 | w.append(float(x.strip())) 67 | else: 68 | v_tmp = [] 69 | for x in lines: 70 | v_tmp.append(float(x.strip())) 71 | v.append(v_tmp) 72 | line_index += 1 73 | f.close() 74 | return w0, np.mat(w).T, np.mat(v) 75 | 76 | 77 | def save_result(file_name, result): 78 | '''保存最终的预测结果 79 | input: file_name(string)需要保存的文件名 80 | result(mat):对测试数据的预测结果 81 | ''' 82 | f = open(file_name, "w") 83 | f.write("\n".join(str(x) for x in result)) 84 | f.close() 85 | 86 | 87 | def main(): 88 | # 1、导入测试数据 89 | print("---------- 1.load data ------------") 90 | dataTest = loadDataSet("test_data.txt") 91 | # 2.Softmax Regression Train、导入FM模型 92 | print("---------- 2.Softmax Regression Train.load model ------------") 93 | w0, w, v = loadModel("../Train/weights_FM") 94 | # 3、预测 95 | print("---------- 3.get Prediction ------------") 96 | result = prediction(dataTest, w0, w, v) 97 | # 4、保存最终的预测结果 98 | print("---------- 4.save prediction ------------") 99 | save_result("predict_result", result) 100 | 101 | 102 | if __name__ == '__main__': 103 | main() 104 | -------------------------------------------------------------------------------- /ML/3.Factorization Machine/Test/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-11 13:59 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : __init__.py.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | 10 | def main(): 11 | pass 12 | 13 | 14 | if __name__ == '__main__': 15 | main() 16 | -------------------------------------------------------------------------------- /ML/3.Factorization Machine/Test/predict_result: -------------------------------------------------------------------------------- 1 | 0.13424317619798848 2 | 0.2663624135610938 3 | 0.4593442249737366 4 | 0.4596333900630152 5 | 0.13096301989126483 6 | 0.12263648366542589 7 | 0.02589309076502434 8 | 0.47716208522666 9 | 0.18689194772682466 10 | 0.007746197752195307 11 | 0.7208521393734716 12 | 0.12855119667994955 13 | 0.05503736552512578 14 | 0.06557527322363538 15 | 0.04023720980897149 16 | 0.07183555523974552 17 | 0.25135194687904033 18 | 0.008353494800524209 19 | 0.01234512005004107 20 | 0.5798150353427968 21 | 0.054680822189805155 22 | 0.35081063298151105 23 | 0.014656347281244216 24 | 0.5473378441732634 25 | 0.29339057002050883 26 | 0.9280366730138554 27 | 0.09524803696964171 28 | 0.05075541568898835 29 | 0.011575895302745215 30 | 0.18952027254700962 31 | 0.004370899250539211 32 | 0.16051522861422973 33 | 0.023297795293765595 34 | 0.004425174067363924 35 | 0.0030797932340617235 36 | 0.007377213876959712 37 | 0.04462519855672316 38 | 0.01090734269075072 39 | 0.45451321915054377 40 | 0.04553017550311664 41 | 0.33168413516731654 42 | 0.24921396250225009 43 | 0.005757316986897379 44 | 0.08343589554966517 45 | 0.28921091962253676 46 | 0.06045148529858086 47 | 0.7099047016394168 48 | 0.42580589541425956 49 | 0.818662587020087 50 | 0.012027096481743896 51 | 0.21991601442318745 52 | 0.881531896925504 53 | 0.08514059703456105 54 | 0.9387961552754363 55 | 0.07680596639732179 56 | 0.14091446817565847 57 | 0.1276372627254908 58 | 0.07035016382032228 59 | 0.09294569119557747 60 | 0.32880957824705054 61 | 0.057741673284343306 62 | 0.01909871113749973 63 | 0.2648276955171957 64 | 0.9310828078889882 65 | 0.09086180377087888 66 | 0.5774911850444885 67 | 0.1374163584578225 68 | 0.8636411815513128 69 | 0.03154868521007119 70 | 0.6173388913193428 71 | 0.08538511545588186 72 | 0.29202901546918186 73 | 0.002600650471537438 74 | 0.022430518846929932 75 | 0.5044679644658181 76 | 0.6234839817297643 77 | 0.42272101921008376 78 | 0.1685576667296938 79 | 0.036744847768604226 80 | 0.061146037469647974 81 | 0.8054344933454681 82 | 0.8977148391542793 83 | 0.7020324773232451 84 | 0.7563864315562563 85 | 0.08250771309788414 86 | 0.6947444668465002 87 | 0.017187583955084827 88 | 0.03536468289210166 89 | 0.7269735704660352 90 | 0.8463514096743493 91 | 0.07520937951938751 92 | 0.3235542330003812 93 | 0.02425586842743472 94 | 0.5929397275673675 95 | 0.754412378846274 96 | 0.2501966394678664 97 | 0.21321601353476546 98 | 0.45238112136308656 99 | 0.0154510799137576 100 | 0.20277494472155905 101 | 0.9999912540338778 102 | 0.9999995412273783 103 | 0.99999487032053 104 | 0.9999999888765557 105 | 0.9993625581443115 106 | 0.9999996840505304 107 | 0.9990772513286675 108 | 0.9980168318804157 109 | 0.999995601586775 110 | 0.9999962549456796 111 | 0.9999782099171816 112 | 0.9989369611780532 113 | 0.9976280749371419 114 | 0.9999931868257818 115 | 0.9999999894211459 116 | 0.9999835321860867 117 | 0.999997350064519 118 | 0.9974446200443322 119 | 0.9999765361162504 120 | 0.9983602224066408 121 | 0.9999073221832147 122 | 0.998573463808158 123 | 0.5534056068566996 124 | 0.9425438620234586 125 | 0.9301969106283295 126 | 0.8955276820688091 127 | 0.9984309463290139 128 | 0.9965172996785246 129 | 0.9998771160254739 130 | 0.9986424009697511 131 | 0.9922614300369103 132 | 0.9999999159062591 133 | 0.8663341124121858 134 | 0.9999299715571686 135 | 0.9212244873497413 136 | 0.9964088688367828 137 | 0.9182586116633221 138 | 0.9974149106203005 139 | 0.8262063419191573 140 | 0.9999997207245698 141 | 0.9605502631734161 142 | 0.999999968570454 143 | 0.9999999792768417 144 | 0.9999632812009565 145 | 0.9795942679687768 146 | 0.7282153103584175 147 | 0.9998500661072203 148 | 0.9664563605302785 149 | 0.9998217970801085 150 | 0.9993049923033426 151 | 0.9999960089007919 152 | 0.9933290294158282 153 | 0.9844689060015275 154 | 0.9981149628421973 155 | 0.9964387371581203 156 | 0.9999976170403092 157 | 0.9999999161389898 158 | 0.9861893659405142 159 | 0.9999602490289631 160 | 0.9893314684448815 161 | 0.9706457230658865 162 | 0.9967929229559067 163 | 0.8399032311928673 164 | 0.999880625494313 165 | 0.9997646757911549 166 | 0.9962016467653788 167 | 0.9999976990652272 168 | 0.96359675384104 169 | 0.9944819908166436 170 | 0.9238552127106756 171 | 0.9926337120338006 172 | 0.545386029700635 173 | 0.5297541101704019 174 | 0.9988711124888155 175 | 0.9787562482421189 176 | 0.7956197450242174 177 | 0.9999499003172266 178 | 0.5548263902855288 179 | 0.5095799158189468 180 | 0.9999738734870879 181 | 0.9291736079328217 182 | 0.9990860839333 183 | 0.9999930781486837 184 | 0.9994141854641528 185 | 0.9567683250864977 186 | 0.9247291438233912 187 | 0.9985360652524726 188 | 0.999775219883548 189 | 0.999995788374115 190 | 0.9989796253213139 191 | 0.9999443493345659 192 | 0.623877247116008 193 | 0.9994974000340888 194 | 0.9999892853562842 195 | 0.9117533237892267 196 | 0.9993122746939441 197 | 0.9999998391255357 198 | 0.9518803708612011 199 | 0.9518180046166856 200 | 0.999999939840248 -------------------------------------------------------------------------------- /ML/3.Factorization Machine/Test/test_data.txt: -------------------------------------------------------------------------------- 1 | 4.22885689100085 -0.383600664906212 2 | 5.98523668756741 -0.401814062733461 3 | 6.95949313301608 -0.406891865829261 4 | 6.38530758271838 -0.397227032160838 5 | 0.688060991180512 -0.324475388783223 6 | 5.30864280694127 -0.403125099918000 7 | 4.07619197041153 -0.404465070647941 8 | 7.18358943205884 -0.409707070836633 9 | 5.31333906565675 -0.396596366661128 10 | 1.05629203329022 -0.369992373291273 11 | 7.78802241824093 -0.406046543133335 12 | 0.908232857874395 -0.328459209606044 13 | 1.53656717591307 -0.351364694061123 14 | 4.40085139001721 -0.397023473641097 15 | 4.57424365687674 -0.406781494159556 16 | 5.18052108361104 -0.408825386295581 17 | 6.37709098072174 -0.409438975016263 18 | 2.40707035480160 -0.391681735236429 19 | 2.89064571674477 -0.394565774329386 20 | 6.95140499551737 -0.400312578984568 21 | 2.54790156597005 -0.368451837817781 22 | 6.67832727013717 -0.408178347498748 23 | 3.44462411301042 -0.401564489789818 24 | 6.75332065747000 -0.398738626936024 25 | 6.02170487581795 -0.400644238962528 26 | 9.15991244131425 -0.407735759555317 27 | 4.62449159242329 -0.395393857312668 28 | 4.60916366028964 -0.404136726205582 29 | 3.22471807186779 -0.401045032515435 30 | 4.71357153710612 -0.386288539403391 31 | 1.75874415683531 -0.389444742567982 32 | 4.73485992965320 -0.389320232054202 33 | 3.41124607049109 -0.394728964552589 34 | 1.91745255461798 -0.391947209268189 35 | 2.42849598318169 -0.405368635885985 36 | 2.69061586686018 -0.398111035816425 37 | 1.88661976791491 -0.360179143779989 38 | 0.911134636865350 -0.362963861105539 39 | 6.83363243294653 -0.405035385989699 40 | 4.25728841871188 -0.399739646814442 41 | 6.47617630172684 -0.405911721457004 42 | 6.35786710514084 -0.409267285897986 43 | 2.08934922426023 -0.391321994675435 44 | 2.36230576993797 -0.359308512101375 45 | 6.07303940685635 -0.401775908847004 46 | 4.58725493648868 -0.401309556947776 47 | 7.70285514803660 -0.405329650590112 48 | 6.62009598359135 -0.402995014575865 49 | 8.41929152691309 -0.409233742963925 50 | 2.56440992229147 -0.389434289051228 51 | 5.82249164527227 -0.402440260191123 52 | 8.69941032358007 -0.407302464085700 53 | 3.18074075481059 -0.372769131302750 54 | 9.39829470344921 -0.409437184944561 55 | 4.79463224948888 -0.401380241908074 56 | 5.44716110526763 -0.403327530651797 57 | 5.43885933999639 -0.404706501938553 58 | 5.22495305777102 -0.409870913891884 59 | 2.18676632399634 -0.354785976361645 60 | 1.09697464523194 -0.315685237138469 61 | 4.04579995857626 -0.392857794216805 62 | 3.65816176838171 -0.401575759068070 63 | 6.27896379614169 -0.406854769054641 64 | 9.32853570278820 -0.409951384849673 65 | 1.92028349427775 -0.350639391440659 66 | 6.96266337082995 -0.400628405912004 67 | 5.25404403859336 -0.400470088641706 68 | 8.61139811393332 -0.407964326116437 69 | 3.93456361215266 -0.399383003431485 70 | 7.41257943454207 -0.405986863392910 71 | 3.47712671277525 -0.377708337486631 72 | 5.86092067231462 -0.398029583170793 73 | 0.444540922782385 -0.374280609001722 74 | 2.42785357820962 -0.378718538743031 75 | 6.87796085120107 -0.403118962470252 76 | 7.36340074301202 -0.404813730429160 77 | 6.83415866967978 -0.406760189201662 78 | 4.42305413383371 -0.383302839595060 79 | 3.30857880214071 -0.386766093846753 80 | 2.70270423432065 -0.369477504927622 81 | 8.21721184961310 -0.406989567278616 82 | 8.87770954256354 -0.408105816619614 83 | 7.69114387388296 -0.405636700880363 84 | 8.08514095887345 -0.408591528245170 85 | 3.77395544835103 -0.383193789027371 86 | 7.90407217966913 -0.409674704507269 87 | 3.27565434075205 -0.396574186626550 88 | 4.38644982586956 -0.405407692694353 89 | 7.68854252429615 -0.403967834158350 90 | 8.61980478702072 -0.409960253976007 91 | 5.14423456505704 -0.407557450203104 92 | 5.88026055308498 -0.396387506042030 93 | 1.99862822857452 -0.370441048919565 94 | 7.48705718215691 -0.408595410641654 95 | 7.89963029944531 -0.405615902741996 96 | 5.34064127370726 -0.392102808985665 97 | 1.11705744193203 -0.323888651993048 98 | 6.78652304800188 -0.404357991973989 99 | 1.89710406017580 -0.374845678813075 100 | 1.47608221976689 -0.330764364979088 101 | 8.50712674289007 -0.276001595625538 102 | 9.29608866756663 -0.250097725623909 103 | 5.82790965175840 -0.223888078369432 104 | 8.79013904597178 -0.192180167430789 105 | 0.00522375356944771 -0.190112056752678 106 | 6.12566469483999 -0.191862695134956 107 | 5.27680069338442 -0.283620503061425 108 | 8.01347605521952 -0.339789839392611 109 | 4.98094291196390 -0.207611250783842 110 | 5.74661219130188 -0.218342161423755 111 | 7.38640291995402 -0.269295369034182 112 | 2.46734525985975 -0.238288880357043 113 | 0.834828136026227 -0.221533784108590 114 | 6.60944557947342 -0.240792638559536 115 | 8.90752116325322 -0.193485913413969 116 | 7.69029085335896 -0.270681700261449 117 | 9.28313062314188 -0.273179012277787 118 | 0.169829383372613 -0.211350464907188 119 | 8.62710718699670 -0.291129025290239 120 | 8.44855674576263 -0.344570921371766 121 | 5.52291341538775 -0.257212674743648 122 | 0.319910157625669 -0.206112865902040 123 | 3.62411462273053 -0.345825593131402 124 | 4.89569989177322 -0.332877280834451 125 | 1.23083747545945 -0.274049401025970 126 | 1.46514910614890 -0.283848942570996 127 | 0.426524109111434 -0.209171520400684 128 | 2.81866855880430 -0.259990624655050 129 | 6.95163039444332 -0.284971060010288 130 | 5.35801055751113 -0.290120737029962 131 | 1.23932277598070 -0.244113245697875 132 | 8.52998155340816 -0.214682803486139 133 | 2.70294332292698 -0.308364561952262 134 | 5.64979570738201 -0.255621743131591 135 | 4.17028951642886 -0.325183087759844 136 | 9.47933121293169 -0.372333882795863 137 | 1.05709426581721 -0.273398725501725 138 | 1.66460440876421 -0.236624584151135 139 | 5.73709764841198 -0.363472281253222 140 | 9.31201384608250 -0.243771077248671 141 | 7.37841653797590 -0.369353466594125 142 | 8.60440563038232 -0.202858282052828 143 | 9.84398312240972 -0.218156455352533 144 | 7.85558989265031 -0.284113153415768 145 | 1.77602460505865 -0.266184853812900 146 | 1.33931250987971 -0.297183829519125 147 | 9.39141706069548 -0.328615962521731 148 | 2.95533834475356 -0.292786212653185 149 | 4.67068187028852 -0.251577685729938 150 | 0.252281814930363 -0.195413359789637 151 | 5.59032544988695 -0.216561073003438 152 | 3.47879194327261 -0.279761697153916 153 | 0.542394844411296 -0.241760848739405 154 | 6.62808061960974 -0.315832328127600 155 | 8.98486137834300 -0.363912806574386 156 | 9.88417928784981 -0.281869017489731 157 | 7.06917419322763 -0.190096666339403 158 | 2.87849344815137 -0.279436738890204 159 | 4.64839941625137 -0.231269500181348 160 | 8.18204038907671 -0.365092731458073 161 | 1.78116953886766 -0.271224091462708 162 | 0.567046890682912 -0.221052401385065 163 | 3.35848974676925 -0.322190001834344 164 | 2.08946673993135 -0.202875908957950 165 | 6.75391177336247 -0.290281646438828 166 | 9.12132474239623 -0.367065550694628 167 | 7.45546073701717 -0.240588293840949 168 | 5.61861425281637 -0.338669639313080 169 | 5.97211350337855 -0.319126449955653 170 | 1.34122932828682 -0.277150727988936 171 | 8.94941675440814 -0.373023763824194 172 | 2.42486558936719 -0.326102224896383 173 | 4.41722057064424 -0.360419855410762 174 | 8.97191350973572 -0.348399498123565 175 | 0.933705167550930 -0.252575481502707 176 | 4.56057666843742 -0.346355386685340 177 | 9.95389727655092 -0.323504140663397 178 | 2.97346815887922 -0.334814880905946 179 | 2.98243971887764 -0.337381814430199 180 | 5.05428142457703 -0.232514641064066 181 | 6.31069999213594 -0.359626030006551 182 | 0.808624231303137 -0.208403519638889 183 | 9.05134744223571 -0.282039797611739 184 | 1.09154212042459 -0.207245206497595 185 | 3.38097718802172 -0.303443898123144 186 | 7.46313427703679 -0.379865308531902 187 | 0.484473392532221 -0.209222705848565 188 | 6.03467983830770 -0.277585570350284 189 | 7.29709448223228 -0.245958371186387 190 | 7.81377051799277 -0.327592448772205 191 | 6.92531986386519 -0.274004053493805 192 | 3.96520792581593 -0.347683790060566 193 | 7.80175531491174 -0.317975871659649 194 | 6.07865907262946 -0.237887651602709 195 | 1.04813241973500 -0.274359906785175 196 | 5.49540107015198 -0.283385560614568 197 | 8.90475679184438 -0.229599130878489 198 | 7.34341083695970 -0.371524950619845 199 | 0.728852990989761 -0.260383341617659 200 | 7.98350864113952 -0.201049508579053 -------------------------------------------------------------------------------- /ML/3.Factorization Machine/Train/Train.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-11 13:59 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Train.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import numpy as np 10 | import time 11 | 12 | 13 | # 导入准备训练的数据集 14 | def load_data(filename): 15 | data = open(filename) 16 | feature = [] 17 | label = [] 18 | for line in data.readlines(): 19 | feature_tmp = [] 20 | lines = line.strip().split('\t') 21 | for x in range(len(lines)-1): 22 | feature_tmp.append(float(lines[x])) 23 | label.append(int(lines[-1])*2-1) 24 | feature.append(feature_tmp) 25 | data.close() 26 | return feature, label 27 | 28 | 29 | # 初始化权重 w 和交叉项权重 v 30 | def initialize_w_v(n, k): 31 | w = np.ones((n, 1)) 32 | v = np.mat(np.zeros((n, k))) 33 | for i in range(n): 34 | for j in range(k): 35 | v[i, j] = np.random.normal(0, 0.2) # 把 v 中的值变为服从 N(0, 0.2) 正态分布的数值 36 | return w, v 37 | 38 | 39 | # 定义 Sigmoid 函数 40 | def sigmoid(x): 41 | return 1 / (1 + np.exp(-x)) 42 | 43 | 44 | # 定义误差损失函数 loss(y', y) = ∑-ln[sigmoid(y'* y)] 45 | def get_cost(predict, classLabels): 46 | m = np.shape(predict)[0] 47 | # m = len(predict) 48 | cost = [] 49 | error = 0.0 50 | for i in range(m): 51 | error -= np.log(sigmoid(predict[i] * classLabels[i])) 52 | cost.append(error) 53 | return error 54 | 55 | 56 | # 用梯度下降法求解模型参数,训练 FM 模型。 57 | def stocGradient(dataMatrix, classLabels, k, max_iter, alpha): 58 | """ 59 | :param dataMatrix: 输入的数据集特征 60 | :param classLabels: 特征对应的标签 61 | :param k: 交叉项矩阵的维度 62 | :param max_iter: 最大迭代次数 63 | :param alpha: 学习率 64 | :return: 65 | """ 66 | m, n = np.shape(dataMatrix) 67 | w0 = 0 68 | w, v = initialize_w_v(n, k) # 初始化参数 69 | for it in range(max_iter): 70 | # print('第 %d 次迭代' % it) 71 | for x in range(m): 72 | v_1 = dataMatrix[x] * v # dataMatrix[x]的 shape 为(1,n),v的 shape 为(n,k)--->v_1的 shape为(1, k) 73 | v_2 = np.multiply(dataMatrix[x], dataMatrix[x]) * np.multiply(v, v) 74 | interaction = 0.5 * np.sum(np.multiply(v_1, v_1) - v_2) 75 | p = w0 + dataMatrix[x] * w + interaction 76 | loss = sigmoid(classLabels[x] * p[0, 0]) - 1 77 | w0 = w0 - alpha * loss * classLabels[x] 78 | for i in range(n): 79 | if dataMatrix[x, i] != 0: 80 | w[i, 0] = w[i, 0] - alpha * loss * classLabels[x] * dataMatrix[x, i] 81 | for j in range(k): 82 | v[i, j] = v[i, j] * alpha * loss * classLabels[x] * (dataMatrix[x, i] * v_1[0, j] - v[i, j] * 83 | dataMatrix[x, i]*dataMatrix[x, i]) 84 | if it % 1000 == 0: 85 | print("\t迭代次数:" + str(it) + ",误差:" + str(get_cost(prediction(np.mat(dataMatrix), w0, w, v), classLabels))) 86 | return w0, w, v 87 | 88 | 89 | # 定义预测结果的函数 90 | def prediction(dataMatrix, w0, w, v): 91 | m = np.shape(dataMatrix)[0] 92 | result = [] 93 | for x in range(m): 94 | inter_1 = dataMatrix[x] * v 95 | inter_2 = np.multiply(dataMatrix[x], dataMatrix[x]) * np.multiply(v, v) 96 | interaction = 0.5 * np.sum(np.multiply(inter_1, inter_1) - inter_2) 97 | p = w0 + dataMatrix[x] * w + interaction 98 | pre = sigmoid(p[0, 0]) 99 | result.append(pre) 100 | return result 101 | 102 | 103 | # 计算准确度 104 | def getaccuracy(predict, classLabels): 105 | m = np.shape(predict)[0] 106 | allItem = 0 107 | error = 0 108 | for i in range(m): 109 | allItem += 1 110 | if float(predict[i]) < 0.5 and classLabels[i] == 1.0: 111 | error += 1 112 | elif float(predict[i]) >= 0.5 and classLabels[i] == -1.0: 113 | error += 1 114 | else: 115 | continue 116 | return float(error)/allItem 117 | 118 | 119 | # 保存模型的参数 120 | def save_model(filename, w0, w, v): 121 | f = open(filename, 'w') 122 | f.write(str(w0)+'\n') 123 | w_array = [] 124 | m = np.shape(w)[0] 125 | for i in range(m): 126 | w_array.append(str(w[i, 0])) 127 | f.write('\t'.join(w_array)+'\n') 128 | m1, n1 = np.shape(v) 129 | for i in range(m1): 130 | v_tmp = [] 131 | for j in range(n1): 132 | v_tmp.append(str(v[i, j])) 133 | f.write('\t'.join(v_tmp)+'\n') 134 | f.close() 135 | 136 | 137 | # 主函数 138 | def main(): 139 | # 第一步:导入数据 140 | feature, label = load_data('train_data.txt') 141 | # print(feature, label) 142 | # 第二步:利用梯度下降训练模型 143 | w0, w, v = stocGradient(np.mat(feature), label, 4, 20001, 0.02) 144 | predict_result = prediction(np.mat(feature), w0, w, v) 145 | print('训练精度为:%f' % (1-getaccuracy(predict_result, label))) 146 | # 第三步保存模型 147 | save_model('weights_FM', w0, w, v) 148 | 149 | if __name__ == '__main__': 150 | start = time.time() 151 | main() 152 | end = time.time() 153 | print('训练模型用时为:%s' % str(end-start)) 154 | -------------------------------------------------------------------------------- /ML/3.Factorization Machine/Train/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-11 13:59 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : __init__.py.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | 10 | def main(): 11 | pass 12 | 13 | 14 | if __name__ == '__main__': 15 | main() 16 | -------------------------------------------------------------------------------- /ML/3.Factorization Machine/Train/train_data.txt: -------------------------------------------------------------------------------- 1 | 8.42383194320181 -0.408530918340288 0 2 | 9.23508197146775 -0.409948204795973 0 3 | 0.580765279496576 -0.279597802215932 0 4 | 7.09767621267017 -0.406498441260137 0 5 | 4.67937536514234 -0.398978513749255 0 6 | 0.943442483964607 -0.390353722860601 0 7 | 3.74734509186577 -0.406970631021985 0 8 | 8.39393102401964 -0.408098172982057 0 9 | 0.959332381553160 -0.327607520319716 0 10 | 0.232976645546809 -0.267919464760288 0 11 | 8.80446107146714 -0.407059057326573 0 12 | 8.59938392690125 -0.409463220043359 0 13 | 8.95457694589616 -0.407156922375112 0 14 | 7.96098805532834 -0.404925626334067 0 15 | 0.323054757630367 -0.277907453214403 0 16 | 5.48175722168676 -0.405673387149798 0 17 | 7.79512012715714 -0.405554237820353 0 18 | 7.08253377287152 -0.403444252353187 0 19 | 6.16060448107641 -0.402178642303065 0 20 | 2.19119512788717 -0.374994207835389 0 21 | 6.39367188624695 -0.407775618172926 0 22 | 7.08725937669242 -0.401984304924651 0 23 | 5.79904363518889 -0.406311564806981 0 24 | 7.59045186538154 -0.404279834086207 0 25 | 3.43767463555210 -0.399625151711070 0 26 | 2.87283978693540 -0.383673362704480 0 27 | 8.39831970761900 -0.409923888575123 0 28 | 6.14553209617717 -0.398027016959763 0 29 | 0.640956937606998 -0.369389256091614 0 30 | 2.29934679549623 -0.390199749245145 0 31 | 8.44224693484357 -0.408805330232292 0 32 | 8.99502222039451 -0.407918113927482 0 33 | 8.83624008312127 -0.406908558560670 0 34 | 4.92895815278608 -0.400438153125454 0 35 | 9.48949088303997 -0.409507907710384 0 36 | 4.72531083707912 -0.388488084816166 0 37 | 2.79710704451344 -0.398760341218867 0 38 | 7.50798039275919 -0.402176743887395 0 39 | 8.48456070568963 -0.409573472614389 0 40 | 6.76039013462219 -0.399155829461459 0 41 | 4.95419059477074 -0.402920870029448 0 42 | 0.368705560882775 -0.295255777879522 0 43 | 3.16444939510907 -0.374571618600867 0 44 | 4.59626051785997 -0.407894572189662 0 45 | 0.490153434008910 -0.308314164887461 0 46 | 8.15134490163779 -0.409410678288747 0 47 | 7.90275758229961 -0.403723870611547 0 48 | 0.792283779528635 -0.400626399527537 0 49 | 5.14139837739626 -0.389768236579979 0 50 | 7.90627365243289 -0.408257660707080 0 51 | 0.953997866754101 -0.387163744616786 0 52 | 7.07159301485602 -0.403993824841355 0 53 | 9.06135640037553 -0.407768984132258 0 54 | 4.60341758246115 -0.397916532966483 0 55 | 6.14049890866498 -0.396185699910743 0 56 | 0.215329214059079 -0.258898195179039 0 57 | 5.55299812527370 -0.402812693630881 0 58 | 7.84043390068847 -0.409698820787648 0 59 | 1.94315237951002 -0.373588761269258 0 60 | 9.11121088568475 -0.408592842513344 0 61 | 2.98012686704141 -0.376214571346192 0 62 | 8.31904174333194 -0.408494949263143 0 63 | 0.0617846529338062 -0.340868672885575 0 64 | 8.15012626726182 -0.408131221643975 0 65 | 3.34934836292938 -0.408375749162869 0 66 | 5.35346540043212 -0.401516989385279 0 67 | 1.15862753436787 -0.362051199835335 0 68 | 8.14973224773878 -0.407970422188632 0 69 | 1.30092154826364 -0.367497518923388 0 70 | 9.51082762250197 -0.408822030204151 0 71 | 4.36731432815911 -0.403937404065732 0 72 | 1.63639911741812 -0.386052118856741 0 73 | 4.34680455530927 -0.404963242319624 0 74 | 5.54411463581684 -0.406131856090851 0 75 | 2.67898058380966 -0.386029299057639 0 76 | 6.49212676676911 -0.399635959788227 0 77 | 1.98095766638061 -0.353260659977698 0 78 | 9.85857911401332 -0.409731715702965 0 79 | 3.72624721185135 -0.391894215651133 0 80 | 3.04418081530650 -0.397177782029156 0 81 | 4.70877424186155 -0.400804752644355 0 82 | 2.08279213037150 -0.370806730680289 0 83 | 1.71073454775069 -0.378354524558988 0 84 | 2.76764193110911 -0.365288535360287 0 85 | 2.60636248760882 -0.401476620424844 0 86 | 7.52960877987452 -0.404993039099243 0 87 | 5.99643601412483 -0.399874324624253 0 88 | 4.36479759540816 -0.389384922905883 0 89 | 4.32073210555306 -0.382780916980243 0 90 | 0.787285208315720 -0.331534381633025 0 91 | 2.08438543917472 -0.393467876583496 0 92 | 4.98213558688768 -0.389168459685345 0 93 | 3.90159130065528 -0.385771872682800 0 94 | 7.55175246633567 -0.409057725558823 0 95 | 8.68762117721727 -0.407081041092895 0 96 | 4.45476757159167 -0.383674139566138 0 97 | 7.76549896072094 -0.405838601725101 0 98 | 6.23329124826219 -0.409628510931385 0 99 | 3.79021180613286 -0.399693570093771 0 100 | 4.52309238928843 -0.395367919485949 0 101 | 2.43758468710462 -0.309939998418769 1 102 | 6.44418448019331 -0.374723936417501 1 103 | 0.108671481485529 -0.199476534945679 1 104 | 3.23472132602922 -0.293911269333190 1 105 | 2.44178385018835 -0.275101689318301 1 106 | 0.481016991996002 -0.192982794728889 1 107 | 0.370857692202135 -0.211953271451565 1 108 | 5.23724591999750 -0.354020696656430 1 109 | 2.31740055767812 -0.277176863084907 1 110 | 4.24719365238813 -0.228720604973186 1 111 | 8.52967025477565 -0.200621670458921 1 112 | 1.18233156697587 -0.274076903571856 1 113 | 1.09122823571812 -0.243999645215050 1 114 | 2.02173302580917 -0.285987222107165 1 115 | 3.40157958868141 -0.252717387598389 1 116 | 6.56671655335725 -0.193815695710377 1 117 | 4.23709249848205 -0.218142740805575 1 118 | 1.89470124377699 -0.269331811708736 1 119 | 5.62107145721497 -0.197866145768924 1 120 | 0.442181978566363 -0.240539910325545 1 121 | 9.08335733334861 -0.250443920103596 1 122 | 8.54289949149346 -0.234576108061670 1 123 | 9.58048914374455 -0.312797618264376 1 124 | 4.21071321246505 -0.316157102407788 1 125 | 4.97486886565608 -0.365878657382866 1 126 | 8.78768695109643 -0.321746252520346 1 127 | 1.94010035906808 -0.284504821876978 1 128 | 9.50071876810555 -0.261254625802206 1 129 | 2.79092290897387 -0.294736667059161 1 130 | 4.42835500663540 -0.208733341730909 1 131 | 3.25337111859363 -0.289340840488577 1 132 | 7.75480499711548 -0.256121320230722 1 133 | 0.0601067147854828 -0.198420599896546 1 134 | 2.49753304116731 -0.297363143424683 1 135 | 1.12461072377156 -0.240865075131135 1 136 | 4.71228257009732 -0.271363789011752 1 137 | 9.34252613513342 -0.375946031143510 1 138 | 0.843081122373650 -0.206798239427842 1 139 | 5.09326655603538 -0.298924436083304 1 140 | 1.18717487407077 -0.225764713465440 1 141 | 2.60896221021649 -0.260828875403664 1 142 | 0.340567170451632 -0.191643171111526 1 143 | 9.32339305356395 -0.284591245201535 1 144 | 6.27445499304504 -0.218749932355235 1 145 | 8.75443656218165 -0.375860128116557 1 146 | 0.712694646768266 -0.251211368306295 1 147 | 5.41638852433666 -0.272060884009967 1 148 | 2.61870887856769 -0.306927321245432 1 149 | 7.36200224757743 -0.303688359058616 1 150 | 5.15489836245557 -0.241798486731131 1 151 | 9.91470220969572 -0.358627726808887 1 152 | 4.74387896326763 -0.281474800994315 1 153 | 4.23914505530651 -0.277643748743069 1 154 | 9.94279588232450 -0.373595713542861 1 155 | 0.138071308907622 -0.190271171365226 1 156 | 9.50180527233723 -0.232862772106994 1 157 | 0.742582446482145 -0.235530788445218 1 158 | 3.59237014923007 -0.192175481878045 1 159 | 9.39045198333577 -0.385328069458975 1 160 | 4.73261365346132 -0.207880132356065 1 161 | 1.43665183723223 -0.276445932905554 1 162 | 1.31465794513618 -0.222524617911456 1 163 | 3.30046965707428 -0.333016613374630 1 164 | 2.21316635113260 -0.210812844878435 1 165 | 7.88116832470351 -0.347570517509302 1 166 | 3.94844968752540 -0.314266793088863 1 167 | 5.98768064837655 -0.251301389475543 1 168 | 0.764400320140725 -0.238386704654004 1 169 | 2.31562057548408 -0.197609507341517 1 170 | 3.59415897605416 -0.306009583621716 1 171 | 0.959444038741402 -0.222606385961562 1 172 | 4.96117878536822 -0.320184560589757 1 173 | 7.32869253873543 -0.199698227443838 1 174 | 9.65718624060435 -0.224157687761801 1 175 | 1.89880655369980 -0.218112784562061 1 176 | 4.59724528984552 -0.342520285993113 1 177 | 3.83115222907876 -0.345264503383366 1 178 | 9.10505674005844 -0.194858902510535 1 179 | 6.98077467036255 -0.333072899621645 1 180 | 6.75437899881917 -0.361705241427897 1 181 | 9.68358853481333 -0.256771347021000 1 182 | 1.13187673107476 -0.282679813491173 1 183 | 0.358556369186400 -0.209628835794929 1 184 | 8.39618590956441 -0.235250064272360 1 185 | 9.99619620484049 -0.268975031963082 1 186 | 3.67237049360799 -0.351774945120537 1 187 | 2.35445411876548 -0.235625809877974 1 188 | 4.29008640794217 -0.332959541281784 1 189 | 5.42742747484534 -0.264438465444690 1 190 | 8.11606658953466 -0.292581532775660 1 191 | 9.18469883425862 -0.217003179402255 1 192 | 5.21837068366336 -0.274530530316992 1 193 | 6.79585182614347 -0.250119897154528 1 194 | 1.89061726771386 -0.258709988718971 1 195 | 6.16955623109348 -0.203747812388698 1 196 | 1.66149183322044 -0.206700226963393 1 197 | 2.74262556141998 -0.254027266166219 1 198 | 1.24804345641085 -0.241408809531456 1 199 | 0.808431917613837 -0.242017996831424 1 200 | 6.74094601972670 -0.373633464603415 1 201 | -------------------------------------------------------------------------------- /ML/3.Factorization Machine/Train/weights_FM: -------------------------------------------------------------------------------- 1 | 21.660687655417806 2 | 1.2649489467261146 75.27079468117786 3 | -0.0 -0.0 -0.0 -0.0 4 | 0.0 0.0 0.0 0.0 5 | -------------------------------------------------------------------------------- /ML/4.SVM-SMO/SVM_Train.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-15 18:58 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : SVM_Train.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import numpy as np 10 | import _pickle as pickle 11 | 12 | 13 | class SVM: 14 | def __init__(self, dataSet, labels, C, toler, kernel_option): 15 | self.train_x = dataSet 16 | self.train_y = labels 17 | self.C = C 18 | self.toler = toler 19 | self.n_samples = np.shape(dataSet)[0] 20 | self.alphas = np.mat(np.zeros((self.n_samples, 1))) 21 | self.b = 0 22 | self.error_tmp = np.mat(np.zeros((self.n_samples, 2))) 23 | self.kernel_opt = kernel_option 24 | self.kernel_mat = calc_kernel(self.train_x, self.kernel_opt) 25 | 26 | 27 | # 核函数矩阵 28 | def calc_kernel(train_x, kernel_option): 29 | m = np.shape(train_x)[0] 30 | kernel_matrix = np.mat(np.zeros((m, m))) 31 | for i in range(m): 32 | kernel_matrix[:, i] = cal_kernel_value(train_x, train_x[i, :], kernel_option) 33 | return kernel_matrix 34 | 35 | 36 | # 定义样本之间的核函数的值 37 | def cal_kernel_value(train_x, train_x_i, kernel_option): 38 | kernel_type = kernel_option[0] 39 | m = np.shape(train_x)[0] 40 | kernel_value = np.mat(np.zeros((m, 1))) 41 | if kernel_type == 'rbf': 42 | sigma = kernel_option[1] 43 | if sigma == 0: 44 | sigma = 1.0 45 | for i in range(m): 46 | diff = train_x[i, :] - train_x_i 47 | kernel_value[i] = np.exp(diff * diff.T/(-2.0 * sigma**2)) 48 | else: 49 | kernel_value = train_x * train_x_i.T 50 | return kernel_value 51 | 52 | 53 | def SVM_training(train_x, train_y, C, toler, max_iter, kernel_option=('rbf', 0.431029)): 54 | # 1.初始化 SVM 分类器 55 | svm = SVM(train_x, train_y, C, toler, kernel_option) 56 | # 2.开始训练 SVM 分类器 57 | entireSet = True 58 | alpha_pairs_changed = 0 59 | iteration = 0 60 | while (iteration < max_iter) and((alpha_pairs_changed > 0) or entireSet): 61 | print('\t 迭代次数为:', iteration) 62 | alpha_pairs_changed = 0 63 | if entireSet: 64 | for x in range(svm.n_samples): 65 | alpha_pairs_changed += choose_and_update(svm, x) 66 | iteration += 1 67 | else: 68 | bound_samples = [] 69 | for i in range(svm.n_samples): 70 | if 0 < svm.alphas[i, 0] < svm.C: 71 | bound_samples.append(i) 72 | for x in bound_samples: 73 | alpha_pairs_changed += choose_and_update(svm, x) 74 | iteration += 1 75 | if entireSet: 76 | entireSet = False 77 | elif alpha_pairs_changed == 0: 78 | entireSet = True 79 | return svm 80 | 81 | 82 | # 选择并更新参数 83 | def choose_and_update(svm, alpha_i): 84 | # 计算第一个样本的误差error_i 85 | error_i = cal_error(svm, alpha_i) 86 | 87 | # 判断选择出的第一个变量是否违反了 KKT 条件 88 | if (svm.train_y[alpha_i]*error_i < -svm.toler) and (svm.alphas[alpha_i] < svm.C) or (svm.train_y[alpha_i]*error_i > 89 | svm.toler) and (svm.alphas[alpha_i] > 0): 90 | # 1.选择第二个变量 91 | alpha_j, error_j = select_second_sample_j(svm, alpha_i, error_i) 92 | alpha_i_old = svm.alphas[alpha_i].copy() 93 | alpha_j_old = svm.alphas[alpha_j].copy() 94 | 95 | # 2.计算上下界 96 | if svm.train_y[alpha_i] != svm.train_y[alpha_j]: 97 | L = max(0, svm.alphas[alpha_j] - svm.alphas[alpha_i]) 98 | H = min(svm.C, svm.C + svm.alphas[alpha_j] - svm.alphas[alpha_i]) 99 | else: 100 | L = max(0, svm.alphas[alpha_j] + svm.alphas[alpha_i] - svm.C) 101 | H = min(svm.C, svm.alphas[alpha_j] + svm.alphas[alpha_i]) 102 | if L == H: 103 | return 0 104 | 105 | # 3.计算 eta 106 | eta = 2.0 * svm.kernel_mat[alpha_i, alpha_j] - svm.kernel_mat[alpha_i, alpha_i] - svm.kernel_mat[alpha_j, alpha_j] 107 | if eta > 0: 108 | return 0 109 | 110 | # 4.更新 alpha_j 111 | svm.alphas[alpha_j] -= svm.train_y[alpha_j] * (error_i - error_j)/eta 112 | 113 | # 5.确定最终的 alpha_j 114 | if svm.alphas[alpha_j] > H: 115 | svm.alphas[alpha_j] = H 116 | if svm.alphas[alpha_j] < L: 117 | svm.alphas[alpha_j] = L 118 | 119 | # 6. 判断是否结束 120 | if abs(alpha_j_old - svm.alphas[alpha_j]) < 0.00001: 121 | update_error_tmp(svm, alpha_j) 122 | return 0 123 | 124 | # 7.更新 alpha_i 125 | svm.alphas[alpha_i] += svm.train_y[alpha_i] * svm.train_y[alpha_j] * (alpha_j_old - svm.alphas[alpha_j]) 126 | 127 | # 8.更新 b 128 | b1 = svm.b - error_i - svm.train_y[alpha_i]*(svm.alphas[alpha_i]-alpha_i_old)*svm.kernel_mat[alpha_i, alpha_i]\ 129 | -svm.train_y[alpha_j]*(svm.alphas[alpha_j]-alpha_j_old)*svm.kernel_mat[alpha_i, alpha_j] 130 | b2 = svm.b - error_j - svm.train_y[alpha_i]*(svm.alphas[alpha_i]-alpha_i_old)*svm.kernel_mat[alpha_i, alpha_j]\ 131 | -svm.train_y[alpha_j]*(svm.alphas[alpha_j]-alpha_j_old)*svm.kernel_mat[alpha_j, alpha_j] 132 | if 0 < svm.alphas[alpha_i] < svm.C: 133 | svm.b = b1 134 | elif 0 < svm.alphas[alpha_j] < svm.C: 135 | svm.b = b2 136 | else: 137 | svm.b = (b1 + b2)/2.0 138 | 139 | # 9.更新 error 140 | update_error_tmp(svm, alpha_j) 141 | update_error_tmp(svm, alpha_i) 142 | return 1 143 | else: 144 | return 0 145 | 146 | 147 | # 计算误差 148 | def cal_error(svm, alpha_k): 149 | output_k = float(np.multiply(svm.alphas, svm.train_y).T * svm.kernel_mat[:, alpha_k]+svm.b) 150 | error_k = output_k - float(svm.train_y[alpha_k]) 151 | return error_k 152 | 153 | 154 | # 选择第二个变量 155 | def select_second_sample_j(svm, alpha_i, error_i): 156 | svm.error_tmp[alpha_i] = [1, error_i] 157 | candidateAlphaList = np.nonzero(svm.error_tmp[:, 0].A)[0] 158 | maxStep = 0 159 | alpha_j = 0 160 | error_j = 0 161 | 162 | if len(candidateAlphaList) > 1: 163 | for alpha_k in candidateAlphaList: 164 | if alpha_k == alpha_i: 165 | continue 166 | error_k = cal_error(svm, alpha_k) 167 | if abs(error_k-error_i) > maxStep: 168 | maxStep = abs(error_k - error_i) 169 | alpha_j = alpha_k 170 | error_j = error_k 171 | else: 172 | alpha_j = alpha_i 173 | while alpha_j == alpha_i: 174 | alpha_j = int(np.random.uniform(0, svm.n_samples)) 175 | error_j = cal_error(svm, alpha_j) 176 | return alpha_j, error_j 177 | 178 | 179 | # 重新计算误差值 180 | def update_error_tmp(svm, alpha_k): 181 | error = cal_error(svm, alpha_k) 182 | svm.error_tmp[alpha_k] = [1, error] 183 | 184 | 185 | def load_data_libsvm(filename): 186 | df = open(filename) 187 | features = [] 188 | labels = [] 189 | for line in df.readlines(): 190 | lines = line.strip().split(' ') # lines = ['+1', '1:0.708333', '2:1', '3:1', '4:-0.320755', '5:-0.105023', '6:-1', '7:1', '8:-0.419847', '9:-1', '10:-0.225806', '12:1', '13:-1'] 191 | # print(lines[1][1]) 192 | labels.append(float(lines[0])) 193 | index = 0 194 | tmp = [] 195 | for i in range(1, len(lines)): 196 | x = lines[i].strip().split(':') 197 | # print(x[1]) 198 | if int(x[0])-1 == index: 199 | tmp.append(float(x[1])) 200 | else: 201 | while int(x[0])-1 > index: 202 | tmp.append(0) 203 | index += 1 204 | tmp.append(float(x[1])) 205 | index += 1 206 | while len(tmp) > 13: 207 | tmp.append(0) 208 | features.append(tmp) 209 | df.close() 210 | return np.mat(features), np.mat(labels).T 211 | 212 | 213 | def cal_accuracy(svm, test_x, test_y): 214 | n_samples = np.shape(test_x)[0] 215 | correct = 0.0 216 | for i in range(n_samples): 217 | predict = svm_predict(svm, test_x[i, :]) 218 | if np.sign(predict) == np.sign(test_y[i]): 219 | correct += 1 220 | accuracy = correct / n_samples 221 | return accuracy 222 | 223 | 224 | def svm_predict(svm, test_sample_x): 225 | kernel_value = cal_kernel_value(svm.train_x, test_sample_x, svm.kernel_opt) 226 | predict = kernel_value.T * np.multiply(svm.train_y, svm.alphas) + svm.b 227 | return predict 228 | 229 | 230 | # 保存模型 231 | def save_model(svm_model, model_file): 232 | with open(model_file, 'wb') as file: 233 | pickle.dump(svm_model, file) 234 | 235 | 236 | def main(): 237 | # 1.导入数据集 238 | print('********* 导入数据集 **********') 239 | train_data, label_data = load_data_libsvm('heart_scale') 240 | print(train_data.shape, label_data.shape) 241 | # 2.训练 SVM 模型 242 | print('********* 训练 SVM 模型 **********') 243 | C = 0.6 244 | toler = 0.001 245 | maxIter = 500 246 | svm_model = SVM_training(train_data, label_data, C, toler, maxIter) 247 | # 3.计算模型的准确性 248 | print('********* 计算模型的准确性 **********') 249 | accuracy = cal_accuracy(svm_model, train_data, label_data) 250 | print('训练精度为:%.3f%%' % (accuracy*100)) 251 | # 4.保存最终的模型 252 | print('********* 保存最终的模型 **********') 253 | save_model(svm_model, "model_file") 254 | 255 | if __name__ == '__main__': 256 | main() 257 | -------------------------------------------------------------------------------- /ML/4.SVM-SMO/__pycache__/SVM_Train.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/ML/4.SVM-SMO/__pycache__/SVM_Train.cpython-36.pyc -------------------------------------------------------------------------------- /ML/4.SVM-SMO/__pycache__/svm.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/ML/4.SVM-SMO/__pycache__/svm.cpython-36.pyc -------------------------------------------------------------------------------- /ML/4.SVM-SMO/heart_scale: -------------------------------------------------------------------------------- 1 | +1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.419847 9:-1 10:-0.225806 12:1 13:-1 2 | -1 1:0.583333 2:-1 3:0.333333 4:-0.603774 5:1 6:-1 7:1 8:0.358779 9:-1 10:-0.483871 12:-1 13:1 3 | +1 1:0.166667 2:1 3:-0.333333 4:-0.433962 5:-0.383562 6:-1 7:-1 8:0.0687023 9:-1 10:-0.903226 11:-1 12:-1 13:1 4 | -1 1:0.458333 2:1 3:1 4:-0.358491 5:-0.374429 6:-1 7:-1 8:-0.480916 9:1 10:-0.935484 12:-0.333333 13:1 5 | -1 1:0.875 2:-1 3:-0.333333 4:-0.509434 5:-0.347032 6:-1 7:1 8:-0.236641 9:1 10:-0.935484 11:-1 12:-0.333333 13:-1 6 | -1 1:0.5 2:1 3:1 4:-0.509434 5:-0.767123 6:-1 7:-1 8:0.0534351 9:-1 10:-0.870968 11:-1 12:-1 13:1 7 | +1 1:0.125 2:1 3:0.333333 4:-0.320755 5:-0.406393 6:1 7:1 8:0.0839695 9:1 10:-0.806452 12:-0.333333 13:0.5 8 | +1 1:0.25 2:1 3:1 4:-0.698113 5:-0.484018 6:-1 7:1 8:0.0839695 9:1 10:-0.612903 12:-0.333333 13:1 9 | +1 1:0.291667 2:1 3:1 4:-0.132075 5:-0.237443 6:-1 7:1 8:0.51145 9:-1 10:-0.612903 12:0.333333 13:1 10 | +1 1:0.416667 2:-1 3:1 4:0.0566038 5:0.283105 6:-1 7:1 8:0.267176 9:-1 10:0.290323 12:1 13:1 11 | -1 1:0.25 2:1 3:1 4:-0.226415 5:-0.506849 6:-1 7:-1 8:0.374046 9:-1 10:-0.83871 12:-1 13:1 12 | -1 2:1 3:1 4:-0.0943396 5:-0.543379 6:-1 7:1 8:-0.389313 9:1 10:-1 11:-1 12:-1 13:1 13 | -1 1:-0.375 2:1 3:0.333333 4:-0.132075 5:-0.502283 6:-1 7:1 8:0.664122 9:-1 10:-1 11:-1 12:-1 13:-1 14 | +1 1:0.333333 2:1 3:-1 4:-0.245283 5:-0.506849 6:-1 7:-1 8:0.129771 9:-1 10:-0.16129 12:0.333333 13:-1 15 | -1 1:0.166667 2:-1 3:1 4:-0.358491 5:-0.191781 6:-1 7:1 8:0.343511 9:-1 10:-1 11:-1 12:-0.333333 13:-1 16 | -1 1:0.75 2:-1 3:1 4:-0.660377 5:-0.894977 6:-1 7:-1 8:-0.175573 9:-1 10:-0.483871 12:-1 13:-1 17 | +1 1:-0.291667 2:1 3:1 4:-0.132075 5:-0.155251 6:-1 7:-1 8:-0.251908 9:1 10:-0.419355 12:0.333333 13:1 18 | +1 2:1 3:1 4:-0.132075 5:-0.648402 6:1 7:1 8:0.282443 9:1 11:1 12:-1 13:1 19 | -1 1:0.458333 2:1 3:-1 4:-0.698113 5:-0.611872 6:-1 7:1 8:0.114504 9:1 10:-0.419355 12:-1 13:-1 20 | -1 1:-0.541667 2:1 3:-1 4:-0.132075 5:-0.666667 6:-1 7:-1 8:0.633588 9:1 10:-0.548387 11:-1 12:-1 13:1 21 | +1 1:0.583333 2:1 3:1 4:-0.509434 5:-0.52968 6:-1 7:1 8:-0.114504 9:1 10:-0.16129 12:0.333333 13:1 22 | -1 1:-0.208333 2:1 3:-0.333333 4:-0.320755 5:-0.456621 6:-1 7:1 8:0.664122 9:-1 10:-0.935484 12:-1 13:-1 23 | -1 1:-0.416667 2:1 3:1 4:-0.603774 5:-0.191781 6:-1 7:-1 8:0.679389 9:-1 10:-0.612903 12:-1 13:-1 24 | -1 1:-0.25 2:1 3:1 4:-0.660377 5:-0.643836 6:-1 7:-1 8:0.0992366 9:-1 10:-0.967742 11:-1 12:-1 13:-1 25 | -1 1:0.0416667 2:-1 3:-0.333333 4:-0.283019 5:-0.260274 6:1 7:1 8:0.343511 9:1 10:-1 11:-1 12:-0.333333 13:-1 26 | -1 1:-0.208333 2:-1 3:0.333333 4:-0.320755 5:-0.319635 6:-1 7:-1 8:0.0381679 9:-1 10:-0.935484 11:-1 12:-1 13:-1 27 | -1 1:-0.291667 2:-1 3:1 4:-0.169811 5:-0.465753 6:-1 7:1 8:0.236641 9:1 10:-1 12:-1 13:-1 28 | -1 1:-0.0833333 2:-1 3:0.333333 4:-0.509434 5:-0.228311 6:-1 7:1 8:0.312977 9:-1 10:-0.806452 11:-1 12:-1 13:-1 29 | +1 1:0.208333 2:1 3:0.333333 4:-0.660377 5:-0.525114 6:-1 7:1 8:0.435115 9:-1 10:-0.193548 12:-0.333333 13:1 30 | -1 1:0.75 2:-1 3:0.333333 4:-0.698113 5:-0.365297 6:1 7:1 8:-0.0992366 9:-1 10:-1 11:-1 12:-0.333333 13:-1 31 | +1 1:0.166667 2:1 3:0.333333 4:-0.358491 5:-0.52968 6:-1 7:1 8:0.206107 9:-1 10:-0.870968 12:-0.333333 13:1 32 | -1 1:0.541667 2:1 3:1 4:0.245283 5:-0.534247 6:-1 7:1 8:0.0229008 9:-1 10:-0.258065 11:-1 12:-1 13:0.5 33 | -1 1:-0.666667 2:-1 3:0.333333 4:-0.509434 5:-0.593607 6:-1 7:-1 8:0.51145 9:-1 10:-1 11:-1 12:-1 13:-1 34 | +1 1:0.25 2:1 3:1 4:0.433962 5:-0.086758 6:-1 7:1 8:0.0534351 9:1 10:0.0967742 11:1 12:-1 13:1 35 | +1 1:-0.125 2:1 3:1 4:-0.0566038 5:-0.6621 6:-1 7:1 8:-0.160305 9:1 10:-0.709677 12:-1 13:1 36 | +1 1:-0.208333 2:1 3:1 4:-0.320755 5:-0.406393 6:1 7:1 8:0.206107 9:1 10:-1 11:-1 12:0.333333 13:1 37 | +1 1:0.333333 2:1 3:1 4:-0.132075 5:-0.630137 6:-1 7:1 8:0.0229008 9:1 10:-0.387097 11:-1 12:-0.333333 13:1 38 | +1 1:0.25 2:1 3:-1 4:0.245283 5:-0.328767 6:-1 7:1 8:-0.175573 9:-1 10:-1 11:-1 12:-1 13:-1 39 | -1 1:-0.458333 2:1 3:0.333333 4:-0.320755 5:-0.753425 6:-1 7:-1 8:0.206107 9:-1 10:-1 11:-1 12:-1 13:-1 40 | -1 1:-0.208333 2:1 3:1 4:-0.471698 5:-0.561644 6:-1 7:1 8:0.755725 9:-1 10:-1 11:-1 12:-1 13:-1 41 | +1 1:-0.541667 2:1 3:1 4:0.0943396 5:-0.557078 6:-1 7:-1 8:0.679389 9:-1 10:-1 11:-1 12:-1 13:1 42 | -1 1:0.375 2:-1 3:1 4:-0.433962 5:-0.621005 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-1 13:-1 43 | -1 1:-0.375 2:1 3:0.333333 4:-0.320755 5:-0.511416 6:-1 7:-1 8:0.648855 9:1 10:-0.870968 11:-1 12:-1 13:-1 44 | -1 1:-0.291667 2:1 3:-0.333333 4:-0.867925 5:-0.675799 6:1 7:-1 8:0.29771 9:-1 10:-1 11:-1 12:-1 13:1 45 | +1 1:0.25 2:1 3:0.333333 4:-0.396226 5:-0.579909 6:1 7:-1 8:-0.0381679 9:-1 10:-0.290323 12:-0.333333 13:0.5 46 | -1 1:0.208333 2:1 3:0.333333 4:-0.132075 5:-0.611872 6:1 7:1 8:0.435115 9:-1 10:-1 11:-1 12:-1 13:-1 47 | +1 1:-0.166667 2:1 3:0.333333 4:-0.54717 5:-0.894977 6:-1 7:1 8:-0.160305 9:-1 10:-0.741935 11:-1 12:1 13:-1 48 | +1 1:-0.375 2:1 3:1 4:-0.698113 5:-0.675799 6:-1 7:1 8:0.618321 9:-1 10:-1 11:-1 12:-0.333333 13:-1 49 | +1 1:0.541667 2:1 3:-0.333333 4:0.245283 5:-0.452055 6:-1 7:-1 8:-0.251908 9:1 10:-1 12:1 13:0.5 50 | +1 1:0.5 2:-1 3:1 4:0.0566038 5:-0.547945 6:-1 7:1 8:-0.343511 9:-1 10:-0.677419 12:1 13:1 51 | +1 1:-0.458333 2:1 3:1 4:-0.207547 5:-0.136986 6:-1 7:-1 8:-0.175573 9:1 10:-0.419355 12:-1 13:0.5 52 | -1 1:-0.0416667 2:1 3:-0.333333 4:-0.358491 5:-0.639269 6:1 7:-1 8:0.725191 9:-1 10:-1 11:-1 12:-1 13:-1 53 | -1 1:0.5 2:-1 3:0.333333 4:-0.132075 5:0.328767 6:1 7:1 8:0.312977 9:-1 10:-0.741935 11:-1 12:-0.333333 13:-1 54 | -1 1:0.416667 2:-1 3:-0.333333 4:-0.132075 5:-0.684932 6:-1 7:-1 8:0.648855 9:-1 10:-1 11:-1 12:0.333333 13:-1 55 | -1 1:-0.333333 2:-1 3:-0.333333 4:-0.320755 5:-0.506849 6:-1 7:1 8:0.587786 9:-1 10:-0.806452 12:-1 13:-1 56 | -1 1:-0.5 2:-1 3:-0.333333 4:-0.792453 5:-0.671233 6:-1 7:-1 8:0.480916 9:-1 10:-1 11:-1 12:-0.333333 13:-1 57 | +1 1:0.333333 2:1 3:1 4:-0.169811 5:-0.817352 6:-1 7:1 8:-0.175573 9:1 10:0.16129 12:-0.333333 13:-1 58 | -1 1:0.291667 2:-1 3:0.333333 4:-0.509434 5:-0.762557 6:1 7:-1 8:-0.618321 9:-1 10:-1 11:-1 12:-1 13:-1 59 | +1 1:0.25 2:-1 3:1 4:0.509434 5:-0.438356 6:-1 7:-1 8:0.0992366 9:1 10:-1 12:-1 13:-1 60 | +1 1:0.375 2:1 3:-0.333333 4:-0.509434 5:-0.292237 6:-1 7:1 8:-0.51145 9:-1 10:-0.548387 12:-0.333333 13:1 61 | -1 1:0.166667 2:1 3:0.333333 4:0.0566038 5:-1 6:1 7:-1 8:0.557252 9:-1 10:-0.935484 11:-1 12:-0.333333 13:1 62 | +1 1:-0.0833333 2:-1 3:1 4:-0.320755 5:-0.182648 6:-1 7:-1 8:0.0839695 9:1 10:-0.612903 12:-1 13:1 63 | -1 1:-0.375 2:1 3:0.333333 4:-0.509434 5:-0.543379 6:-1 7:-1 8:0.496183 9:-1 10:-1 11:-1 12:-1 13:-1 64 | -1 1:0.291667 2:-1 3:-1 4:0.0566038 5:-0.479452 6:-1 7:-1 8:0.526718 9:-1 10:-0.709677 11:-1 12:-1 13:-1 65 | -1 1:0.416667 2:1 3:-1 4:-0.0377358 5:-0.511416 6:1 7:1 8:0.206107 9:-1 10:-0.258065 11:1 12:-1 13:0.5 66 | +1 1:0.166667 2:1 3:1 4:0.0566038 5:-0.315068 6:-1 7:1 8:-0.374046 9:1 10:-0.806452 12:-0.333333 13:0.5 67 | -1 1:-0.0833333 2:1 3:1 4:-0.132075 5:-0.383562 6:-1 7:1 8:0.755725 9:1 10:-1 11:-1 12:-1 13:-1 68 | +1 1:0.208333 2:-1 3:-0.333333 4:-0.207547 5:-0.118721 6:1 7:1 8:0.236641 9:-1 10:-1 11:-1 12:0.333333 13:-1 69 | -1 1:-0.375 2:-1 3:0.333333 4:-0.54717 5:-0.47032 6:-1 7:-1 8:0.19084 9:-1 10:-0.903226 12:-0.333333 13:-1 70 | +1 1:-0.25 2:1 3:0.333333 4:-0.735849 5:-0.465753 6:-1 7:-1 8:0.236641 9:-1 10:-1 11:-1 12:-1 13:-1 71 | +1 1:0.333333 2:1 3:1 4:-0.509434 5:-0.388128 6:-1 7:-1 8:0.0534351 9:1 10:0.16129 12:-0.333333 13:1 72 | -1 1:0.166667 2:-1 3:1 4:-0.509434 5:0.0410959 6:-1 7:-1 8:0.40458 9:1 10:-0.806452 11:-1 12:-1 13:-1 73 | -1 1:0.708333 2:1 3:-0.333333 4:0.169811 5:-0.456621 6:-1 7:1 8:0.0992366 9:-1 10:-1 11:-1 12:-1 13:-1 74 | -1 1:0.958333 2:-1 3:0.333333 4:-0.132075 5:-0.675799 6:-1 8:-0.312977 9:-1 10:-0.645161 12:-1 13:-1 75 | -1 1:0.583333 2:-1 3:1 4:-0.773585 5:-0.557078 6:-1 7:-1 8:0.0839695 9:-1 10:-0.903226 11:-1 12:0.333333 13:-1 76 | +1 1:-0.333333 2:1 3:1 4:-0.0943396 5:-0.164384 6:-1 7:1 8:0.160305 9:1 10:-1 12:1 13:1 77 | -1 1:-0.333333 2:1 3:1 4:-0.811321 5:-0.625571 6:-1 7:1 8:0.175573 9:1 10:-0.0322581 12:-1 13:-1 78 | -1 1:-0.583333 2:-1 3:0.333333 4:-1 5:-0.666667 6:-1 7:-1 8:0.648855 9:-1 10:-1 11:-1 12:-1 13:-1 79 | -1 1:-0.458333 2:-1 3:0.333333 4:-0.509434 5:-0.621005 6:-1 7:-1 8:0.557252 9:-1 10:-1 12:-1 13:-1 80 | -1 1:0.125 2:1 3:-0.333333 4:-0.509434 5:-0.497717 6:-1 7:-1 8:0.633588 9:-1 10:-0.741935 11:-1 12:-1 13:-1 81 | +1 1:0.208333 2:1 3:1 4:-0.0188679 5:-0.579909 6:-1 7:-1 8:-0.480916 9:-1 10:-0.354839 12:-0.333333 13:1 82 | +1 1:-0.75 2:1 3:1 4:-0.509434 5:-0.671233 6:-1 7:-1 8:-0.0992366 9:1 10:-0.483871 12:-1 13:1 83 | +1 1:0.208333 2:1 3:1 4:0.0566038 5:-0.342466 6:-1 7:1 8:-0.389313 9:1 10:-0.741935 11:-1 12:-1 13:1 84 | -1 1:-0.5 2:1 3:0.333333 4:-0.320755 5:-0.598174 6:-1 7:1 8:0.480916 9:-1 10:-0.354839 12:-1 13:-1 85 | -1 1:0.166667 2:1 3:1 4:-0.698113 5:-0.657534 6:-1 7:-1 8:-0.160305 9:1 10:-0.516129 12:-1 13:0.5 86 | -1 1:-0.458333 2:1 3:-1 4:0.0188679 5:-0.461187 6:-1 7:1 8:0.633588 9:-1 10:-0.741935 11:-1 12:0.333333 13:-1 87 | -1 1:0.375 2:1 3:-0.333333 4:-0.358491 5:-0.625571 6:1 7:1 8:0.0534351 9:-1 10:-1 11:-1 12:-1 13:-1 88 | -1 1:0.25 2:1 3:-1 4:0.584906 5:-0.342466 6:-1 7:1 8:0.129771 9:-1 10:0.354839 11:1 12:-1 13:1 89 | -1 1:-0.5 2:-1 3:-0.333333 4:-0.396226 5:-0.178082 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-1 13:-1 90 | +1 1:-0.125 2:1 3:1 4:0.0566038 5:-0.465753 6:-1 7:1 8:-0.129771 9:-1 10:-0.16129 12:-1 13:1 91 | -1 1:0.25 2:1 3:-0.333333 4:-0.132075 5:-0.56621 6:-1 7:-1 8:0.419847 9:1 10:-1 11:-1 12:-1 13:-1 92 | +1 1:0.333333 2:-1 3:1 4:-0.320755 5:-0.0684932 6:-1 7:1 8:0.496183 9:-1 10:-1 11:-1 12:-1 13:-1 93 | +1 1:0.0416667 2:1 3:1 4:-0.433962 5:-0.360731 6:-1 7:1 8:-0.419847 9:1 10:-0.290323 12:-0.333333 13:1 94 | +1 1:0.0416667 2:1 3:1 4:-0.698113 5:-0.634703 6:-1 7:1 8:-0.435115 9:1 10:-1 12:-0.333333 13:-1 95 | +1 1:-0.0416667 2:1 3:1 4:-0.415094 5:-0.607306 6:-1 7:-1 8:0.480916 9:-1 10:-0.677419 11:-1 12:0.333333 13:1 96 | +1 1:-0.25 2:1 3:1 4:-0.698113 5:-0.319635 6:-1 7:1 8:-0.282443 9:1 10:-0.677419 12:-0.333333 13:-1 97 | -1 1:0.541667 2:1 3:1 4:-0.509434 5:-0.196347 6:-1 7:1 8:0.221374 9:-1 10:-0.870968 12:-1 13:-1 98 | +1 1:0.208333 2:1 3:1 4:-0.886792 5:-0.506849 6:-1 7:-1 8:0.29771 9:-1 10:-0.967742 11:-1 12:-0.333333 13:1 99 | -1 1:0.458333 2:-1 3:0.333333 4:-0.132075 5:-0.146119 6:-1 7:-1 8:-0.0534351 9:-1 10:-0.935484 11:-1 12:-1 13:1 100 | -1 1:-0.125 2:-1 3:-0.333333 4:-0.509434 5:-0.461187 6:-1 7:-1 8:0.389313 9:-1 10:-0.645161 11:-1 12:-1 13:-1 101 | -1 1:-0.375 2:-1 3:0.333333 4:-0.735849 5:-0.931507 6:-1 7:-1 8:0.587786 9:-1 10:-0.806452 12:-1 13:-1 102 | +1 1:0.583333 2:1 3:1 4:-0.509434 5:-0.493151 6:-1 7:-1 8:-1 9:-1 10:-0.677419 12:-1 13:-1 103 | -1 1:-0.166667 2:-1 3:1 4:-0.320755 5:-0.347032 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-1 13:-1 104 | +1 1:0.166667 2:1 3:1 4:0.339623 5:-0.255708 6:1 7:1 8:-0.19084 9:-1 10:-0.677419 12:1 13:1 105 | +1 1:0.416667 2:1 3:1 4:-0.320755 5:-0.415525 6:-1 7:1 8:0.160305 9:-1 10:-0.548387 12:-0.333333 13:1 106 | +1 1:-0.208333 2:1 3:1 4:-0.433962 5:-0.324201 6:-1 7:1 8:0.450382 9:-1 10:-0.83871 12:-1 13:1 107 | -1 1:-0.0833333 2:1 3:0.333333 4:-0.886792 5:-0.561644 6:-1 7:-1 8:0.0992366 9:1 10:-0.612903 12:-1 13:-1 108 | +1 1:0.291667 2:-1 3:1 4:0.0566038 5:-0.39726 6:-1 7:1 8:0.312977 9:-1 10:-0.16129 12:0.333333 13:1 109 | +1 1:0.25 2:1 3:1 4:-0.132075 5:-0.767123 6:-1 7:-1 8:0.389313 9:1 10:-1 11:-1 12:-0.333333 13:1 110 | -1 1:-0.333333 2:-1 3:-0.333333 4:-0.660377 5:-0.844749 6:-1 7:-1 8:0.0229008 9:-1 10:-1 12:-1 13:-1 111 | +1 1:0.0833333 2:-1 3:1 4:0.622642 5:-0.0821918 6:-1 8:-0.29771 9:1 10:0.0967742 12:-1 13:-1 112 | -1 1:-0.5 2:1 3:-0.333333 4:-0.698113 5:-0.502283 6:-1 7:-1 8:0.251908 9:-1 10:-1 11:-1 12:-1 13:-1 113 | +1 1:0.291667 2:-1 3:1 4:0.207547 5:-0.182648 6:-1 7:1 8:0.374046 9:-1 10:-1 11:-1 12:-1 13:-1 114 | -1 1:0.0416667 2:-1 3:0.333333 4:-0.226415 5:-0.187215 6:1 7:-1 8:0.51145 9:-1 10:-1 11:-1 12:-1 13:-1 115 | -1 1:-0.458333 2:1 3:-0.333333 4:-0.509434 5:-0.228311 6:-1 7:-1 8:0.389313 9:-1 10:-1 11:-1 12:-1 13:-1 116 | -1 1:-0.166667 2:-1 3:-0.333333 4:-0.245283 5:-0.3379 6:-1 7:-1 8:0.389313 9:-1 10:-1 12:-1 13:-1 117 | +1 1:-0.291667 2:1 3:1 4:-0.509434 5:-0.438356 6:-1 7:1 8:0.114504 9:-1 10:-0.741935 11:-1 12:-1 13:1 118 | +1 1:0.125 2:-1 3:1 4:1 5:-0.260274 6:1 7:1 8:-0.0534351 9:1 10:0.290323 11:1 12:0.333333 13:1 119 | -1 1:0.541667 2:-1 3:-1 4:0.0566038 5:-0.543379 6:-1 7:-1 8:-0.343511 9:-1 10:-0.16129 11:1 12:-1 13:-1 120 | +1 1:0.125 2:1 3:1 4:-0.320755 5:-0.283105 6:1 7:1 8:-0.51145 9:1 10:-0.483871 11:1 12:-1 13:1 121 | +1 1:-0.166667 2:1 3:0.333333 4:-0.509434 5:-0.716895 6:-1 7:-1 8:0.0381679 9:-1 10:-0.354839 12:1 13:1 122 | +1 1:0.0416667 2:1 3:1 4:-0.471698 5:-0.269406 6:-1 7:1 8:-0.312977 9:1 10:0.0322581 12:0.333333 13:-1 123 | +1 1:0.166667 2:1 3:1 4:0.0943396 5:-0.324201 6:-1 7:-1 8:-0.740458 9:1 10:-0.612903 12:-0.333333 13:1 124 | -1 1:0.5 2:-1 3:0.333333 4:0.245283 5:0.0684932 6:-1 7:1 8:0.221374 9:-1 10:-0.741935 11:-1 12:-1 13:-1 125 | -1 1:0.0416667 2:1 3:0.333333 4:-0.415094 5:-0.328767 6:-1 7:1 8:0.236641 9:-1 10:-0.83871 11:1 12:-0.333333 13:-1 126 | -1 1:0.0416667 2:-1 3:0.333333 4:0.245283 5:-0.657534 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-0.333333 13:-1 127 | +1 1:0.375 2:1 3:1 4:-0.509434 5:-0.356164 6:-1 7:-1 8:-0.572519 9:1 10:-0.419355 12:0.333333 13:1 128 | -1 1:-0.0416667 2:-1 3:0.333333 4:-0.207547 5:-0.680365 6:-1 7:1 8:0.496183 9:-1 10:-0.967742 12:-1 13:-1 129 | -1 1:-0.0416667 2:1 3:-0.333333 4:-0.245283 5:-0.657534 6:-1 7:-1 8:0.328244 9:-1 10:-0.741935 11:-1 12:-0.333333 13:-1 130 | +1 1:0.291667 2:1 3:1 4:-0.566038 5:-0.525114 6:1 7:-1 8:0.358779 9:1 10:-0.548387 11:-1 12:0.333333 13:1 131 | +1 1:0.416667 2:-1 3:1 4:-0.735849 5:-0.347032 6:-1 7:-1 8:0.496183 9:1 10:-0.419355 12:0.333333 13:-1 132 | +1 1:0.541667 2:1 3:1 4:-0.660377 5:-0.607306 6:-1 7:1 8:-0.0687023 9:1 10:-0.967742 11:-1 12:-0.333333 13:-1 133 | -1 1:-0.458333 2:1 3:1 4:-0.132075 5:-0.543379 6:-1 7:-1 8:0.633588 9:-1 10:-1 11:-1 12:-1 13:-1 134 | +1 1:0.458333 2:1 3:1 4:-0.509434 5:-0.452055 6:-1 7:1 8:-0.618321 9:1 10:-0.290323 11:1 12:-0.333333 13:-1 135 | -1 1:0.0416667 2:1 3:0.333333 4:0.0566038 5:-0.515982 6:-1 7:1 8:0.435115 9:-1 10:-0.483871 11:-1 12:-1 13:1 136 | -1 1:-0.291667 2:-1 3:0.333333 4:-0.0943396 5:-0.767123 6:-1 7:1 8:0.358779 9:1 10:-0.548387 11:1 12:-1 13:-1 137 | -1 1:0.583333 2:-1 3:0.333333 4:0.0943396 5:-0.310502 6:-1 7:-1 8:0.541985 9:-1 10:-1 11:-1 12:-0.333333 13:-1 138 | +1 1:0.125 2:1 3:1 4:-0.415094 5:-0.438356 6:1 7:1 8:0.114504 9:1 10:-0.612903 12:-0.333333 13:-1 139 | -1 1:-0.791667 2:-1 3:-0.333333 4:-0.54717 5:-0.616438 6:-1 7:-1 8:0.847328 9:-1 10:-0.774194 11:-1 12:-1 13:-1 140 | -1 1:0.166667 2:1 3:1 4:-0.283019 5:-0.630137 6:-1 7:-1 8:0.480916 9:1 10:-1 11:-1 12:-1 13:1 141 | +1 1:0.458333 2:1 3:1 4:-0.0377358 5:-0.607306 6:-1 7:1 8:-0.0687023 9:-1 10:-0.354839 12:0.333333 13:0.5 142 | -1 1:0.25 2:1 3:1 4:-0.169811 5:-0.3379 6:-1 7:1 8:0.694656 9:-1 10:-1 11:-1 12:-1 13:-1 143 | +1 1:-0.125 2:1 3:0.333333 4:-0.132075 5:-0.511416 6:-1 7:-1 8:0.40458 9:-1 10:-0.806452 12:-0.333333 13:1 144 | -1 1:-0.0833333 2:1 3:-1 4:-0.415094 5:-0.60274 6:-1 7:1 8:-0.175573 9:1 10:-0.548387 11:-1 12:-0.333333 13:-1 145 | +1 1:0.0416667 2:1 3:-0.333333 4:0.849057 5:-0.283105 6:-1 7:1 8:0.89313 9:-1 10:-1 11:-1 12:-0.333333 13:1 146 | +1 2:1 3:1 4:-0.45283 5:-0.287671 6:-1 7:-1 8:-0.633588 9:1 10:-0.354839 12:0.333333 13:1 147 | +1 1:-0.0416667 2:1 3:1 4:-0.660377 5:-0.525114 6:-1 7:-1 8:0.358779 9:-1 10:-1 11:-1 12:-0.333333 13:-1 148 | +1 1:-0.541667 2:1 3:1 4:-0.698113 5:-0.812785 6:-1 7:1 8:-0.343511 9:1 10:-0.354839 12:-1 13:1 149 | +1 1:0.208333 2:1 3:0.333333 4:-0.283019 5:-0.552511 6:-1 7:1 8:0.557252 9:-1 10:0.0322581 11:-1 12:0.333333 13:1 150 | -1 1:-0.5 2:-1 3:0.333333 4:-0.660377 5:-0.351598 6:-1 7:1 8:0.541985 9:1 10:-1 11:-1 12:-1 13:-1 151 | -1 1:-0.5 2:1 3:0.333333 4:-0.660377 5:-0.43379 6:-1 7:-1 8:0.648855 9:-1 10:-1 11:-1 12:-1 13:-1 152 | -1 1:-0.125 2:-1 3:0.333333 4:-0.509434 5:-0.575342 6:-1 7:-1 8:0.328244 9:-1 10:-0.483871 12:-1 13:-1 153 | -1 1:0.0416667 2:-1 3:0.333333 4:-0.735849 5:-0.356164 6:-1 7:1 8:0.465649 9:-1 10:-1 11:-1 12:-1 13:-1 154 | -1 1:0.458333 2:-1 3:1 4:-0.320755 5:-0.191781 6:-1 7:-1 8:-0.221374 9:-1 10:-0.354839 12:0.333333 13:-1 155 | -1 1:-0.0833333 2:-1 3:0.333333 4:-0.320755 5:-0.406393 6:-1 7:1 8:0.19084 9:-1 10:-0.83871 11:-1 12:-1 13:-1 156 | -1 1:-0.291667 2:-1 3:-0.333333 4:-0.792453 5:-0.643836 6:-1 7:-1 8:0.541985 9:-1 10:-1 11:-1 12:-1 13:-1 157 | +1 1:0.0833333 2:1 3:1 4:-0.132075 5:-0.584475 6:-1 7:-1 8:-0.389313 9:1 10:0.806452 11:1 12:-1 13:1 158 | -1 1:-0.333333 2:1 3:-0.333333 4:-0.358491 5:-0.16895 6:-1 7:1 8:0.51145 9:-1 10:-1 11:-1 12:-1 13:-1 159 | -1 1:0.125 2:1 3:-1 4:-0.509434 5:-0.694064 6:-1 7:1 8:0.389313 9:-1 10:-0.387097 12:-1 13:1 160 | +1 1:0.541667 2:-1 3:1 4:0.584906 5:-0.534247 6:1 7:-1 8:0.435115 9:1 10:-0.677419 12:0.333333 13:1 161 | +1 1:-0.625 2:1 3:-1 4:-0.509434 5:-0.520548 6:-1 7:-1 8:0.694656 9:1 10:0.225806 12:-1 13:1 162 | +1 1:0.375 2:-1 3:1 4:0.0566038 5:-0.461187 6:-1 7:-1 8:0.267176 9:1 10:-0.548387 12:-1 13:-1 163 | -1 1:0.0833333 2:1 3:-0.333333 4:-0.320755 5:-0.378995 6:-1 7:-1 8:0.282443 9:-1 10:-1 11:-1 12:-1 13:-1 164 | +1 1:0.208333 2:1 3:1 4:-0.358491 5:-0.392694 6:-1 7:1 8:-0.0992366 9:1 10:-0.0322581 12:0.333333 13:1 165 | -1 1:-0.416667 2:1 3:1 4:-0.698113 5:-0.611872 6:-1 7:-1 8:0.374046 9:-1 10:-1 11:-1 12:-1 13:1 166 | -1 1:0.458333 2:-1 3:1 4:0.622642 5:-0.0913242 6:-1 7:-1 8:0.267176 9:1 10:-1 11:-1 12:-1 13:-1 167 | -1 1:-0.125 2:-1 3:1 4:-0.698113 5:-0.415525 6:-1 7:1 8:0.343511 9:-1 10:-1 11:-1 12:-1 13:-1 168 | -1 2:1 3:0.333333 4:-0.320755 5:-0.675799 6:1 7:1 8:0.236641 9:-1 10:-0.612903 11:1 12:-1 13:-1 169 | -1 1:-0.333333 2:-1 3:1 4:-0.169811 5:-0.497717 6:-1 7:1 8:0.236641 9:1 10:-0.935484 12:-1 13:-1 170 | +1 1:0.5 2:1 3:-1 4:-0.169811 5:-0.287671 6:1 7:1 8:0.572519 9:-1 10:-0.548387 12:-0.333333 13:-1 171 | -1 1:0.666667 2:1 3:-1 4:0.245283 5:-0.506849 6:1 7:1 8:-0.0839695 9:-1 10:-0.967742 12:-0.333333 13:-1 172 | +1 1:0.666667 2:1 3:0.333333 4:-0.132075 5:-0.415525 6:-1 7:1 8:0.145038 9:-1 10:-0.354839 12:1 13:1 173 | +1 1:0.583333 2:1 3:1 4:-0.886792 5:-0.210046 6:-1 7:1 8:-0.175573 9:1 10:-0.709677 12:0.333333 13:-1 174 | -1 1:0.625 2:-1 3:0.333333 4:-0.509434 5:-0.611872 6:-1 7:1 8:-0.328244 9:-1 10:-0.516129 12:-1 13:-1 175 | -1 1:-0.791667 2:1 3:-1 4:-0.54717 5:-0.744292 6:-1 7:1 8:0.572519 9:-1 10:-1 11:-1 12:-1 13:-1 176 | +1 1:0.375 2:-1 3:1 4:-0.169811 5:-0.232877 6:1 7:-1 8:-0.465649 9:-1 10:-0.387097 12:1 13:-1 177 | +1 1:-0.0833333 2:1 3:1 4:-0.132075 5:-0.214612 6:-1 7:-1 8:-0.221374 9:1 10:0.354839 12:1 13:1 178 | +1 1:-0.291667 2:1 3:0.333333 4:0.0566038 5:-0.520548 6:-1 7:-1 8:0.160305 9:-1 10:0.16129 12:-1 13:-1 179 | +1 1:0.583333 2:1 3:1 4:-0.415094 5:-0.415525 6:1 7:-1 8:0.40458 9:-1 10:-0.935484 12:0.333333 13:1 180 | -1 1:-0.125 2:1 3:0.333333 4:-0.339623 5:-0.680365 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-1 13:-1 181 | -1 1:-0.458333 2:1 3:0.333333 4:-0.509434 5:-0.479452 6:1 7:-1 8:0.877863 9:-1 10:-0.741935 11:1 12:-1 13:1 182 | +1 1:0.125 2:-1 3:1 4:-0.245283 5:0.292237 6:-1 7:1 8:0.206107 9:1 10:-0.387097 12:0.333333 13:1 183 | +1 1:-0.5 2:1 3:1 4:-0.698113 5:-0.789954 6:-1 7:1 8:0.328244 9:-1 10:-1 11:-1 12:-1 13:1 184 | -1 1:-0.458333 2:-1 3:1 4:-0.849057 5:-0.365297 6:-1 7:1 8:-0.221374 9:-1 10:-0.806452 12:-1 13:-1 185 | -1 2:1 3:0.333333 4:-0.320755 5:-0.452055 6:1 7:1 8:0.557252 9:-1 10:-1 11:-1 12:1 13:-1 186 | -1 1:-0.416667 2:1 3:0.333333 4:-0.320755 5:-0.136986 6:-1 7:-1 8:0.389313 9:-1 10:-0.387097 11:-1 12:-0.333333 13:-1 187 | +1 1:0.125 2:1 3:1 4:-0.283019 5:-0.73516 6:-1 7:1 8:-0.480916 9:1 10:-0.322581 12:-0.333333 13:0.5 188 | -1 1:-0.0416667 2:1 3:1 4:-0.735849 5:-0.511416 6:1 7:-1 8:0.160305 9:-1 10:-0.967742 11:-1 12:1 13:1 189 | -1 1:0.375 2:-1 3:1 4:-0.132075 5:0.223744 6:-1 7:1 8:0.312977 9:-1 10:-0.612903 12:-1 13:-1 190 | +1 1:0.708333 2:1 3:0.333333 4:0.245283 5:-0.347032 6:-1 7:-1 8:-0.374046 9:1 10:-0.0645161 12:-0.333333 13:1 191 | -1 1:0.0416667 2:1 3:1 4:-0.132075 5:-0.484018 6:-1 7:-1 8:0.358779 9:-1 10:-0.612903 11:-1 12:-1 13:-1 192 | +1 1:0.708333 2:1 3:1 4:-0.0377358 5:-0.780822 6:-1 7:-1 8:-0.175573 9:1 10:-0.16129 11:1 12:-1 13:1 193 | -1 1:0.0416667 2:1 3:-0.333333 4:-0.735849 5:-0.164384 6:-1 7:-1 8:0.29771 9:-1 10:-1 11:-1 12:-1 13:1 194 | +1 1:-0.75 2:1 3:1 4:-0.396226 5:-0.287671 6:-1 7:1 8:0.29771 9:1 10:-1 11:-1 12:-1 13:1 195 | -1 1:-0.208333 2:1 3:0.333333 4:-0.433962 5:-0.410959 6:1 7:-1 8:0.587786 9:-1 10:-1 11:-1 12:0.333333 13:-1 196 | -1 1:0.0833333 2:-1 3:-0.333333 4:-0.226415 5:-0.43379 6:-1 7:1 8:0.374046 9:-1 10:-0.548387 12:-1 13:-1 197 | -1 1:0.208333 2:-1 3:1 4:-0.886792 5:-0.442922 6:-1 7:1 8:-0.221374 9:-1 10:-0.677419 12:-1 13:-1 198 | -1 1:0.0416667 2:-1 3:0.333333 4:-0.698113 5:-0.598174 6:-1 7:-1 8:0.328244 9:-1 10:-0.483871 12:-1 13:-1 199 | -1 1:0.666667 2:-1 3:-1 4:-0.132075 5:-0.484018 6:-1 7:-1 8:0.221374 9:-1 10:-0.419355 11:-1 12:0.333333 13:-1 200 | +1 1:1 2:1 3:1 4:-0.415094 5:-0.187215 6:-1 7:1 8:0.389313 9:1 10:-1 11:-1 12:1 13:-1 201 | -1 1:0.625 2:1 3:0.333333 4:-0.54717 5:-0.310502 6:-1 7:-1 8:0.221374 9:-1 10:-0.677419 11:-1 12:-0.333333 13:1 202 | +1 1:0.208333 2:1 3:1 4:-0.415094 5:-0.205479 6:-1 7:1 8:0.526718 9:-1 10:-1 11:-1 12:0.333333 13:1 203 | +1 1:0.291667 2:1 3:1 4:-0.415094 5:-0.39726 6:-1 7:1 8:0.0687023 9:1 10:-0.0967742 12:-0.333333 13:1 204 | +1 1:-0.0833333 2:1 3:1 4:-0.132075 5:-0.210046 6:-1 7:-1 8:0.557252 9:1 10:-0.483871 11:-1 12:-1 13:1 205 | +1 1:0.0833333 2:1 3:1 4:0.245283 5:-0.255708 6:-1 7:1 8:0.129771 9:1 10:-0.741935 12:-0.333333 13:1 206 | -1 1:-0.0416667 2:1 3:-1 4:0.0943396 5:-0.214612 6:1 7:-1 8:0.633588 9:-1 10:-0.612903 12:-1 13:1 207 | -1 1:0.291667 2:-1 3:0.333333 4:-0.849057 5:-0.123288 6:-1 7:-1 8:0.358779 9:-1 10:-1 11:-1 12:-0.333333 13:-1 208 | -1 1:0.208333 2:1 3:0.333333 4:-0.792453 5:-0.479452 6:-1 7:1 8:0.267176 9:1 10:-0.806452 12:-1 13:1 209 | +1 1:0.458333 2:1 3:0.333333 4:-0.415094 5:-0.164384 6:-1 7:-1 8:-0.0839695 9:1 10:-0.419355 12:-1 13:1 210 | -1 1:-0.666667 2:1 3:0.333333 4:-0.320755 5:-0.43379 6:-1 7:-1 8:0.770992 9:-1 10:0.129032 11:1 12:-1 13:-1 211 | +1 1:0.25 2:1 3:-1 4:0.433962 5:-0.260274 6:-1 7:1 8:0.343511 9:-1 10:-0.935484 12:-1 13:1 212 | -1 1:-0.0833333 2:1 3:0.333333 4:-0.415094 5:-0.456621 6:1 7:1 8:0.450382 9:-1 10:-0.225806 12:-1 13:-1 213 | -1 1:-0.416667 2:-1 3:0.333333 4:-0.471698 5:-0.60274 6:-1 7:-1 8:0.435115 9:-1 10:-0.935484 12:-1 13:-1 214 | +1 1:0.208333 2:1 3:1 4:-0.358491 5:-0.589041 6:-1 7:1 8:-0.0839695 9:1 10:-0.290323 12:1 13:1 215 | -1 1:-1 2:1 3:-0.333333 4:-0.320755 5:-0.643836 6:-1 7:1 8:1 9:-1 10:-1 11:-1 12:-1 13:-1 216 | -1 1:-0.5 2:-1 3:-0.333333 4:-0.320755 5:-0.643836 6:-1 7:1 8:0.541985 9:-1 10:-0.548387 11:-1 12:-1 13:-1 217 | -1 1:0.416667 2:-1 3:0.333333 4:-0.226415 5:-0.424658 6:-1 7:1 8:0.541985 9:-1 10:-1 11:-1 12:-1 13:-1 218 | -1 1:-0.0833333 2:1 3:0.333333 4:-1 5:-0.538813 6:-1 7:-1 8:0.267176 9:1 10:-1 11:-1 12:-0.333333 13:1 219 | -1 1:0.0416667 2:1 3:0.333333 4:-0.509434 5:-0.39726 6:-1 7:1 8:0.160305 9:-1 10:-0.870968 12:-1 13:1 220 | -1 1:-0.375 2:1 3:-0.333333 4:-0.509434 5:-0.570776 6:-1 7:-1 8:0.51145 9:-1 10:-1 11:-1 12:-1 13:-1 221 | +1 1:0.0416667 2:1 3:1 4:-0.698113 5:-0.484018 6:-1 7:-1 8:-0.160305 9:1 10:-0.0967742 12:-0.333333 13:1 222 | +1 1:0.5 2:1 3:1 4:-0.226415 5:-0.415525 6:-1 7:1 8:-0.145038 9:-1 10:-0.0967742 12:-0.333333 13:1 223 | -1 1:0.166667 2:1 3:0.333333 4:0.0566038 5:-0.808219 6:-1 7:-1 8:0.572519 9:-1 10:-0.483871 11:-1 12:-1 13:-1 224 | +1 1:0.416667 2:1 3:1 4:-0.320755 5:-0.0684932 6:1 7:1 8:-0.0687023 9:1 10:-0.419355 11:-1 12:1 13:1 225 | -1 1:-0.75 2:-1 3:1 4:-0.169811 5:-0.739726 6:-1 7:-1 8:0.694656 9:-1 10:-0.548387 11:-1 12:-1 13:-1 226 | -1 1:-0.5 2:1 3:-0.333333 4:-0.226415 5:-0.648402 6:-1 7:-1 8:-0.0687023 9:-1 10:-1 12:-1 13:0.5 227 | +1 1:0.375 2:-1 3:0.333333 4:-0.320755 5:-0.374429 6:-1 7:-1 8:-0.603053 9:-1 10:-0.612903 12:-0.333333 13:1 228 | +1 1:-0.416667 2:-1 3:1 4:-0.283019 5:-0.0182648 6:1 7:1 8:-0.00763359 9:1 10:-0.0322581 12:-1 13:1 229 | -1 1:0.208333 2:-1 3:-1 4:0.0566038 5:-0.283105 6:1 7:1 8:0.389313 9:-1 10:-0.677419 11:-1 12:-1 13:-1 230 | -1 1:-0.0416667 2:1 3:-1 4:-0.54717 5:-0.726027 6:-1 7:1 8:0.816794 9:-1 10:-1 12:-1 13:0.5 231 | +1 1:0.333333 2:-1 3:1 4:-0.0377358 5:-0.173516 6:-1 7:1 8:0.145038 9:1 10:-0.677419 12:-1 13:1 232 | +1 1:-0.583333 2:1 3:1 4:-0.54717 5:-0.575342 6:-1 7:-1 8:0.0534351 9:-1 10:-0.612903 12:-1 13:1 233 | -1 1:-0.333333 2:1 3:1 4:-0.603774 5:-0.388128 6:-1 7:1 8:0.740458 9:-1 10:-1 11:-1 12:-1 13:-1 234 | +1 1:-0.0416667 2:1 3:1 4:-0.358491 5:-0.410959 6:-1 7:-1 8:0.374046 9:1 10:-1 11:-1 12:-0.333333 13:1 235 | -1 1:0.375 2:1 3:0.333333 4:-0.320755 5:-0.520548 6:-1 7:-1 8:0.145038 9:-1 10:-0.419355 12:1 13:1 236 | +1 1:0.375 2:-1 3:1 4:0.245283 5:-0.826484 6:-1 7:1 8:0.129771 9:-1 10:1 11:1 12:1 13:1 237 | -1 2:-1 3:1 4:-0.169811 5:-0.506849 6:-1 7:1 8:0.358779 9:-1 10:-1 11:-1 12:-1 13:-1 238 | +1 1:-0.416667 2:1 3:1 4:-0.509434 5:-0.767123 6:-1 7:1 8:-0.251908 9:1 10:-0.193548 12:-1 13:1 239 | -1 1:-0.25 2:1 3:0.333333 4:-0.169811 5:-0.401826 6:-1 7:1 8:0.29771 9:-1 10:-1 11:-1 12:-1 13:-1 240 | -1 1:-0.0416667 2:1 3:-0.333333 4:-0.509434 5:-0.0913242 6:-1 7:-1 8:0.541985 9:-1 10:-0.935484 11:-1 12:-1 13:-1 241 | +1 1:0.625 2:1 3:0.333333 4:0.622642 5:-0.324201 6:1 7:1 8:0.206107 9:1 10:-0.483871 12:-1 13:1 242 | -1 1:-0.583333 2:1 3:0.333333 4:-0.132075 5:-0.109589 6:-1 7:1 8:0.694656 9:-1 10:-1 11:-1 12:-1 13:-1 243 | -1 2:-1 3:1 4:-0.320755 5:-0.369863 6:-1 7:1 8:0.0992366 9:-1 10:-0.870968 12:-1 13:-1 244 | +1 1:0.375 2:-1 3:1 4:-0.132075 5:-0.351598 6:-1 7:1 8:0.358779 9:-1 10:0.16129 11:1 12:0.333333 13:-1 245 | -1 1:-0.0833333 2:-1 3:0.333333 4:-0.132075 5:-0.16895 6:-1 7:1 8:0.0839695 9:-1 10:-0.516129 11:-1 12:-0.333333 13:-1 246 | +1 1:0.291667 2:1 3:1 4:-0.320755 5:-0.420091 6:-1 7:-1 8:0.114504 9:1 10:-0.548387 11:-1 12:-0.333333 13:1 247 | +1 1:0.5 2:1 3:1 4:-0.698113 5:-0.442922 6:-1 7:1 8:0.328244 9:-1 10:-0.806452 11:-1 12:0.333333 13:0.5 248 | -1 1:0.5 2:-1 3:0.333333 4:0.150943 5:-0.347032 6:-1 7:-1 8:0.175573 9:-1 10:-0.741935 11:-1 12:-1 13:-1 249 | +1 1:0.291667 2:1 3:0.333333 4:-0.132075 5:-0.730594 6:-1 7:1 8:0.282443 9:-1 10:-0.0322581 12:-1 13:-1 250 | +1 1:0.291667 2:1 3:1 4:-0.0377358 5:-0.287671 6:-1 7:1 8:0.0839695 9:1 10:-0.0967742 12:0.333333 13:1 251 | +1 1:0.0416667 2:1 3:1 4:-0.509434 5:-0.716895 6:-1 7:-1 8:-0.358779 9:-1 10:-0.548387 12:-0.333333 13:1 252 | -1 1:-0.375 2:1 3:-0.333333 4:-0.320755 5:-0.575342 6:-1 7:1 8:0.78626 9:-1 10:-1 11:-1 12:-1 13:-1 253 | +1 1:-0.375 2:1 3:1 4:-0.660377 5:-0.251142 6:-1 7:1 8:0.251908 9:-1 10:-1 11:-1 12:-0.333333 13:-1 254 | -1 1:-0.0833333 2:1 3:0.333333 4:-0.698113 5:-0.776256 6:-1 7:-1 8:-0.206107 9:-1 10:-0.806452 11:-1 12:-1 13:-1 255 | -1 1:0.25 2:1 3:0.333333 4:0.0566038 5:-0.607306 6:1 7:-1 8:0.312977 9:-1 10:-0.483871 11:-1 12:-1 13:-1 256 | -1 1:0.75 2:-1 3:-0.333333 4:0.245283 5:-0.196347 6:-1 7:-1 8:0.389313 9:-1 10:-0.870968 11:-1 12:0.333333 13:-1 257 | -1 1:0.333333 2:1 3:0.333333 4:0.0566038 5:-0.465753 6:1 7:-1 8:0.00763359 9:1 10:-0.677419 12:-1 13:-1 258 | +1 1:0.0833333 2:1 3:1 4:-0.283019 5:0.0365297 6:-1 7:-1 8:-0.0687023 9:1 10:-0.612903 12:-0.333333 13:1 259 | +1 1:0.458333 2:1 3:0.333333 4:-0.132075 5:-0.0456621 6:-1 7:-1 8:0.328244 9:-1 10:-1 11:-1 12:-1 13:-1 260 | -1 1:-0.416667 2:1 3:1 4:0.0566038 5:-0.447489 6:-1 7:-1 8:0.526718 9:-1 10:-0.516129 11:-1 12:-1 13:-1 261 | -1 1:0.208333 2:-1 3:0.333333 4:-0.509434 5:-0.0228311 6:-1 7:-1 8:0.541985 9:-1 10:-1 11:-1 12:-1 13:-1 262 | +1 1:0.291667 2:1 3:1 4:-0.320755 5:-0.634703 6:-1 7:1 8:-0.0687023 9:1 10:-0.225806 12:0.333333 13:1 263 | +1 1:0.208333 2:1 3:-0.333333 4:-0.509434 5:-0.278539 6:-1 7:1 8:0.358779 9:-1 10:-0.419355 12:-1 13:-1 264 | -1 1:-0.166667 2:1 3:-0.333333 4:-0.320755 5:-0.360731 6:-1 7:-1 8:0.526718 9:-1 10:-0.806452 11:-1 12:-1 13:-1 265 | +1 1:-0.208333 2:1 3:-0.333333 4:-0.698113 5:-0.52968 6:-1 7:-1 8:0.480916 9:-1 10:-0.677419 11:1 12:-1 13:1 266 | -1 1:-0.0416667 2:1 3:0.333333 4:0.471698 5:-0.666667 6:1 7:-1 8:0.389313 9:-1 10:-0.83871 11:-1 12:-1 13:1 267 | -1 1:-0.375 2:1 3:-0.333333 4:-0.509434 5:-0.374429 6:-1 7:-1 8:0.557252 9:-1 10:-1 11:-1 12:-1 13:1 268 | -1 1:0.125 2:-1 3:-0.333333 4:-0.132075 5:-0.232877 6:-1 7:1 8:0.251908 9:-1 10:-0.580645 12:-1 13:-1 269 | -1 1:0.166667 2:1 3:1 4:-0.132075 5:-0.69863 6:-1 7:-1 8:0.175573 9:-1 10:-0.870968 12:-1 13:0.5 270 | +1 1:0.583333 2:1 3:1 4:0.245283 5:-0.269406 6:-1 7:1 8:-0.435115 9:1 10:-0.516129 12:1 13:-1 271 | -------------------------------------------------------------------------------- /ML/4.SVM-SMO/model_file: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/ML/4.SVM-SMO/model_file -------------------------------------------------------------------------------- /ML/4.SVM-SMO/svm.py: -------------------------------------------------------------------------------- 1 | #coding:UTF-8 2 | import numpy as np 3 | import _pickle as pickle 4 | 5 | class SVM: 6 | def __init__(self, dataSet, labels, C, toler, kernel_option): 7 | self.train_x = dataSet # 训练特征 8 | self.train_y = labels # 训练标签 9 | self.C = C # 惩罚参数 10 | self.toler = toler # 迭代的终止条件之一 11 | self.n_samples = np.shape(dataSet)[0] # 训练样本的个数 12 | self.alphas = np.mat(np.zeros((self.n_samples, 1))) # 拉格朗日乘子 13 | self.b = 0 14 | self.error_tmp = np.mat(np.zeros((self.n_samples, 2))) # 保存E的缓存 15 | self.kernel_opt = kernel_option # 选用的核函数及其参数 16 | self.kernel_mat = calc_kernel(self.train_x, self.kernel_opt) # 核函数的输出 17 | 18 | def cal_kernel_value(train_x, train_x_i, kernel_option): 19 | '''样本之间的核函数的值 20 | input: train_x(mat):训练样本 21 | train_x_i(mat):第i个训练样本 22 | kernel_option(tuple):核函数的类型以及参数 23 | output: kernel_value(mat):样本之间的核函数的值 24 | 25 | ''' 26 | kernel_type = kernel_option[0] # 核函数的类型,分为rbf和其他 27 | m = np.shape(train_x)[0] # 样本的个数 28 | 29 | kernel_value = np.mat(np.zeros((m, 1))) 30 | 31 | if kernel_type == 'rbf': # rbf核函数 32 | sigma = kernel_option[1] 33 | if sigma == 0: 34 | sigma = 1.0 35 | for i in range(m): 36 | diff = train_x[i, :] - train_x_i 37 | kernel_value[i] = np.exp(diff * diff.T / (-2.0 * sigma**2)) 38 | else: # 不使用核函数 39 | kernel_value = train_x * train_x_i.T 40 | return kernel_value 41 | 42 | 43 | def calc_kernel(train_x, kernel_option): 44 | '''计算核函数矩阵 45 | input: train_x(mat):训练样本的特征值 46 | kernel_option(tuple):核函数的类型以及参数 47 | output: kernel_matrix(mat):样本的核函数的值 48 | ''' 49 | m = np.shape(train_x)[0] # 样本的个数 50 | kernel_matrix = np.mat(np.zeros((m, m))) # 初始化样本之间的核函数值 51 | for i in range(m): 52 | kernel_matrix[:, i] = cal_kernel_value(train_x, train_x[i, :], kernel_option) 53 | return kernel_matrix 54 | 55 | def cal_error(svm, alpha_k): 56 | '''误差值的计算 57 | input: svm:SVM模型 58 | alpha_k(int):选择出的变量 59 | output: error_k(float):误差值 60 | ''' 61 | output_k = float(np.multiply(svm.alphas, svm.train_y).T * svm.kernel_mat[:, alpha_k] + svm.b) 62 | error_k = output_k - float(svm.train_y[alpha_k]) 63 | return error_k 64 | 65 | 66 | def update_error_tmp(svm, alpha_k): 67 | '''重新计算误差值 68 | input: svm:SVM模型 69 | alpha_k(int):选择出的变量 70 | output: 对应误差值 71 | ''' 72 | error = cal_error(svm, alpha_k) 73 | svm.error_tmp[alpha_k] = [1, error] 74 | 75 | def select_second_sample_j(svm, alpha_i, error_i): 76 | '''选择第二个样本 77 | input: svm:SVM模型 78 | alpha_i(int):选择出的第一个变量 79 | error_i(float):E_i 80 | output: alpha_j(int):选择出的第二个变量 81 | error_j(float):E_j 82 | ''' 83 | # 标记为已被优化 84 | svm.error_tmp[alpha_i] = [1, error_i] 85 | candidateAlphaList = np.nonzero(svm.error_tmp[:, 0].A)[0] 86 | 87 | maxStep = 0 88 | alpha_j = 0 89 | error_j = 0 90 | 91 | if len(candidateAlphaList) > 1: 92 | for alpha_k in candidateAlphaList: 93 | if alpha_k == alpha_i: 94 | continue 95 | error_k = cal_error(svm, alpha_k) 96 | if abs(error_k - error_i) > maxStep: 97 | maxStep = abs(error_k - error_i) 98 | alpha_j = alpha_k 99 | error_j = error_k 100 | else: # 随机选择 101 | alpha_j = alpha_i 102 | while alpha_j == alpha_i: 103 | alpha_j = int(np.random.uniform(0, svm.n_samples)) 104 | error_j = cal_error(svm, alpha_j) 105 | 106 | return alpha_j, error_j 107 | 108 | def choose_and_update(svm, alpha_i): 109 | '''判断和选择两个alpha进行更新 110 | input: svm:SVM模型 111 | alpha_i(int):选择出的第一个变量 112 | ''' 113 | error_i = cal_error(svm, alpha_i) # 计算第一个样本的E_i 114 | 115 | # 判断选择出的第一个变量是否违反了KKT条件 116 | if (svm.train_y[alpha_i] * error_i < -svm.toler) and (svm.alphas[alpha_i] < svm.C) or\ 117 | (svm.train_y[alpha_i] * error_i > svm.toler) and (svm.alphas[alpha_i] > 0): 118 | 119 | # 1、选择第二个变量 120 | alpha_j, error_j = select_second_sample_j(svm, alpha_i, error_i) 121 | alpha_i_old = svm.alphas[alpha_i].copy() 122 | alpha_j_old = svm.alphas[alpha_j].copy() 123 | 124 | # 2.Softmax Regression Train、计算上下界 125 | if svm.train_y[alpha_i] != svm.train_y[alpha_j]: 126 | L = max(0, svm.alphas[alpha_j] - svm.alphas[alpha_i]) 127 | H = min(svm.C, svm.C + svm.alphas[alpha_j] - svm.alphas[alpha_i]) 128 | else: 129 | L = max(0, svm.alphas[alpha_j] + svm.alphas[alpha_i] - svm.C) 130 | H = min(svm.C, svm.alphas[alpha_j] + svm.alphas[alpha_i]) 131 | if L == H: 132 | return 0 133 | 134 | # 3、计算eta 135 | eta = 2.0 * svm.kernel_mat[alpha_i, alpha_j] - svm.kernel_mat[alpha_i, alpha_i] \ 136 | - svm.kernel_mat[alpha_j, alpha_j] 137 | if eta >= 0: 138 | return 0 139 | 140 | # 4、更新alpha_j 141 | svm.alphas[alpha_j] -= svm.train_y[alpha_j] * (error_i - error_j) / eta 142 | 143 | # 5、确定最终的alpha_j 144 | if svm.alphas[alpha_j] > H: 145 | svm.alphas[alpha_j] = H 146 | if svm.alphas[alpha_j] < L: 147 | svm.alphas[alpha_j] = L 148 | 149 | # 6、判断是否结束 150 | if abs(alpha_j_old - svm.alphas[alpha_j]) < 0.00001: 151 | update_error_tmp(svm, alpha_j) 152 | return 0 153 | 154 | # 7、更新alpha_i 155 | svm.alphas[alpha_i] += svm.train_y[alpha_i] * svm.train_y[alpha_j] \ 156 | * (alpha_j_old - svm.alphas[alpha_j]) 157 | 158 | # 8、更新b 159 | b1 = svm.b - error_i - svm.train_y[alpha_i] * (svm.alphas[alpha_i] - alpha_i_old) \ 160 | * svm.kernel_mat[alpha_i, alpha_i] \ 161 | - svm.train_y[alpha_j] * (svm.alphas[alpha_j] - alpha_j_old) \ 162 | * svm.kernel_mat[alpha_i, alpha_j] 163 | b2 = svm.b - error_j - svm.train_y[alpha_i] * (svm.alphas[alpha_i] - alpha_i_old) \ 164 | * svm.kernel_mat[alpha_i, alpha_j] \ 165 | - svm.train_y[alpha_j] * (svm.alphas[alpha_j] - alpha_j_old) \ 166 | * svm.kernel_mat[alpha_j, alpha_j] 167 | if (0 < svm.alphas[alpha_i]) and (svm.alphas[alpha_i] < svm.C): 168 | svm.b = b1 169 | elif (0 < svm.alphas[alpha_j]) and (svm.alphas[alpha_j] < svm.C): 170 | svm.b = b2 171 | else: 172 | svm.b = (b1 + b2) / 2.0 173 | 174 | # 9、更新error 175 | update_error_tmp(svm, alpha_j) 176 | update_error_tmp(svm, alpha_i) 177 | 178 | return 1 179 | else: 180 | return 0 181 | 182 | def SVM_training(train_x, train_y, C, toler, max_iter, kernel_option = ('rbf', 0.431029)): 183 | '''SVM的训练 184 | input: train_x(mat):训练数据的特征 185 | train_y(mat):训练数据的标签 186 | C(float):惩罚系数 187 | toler(float):迭代的终止条件之一 188 | max_iter(int):最大迭代次数 189 | kerner_option(tuple):核函数的类型及其参数 190 | output: svm模型 191 | ''' 192 | # 1、初始化SVM分类器 193 | svm = SVM(train_x, train_y, C, toler, kernel_option) 194 | 195 | # 2.Softmax Regression Train、开始训练 196 | entireSet = True 197 | alpha_pairs_changed = 0 198 | iteration = 0 199 | 200 | while (iteration < max_iter) and ((alpha_pairs_changed > 0) or entireSet): 201 | print("\t iterration: ", iteration) 202 | alpha_pairs_changed = 0 203 | 204 | if entireSet: 205 | # 对所有的样本 206 | for x in range(svm.n_samples): 207 | alpha_pairs_changed += choose_and_update(svm, x) 208 | iteration += 1 209 | else: 210 | # 非边界样本 211 | bound_samples = [] 212 | for i in range(svm.n_samples): 213 | if svm.alphas[i,0] > 0 and svm.alphas[i,0] < svm.C: 214 | bound_samples.append(i) 215 | for x in bound_samples: 216 | alpha_pairs_changed += choose_and_update(svm, x) 217 | iteration += 1 218 | 219 | # 在所有样本和非边界样本之间交替 220 | if entireSet: 221 | entireSet = False 222 | elif alpha_pairs_changed == 0: 223 | entireSet = True 224 | 225 | return svm 226 | 227 | def svm_predict(svm, test_sample_x): 228 | '''利用SVM模型对每一个样本进行预测 229 | input: svm:SVM模型 230 | test_sample_x(mat):样本 231 | output: predict(float):对样本的预测 232 | ''' 233 | # 1、计算核函数矩阵 234 | kernel_value = cal_kernel_value(svm.train_x, test_sample_x, svm.kernel_opt) 235 | # 2.Softmax Regression Train、计算预测值 236 | predict = kernel_value.T * np.multiply(svm.train_y, svm.alphas) + svm.b 237 | return predict 238 | 239 | def cal_accuracy(svm, test_x, test_y): 240 | '''计算预测的准确性 241 | input: svm:SVM模型 242 | test_x(mat):测试的特征 243 | test_y(mat):测试的标签 244 | output: accuracy(float):预测的准确性 245 | ''' 246 | n_samples = np.shape(test_x)[0] # 样本的个数 247 | correct = 0.0 248 | for i in range(n_samples): 249 | # 对每一个样本得到预测值 250 | predict=svm_predict(svm, test_x[i, :]) 251 | # 判断每一个样本的预测值与真实值是否一致 252 | if np.sign(predict) == np.sign(test_y[i]): 253 | correct += 1 254 | accuracy = correct / n_samples 255 | return accuracy 256 | 257 | 258 | def save_svm_model(svm_model, model_file): 259 | '''保存SVM模型 260 | input: svm_model:SVM模型 261 | model_file(string):SVM模型需要保存到的文件 262 | ''' 263 | with open(model_file, 'wb') as f: 264 | pickle.dump(svm_model, f) 265 | -------------------------------------------------------------------------------- /ML/4.SVM-SMO/svm_test.py: -------------------------------------------------------------------------------- 1 | # coding:UTF-8 2 | 3 | import numpy as np 4 | import _pickle as pickle 5 | from svm import svm_predict 6 | 7 | 8 | def load_test_data(test_file): 9 | data = [] 10 | f = open(test_file) 11 | for line in f.readlines(): 12 | lines = line.strip().split(' ') 13 | 14 | # 处理测试样本中的特征 15 | index = 0 16 | tmp = [] 17 | for i in range(0, len(lines)): 18 | li = lines[i].strip().split(":") 19 | if int(li[0]) - 1 == index: 20 | tmp.append(float(li[1])) 21 | else: 22 | while int(li[0]) - 1 > index: 23 | tmp.append(0) 24 | index += 1 25 | tmp.append(float(li[1])) 26 | index += 1 27 | while len(tmp) < 13: 28 | tmp.append(0) 29 | data.append(tmp) 30 | f.close() 31 | return np.mat(data) 32 | 33 | 34 | def load_svm_model(svm_model_file): 35 | with open(svm_model_file, 'rb') as f: 36 | svm_model = pickle.load(f) 37 | return svm_model 38 | 39 | 40 | def get_prediction(test_data, svm): 41 | m = np.shape(test_data)[0] 42 | prediction = [] 43 | for i in range(m): 44 | # 对每一个样本得到预测值 45 | predict = svm_predict(svm, test_data[i, :]) 46 | # 得到最终的预测类别 47 | prediction.append(str(np.sign(predict)[0, 0])) 48 | return prediction 49 | 50 | 51 | def save_prediction(result_file, prediction): 52 | f = open(result_file, 'w') 53 | f.write(" ".join(prediction)) 54 | f.close() 55 | 56 | 57 | if __name__ == "__main__": 58 | # 1、导入测试数据 59 | print("--------- 1.load data ---------") 60 | test_data = load_test_data("svm_test_data") 61 | print(test_data.shape) 62 | # 2.Softmax Regression Train、导入SVM模型 63 | print("--------- 2.Softmax Regression Train.load model ----------") 64 | svm_model = load_svm_model("model_file") 65 | # 3、得到预测值 66 | print("--------- 3.get prediction ---------") 67 | prediction = get_prediction(test_data, svm_model) 68 | # 4、保存最终的预测值 69 | print("--------- 4.save result ----------") 70 | save_prediction("result", prediction) 71 | -------------------------------------------------------------------------------- /ML/4.SVM-SMO/svm_test_data: -------------------------------------------------------------------------------- 1 | 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.419847 9:-1 10:-0.225806 12:1 13:-1 2 | 1:0.583333 2:-1 3:0.333333 4:-0.603774 5:1 6:-1 7:1 8:0.358779 9:-1 10:-0.483871 12:-1 13:1 3 | 1:0.166667 2:1 3:-0.333333 4:-0.433962 5:-0.383562 6:-1 7:-1 8:0.0687023 9:-1 10:-0.903226 11:-1 12:-1 13:1 4 | 1:0.458333 2:1 3:1 4:-0.358491 5:-0.374429 6:-1 7:-1 8:-0.480916 9:1 10:-0.935484 12:-0.333333 13:1 5 | 1:0.875 2:-1 3:-0.333333 4:-0.509434 5:-0.347032 6:-1 7:1 8:-0.236641 9:1 10:-0.935484 11:-1 12:-0.333333 13:-1 6 | 1:0.5 2:1 3:1 4:-0.509434 5:-0.767123 6:-1 7:-1 8:0.0534351 9:-1 10:-0.870968 11:-1 12:-1 13:1 7 | 1:0.125 2:1 3:0.333333 4:-0.320755 5:-0.406393 6:1 7:1 8:0.0839695 9:1 10:-0.806452 12:-0.333333 13:0.5 8 | 1:0.25 2:1 3:1 4:-0.698113 5:-0.484018 6:-1 7:1 8:0.0839695 9:1 10:-0.612903 12:-0.333333 13:1 9 | 1:0.291667 2:1 3:1 4:-0.132075 5:-0.237443 6:-1 7:1 8:0.51145 9:-1 10:-0.612903 12:0.333333 13:1 10 | 1:0.416667 2:-1 3:1 4:0.0566038 5:0.283105 6:-1 7:1 8:0.267176 9:-1 10:0.290323 12:1 13:1 11 | 1:0.25 2:1 3:1 4:-0.226415 5:-0.506849 6:-1 7:-1 8:0.374046 9:-1 10:-0.83871 12:-1 13:1 12 | 2:1 3:1 4:-0.0943396 5:-0.543379 6:-1 7:1 8:-0.389313 9:1 10:-1 11:-1 12:-1 13:1 13 | 1:-0.375 2:1 3:0.333333 4:-0.132075 5:-0.502283 6:-1 7:1 8:0.664122 9:-1 10:-1 11:-1 12:-1 13:-1 14 | 1:0.333333 2:1 3:-1 4:-0.245283 5:-0.506849 6:-1 7:-1 8:0.129771 9:-1 10:-0.16129 12:0.333333 13:-1 15 | 1:0.166667 2:-1 3:1 4:-0.358491 5:-0.191781 6:-1 7:1 8:0.343511 9:-1 10:-1 11:-1 12:-0.333333 13:-1 16 | 1:0.75 2:-1 3:1 4:-0.660377 5:-0.894977 6:-1 7:-1 8:-0.175573 9:-1 10:-0.483871 12:-1 13:-1 17 | 1:-0.291667 2:1 3:1 4:-0.132075 5:-0.155251 6:-1 7:-1 8:-0.251908 9:1 10:-0.419355 12:0.333333 13:1 18 | 2:1 3:1 4:-0.132075 5:-0.648402 6:1 7:1 8:0.282443 9:1 11:1 12:-1 13:1 19 | 1:0.458333 2:1 3:-1 4:-0.698113 5:-0.611872 6:-1 7:1 8:0.114504 9:1 10:-0.419355 12:-1 13:-1 20 | 1:-0.541667 2:1 3:-1 4:-0.132075 5:-0.666667 6:-1 7:-1 8:0.633588 9:1 10:-0.548387 11:-1 12:-1 13:1 21 | 1:0.583333 2:1 3:1 4:-0.509434 5:-0.52968 6:-1 7:1 8:-0.114504 9:1 10:-0.16129 12:0.333333 13:1 22 | 1:-0.208333 2:1 3:-0.333333 4:-0.320755 5:-0.456621 6:-1 7:1 8:0.664122 9:-1 10:-0.935484 12:-1 13:-1 23 | 1:-0.416667 2:1 3:1 4:-0.603774 5:-0.191781 6:-1 7:-1 8:0.679389 9:-1 10:-0.612903 12:-1 13:-1 24 | 1:-0.25 2:1 3:1 4:-0.660377 5:-0.643836 6:-1 7:-1 8:0.0992366 9:-1 10:-0.967742 11:-1 12:-1 13:-1 25 | 1:0.0416667 2:-1 3:-0.333333 4:-0.283019 5:-0.260274 6:1 7:1 8:0.343511 9:1 10:-1 11:-1 12:-0.333333 13:-1 26 | 1:-0.208333 2:-1 3:0.333333 4:-0.320755 5:-0.319635 6:-1 7:-1 8:0.0381679 9:-1 10:-0.935484 11:-1 12:-1 13:-1 27 | 1:-0.291667 2:-1 3:1 4:-0.169811 5:-0.465753 6:-1 7:1 8:0.236641 9:1 10:-1 12:-1 13:-1 28 | 1:-0.0833333 2:-1 3:0.333333 4:-0.509434 5:-0.228311 6:-1 7:1 8:0.312977 9:-1 10:-0.806452 11:-1 12:-1 13:-1 29 | 1:0.208333 2:1 3:0.333333 4:-0.660377 5:-0.525114 6:-1 7:1 8:0.435115 9:-1 10:-0.193548 12:-0.333333 13:1 30 | 1:0.75 2:-1 3:0.333333 4:-0.698113 5:-0.365297 6:1 7:1 8:-0.0992366 9:-1 10:-1 11:-1 12:-0.333333 13:-1 31 | 1:0.166667 2:1 3:0.333333 4:-0.358491 5:-0.52968 6:-1 7:1 8:0.206107 9:-1 10:-0.870968 12:-0.333333 13:1 32 | 1:0.541667 2:1 3:1 4:0.245283 5:-0.534247 6:-1 7:1 8:0.0229008 9:-1 10:-0.258065 11:-1 12:-1 13:0.5 33 | 1:-0.666667 2:-1 3:0.333333 4:-0.509434 5:-0.593607 6:-1 7:-1 8:0.51145 9:-1 10:-1 11:-1 12:-1 13:-1 34 | 1:0.25 2:1 3:1 4:0.433962 5:-0.086758 6:-1 7:1 8:0.0534351 9:1 10:0.0967742 11:1 12:-1 13:1 35 | 1:-0.125 2:1 3:1 4:-0.0566038 5:-0.6621 6:-1 7:1 8:-0.160305 9:1 10:-0.709677 12:-1 13:1 36 | 1:-0.208333 2:1 3:1 4:-0.320755 5:-0.406393 6:1 7:1 8:0.206107 9:1 10:-1 11:-1 12:0.333333 13:1 37 | 1:0.333333 2:1 3:1 4:-0.132075 5:-0.630137 6:-1 7:1 8:0.0229008 9:1 10:-0.387097 11:-1 12:-0.333333 13:1 38 | 1:0.25 2:1 3:-1 4:0.245283 5:-0.328767 6:-1 7:1 8:-0.175573 9:-1 10:-1 11:-1 12:-1 13:-1 39 | 1:-0.458333 2:1 3:0.333333 4:-0.320755 5:-0.753425 6:-1 7:-1 8:0.206107 9:-1 10:-1 11:-1 12:-1 13:-1 40 | 1:-0.208333 2:1 3:1 4:-0.471698 5:-0.561644 6:-1 7:1 8:0.755725 9:-1 10:-1 11:-1 12:-1 13:-1 41 | 1:-0.541667 2:1 3:1 4:0.0943396 5:-0.557078 6:-1 7:-1 8:0.679389 9:-1 10:-1 11:-1 12:-1 13:1 42 | 1:0.375 2:-1 3:1 4:-0.433962 5:-0.621005 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-1 13:-1 43 | 1:-0.375 2:1 3:0.333333 4:-0.320755 5:-0.511416 6:-1 7:-1 8:0.648855 9:1 10:-0.870968 11:-1 12:-1 13:-1 44 | 1:-0.291667 2:1 3:-0.333333 4:-0.867925 5:-0.675799 6:1 7:-1 8:0.29771 9:-1 10:-1 11:-1 12:-1 13:1 45 | 1:0.25 2:1 3:0.333333 4:-0.396226 5:-0.579909 6:1 7:-1 8:-0.0381679 9:-1 10:-0.290323 12:-0.333333 13:0.5 46 | 1:0.208333 2:1 3:0.333333 4:-0.132075 5:-0.611872 6:1 7:1 8:0.435115 9:-1 10:-1 11:-1 12:-1 13:-1 47 | 1:-0.166667 2:1 3:0.333333 4:-0.54717 5:-0.894977 6:-1 7:1 8:-0.160305 9:-1 10:-0.741935 11:-1 12:1 13:-1 48 | 1:-0.375 2:1 3:1 4:-0.698113 5:-0.675799 6:-1 7:1 8:0.618321 9:-1 10:-1 11:-1 12:-0.333333 13:-1 49 | 1:0.541667 2:1 3:-0.333333 4:0.245283 5:-0.452055 6:-1 7:-1 8:-0.251908 9:1 10:-1 12:1 13:0.5 50 | 1:0.5 2:-1 3:1 4:0.0566038 5:-0.547945 6:-1 7:1 8:-0.343511 9:-1 10:-0.677419 12:1 13:1 51 | 1:-0.458333 2:1 3:1 4:-0.207547 5:-0.136986 6:-1 7:-1 8:-0.175573 9:1 10:-0.419355 12:-1 13:0.5 52 | 1:-0.0416667 2:1 3:-0.333333 4:-0.358491 5:-0.639269 6:1 7:-1 8:0.725191 9:-1 10:-1 11:-1 12:-1 13:-1 53 | 1:0.5 2:-1 3:0.333333 4:-0.132075 5:0.328767 6:1 7:1 8:0.312977 9:-1 10:-0.741935 11:-1 12:-0.333333 13:-1 54 | 1:0.416667 2:-1 3:-0.333333 4:-0.132075 5:-0.684932 6:-1 7:-1 8:0.648855 9:-1 10:-1 11:-1 12:0.333333 13:-1 55 | 1:-0.333333 2:-1 3:-0.333333 4:-0.320755 5:-0.506849 6:-1 7:1 8:0.587786 9:-1 10:-0.806452 12:-1 13:-1 56 | 1:-0.5 2:-1 3:-0.333333 4:-0.792453 5:-0.671233 6:-1 7:-1 8:0.480916 9:-1 10:-1 11:-1 12:-0.333333 13:-1 57 | 1:0.333333 2:1 3:1 4:-0.169811 5:-0.817352 6:-1 7:1 8:-0.175573 9:1 10:0.16129 12:-0.333333 13:-1 58 | 1:0.291667 2:-1 3:0.333333 4:-0.509434 5:-0.762557 6:1 7:-1 8:-0.618321 9:-1 10:-1 11:-1 12:-1 13:-1 59 | 1:0.25 2:-1 3:1 4:0.509434 5:-0.438356 6:-1 7:-1 8:0.0992366 9:1 10:-1 12:-1 13:-1 60 | 1:0.375 2:1 3:-0.333333 4:-0.509434 5:-0.292237 6:-1 7:1 8:-0.51145 9:-1 10:-0.548387 12:-0.333333 13:1 61 | 1:0.166667 2:1 3:0.333333 4:0.0566038 5:-1 6:1 7:-1 8:0.557252 9:-1 10:-0.935484 11:-1 12:-0.333333 13:1 62 | 1:-0.0833333 2:-1 3:1 4:-0.320755 5:-0.182648 6:-1 7:-1 8:0.0839695 9:1 10:-0.612903 12:-1 13:1 63 | 1:-0.375 2:1 3:0.333333 4:-0.509434 5:-0.543379 6:-1 7:-1 8:0.496183 9:-1 10:-1 11:-1 12:-1 13:-1 64 | 1:0.291667 2:-1 3:-1 4:0.0566038 5:-0.479452 6:-1 7:-1 8:0.526718 9:-1 10:-0.709677 11:-1 12:-1 13:-1 65 | 1:0.416667 2:1 3:-1 4:-0.0377358 5:-0.511416 6:1 7:1 8:0.206107 9:-1 10:-0.258065 11:1 12:-1 13:0.5 66 | 1:0.166667 2:1 3:1 4:0.0566038 5:-0.315068 6:-1 7:1 8:-0.374046 9:1 10:-0.806452 12:-0.333333 13:0.5 67 | 1:-0.0833333 2:1 3:1 4:-0.132075 5:-0.383562 6:-1 7:1 8:0.755725 9:1 10:-1 11:-1 12:-1 13:-1 68 | 1:0.208333 2:-1 3:-0.333333 4:-0.207547 5:-0.118721 6:1 7:1 8:0.236641 9:-1 10:-1 11:-1 12:0.333333 13:-1 69 | 1:-0.375 2:-1 3:0.333333 4:-0.54717 5:-0.47032 6:-1 7:-1 8:0.19084 9:-1 10:-0.903226 12:-0.333333 13:-1 70 | 1:-0.25 2:1 3:0.333333 4:-0.735849 5:-0.465753 6:-1 7:-1 8:0.236641 9:-1 10:-1 11:-1 12:-1 13:-1 71 | -------------------------------------------------------------------------------- /ML/5.Random Forest/Random_Forest_Test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-20 20:16 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Random_Forest_Test.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import _pickle as pickle 10 | from random_forests_train import get_predict 11 | 12 | 13 | def load_data(file_name): 14 | f = open(file_name) 15 | test_data = [] 16 | for line in f.readlines(): 17 | test_data_tmp = [] 18 | lines = line.strip().split('\t') 19 | for x in lines: 20 | test_data_tmp.append(float(x)) 21 | test_data_tmp.append(0) 22 | test_data.append(test_data_tmp) 23 | f.close() 24 | return test_data 25 | 26 | 27 | def load_model(result_file, feature_file): 28 | trees_fiture = [] 29 | f_tea = open(feature_file) 30 | for line in f_tea.readlines(): 31 | tmp = [] 32 | lines = line.strip().split('\t') 33 | for x in lines: 34 | tmp.append(int(x)) 35 | trees_fiture.append(tmp) 36 | f_tea.close() 37 | 38 | with open(result_file, 'rb') as f: 39 | tree_result = pickle.load(f) 40 | return tree_result, trees_fiture 41 | 42 | 43 | def save_result(data_test, prediction, result_file): 44 | '''保存最终的预测结果 45 | input: data_test(list):待预测的数据 46 | prediction(list):预测的结果 47 | result_file(string):存储最终预测结果的文件名 48 | ''' 49 | m = len(prediction) 50 | n = len(data_test[0]) 51 | 52 | f_result = open(result_file, "w") 53 | for i in range(m): 54 | tmp = [] 55 | for j in range(n - 1): 56 | tmp.append(str(data_test[i][j])) 57 | tmp.append(str(prediction[i])) 58 | f_result.writelines("\t".join(tmp) + "\n") 59 | f_result.close() 60 | 61 | def main(): 62 | data_test = load_data('test_data.txt') 63 | trees_result, trees_feature = load_model('result_file', 'feature_file') 64 | prediction = get_predict(trees_result, trees_feature, data_test) 65 | print(prediction) 66 | save_result(data_test, prediction, "final_result") 67 | 68 | 69 | if __name__ == '__main__': 70 | main() 71 | -------------------------------------------------------------------------------- /ML/5.Random Forest/Random_Forest_Train.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-20 20:16 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Random_Forest_Train.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import numpy as np 10 | from math import log 11 | import _pickle as pickle 12 | from Tree import build_tree, predict, cal_gini_index, label_uniq_cnt 13 | 14 | 15 | # 随机选择样本及特征---》从样本中随机选择样本及其特征 16 | def choose_samples(data, k): 17 | m, n = np.shape(data) 18 | # 1.选择出 k 个特征的 index 19 | feature = [] 20 | for j in range(k): 21 | feature.append(np.random.randint(0, n - 2)) 22 | # 2.选择出 m 个样本的 index 23 | index = [] 24 | for i in range(m): 25 | index.append(np.random.randint(0, m - 1)) 26 | # 3.从 data 中选择出 m 个样本的 k 个特征,组成数据集 data_samples 27 | data_samples = [] 28 | for i in range(m): 29 | data_tmp = [] 30 | for fea in feature: 31 | data_tmp.append(data[i][fea]) 32 | data_tmp.append(data[i][-1]) 33 | data_samples.append(data_tmp) 34 | return data_samples, feature 35 | 36 | 37 | def random_forest_training(data_train, trees_num): 38 | trees_result = [] 39 | trees_feature = [] 40 | n = np.shape(data_train)[1] 41 | if n > 2: 42 | k = int(log(n-1, 2)) + 1 43 | else: 44 | k = 1 45 | for i in range(trees_num): 46 | # 1.随机选择 m 个样本,k 个特征 47 | data_samples, feature = choose_samples(data_train, k) 48 | # 2.构建每一颗分类树 49 | tree = build_tree(data_samples) 50 | # 3.保存训练好的分类树模型 51 | trees_result.append(tree) 52 | # 4.保存好该分类树使用到的特征 53 | trees_feature.append(feature) 54 | return trees_result, trees_feature 55 | 56 | 57 | # 导入数据集 58 | def load_data(file_name): 59 | data_train = [] 60 | f = open(file_name) 61 | for line in f.readlines(): 62 | lines = line.strip().split('\t') 63 | data_tmp = [] 64 | for x in lines: 65 | data_tmp.append(float(x)) 66 | data_train.append(data_tmp) 67 | f.close() 68 | return data_train 69 | 70 | 71 | def get_predict(trees_result, trees_feature, data_train): 72 | m_tree = len(trees_result) 73 | m = np.shape(data_train)[0] 74 | result = [] 75 | for i in range(m_tree): 76 | clf = trees_result[i] 77 | feature = trees_feature[i] 78 | data = split_data(data_train, feature) 79 | result_i = [] 80 | for i in range(m): 81 | # result_i.append(list((predict(data[i][0:, -1], clf).keys()))[0]) 82 | result_i.append(list((predict(data[i][0:-1], clf).keys()))[0]) 83 | result.append(result_i) 84 | final_predict = np.sum(result, axis=0) 85 | return final_predict 86 | 87 | 88 | # 计算准确度 89 | def cal_correct_rate(data_train, final_predict): 90 | m = len(final_predict) 91 | corr = 0.0 92 | for i in range(m): 93 | if data_train[i][-1]*final_predict[i] > 0: 94 | corr += 1 95 | return corr/m 96 | 97 | 98 | def split_data(data_train, feature): 99 | m = np.shape(data_train)[0] 100 | data = [] 101 | for i in range(m): 102 | data_x_tmp = [] 103 | for x in feature: 104 | data_x_tmp.append(data_train[i][x]) 105 | data_x_tmp.append(data_train[i][-1]) 106 | data.append(data_x_tmp) 107 | return data 108 | 109 | 110 | def save_model(trees_result, trees_feature, result_file, feature_file): 111 | m = len(trees_feature) 112 | f_fea = open(feature_file, 'w') 113 | for i in range(m): 114 | fea_tmp = [] 115 | for x in trees_feature[i]: 116 | fea_tmp.append(str(x)) 117 | f_fea.writelines('\t'.join(fea_tmp)+'\n') 118 | f_fea.close() 119 | with open(result_file, 'wb') as f: 120 | pickle.dump(trees_result, f) 121 | 122 | 123 | def main(): 124 | # 1.导入数据集 125 | data_train = load_data('data.txt') 126 | print('********** Start **********') 127 | print(np.shape(data_train)) 128 | # 2.训练模型 129 | trees_result, trees_feature = random_forest_training(data_train, 50) 130 | print('********** Train **********') 131 | # 3.得到训练准确性 132 | result = get_predict(trees_result, trees_feature, data_train) 133 | corr_rate = cal_correct_rate(data_train, result) 134 | print('正确率为:', corr_rate) 135 | # 4.保存最终的随机森林模型 136 | save_model(trees_result, trees_feature, 'result_file', 'feature_file') 137 | 138 | 139 | if __name__ == '__main__': 140 | main() 141 | -------------------------------------------------------------------------------- /ML/5.Random Forest/Tree.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Time : 2019-1-20 20:58 3 | # @Author : Chaucer_Gxm 4 | # @Email : gxm4167235@163.com 5 | # @File : Tree.py 6 | # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm 7 | # @blog : https://blog.csdn.net/qq_24819773 8 | # @Software: PyCharm 9 | import numpy as np 10 | # from Random_Forest_Train import cal_gini_index, label_uniq_cnt 11 | 12 | 13 | class node: 14 | def __init__(self, fea=-1, value=None, results=None, right=None, left=None): 15 | self.fea = fea # 用于切分数据集的属性的列索引值 16 | self.value = value # 设置划分的值 17 | self.results = results # 存储叶节点所属的类别 18 | self.right = right # 右子树 19 | self.left = left # 左子树 20 | 21 | 22 | def split_tree(data, fea, value): 23 | set_1 = [] 24 | set_2 = [] 25 | # 划分左右子树 26 | for x in data: 27 | if x[fea] >= value: 28 | set_1.append(x) 29 | else: 30 | set_2.append(x) 31 | return set_1, set_2 32 | 33 | 34 | # 计算数据集中不同标签的个数 35 | def label_uniq_cnt(data): 36 | label_uniq_cnt = {} 37 | for x in data: 38 | label = x[len(x) - 1] 39 | if label not in label_uniq_cnt: 40 | label_uniq_cnt[label] = 0 41 | label_uniq_cnt[label] = label_uniq_cnt[label] + 1 42 | return label_uniq_cnt 43 | 44 | 45 | # 计算基尼系数 46 | def cal_gini_index(data): 47 | total_sample = len(data) # 得到样本个数 48 | if total_sample == 0: 49 | return 0 50 | label_counts = label_uniq_cnt(data) # 统计数据集中不同标签的个数 51 | gini = 0 52 | for label in label_counts: 53 | gini += pow(label_counts[label], 2) 54 | gini = 1 - float(gini) / pow(total_sample, 2) 55 | return gini 56 | 57 | 58 | 59 | def build_tree(data): 60 | # 构建决策树,函数返回该决策树的根节点 61 | if len(data) == 0: 62 | return node() 63 | currentGini = cal_gini_index(data) 64 | bestCriteria = None 65 | bestGain = 0.0 66 | bestSets = None 67 | 68 | feature_num = len(data[0]) - 1 69 | for fea in range(0, feature_num): 70 | feature_values = {} 71 | for sample in data: 72 | feature_values[sample[fea]] = 1 73 | for value in feature_values.keys(): 74 | (set_1, set_2) = split_tree(data, fea, value) 75 | nowGini = float(len(set_1)*cal_gini_index(set_1)+len(set_2)*cal_gini_index(set_2))/len(data) 76 | gain = currentGini - nowGini 77 | if gain > bestGain and len(set_1) > 0 and len(set_2) > 0: 78 | bestGain = gain 79 | bestCriteria = (fea, value) 80 | bestSets = (set_1, set_2) 81 | if bestGain > 0: 82 | right = build_tree(bestSets[0]) 83 | left = build_tree(bestSets[1]) 84 | return node(fea=bestCriteria[0], value=bestCriteria[1], right=right, left=left) 85 | else: 86 | return node(results=label_uniq_cnt(data)) 87 | 88 | 89 | # 利用训练好的分类树对新样本进行预测 90 | def predict(sample, tree): 91 | if tree.results != None: 92 | return tree.results 93 | else: 94 | val_sample = sample[tree.fea] 95 | branch = None 96 | if val_sample >= tree.value: 97 | branch = tree.right 98 | else: 99 | branch = tree.left 100 | return predict(sample, branch) 101 | -------------------------------------------------------------------------------- /ML/5.Random Forest/__pycache__/Random_Forest_Train.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/ML/5.Random Forest/__pycache__/Random_Forest_Train.cpython-36.pyc -------------------------------------------------------------------------------- /ML/5.Random Forest/__pycache__/Tree.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/ML/5.Random Forest/__pycache__/Tree.cpython-36.pyc -------------------------------------------------------------------------------- /ML/5.Random Forest/data.txt: -------------------------------------------------------------------------------- 1 | 0.115849512118223 3.44740676200324 -1 2 | 0.794159188851836 2.31500783196472 -1 3 | 0.515272923684638 2.74277955207365 -1 4 | 0.757807349055913 2.62778046061851 -1 5 | 0.800737203116005 2.42016300245617 -1 6 | 0.834096943943069 3.81320130782399 -1 7 | 0.262438178404533 2.19315211399158 -1 8 | 0.566182051162668 2.11760811264251 -1 9 | 0.804109117414597 3.78080714675296 -1 10 | 0.699502232372833 2.55016107990424 -1 11 | 0.600846852214735 3.83696493230910 -1 12 | 0.861176710582884 3.22151847958122 -1 13 | 0.773521576094226 3.64690992540663 -1 14 | 0.576119926958350 2.10128594234835 -1 15 | 0.313583860583338 2.89910490174329 -1 16 | 0.466054939140034 3.71918112266350 -1 17 | 0.157494806438721 2.72882756151362 -1 18 | 0.250502756606950 2.55516512148160 -1 19 | 0.718646566744270 2.63317904440953 -1 20 | 0.301740419069536 2.35836917782875 -1 21 | 0.287620739298153 3.61246140370847 -1 22 | 0.421494697051518 3.33120315563518 -1 23 | 0.895441295222718 2.88899006770918 -1 24 | 0.614386029853818 3.89373119104299 -1 25 | 0.409953309427252 3.32922802071079 -1 26 | 0.367541820590805 3.73857430054956 -1 27 | 0.351213020550467 2.64642724829296 -1 28 | 0.871019756798164 3.01183648676965 -1 29 | 0.276068703037833 2.14776560928867 -1 30 | 0.244677596329969 2.65933001333144 -1 31 | 0.917223668940161 3.54976625291633 -1 32 | 0.672267991632200 3.65533619292029 -1 33 | 0.457726138369460 3.40191409674200 -1 34 | 0.509093626432184 3.61806556843821 -1 35 | 0.471781673618599 3.69095161083143 -1 36 | 0.107562648882614 3.94265757096865 -1 37 | 0.333920901223481 2.16200535809161 -1 38 | 0.229284962832740 2.98725288569131 -1 39 | 0.855515989945369 2.62432535062838 -1 40 | 0.766507973353665 2.41522612659492 -1 41 | 0.721599517378556 2.55625039302706 -1 42 | 0.169564787310770 3.67788117867803 -1 43 | 0.683569336161465 3.87250464489691 -1 44 | 0.937297263846035 3.08923684430450 -1 45 | 0.870083382254398 2.98179994775794 -1 46 | 0.856220564004172 2.14412205534023 -1 47 | 0.209416107659041 3.47018437854150 -1 48 | 0.432367174450980 2.82821265275052 -1 49 | 0.611571094630495 2.14418812539128 -1 50 | 0.429520335472156 2.20498033598013 -1 51 | 0.686134978976112 3.06272317637376 -1 52 | 0.164673490081409 2.85158052944554 -1 53 | 0.465680943161390 3.31972770829263 -1 54 | 0.285632498320523 2.25001401865437 -1 55 | 0.497520921334222 3.44804267555166 -1 56 | 0.621886260781796 3.76610582754446 -1 57 | 0.314862457922661 2.50717633779762 -1 58 | 0.300434243616144 3.59873959319048 -1 59 | 0.697047694161418 3.72523398659835 -1 60 | 0.704359545527352 3.32299844318554 -1 61 | 0.473640818733640 3.14116816038346 -1 62 | 0.941764624889760 3.87103351140016 -1 63 | 0.386295526052718 2.38759322523001 -1 64 | 0.454517251901134 3.12578247969414 -1 65 | 0.125289300972197 3.70340660264118 -1 66 | 0.860252106913250 3.01980221099803 -1 67 | 0.277893952737792 3.43495469420866 -1 68 | 0.694758687973274 2.95238237770422 -1 69 | 0.245004089640840 3.56958717996792 -1 70 | 0.855224693147061 2.36365185748033 -1 71 | 0.898720856831806 2.10048258206548 -1 72 | 0.472984263625543 2.12066037403411 -1 73 | 0.299338180884850 3.40792077489622 -1 74 | 0.690039710428214 2.59047355220806 -1 75 | 0.377587575333090 2.27248278348661 -1 76 | 0.135359449639745 3.50466827978977 -1 77 | 0.376143319480201 3.06537035291078 -1 78 | 0.909104699844584 3.33761007135719 -1 79 | 0.502153377784738 3.30133849512946 -1 80 | 0.665790527119754 2.52577276849004 -1 81 | 0.784156222712529 3.03015233421691 -1 82 | 0.468925256746504 3.41171212949679 -1 83 | 0.571974388002879 2.57175701059423 -1 84 | 0.163920483114305 3.18983940285623 -1 85 | 0.673105759989067 3.18782481341753 -1 86 | 0.553646918668128 2.97434028671212 -1 87 | 0.375239516474515 2.89732426221934 -1 88 | 0.750163130434160 3.94345594889218 -1 89 | 0.325265542372484 3.56532395245783 -1 90 | 0.214417871418476 2.16574187457740 -1 91 | 0.485854515576255 3.14150756997340 -1 92 | 0.296250376030394 2.36781048577958 -1 93 | 0.675531454825694 2.75503044476320 -1 94 | 0.533901460932274 3.38520730646036 -1 95 | 0.605999006093022 3.03630489696437 -1 96 | 0.759939366986141 3.36762507566984 -1 97 | 0.371569594402953 3.63946433232063 -1 98 | 0.661698327839941 3.18607620809137 -1 99 | 0.116216665471922 3.64835755711449 -1 100 | 0.196898184951640 3.65477782688105 -1 101 | 2.06186566868747 2.89445591453794 1 102 | 3.36226921049326 2.36189984027948 1 103 | 2.83309025329870 3.82166332119713 1 104 | 2.91386472147203 2.18837571636517 1 105 | 3.09028645665634 2.57565262744699 1 106 | 3.06906928577639 2.77188257668331 1 107 | 2.64437004792940 2.51096254735861 1 108 | 3.68063888769727 2.79105115847121 1 109 | 1.32462701748458 2.15722038332725 1 110 | 2.88666729301799 3.83344377806857 1 111 | 2.01181559287938 3.47857324692560 1 112 | 1.92344595314315 2.33005498123395 1 113 | 2.56513083162471 2.81689695794558 1 114 | 3.88903782670131 2.63986281503723 1 115 | 2.78817630616036 3.15700942075852 1 116 | 2.85518957757959 3.13769138487845 1 117 | 1.89759927626629 2.38092182168999 1 118 | 3.18908564074572 2.17344226274103 1 119 | 3.38462357299798 2.85187124599542 1 120 | 3.46553259712096 3.89641564800734 1 121 | 2.30407670782777 2.19972396667067 1 122 | 3.34305480700432 3.26564249009116 1 123 | 2.55013524651715 3.15021939066072 1 124 | 1.98119397144936 3.48220154983760 1 125 | 2.05948342312106 3.73420559672502 1 126 | 2.05091937072903 3.57262695240525 1 127 | 3.39363391202350 2.19294155117348 1 128 | 2.24713962289868 3.88560453935395 1 129 | 2.28097143899543 2.71146860566225 1 130 | 2.91366120987037 3.68734693488749 1 131 | 3.34660935447672 3.09605802488523 1 132 | 3.21376077204346 3.46848312315924 1 133 | 2.36766462944760 3.67325823776567 1 134 | 2.50942435036273 3.85409143940374 1 135 | 3.37369634005817 2.34613247940030 1 136 | 1.77764379999793 2.33628841412390 1 137 | 2.62224313699996 2.20488887642263 1 138 | 2.43543874539397 3.91797243550208 1 139 | 3.04433031055018 3.71116632841993 1 140 | 2.51420011153215 2.63217508966315 1 141 | 1.62160505455343 3.87825517296032 1 142 | 3.25534520577309 3.85815395745087 1 143 | 3.01310994974114 2.44420102873164 1 144 | 1.92604958070783 2.20768447531945 1 145 | 1.22558873386880 2.61718383795126 1 146 | 1.58583841933930 3.35263603557390 1 147 | 3.67189109572976 2.75117654506517 1 148 | 1.38545177083411 3.56705413610877 1 149 | 2.43503242274966 3.37158123275308 1 150 | 3.04677380693014 2.87965638885886 1 151 | 2.97912619944475 3.27184018903722 1 152 | 2.59666612498410 2.45387068217098 1 153 | 2.55893212776136 3.48441098618643 1 154 | 2.29650671249819 3.46433692603390 1 155 | 3.05643304162774 3.86809312320394 1 156 | 1.97753341479097 2.73325659571657 1 157 | 2.20304568021616 2.14842542491311 1 158 | 2.90560725458383 3.14571445923363 1 159 | 2.14170752656901 2.87969859288948 1 160 | 2.41727077528353 2.68171173570587 1 161 | 1.74313582034297 2.31448323553089 1 162 | 1.48180340118283 3.85106837077205 1 163 | 2.50343396811492 3.48419520985088 1 164 | 1.98616520471399 2.11425477681923 1 165 | 2.85452176260787 2.76151067766848 1 166 | 2.94916285251217 3.66505787292290 1 167 | 1.38885665425628 3.18835839349529 1 168 | 2.75978923752877 3.06533695739141 1 169 | 2.39560951514809 3.88839870936838 1 170 | 1.61957482932077 3.13487543828687 1 171 | 2.88545229476449 2.15574049495734 1 172 | 2.93047821322494 3.65059809369454 1 173 | 2.22613764732842 2.90908307565774 1 174 | 1.34191591750316 3.17554832693538 1 175 | 3.58429363285777 2.48277809261837 1 176 | 1.36963859187352 2.44771156895164 1 177 | 3.00333424371487 3.45497867517964 1 178 | 2.89837977934149 2.96618138671554 1 179 | 1.99593264340350 3.28671332527791 1 180 | 2.70177665184526 3.32789433811290 1 181 | 1.45589957658183 3.34695027413325 1 182 | 3.03888714292949 3.82072226651460 1 183 | 3.46213611975311 2.42948565611593 1 184 | 3.80533578761429 2.46930227054262 1 185 | 1.72038988608645 2.11576558392949 1 186 | 1.95678574856858 2.60777102205076 1 187 | 2.77137808459655 2.83362276784383 1 188 | 1.63124790546890 2.82671033753637 1 189 | 1.54059244348063 3.63976324058422 1 190 | 1.52679341147427 3.35716001064927 1 191 | 3.23775645294235 2.12754651519433 1 192 | 1.50540251962225 2.15671286785319 1 193 | 3.40478221130309 2.18722001732340 1 194 | 2.99861721259154 2.94366000582879 1 195 | 3.38853968096541 2.76311197576874 1 196 | 1.17220202854761 2.50438808914227 1 197 | 3.12610746509916 3.11550345462586 1 198 | 3.57862514317788 3.46684596321900 1 199 | 1.39420587742465 3.89715144642679 1 200 | 3.20533093151484 2.12563651712628 1 201 | 2.03549674829078 0.998180052432334 -1 202 | 0.198947298828911 0.311713297963476 -1 203 | 0.318381565349875 1.22447565490465 -1 204 | 0.765377678186267 0.362721170565693 -1 205 | 1.61099361505073 1.82105747572353 -1 206 | 1.85817289535330 1.37630004758540 -1 207 | 0.768113720014578 0.482661140858469 -1 208 | 0.206493857863197 1.32722518705998 -1 209 | 1.54567552628483 0.998730831255290 -1 210 | 2.55583077484840 1.88620257322563 -1 211 | 2.26511061567092 0.556356978969898 -1 212 | 2.02736226385857 1.50471507276992 -1 213 | 1.77064097003782 1.62151264405414 -1 214 | 2.74148016440909 1.27949872419372 -1 215 | 0.153972922312975 0.441030167632165 -1 216 | 0.176211021227216 1.82974754139994 -1 217 | 1.48073197127804 1.57035328421615 -1 218 | 1.40198071925994 1.01428713269700 -1 219 | 1.31328476462101 1.74977848699356 -1 220 | 2.59171259183672 1.27132818361712 -1 221 | 0.932996041466009 0.639604929716490 -1 222 | 0.799675124465009 1.40304365240358 -1 223 | 2.30764383784610 1.62190908814342 -1 224 | 2.39764049034112 0.569476263944224 -1 225 | 2.38142460319088 1.26496113655585 -1 226 | 0.759779466441336 0.804992008875767 -1 227 | 1.39162467514623 1.02603518250297 -1 228 | 1.64062890374660 0.738518543476274 -1 229 | 0.930575579903303 1.40152789201288 -1 230 | 2.76212348141244 1.19922330099496 -1 231 | 1.79030870232287 1.46665241264132 -1 232 | 1.16203307305788 0.938550194871949 -1 233 | 1.55172035888589 1.81811504531380 -1 234 | 1.38474524894693 1.27925450589941 -1 235 | 1.40918699479885 1.12448274991730 -1 236 | 0.217513077934885 1.58407403097684 -1 237 | 2.01380815951318 0.470606050362557 -1 238 | 0.141571164450984 1.51185714644799 -1 239 | 1.71740851560770 1.31029553149267 -1 240 | 1.90901010724253 1.45851087137376 -1 241 | 2.62888075143770 1.33193166937626 -1 242 | 0.443922614083249 1.42041084490091 -1 243 | 1.37459746265778 1.28581735262900 -1 244 | 1.94935396655576 1.35499447981981 -1 245 | 0.267658771343956 0.514313537653678 -1 246 | 0.578747493010430 1.81777940242015 -1 247 | 1.56840832366029 1.34572250637791 -1 248 | 2.00323064162444 0.250703636616019 -1 249 | 0.741459389262274 0.625750037534384 -1 250 | 2.70422661686471 0.523837895257407 -1 251 | 0.808568166625519 1.35113789514029 -1 252 | 1.94819838552713 1.40180217729796 -1 253 | 2.48574434524049 1.44549705059217 -1 254 | 0.155388010944798 1.17993098612622 -1 255 | 2.72449894706634 0.269604312481000 -1 256 | 1.68049773899131 1.82198517027158 -1 257 | 0.668440888648738 1.07184123164428 -1 258 | 0.461206457350546 1.48514546823001 -1 259 | 0.996805773461384 0.446272142942843 -1 260 | 2.89412610982058 1.41873019630946 -1 261 | 2.45310247148540 1.36395666562879 -1 262 | 2.06479513579518 1.08212038141295 -1 263 | 0.638233050308883 1.01207905751006 -1 264 | 1.69468861955132 1.63231850113037 -1 265 | 0.678175529594799 1.07633050633551 -1 266 | 2.40226194187573 0.756943249187720 -1 267 | 1.66110184881588 1.42726000610796 -1 268 | 0.677160239853560 1.29095026271775 -1 269 | 0.100253695552792 1.04937741336178 -1 270 | 1.72401268731529 0.113017894853740 -1 271 | 2.64354649119048 1.64140817083866 -1 272 | 2.30922155989953 1.55805092728463 -1 273 | 1.16484430406174 0.381777158849395 -1 274 | 1.10394186345112 1.29343115814644 -1 275 | 2.75601972888021 0.272643915160076 -1 276 | 2.20551664901368 0.202244613296998 -1 277 | 2.25790163806497 0.957455306958796 -1 278 | 0.228262677509909 1.65775458320408 -1 279 | 0.569427952058400 0.312886780479150 -1 280 | 0.873948483938227 0.681614631656324 -1 281 | 1.82735542637023 1.35331340735576 -1 282 | 2.91478043206925 1.93753239119001 -1 283 | 2.25897857422369 0.608860272120606 -1 284 | 2.81893917237443 0.860502391922779 -1 285 | 0.717376359913824 1.26389064565984 -1 286 | 0.142376754545390 0.180104604412868 -1 287 | 0.613747081810785 0.470860423397658 -1 288 | 2.15016905431643 0.919304987887956 -1 289 | 2.50965399402801 0.821111688444680 -1 290 | 2.48702376573659 1.48457450800555 -1 291 | 1.76400397677980 0.397061155423986 -1 292 | 1.60719452969078 0.955941172958504 -1 293 | 1.18169996503304 0.272475475617071 -1 294 | 0.838564525744899 0.721297789161336 -1 295 | 1.16906980742654 0.370580361918136 -1 296 | 1.00279415864880 0.620267001368786 -1 297 | 2.43735232888969 0.732865867373166 -1 298 | 2.58520611458032 0.595854553430141 -1 299 | 2.30998053604725 1.22703663981481 -1 300 | 0.151211192948536 1.39779617745821 -1 301 | 3.11338946089479 1.36520227757724 1 302 | 3.84621639044762 0.898841224830471 1 303 | 3.63664147793610 1.18417537396380 1 304 | 3.29329955477533 1.55161115840975 1 305 | 3.34449085164534 1.81009486506193 1 306 | 3.35370386734635 1.09216089468913 1 307 | 3.38360036067884 0.538993829979005 1 308 | 3.56345334248508 0.299218402878409 1 309 | 3.21658201820300 0.274690835426916 1 310 | 3.36425107470094 1.60337731368557 1 311 | 3.53652403215304 1.70298137883675 1 312 | 3.93341412568298 0.704307387116857 1 313 | 3.28667434303418 1.05871392067502 1 314 | 3.40005497337880 1.69003271715192 1 315 | 3.32101755641392 1.66088390735722 1 316 | 3.59538132117667 1.43090023710986 1 317 | 3.39199521031534 0.117156748477439 1 318 | 3.26357950608253 1.03866739845809 1 319 | 3.12060556279056 1.11757666217913 1 320 | 3.33475648619580 1.38804623697429 1 321 | 3.84914989245852 0.145660594152881 1 322 | 3.39002549101682 0.879600503433679 1 323 | 3.16800948486769 0.246804546970639 1 324 | 3.16151981181886 1.76563086125001 1 325 | 3.92391205403645 0.823917873801388 1 326 | 3.36654733913717 1.12363012288427 1 327 | 3.77324085312278 1.57688105369839 1 328 | 3.83376975947592 1.57623007822752 1 329 | 3.10541994869617 1.80181752179951 1 330 | 3.11526790510976 0.154420487772570 1 331 | 3.70473412891305 1.09606624509926 1 332 | 3.54590029344113 1.02914387126992 1 333 | 3.15582162349148 0.525575153952484 1 334 | 3.19948427184066 1.93107646362737 1 335 | 3.15993901742046 0.211043763443185 1 336 | 3.66234053629976 0.736506873533874 1 337 | 3.20923634599876 1.27870097811859 1 338 | 3.73451494446157 1.58974450062555 1 339 | 3.32867306184816 0.902862591119215 1 340 | 3.44153551417452 1.22506332795466 1 341 | 3.75457433423880 1.84107933974275 1 342 | 3.21174506961670 0.573087044074772 1 343 | 3.42156418655920 1.93841103678750 1 344 | 3.38980855744338 1.76417138777993 1 345 | 3.30185937028677 0.507002003242266 1 346 | 3.94241493990327 1.85954434635012 1 347 | 3.64248425397657 1.03264205612413 1 348 | 3.40440059427701 1.52163641082286 1 349 | 3.76670845464854 0.153498471202762 1 350 | 3.14457504934834 0.698325120354148 1 351 | 3.77973363108395 1.26592506942454 1 352 | 3.93537800076048 0.392762013076443 1 353 | 3.62417479711488 0.671126852665278 1 354 | 3.17231673044977 1.72213468090107 1 355 | 3.60209622272757 0.660777804219900 1 356 | 3.24285958696436 0.728771993214111 1 357 | 3.15732919672302 1.30895623143689 1 358 | 3.30764273805584 1.50214873150654 1 359 | 3.35416207332832 0.874807925055388 1 360 | 3.14745736469175 0.157084191775508 1 361 | 3.25859332643927 0.601175451057631 1 362 | 3.15630679803055 0.716963331055989 1 363 | 3.14241973679970 0.287420726547693 1 364 | 3.16108930925749 1.77334908122516 1 365 | 3.36502814016782 1.19477250699387 1 366 | 3.42098136402381 1.86039309728151 1 367 | 3.73103590155364 1.74112272144867 1 368 | 3.60906173223182 1.37581524735130 1 369 | 3.43244902877857 0.513056315083341 1 370 | 3.21157102456403 0.419084899402526 1 371 | 3.89780821561812 0.163791613448003 1 372 | 3.84762344293956 1.06630578090842 1 373 | 3.78701026193239 0.928022505087621 1 374 | 3.39455811140813 1.50072891368550 1 375 | 3.40618098547300 1.12972235541264 1 376 | 3.85541306664408 1.65430313716261 1 377 | 3.30742312588679 0.905893693625409 1 378 | 3.80426992721024 0.382501076444865 1 379 | 3.74426084458129 1.72154070177666 1 380 | 3.52680505471462 1.15499345840481 1 381 | 3.82375966156319 1.92158906091683 1 382 | 3.77168151905996 0.748763442213954 1 383 | 3.52661911507849 0.572142718552032 1 384 | 3.53339090000226 0.245446726842109 1 385 | 3.14751438109290 1.27472755326068 1 386 | 3.62436787107333 0.250725912543803 1 387 | 3.61809259078740 1.10764068430621 1 388 | 3.82753339140559 1.28897566569877 1 389 | 3.33088790215491 0.297488724077894 1 390 | 3.15143641130268 1.25048356169189 1 391 | 3.69420954225955 0.608639055675723 1 392 | 3.45867389889274 0.776771996541682 1 393 | 3.16983491320712 0.229074108227552 1 394 | 3.20275617640805 1.35402804425247 1 395 | 3.28609580252426 0.538967880762340 1 396 | 3.10733942816074 1.44515579376077 1 397 | 3.57687905849575 1.12114932543055 1 398 | 3.73091553940855 1.62557148667084 1 399 | 3.43438550210054 0.351495752852716 1 400 | 3.26638172820676 1.43255350143957 1 401 | -------------------------------------------------------------------------------- /ML/5.Random Forest/feature_file: -------------------------------------------------------------------------------- 1 | 0 0 2 | 0 0 3 | 0 0 4 | 0 0 5 | 0 0 6 | 0 0 7 | 0 0 8 | 0 0 9 | 0 0 10 | 0 0 11 | 0 0 12 | 0 0 13 | 0 0 14 | 0 0 15 | 0 0 16 | 0 0 17 | 0 0 18 | 0 0 19 | 0 0 20 | 0 0 21 | 0 0 22 | 0 0 23 | 0 0 24 | 0 0 25 | 0 0 26 | 0 0 27 | 0 0 28 | 0 0 29 | 0 0 30 | 0 0 31 | 0 0 32 | 0 0 33 | 0 0 34 | 0 0 35 | 0 0 36 | 0 0 37 | 0 0 38 | 0 0 39 | 0 0 40 | 0 0 41 | 0 0 42 | 0 0 43 | 0 0 44 | 0 0 45 | 0 0 46 | 0 0 47 | 0 0 48 | 0 0 49 | 0 0 50 | 0 0 51 | -------------------------------------------------------------------------------- /ML/5.Random Forest/result_file: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chaucergit/Code-and-Algorithm/1b9773e0c845e681fcace1ad74094a063f5910d6/ML/5.Random Forest/result_file -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Code-and-Algorithm 2 | Machine Learning & Deep Learning 的学习记录,后期整理出自己完成的一些小Project并上传Github 3 | -------------------------------------------------------------------------------- /机器学习——基础知识.md: -------------------------------------------------------------------------------- 1 | 信息论相关的内容: 2 | 1、随机变量的熵: 3 | Entropy(x)=-∑p(x)logp(x) 4 | 注: 5 | (1)、Entropy(x)表示 6 | (2)、熵用来表示所有信息量的期望; 7 | (3)、信息熵越大,包含的信息就越多,那么随机变量的不确定性就越大。 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | --------------------------------------------------------------------------------