def knn(unknown_data, feature, feature_u, feature_sigma, label): try: # 未知数据归一化 unknown_data = (unknown_data - feature_u) / feature_sigma feature_size = np.shape(feature) # 未知数据与已知数据的距离 unknown_data = unknown_data.repeat(feature_size[0], axis=0) unknown_data = unknown_data - feature unknown_data = np.multiply(unknown_data, unknown_data) unknown_data = np.sum(unknown_data, axis=1) unknown_data = np.sqrt(unknown_data) # 未知数据距离排序取前五的类别 unknown_data = [i[0] for i in unknown_data.tolist()] k_label = [ i[0] for i in sorted(enumerate(unknown_data), key=lambda x: x[1]) ] k_label = k_label[0:5] k_label = [label_data[i] for i in k_label] return k_label except Exception as msg: tools.printInfo(2, msg) sys.exit()
def tensorflow_train(feature, label, maxCycle, alpha): try: # 定义神经网络的参数 w = tf.Variable(tf.random_normal([3, 4])) # 存放训练数据的位置 x = tf.placeholder(tf.float32, shape=(None, 3), name='x-input') y_real = tf.placeholder(tf.float32, shape=(None, 1), name='y-input') # 定义神经网络的传播过程 y_predict = tf.matmul(x, w) cross_entropy = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( logits=y_predict, labels=[int(i[0]) for i in label.tolist()])) train_step = tf.train.GradientDescentOptimizer(alpha).minimize( cross_entropy) # 创建会话 with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(maxCycle): sess.run(train_step, feed_dict={x: feature, y_real: label}) if i % 200 == 0: total_cross_entropy = sess.run(cross_entropy, feed_dict={ x: feature, y_real: label }) print(i, total_cross_entropy) return sess.run(w) except Exception as msg: tools.printInfo(2, msg) sys.exit()
def show_model(feature, w, label): try: w_size = np.shape(w) feature_x = [float(i[0]) for i in feature[:, 1]] plt.title('logistic regression') plt.xlabel('x') plt.ylabel('y') point_type = ['o', '*', '+', '.'] for i in range(w_size[1]): w_temp = w[:, i] w_temp = w_temp.tolist() print(w_temp) point_x = [] point_y = [] for j in range(len(label)): if label[j] == i: point_x.append(float(feature[:, 1][j])) point_y.append(float(feature[:, 2][j])) feature_y = [ -(w_temp[0] + w_temp[1] * k) / w_temp[2] for k in feature_x ] plt.plot(point_x, point_y, point_type[i]) plt.plot(feature_x, feature_y, 'r') plt.show() except Exception as msg: tools.printInfo(2, msg) sys.exit()
def save_model(w): try: w = w.tolist() w = [str(w[0][0]), str(w[1][0]), str(w[2][0])] tools.writeFile(2, 'w_info.txt', '\t'.join(w)) except Exception as msg: tools.printInfo(2, msg) sys.exit()
def sigmoid_show(): try: x = [i for i in range(-10, 11)] y = [1 / (1 + math.exp(-i)) for i in x] plt.title("sigmoid") plt.xlabel("x") plt.ylabel("y") plt.plot(x, y) plt.show() except Exception as msg: tools.printInfo(2, msg)
def save_model(w): try: if os.path.exists('w_info.txt'): os.remove('w_info.txt') w = w.tolist() for i in w: i = [str(j) for j in i] tools.writeFile(1, 'w_info.txt', '\t'.join(i)) except Exception as msg: tools.printInfo(2, msg) sys.exit()
def save_model(w): try: if os.path.exists('w_info.txt'): os.remove('w_info.txt') for i in range(np.shape(w)[1]): w_temp = w[:, i] w_temp = w_temp.tolist() w_temp = [str(w_temp[0]), str(w_temp[1]), str(w_temp[2])] tools.writeFile(1, 'w_info.txt', '\t'.join(w_temp)) except Exception as msg: tools.printInfo(2, msg) sys.exit()
def cost(err, label): try: m = np.shape(err)[0] sum_cost = 0.0 for i in range(m): if err[i, label[i, 0]] / np.sum(err[i, :]) > 0: sum_cost -= np.log(err[i, label[i, 0]] / np.sum(err[i, :])) else: sum_cost -= 0 return sum_cost / m except Exception as msg: tools.printInfo(2, msg) sys.exit()
def load_data(file_path): try: feature = [] label = [] with open(file_path) as f: for line in f: feature_temp = [] line = [float(i) for i in line.strip().split()] feature_temp.append(line[0:3]) label.append(int(line[-1])) feature.extend(feature_temp) return np.mat(feature), label except Exception as msg: tools.printInfo(2, msg) sys.exit()
def error_rate(sigmoid_value, label): try: data_num = np.shape(sigmoid_value)[0] sum_err = 0.0 for i in range(data_num): if 0 < sigmoid_value[i, 0] < 1: sum_err -= ( label[i, 0] * np.log(sigmoid_value[i, 0]) + (1 - label[i, 0]) * np.log(1 - sigmoid_value[i, 0])) else: sum_err -= 0 return float(sum_err / data_num) except Exception as msg: tools.printInfo(2, msg) sys.exit()
def show_model(feature, w): try: w = w.tolist() feature_x = [float(i[0]) for i in feature[:, 1]] feature_y = [float(i[0]) for i in feature[:, 2]] w_y = [(w[0][0] - w[1][0] * i) / w[2][0] for i in feature_x] plt.title('logistic regression') plt.xlabel('x') plt.ylabel('y') plt.plot(feature_x, feature_y, 'ob') plt.plot(feature_x, w_y, 'r') plt.show() except Exception as msg: tools.printInfo(2, msg)
def load_data(file_path): try: feature_data = [] label_data = [] with open(file_path) as f: for line in f: feature_temp = [ 1, ] line = [float(i) for i in line.strip().split('\t')] feature_temp.extend(line[0:2]) label_data.append(int(line[-1])) feature_data.append(feature_temp) return np.mat(feature_data), np.mat(label_data).T, len(set(label_data)) except Exception as msg: tools.printInfo(2, msg) sys.exit()
def tensorflow_train(feature, label, maxCycle, alpha): try: # 定义神经网络的参数 w = tf.Variable(tf.ones([3, 1])) # 存放训练数据的位置 x = tf.placeholder(tf.float32, shape=(None, 3), name='x-input') y_real = tf.placeholder(tf.float32, shape=(None, 1), name='y-input') # 定义神经网络的传播过程 y_predict = tf.matmul(x, w) # 定义损失函数 y_predict = tf.sigmoid(y_predict) cross_entropy = -tf.reduce_mean( y_real * tf.log(tf.clip_by_value(y_predict, 1e-10, 1.0)) + (1 - y_real) * tf.log(tf.clip_by_value(1 - y_predict, 1e-10, 1.0))) train_step = tf.train.AdamOptimizer(alpha).minimize(cross_entropy) # 创建会话 with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(maxCycle): sess.run(train_step, feed_dict={x: feature, y_real: label}) if i % 100 == 0: total_cross_entropy = sess.run(cross_entropy, feed_dict={ x: feature, y_real: label }) print(i, total_cross_entropy) return sess.run(w) except Exception as msg: tools.printInfo(2, msg) sys.exit()
def normalization_data(feature): try: # 计算特征的均值和标准差 feature_u = feature.mean(axis=0) feature_sigma = np.std(feature, axis=0) # 计算特征矩阵的大小 feature_size = np.shape(feature) feature_u_temp = feature_u.repeat(feature_size[0], axis=0) feature_sigma_temp = feature_sigma.repeat(feature_size[0], axis=0) # 归一化 feature = (feature - feature_u_temp) / feature_sigma_temp return feature, feature_u, feature_sigma except Exception as msg: tools.printInfo(2, msg) sys.exit()
def train_data(feature, label, maxCycle, alpha): try: feature_num = np.shape(feature)[1] w = np.mat(np.ones((feature_num, 1))) i = 0 while i < maxCycle: i += 1 sigmoid_value = sigmoid(feature * w) err = label - sigmoid_value if i % 100 == 0: tools.printInfo( 1, '训练次数:{0},错误率:{1}'.format(i, error_rate(sigmoid_value, label))) w = w + alpha * feature.T * err return w except Exception as msg: tools.printInfo(2, msg) sys.exit()
def gradient_descent(): try: x_before = float(input('请输出初始x的值(-9,9):')) step = float(input('请输入步长(0,1):')) stop_value = float(input('请输入梯度下降的停止值:')) if x_before <= -9 or x_before >= 9 or step <= 0 or step >= 1: tools.printInfo(3, '输入有误') sys.exit() x = [i for i in range(-10, 11)] y = [i**2 for i in x] x_temp = [] y_temp = [] OK = True while OK: x_after = x_before - step * 2 * x_before y_before = x_before * x_before y_after = x_after * x_after y_change = math.fabs(y_before - y_after) if y_change < stop_value: OK = False else: x_temp.extend([x_before, x_after, x_after]) y_temp.extend([y_before, y_before, y_after]) x_before = x_after plt.title("gradient descent") plt.xlabel("x") plt.ylabel("y") plt.plot(x, y) plt.plot(x_temp, y_temp, 'r') plt.show() except Exception as msg: tools.printInfo(2, msg)
def train_data(feature, label, label_num, maxCycle, alpha): try: data_num, feature_num = np.shape(feature) w = np.mat(np.ones((feature_num, label_num))) i = 0 while i < maxCycle: i += 1 err = np.exp(feature * w) if i % 500 == 0: tools.printInfo( 1, '训练次数:{0},损失函数值:{1}'.format(str(i), str(cost(err, label)))) rowSum = -err.sum(axis=1) rowSum = rowSum.repeat(label_num, axis=1) err = err / rowSum for x in range(data_num): err[x, label[x, 0]] += 1 w = w + (alpha / data_num) * feature.T * err return w except Exception as msg: tools.printInfo(2, msg) sys.exit()
def load_data(file_path): """ 导入训练数据 :param file_path:数据绝对路径 :return: 返回特征,标签 """ try: feature_data = [] label_data = [] with open(file_path) as f: for line in f: feature_temp = [] label_temp = [] line = line.strip().split('\t') line = [float(i) for i in line] feature_temp.append(1) feature_temp.extend(line[0:2]) label_temp.append(line[-1]) feature_data.append(feature_temp) label_data.append(label_temp) return np.mat(feature_data), np.mat(label_data) except Exception as msg: tools.printInfo(2, msg) sys.exit()
m = np.shape(err)[0] sum_cost = 0.0 for i in range(m): if err[i, label[i, 0]] / np.sum(err[i, :]) > 0: sum_cost -= np.log(err[i, label[i, 0]] / np.sum(err[i, :])) else: sum_cost -= 0 return sum_cost / m except Exception as msg: tools.printInfo(2, msg) sys.exit() if __name__ == '__main__': # 导入训练数据 tools.printInfo(1, '导入训练数据') data_path = os.path.abspath(sys.argv[1]) feature, label, label_num = load_data(data_path) # 训练数据 maxCycle = int(input('请输入最大循环次数(比0大):\n')) alpha = float(input('请输入学习率(0, 1):\n')) if maxCycle <= 0: tools.printInfo(3, '最大循环次数数值错误,请重新运行并输入正确的值!') sys.exit() if alpha <= 0 or alpha >= 1: tools.printInfo(3, '学习率数值错误,请重新运行并输入正确的值!') sys.exit() tools.printInfo(1, '最大循环次数和学习率符合范围,开始训练数据:') w = train_data(feature, label, label_num, maxCycle, alpha)
grid_predictions = grid_predictions.reshape(xx.shape) plt.subplot(2, 2, 1) plt.contourf(xx, yy, grid_predictions, cmap=plt.cm.Paired, alpha=0.8) plt.plot(class_1_x, class_1_y, 'ro', label='class 1') plt.plot(class_2_x, class_2_y, 'kx', label='class 2') plt.legend(loc='lower right') plt.ylim([-1.5, 1.5]) plt.xlim([-1.5, 1.5]) plt.subplot(2, 2, 2) plt.plot(batch_accuracy, 'k-', label='accuracy') plt.title('batch accuracy') plt.xlabel('generation') plt.ylabel('accuracy') plt.legend('lower eight') plt.subplot(2, 2, 3) plt.plot(loss_values, 'k-') plt.title('loss per generation') plt.xlabel('generation') plt.ylabel('loss') plt.show() except Exception as msg: tools.printInfo(2, msg) sys.exit()
def sigmoid(x): try: return 1.0 / (1 + np.exp(-x)) except Exception as msg: tools.printInfo(2, msg) sys.exit()