python一线开发 [2021]从0开始的tensorflow2.0 (一) 开个新坑,最近打算写一些不专业的tensorflow2.0的入门。不专业是因为我对算法这块研究不是特别多,所以有些写错了请在评论区指出来 首先了下面这种方程 ![](https://key08.com/usr/uploads/2021/07/794512951.png) 假设x为变量、Y就是真实值, 所谓机器学习 就是用一些算法如种群算法、遗传算法等,求出这个方程的最接近Y = 0的值,那么这个Y就自然是跟 真实的值很接近了. 这就是机器学习的**不怎么正确**但是搭边的一个理解 ## 安装 请记住 tensorflow2.0是不支持比较新的python版本的! 不要直接装新版本的python 会跑不起来的!装**Python 3.7.9**就挺合适的 阅读全文 2021-07-04 huoji 0 条评论
python一线开发 [2021]手撸机器学习入门之jaya算法 总所周知,所谓机器学习就是一个解方程给x求y的过程罢了. 这是一篇入门的算法代码,又叫做jaya算法(不是java!!!),网上没什么资料,以下是自己python写的. ```python import math import random import matplotlib.pyplot as plt import numpy as np g_group_size = 5000 g_var_weight = 2 g_max_iteration = 100 g_array_f = [0 for i in range(0, g_group_size)] g_array_best = [0 for i in range(0, g_group_size)] g_array_worst = [0 for i in range(0, g_group_size)] g_array_pop_new = [[0 for i in range(0, g_var_weight)] for i in range(0, g_group_size)] #g_number_a = -2 * math.pi #g_number_b = 2 * math.pi #g_number_a = -5.12 #g_number_b = 5.12 g_number_a = -100 g_number_b = 100 g_result = [] array_pop = [[0 for i in range(0, g_var_weight)] for i in range(0, g_group_size)] def fn_objective(pArray): result = 0 for i in range(0, g_var_weight): # result += pArray[i] * pArray[i] # result += math.sin(pArray[i]) * math.pow(math.e, math.pow(1 - math.cos(result), 2)) + math.cos(result) * math.pow(math.e, math.pow(1 - math.sin(pArray[i]), 2)) + math.pow(pArray[i] - result, 2) # result += -(1 + math.cos(12 * math.sqrt(math.pow(pArray[i], 2) + math.pow(result, 2)))) / (0.5 * (math.pow(pArray[i], 2) + math.pow(result, 2)) + 2) result += math.pow(pArray[i], 2) + 2 * math.pow(result, 2) - 0.3 * math.cos(3 * math.pi * pArray[i]) - 0.4 * math.cos(4 * math.pi * result) + 0.7 return result def fn_clean_array(pArray): for i in range(0, g_group_size): for z in range(0, g_var_weight): if pArray[i][z] < g_number_a: pArray[i][z] = g_number_a if pArray[i][z] > g_number_b: pArray[i][z] = g_number_b return pArray for i in range(0, len(array_pop)): for z in range(0, len(array_pop[i])): array_pop[i][z] = random.uniform(g_number_a, g_number_b) g_array_f[i] = fn_objective(array_pop[i]) print("array_pop: ") print(array_pop) print("g_array_f: ") print(g_array_f) iter_num = 1 min_index = 0 max_index = 0 while iter_num <= g_max_iteration: min_value = min(g_array_f) min_index = g_array_f.index(min_value) - 1 g_array_best = array_pop[min_index] max_value = max(g_array_f) max_index = g_array_f.index(max_value) - 1 g_array_worst = array_pop[max_index] for i in range(0, g_group_size): for z in range(0, g_var_weight): wrost_number = g_array_worst[z] - math.fabs(array_pop[i][z]) best_number = g_array_best[z] - math.fabs(array_pop[i][z]) g_array_pop_new[i][z] = array_pop[i][z] + \ random.random() * best_number - wrost_number g_array_pop_new = fn_clean_array(g_array_pop_new) f_new = 0 for i in range(0, len(g_array_pop_new)): f_new = fn_objective(g_array_pop_new[i]) if f_new < g_array_f[i]: g_array_f[i] = f_new for z in range(g_var_weight): array_pop[i][z] = g_array_pop_new[i][z] min_value = min(g_array_f) min_index = g_array_f.index(min_value) g_array_best = array_pop[min_index] print("min number {} ".format(g_array_f[min_index])) print("x=") print(g_array_best) #g_result.insert(0, g_array_f[min_index]) g_result.append(g_array_f[min_index]) iter_num += 1 x = np.arange(0, len(g_result)) y = np.array(g_result) plt.title("jaya") plt.xlabel("min number") plt.ylabel("y") plt.plot(x, y) plt.show() ``` 请注意有一些公式需要很大的族群范围才能收敛 论文里面的收敛图: ![](https://key08.com/usr/uploads/2021/05/2290756614.png) 这边的: ![](https://key08.com/usr/uploads/2021/05/3846208643.png) 阅读全文 2021-05-24 huoji 0 条评论
python一线开发 [2021]最佳路径算法与状态转移方程与三角形最大路径问题 做游戏的动态巡路用 ```python # 状态转移方程: 最佳路径(i, j) = 目标数组(i, j) + max{最佳路径(i + 1, j), 最佳路径(i + 1, j + 1)}。 def walk_array(pArray): saved_path = [] saved_array = pArray[-1] # 最佳路径要倒叙 for i in range(len(pArray)-2, -1, -1): # 从底往上 # 更改dp前j个的值 save_path_num = 0 for j in range(i+1): max_num = max(saved_array[j], saved_array[j+1]) saved_array[j] = pArray[i][j] + max_num save_path_num = pArray[i][j] saved_path.append(save_path_num) return (saved_array[0], saved_path) ``` ```python (max_num, saved_path) = walk_array(triangle_array) print('最大值: {}, 路径:'.format(max_num)) for item in range(len(saved_path)-1, -1, -1): print("{}->".format(saved_path[item])) ``` ```python triangle_array = [ [7], [3, 8], [8, 1, 0], [2, 7, 4, 4], [4, 5, 2, 6, 5] ] ``` 时间复杂度n2 阅读全文 2021-04-05 huoji 0 条评论