python
飞翔的貅貅
这个作者很懒,什么都没留下…
展开
-
子串和子序列(python)
子串:串中任意个连续的字符组成的子序列称为该串的子串; 子序列:序列的一部分项按原有次序排列而得的序列;# -*- coding=utf-8 -*-##### 1: 连续子串最大和 #####def MaxSum(arr): res, s = arr[0], arr[0] for x in arr[1:]: s = max(x, s+x) res = max(res, s) return res##### 2: 连续子串最大乘积 ###原创 2020-09-04 17:07:32 · 1153 阅读 · 1 评论 -
0-1 背包问题(python)
#-*- coding: utf-8 -*-def recur(k, cap): if k < 0: return 0 if weight[k] > cap: return recur(k-1, cap) else: return max(recur(k-1, cap), recur(k-1, cap-weight[k]) + value[k])def dp(n, cap): B = [[0] * (c...原创 2020-08-06 16:36:28 · 359 阅读 · 0 评论 -
DP 动态规划 python实现
注:全文参考正月点灯笼b站up主!!1、求数组内不相邻数的最大和。arr = [1,2,4,1,7,8,3]def ret_opt(arr, i): if i == 0: return arr[0] elif i == 1: return max(arr[0], arr[1]) else: A = ret_opt(arr, i-2) + arr[i] B = ret_opt(arr, i-1)原创 2020-06-20 10:18:09 · 1002 阅读 · 1 评论 -
BFS、DFS以及Dijkstra算法 python实现
注:全文参考正月点灯笼b站up主!!BFS:广度优先搜索,队列,先进先出;DFS:深度优先搜索,栈, 先进后出;Dijkstra:最短路径问题;1、BFS和DFSgraph = { "A" : ["B","C"], "B" : ["A","C","D"], "C" : ["A","B","D","E"], "D" : ["B","C","E","F"], "E" : ["C","D"], "F" : ["D"]}def BFS(原创 2020-06-19 16:55:26 · 378 阅读 · 0 评论 -
二叉树遍历(python)
注:参考博客 不积跬步无以至千里 ,以这个二叉树为例:# -*- coding:utf-8 -*-class TreeNode: def __init__(self, x=None, left=None, right=None): self.val = x self.left = left self.right = right class Order: def PreOrder(self, pRoot): i原创 2020-06-16 20:56:55 · 303 阅读 · 0 评论 -
梯度下降法求函数收敛值
def f(x,y): return x-y+2*x*x+2*x*y+y*y def fx(x,y): return 1+4*x+2*y def fy(x,y): return -1+2*x+2*ylr = 0.001 x, y = 0, 0n_x, n_y = x, yerror = Falsewhile error == False: n_x -= lr*fx(x,y) n_y -= lr*fy(x,y) if f(x,y) - f(n_x,n_y) .原创 2020-06-10 16:49:09 · 378 阅读 · 0 评论 -
python 排序算法
def mergeSort(data): def merge(left,right): result = [] i = j = 0 while i < len(left) and j < len(right): if left[i] <= right[j]: result.append(left[i]) i += 1 el.原创 2020-06-07 13:22:38 · 219 阅读 · 0 评论 -
python 输入
1、单行输入:s_in = input().strip()2、多行输入:import syss_in = []for line in sys.stdin: s_in.append(line.strip().split(' '))原创 2020-06-07 10:30:03 · 144 阅读 · 0 评论 -
python 列表全排列
def perm(data): if len(data) == 1: return [data] r = [] for i in range(len(data)): s = data[:i] + data[i+1:] p = perm(s) for x in p: r.append(data[i:i+1]+x) return rarr = [1,2,3]print(perm(arr.原创 2020-05-10 22:15:32 · 807 阅读 · 0 评论 -
conda-离线
1、离线创建虚拟环境(1)复制envs目录下的已有环境文件夹(2)复制anaconda3/pkgs文件夹(3)conda create -n [name] --clone [env-filepath] --offline(4)或者复制已有环境:conda create -n [name] --clone base2、离线安装包(1)whl文件:pip install xxx...原创 2020-01-07 10:20:22 · 1747 阅读 · 0 评论 -
python asyncore异步通信
import asyncoreimport socketclass Server(asyncore.dispatcher): def __init__(self, host, port): asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK...原创 2019-12-09 20:01:04 · 366 阅读 · 1 评论 -
python-random
随机种子:tf.set_random_seed(0)np.random.seed(0)random.seed(0)1、random.random() #产生 0 到 1 之间的随机浮点数2、random.randint(1,10) #产生 1 到 10 的一个整数型随机数3、random.uniform(1.2,2.4) #产生 1.2 到 2.4 之间的随机浮...原创 2019-12-08 20:39:56 · 116 阅读 · 0 评论 -
mnist-TSNE特征
import kerasimport matplotlib.pyplot as pltfrom sklearn.manifold import TSNE(x_train,y_train), _ = keras.datasets.mnist.load_data()x_train = x_train.reshape((x_train.shape[0],-1))tsne = TSNE(n...原创 2019-11-28 22:33:00 · 1198 阅读 · 2 评论 -
cycle-GAN keras
./datasets/lane2road/trainA(B)from keras_contrib.layers.normalization.instancenormalization import InstanceNormalizationfrom keras.layers import Input, Concatenate, Activationfrom keras.layers.ad...原创 2019-11-16 13:28:33 · 1209 阅读 · 2 评论 -
GAN-keras
from keras.datasets import mnistfrom keras.layers import Input, Dense, Reshape, Flattenfrom keras.layers import BatchNormalization, Activationfrom keras.layers.advanced_activations import LeakyReL...原创 2019-11-15 11:21:39 · 236 阅读 · 0 评论 -
TCP测试
server.pyfrom socket import *tcpSerSock = socket(AF_INET,SOCK_STREAM)tcpSerSock.bind(('127.0.0.1',20000))tcpSerSock.listen(5)tcpCliSock, addr = tcpSerSock.accept()while True: data = str(...原创 2019-10-16 14:46:13 · 339 阅读 · 0 评论 -
image
from PIL import Imageimg = Image.open('xxx.jpg')img = img.resize((224,224),Image.ANTIALIAS)box = (0, 180, 640, 480) #设置图像裁剪区域img = img.crop(box) #图像裁剪img.show()img.save('xxx1.jpg')from skim...原创 2019-03-26 10:46:48 · 797 阅读 · 0 评论 -
Matplotlib画图
import numpy as np import matplotlib.pyplot as pltx = np.array([1,2,3,4,5,6,7,8]) y = np.array([3,5,7,6,2,6,10,15]) plt.plot(x,y)# 折线 plt.show()cnt = 0plt.figure()for i in range(1,33): ...原创 2019-03-24 19:59:27 · 143 阅读 · 0 评论 -
txt读写
一、txt文件的打开和创建result = []with open('xxx.txt', 'r') as f: while True: line = f.readline() if not line: break pass tmp = [a for a in lines.split()] result.append(tmp) ...原创 2019-04-26 21:02:42 · 264 阅读 · 0 评论 -
opencv-python
# pip install opencv-pythonimport cv2#open imageimg = cv2.imread("xxx.img",cv2.IMREAD_COLOR) #cv2.IMREAD_GRAYSCALErows,cols = img.shapecv2.imshow("image",img)resize = cv2.resize(img,(640, 480...原创 2019-03-24 17:45:13 · 200 阅读 · 0 评论 -
os sys
import osos.getcwd()os.listdir('/home/hp')os.path.split(path)os.path.join(path1,path2) os.path.dirname(path)os.path.basename(path)os.path.getsize(path)os.path.exists(path)os.path.isdir('xxx...原创 2019-03-27 13:39:42 · 365 阅读 · 0 评论 -
math
import mathceil:取大于等于x的最小的整数值,如果x是一个整数,则返回xcopysign:把y的正负号加到x前面,可以使用0cos:求x的余弦,x必须是弧度degrees:把x从弧度转换成角度e:表示一个常量exp:返回math.e,也就是2.71828的x次方expm1:返回math.e的x(其值为2.71828)次方的值减1fabs:返回x的绝对值...翻译 2019-03-27 13:55:34 · 301 阅读 · 0 评论 -
Augmentor
#pip install Augmentorimport Augmentorp = Augmentor.Pipeline("image/")p.random_erasing(1, 0.3)p.skew_top_bottom(1,0.005)p.rotate(probability=0.7, max_left_rotation=10, max_right_rotation=10)...原创 2019-04-09 11:22:56 · 655 阅读 · 0 评论 -
Matplotlib使用总结图
Matplotlib使用总结图 # 使用该魔法,不用写plt.show(),以及可以边写边运行%matplotlib notebookimport matplotlib.pyplot as pltplt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签plt.rcParams['axes.unicode_minus']=False ...转载 2019-06-12 11:26:50 · 184 阅读 · 0 评论 -
输入
import syssin = []for line in sys.stdin: a = line.split() sin.append(a)print(sin)注:Ctrl+D 退出输入原创 2019-08-03 20:16:20 · 150 阅读 · 0 评论 -
keras 二分类
文件结构:classify train.py test.py train cat xxx.jpg xxx.jpg dog xxx.jpg xxx.jpg test xxx.jpg xxx.jpg train.py:import globfrom PIL import Image, ImageOpsimport ...原创 2019-08-31 17:37:21 · 379 阅读 · 0 评论 -
Policy_Gradient-cartpole (keras)
import osos.environ["TF_CPP_MIN_LOG_LEVEL"]='3'import sysimport gymimport numpy as npfrom keras.layers import Densefrom keras.models import Sequentialfrom keras.optimizers import Adamclass P...原创 2019-09-17 22:04:25 · 617 阅读 · 0 评论 -
numpy
#pip install numpyimport numpy as npa = np.zeros((5,3),np.float32)#print(a.dtype,a.shape)b = a.T.reshape(-1,2)c = np.mgrid[0:5:5j,0:6:5j]a = np.array([[1, 2], [3, 4]])b = np.array([[5, 6...原创 2019-03-24 19:45:46 · 122 阅读 · 0 评论