python
hustfc
这个作者很懒,什么都没留下…
展开
-
使用python tensorly 实现张量CP分解
使用tensorly.decomposition来进行张量分解import numpy as npimport tensorly as tlfrom tensorly.decomposition import parafac #CPdecomposition就像一个矩阵可以分解为两个向量的外积总和,一个三阶张量可以表示为三个向量的外积之和,求和中的项数成为张量的Kruska...原创 2018-06-22 11:43:36 · 9259 阅读 · 21 评论 -
使用python tensorly 实现张量tucker分解
Tucker Decomposition可以看作是张量的PCA(principal components analysis),将一个张量分解为一个核心张量和因子矩阵乘积形式 采用tucker函数来进行TD分解,首先需要设定rankimport tensorly as tlimport numpy as npfrom tensorly.decomposition import tu...原创 2018-06-22 11:51:25 · 10054 阅读 · 14 评论 -
python map() reduce() filter()函数
b = map(lambda x : x ** 2, a) #计算平方print(list(b))c = map(lambda x, y : x + y, [1, 3, 4], [2, 4, 6]) #可以处理多个链表print(list(c))def square(x): return x ** 2b = map(square, a) #也可以自己定义函数print(lis...原创 2018-06-23 21:01:48 · 114 阅读 · 0 评论 -
python图标绘制基础:matplotlib
import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 10, 1000) #作图的自变量0~10,中间1000个单位y = np.sin(x) + 1z = np.cos(x ** 2) + 1plt.figure(num = 2, figsize=(8, 4)) #图像编号,设置图像大小plt....原创 2018-06-23 21:03:07 · 1539 阅读 · 0 评论 -
OJ数串 *c++ python*
题目描述设有n个正整数,将他们连接成一排,组成一个最大的多位整数。如:n=3时,3个整数13,312,343,连成的最大整数为34331213。如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。输入描述有多组测试样例,每组测试样例包含两行,第一行为一个整数N(N<=100),第二行包含N个数(每个数不超过1000,空格分开)。输出...原创 2018-06-10 16:44:33 · 165 阅读 · 0 评论 -
张量tensorly笔记(1)——张量基础
一个非常形象的图片表示了从标量到张量的演变过程使用python里面的numpy库和tensorly库来学习张量张量构建 import numpy as npimport tensorly as tlX = tl.tensor(np.arange(24).reshape(3, 4, 2))reshape将np创建的数组变换维度,返回的是一个3*4*2的三维张量 ...原创 2018-06-20 11:23:59 · 4736 阅读 · 6 评论 -
python 统计缺失值,异常值,画箱型图
import pandas as pdcatering_sale = 'B:\pycharm\DataMining\catering_sale.xls'data = pd.read_excel(catering_sale, index_col=u'日期') # 指定主码,有中文出现的时候,需要加udescribe = data.describe()print(describe)cou...原创 2018-07-03 10:53:58 · 5730 阅读 · 0 评论 -
python 数据基本分析
import pandas as pdcatering_sale = 'B:\pycharm\DataMining\catering_sale.xls'data = pd.read_excel(catering_sale, index_col=u'日期')data = data[(data[u'销量'] > 400) & (data[u'销量'] < 5000)] #过...原创 2018-07-03 11:37:09 · 680 阅读 · 0 评论 -
python决策树sklearn.tree、graphviz
根据天气的好坏、是否是周末、是否促销来判断销量的高低,对此使用决策树来决策 数据预处理import pandas as pdfilename = 'B:\pycharm\DataMining\data\sales_data.xls'data = pd.read_excel(filename, index_col='No')data[data == u'好'] = 1data[da...原创 2018-07-10 10:20:44 · 5290 阅读 · 0 评论 -
使用corr()计算相关系数
import pandas as pdcatering_sale = '.\data\catering_sale_all.xls'data = pd.read_excel(catering_sale, index_col=u'日期')#print(data)print(data.corr()) #相关系数矩阵,即给出任意两款菜之间的相关系数print(data.corr()[u'百合...原创 2018-07-03 15:29:11 · 35592 阅读 · 1 评论 -
python画图 折线图 饼图 柱状图
折线图import numpy as npfrom matplotlib import pyplot as pltplt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.reParams是一个配置表plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号plt.fi...原创 2018-07-03 16:48:31 · 1224 阅读 · 0 评论 -
modulenotfounderror: no module named 'xlwt'解决方案 python padans 输出excel出错
找到使用的编译器的地址 在当前文件夹下面pip install xlwt原创 2018-07-04 10:11:02 · 50035 阅读 · 1 评论 -
OJ 树的高度 *C++ python*
题目描述 现在有一棵合法的二叉树,树的节点都是用数字表示,现在给定这棵树上所有的父子关系,求这棵树的高度输入描述 输入的第一行表示节点的个数n(1 ≤ n ≤ 1000,节点的编号为0到n-1)组成,下面是n-1行,每行有两个整数,第一个数表示父节点的编号,第二个数表示子节点的编号输出描述 输出树的高度,为一个整数输入 50 10 21 3...原创 2018-07-18 10:47:27 · 341 阅读 · 0 评论 -
OJ句子反转*python C++*
题目描述 给定一个句子(只包含字母和空格), 将句子中的单词位置反转,单词用空格分割, 单词之间只有一个空格,前后没有空格。 比如: (1) “hello xiao mi”-&gt; “mi xiao hell输入描述 输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符)输出描述 对于每个测试示例,要求输出句子中单词反转后形成的句子...原创 2018-07-18 15:30:39 · 204 阅读 · 0 评论 -
python主成分分析PCA
主成分PCA分析的基本步骤:· 对数据进行归一化处理(代码中并非这么做的,而是直接减去均值) · 计算归一化后的数据集的协方差矩阵 · 计算协方差矩阵的特征值和特征向量 · 保留最重要的k个特征(通常k要小于n),也可以自己制定,也可以选择一个阈值,然后通过前k个特征值之 和减去后面n-k个特征值之和大于这个阈值,则选择这个k · 找出k个特征值对应的特征向量 · 将m...原创 2018-07-06 11:43:11 · 3452 阅读 · 0 评论 -
OJ电话号码分身 *C++ PYTHON*
题目描述 继MIUI8推出手机分身功能之后,MIUI9计划推出一个电话号码分身的功能: (&quot;ZERO&quot;, &quot;ONE&quot;, &quot;TWO&quot;, &quot;THREE&quot;, &quot;FOUR&quot;, &quot;FIVE&quot;,原创 2018-07-19 09:39:08 · 220 阅读 · 0 评论 -
OJ水仙花数 *C++ python*
题目描述 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的: “水仙花数”是指一个三位数,它的各位数字的立方和等于其本身, 比如:153=1^3+5^3+3^3。 现在要求输出所有在m和n范围内的水仙花数。输入描述 输入数据有多组,每组占一行,包括两个整数m和n(100 ≤ m ≤ n ≤ 999)。输出描述 对于每...原创 2018-07-20 10:23:38 · 530 阅读 · 0 评论 -
【leetcode】两个排序数组中的中位数(Median of Two Sorted Arrays)【python】
需要注意两点可能存在一个数组遍历完毕的情况,这时候只需要遍历另外一个数组数组可能为空,必须事先进行判断,否则会越界 算法思想 一次遍历两个数组,将较小的加入到新数组sums中,这样sums就是有序的,跟合并两个有序的链表的思想一样class Solution: def findMedianSortedArrays(self, nums1, nums2): ...原创 2018-08-30 16:25:44 · 924 阅读 · 0 评论 -
【leetcode】 两数之和(tow sum)【c++ python】
C++class Solution {public: vector&lt;int&gt; twoSum(vector&lt;int&gt;&amp; nums, int target) { vector&lt;int&gt; indexs; for(int i = 0;i &lt; nums.size();i ++){原创 2018-08-29 16:21:03 · 167 阅读 · 0 评论 -
python创建二维数组的坑
array = ['!'] * 10 rows = [array] * numRows #二维数组 但是你会发现更改一个值之后,所有行都被修改!!! 相当于创建了numRows个相同的指向array的指针,每次修改其中一个指针之后,所有的指针都会被修改!!!!!所以需要用另外一种方式进行创建多维数组,以免浅拷贝:&gt;&gt;&gt; l...原创 2018-09-04 21:03:32 · 3015 阅读 · 0 评论 -
【leetcode】Z字形变换【python】
坑1:二维数组创建错误:具体参考上一篇博客 坑2:numRows=1的情况下,步长设置错误 if numRows == 1: return sAC解答class Solution: def convert(self, s, numRows): &quot;&quot;&quot; :type s: s...原创 2018-09-04 21:15:11 · 304 阅读 · 0 评论 -
【leetcode】无重复字符的最长子串(Longest Substring Without Repeating Characters)【python】
题目链接:无重复字符的最长子串pythonclass Solution: def lengthOfLongestSubstring(self, s): &quot;&quot;&quot; :type s: str :rtype: int &quot;&quot;&quot; max = 0 a原创 2018-08-30 10:18:27 · 92 阅读 · 0 评论 -
【leetcode】反转整数(Reverse Integer)【python】
class Solution: def reverse(self, x): """ :type x: int :rtype: int """ if x == -2 ** 31: return 0 reversestr = '' if x &am原创 2018-09-09 14:14:07 · 493 阅读 · 0 评论 -
【leetcode】字符串转整数(String to Integer (atoi))【python】
题目链接 def isnum(x): if ord(x) &gt;= ord('0') and ord(x) &lt;= ord('9'): return 1 else: return 0class Solution: def myAtoi(self, str): """ :type str: st...原创 2018-09-09 15:28:18 · 150 阅读 · 0 评论 -
【leetcode】回文数(Palindrome Number)【python】
题目链接 class Solution: def isPalindrome(self, x): """ :type x: int :rtype: bool """ string = str(x) i, j = 0, len(string) - 1 while i != ..原创 2018-09-09 20:35:22 · 183 阅读 · 0 评论 -
【leetcode】回文链表(Palindrome Linked List)【python】三种方法
题目链接 时间复杂度O(N),空间复杂度O(N)class ListNode: def __init__(self, x): self.val = x self.next = Nonedef str_isPalindrome(nums, length): i, j = 0, length - 1 while i != j...原创 2018-09-10 20:57:28 · 1411 阅读 · 1 评论 -
Could not find a version that satisfies the requirement cv2 (from versions: )......解决方案
pythonCould not find a version that satisfies the requirement cv2 (from versions: ) No matching distribution found for cv2opencv中的cv2在opencv-python包里面pip install opencv-python即可使用cv2...原创 2018-10-11 10:37:07 · 15892 阅读 · 0 评论 -
【python + openCV】视频转图片详解
输入输出路径import cv2videoFile = '/Users/fanc/Downloads/image/PeppaPig.mp4'outputFile = '/Users/fanc/Downloads/image/frame'cv2在opencv-python包里面,安装的时候pip install opencv-python上面是输入文件的路径,下面是输出文件的路径,注意最...原创 2018-10-11 11:28:01 · 5743 阅读 · 1 评论 -
Opencv 图片转视频 & 视频压缩(python)
需要注意的是参数的选取和图像大小如果不能正确输出视频,需要使用resize函数将所有帧转化为与视频同样的大小import cv2from cv2 import VideoWriter, VideoWriter_fourcc, imread, resizeimport osimport globimg_root = '/Users/fanc/Downloads/image/'out_...原创 2018-10-11 20:57:12 · 14079 阅读 · 2 评论 -
【剑指offer】二维数组中的查找【python】
class Solution: # array 二维列表 def Find(self, target, array): # write code here cols = len(array[0]) rows = len(array) print(rows, cols) row, col = 0, co...原创 2018-10-12 15:54:28 · 126 阅读 · 0 评论 -
【剑指offer】替换空格【python】
# -*- coding:utf-8 -*-class Solution: # s 源字符串 def replaceSpace(self, s): # write code here spaceNum = 0 for c in s: if c == ' ': spaceNum...原创 2018-10-12 16:35:25 · 616 阅读 · 0 评论 -
【leetcode】正则表达式匹配【python】【递归,后序遍历】
题目地址class Solution(object): def myMatch(self, s, p, s_index, p_index): print(s_index, p_index) if p == '' and s != '': #print(1) return False if s...原创 2018-10-06 21:15:38 · 518 阅读 · 0 评论 -
【leetcode】盛最多水的容器【python】
题目链接采用暴力解法回超时,时间复杂度为O(N^2)采用双指针的办法,注意,比较低的那条边决定着面积,因此,将比较低的那条边往中间挪动就好了,直到相邻之后输出,而不是左边移动一次,右边移动一次,需要注意只有两个数的情况,在python里面,range(1,1)是没有结果的class Solution(object): def maxArea(self, height): ...原创 2018-10-07 14:44:17 · 296 阅读 · 0 评论 -
python队列和栈操作,两个栈实现队列,两个队列实现栈
参考:http://docs.python.org/2/tutorial/datastructures.html#more-on-lists的官方代码栈&amp;amp;gt;&amp;amp;gt;&amp;amp;gt; stack = [3, 4, 5]&amp;amp;gt;&amp;amp;gt;&amp;amp;gt; stack.append(6)&amp;amp;gt;&a原创 2018-10-13 20:34:27 · 192 阅读 · 0 评论 -
【leetcode】整数转罗马数字、罗马数字转整数【Python】
题目链接递归算法,需要注意40~50的情况特殊性class Solution(object): def intToRoman(self, num): if num == 0: return '' if num == 4: return 'IV' if num == 9: return 'IX' if num == 40:...原创 2018-10-07 15:53:20 · 349 阅读 · 0 评论 -
【leetcode】最长公共前缀 Longest Common Prefix【python】
题目链接class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if strs == []: return '' str = '' ..原创 2018-10-07 16:31:15 · 390 阅读 · 0 评论 -
PYTHON关键语句整理
Python 3 Cheat Sheet 一共包含两页,分成了多个框图,涉及基本的 Python 数据结构、数学运算、条件和循环语句、文件读写,以及异常值处理等。在每个框图中,右上角是类型名称,蓝色和红色字体是该类型包含的关键字,绿色字体是示例,黑色斜体字提供更详细的信息。pdf地址:项目地址...原创 2018-10-08 17:23:41 · 524 阅读 · 0 评论 -
【leetcode】三数之和(3Sum)【python】AC解答
题目链接:三数之和利用和为0的性质,可以不用O(N^3)而使用O(N^2)来解决class Solution: def isNotSame(self, lists, list): # 判断是否重复函数 for i in range(len(lists)): if list == lists[i]: retur...原创 2018-10-08 21:12:00 · 405 阅读 · 0 评论 -
【剑指offer】旋转数组中的最下数字【python】
# -*- coding:utf-8 -*-class Solution: def minArray(self, array, low, high): minElem = array[low] for i in range(low, high + 1): if array[i] < minElem: ...原创 2018-10-15 17:03:06 · 112 阅读 · 0 评论 -
【剑指offer】矩阵中的路径(回溯法)
# -*- coding:utf-8 -*-class Solution: def hasPathCore(self, matrix, rows, cols, row, col, path, pathIndex, visited): if pathIndex == len(path): return True #print(row, col) ...原创 2018-10-16 12:15:39 · 251 阅读 · 0 评论