numpy

Wondering why scipy.spatial.distance.sqeuclidean is twice slower than numpy.sum((y1-y2)**2)

扶醉桌前 提交于 2021-02-05 08:25:52
问题 Here is my code import numpy as np import time from scipy.spatial import distance y1=np.array([0,0,0,0,1,0,0,0,0,0]) y2=np.array([0. , 0.1, 0. , 0. , 0.7, 0.2, 0. , 0. , 0. , 0. ]) start_time = time.time() for i in range(1000000): distance.sqeuclidean(y1,y2) print("--- %s seconds ---" % (time.time() - start_time)) ---15.212640523910522 seconds--- start_time = time.time() for i in range(1000000): np.sum((y1-y2)**2) print("--- %s seconds ---" % (time.time() - start_time)) ---8.381187438964844--

Reshaping a dataframe in python into 3D

▼魔方 西西 提交于 2021-02-05 08:20:07
问题 I am trying to reshape a handwritten character dataset into 3D form so that it can be concatenated with digit recognition dataset. I tried multiple times, but I couldnt figure out how it can be done. The actual digit recognition dataset has the shape (60000, 28, 28) The character recognition dataset has the shape (372450, 785) and the first column is target variable. Since excluding first column 28*28=784 there is a possibility that it can be converted to 3D same as digit dataset. Please

Taking the mean value of N last days

泪湿孤枕 提交于 2021-02-05 08:19:47
问题 I have this data frame: ID Date X 123_Var 456_Var 789_Var A 16-07-19 3 777 250 810 A 17-07-19 9 637 121 529 A 20-07-19 2 295 272 490 A 21-07-19 3 778 600 544 A 22-07-19 6 741 792 907 A 25-07-19 6 435 416 820 A 26-07-19 8 590 455 342 A 27-07-19 6 763 476 753 A 02-08-19 6 717 211 454 A 03-08-19 6 152 442 475 A 05-08-19 6 564 340 302 A 07-08-19 6 105 929 633 A 08-08-19 6 948 366 586 B 07-08-19 4 509 690 406 B 08-08-19 2 413 725 414 B 12-08-19 2 170 702 912 B 13-08-19 3 851 616 477 B 14-08-19 9

Python: type(i) is int… but i is int = False

痞子三分冷 提交于 2021-02-05 08:16:08
问题 I'm quite sure I'm doing something foolish but can't figure out what it is. predict = fn_abc(data) In[3]: predict Out[3]: array([2]) In[4]: type(predict) Out[4]: numpy.ndarray Now wrapping my fn_abc with int predict = int(fn_abc(data)) In[6]: predict Out[6]: 2 In[7]: type(predict) Out[7]: int In[8]: predict is int Out[8]: False What am I doing wrong? 回答1: Use isinstance. isinstance(predict, int) 回答2: predict is int checks whether the integer has the same identity as the integer type object.

Reading a file with Fortran formatted small floats, using numpy

折月煮酒 提交于 2021-02-05 08:15:01
问题 I am trying to read a data file written by a Fortran program, in which every once in a while there is a very small float like 0.3299880-104 . The error message is: >np.loadtxt(filename, usecols = (1,)) File "/home/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 928, in loadtxt items = [conv(val) for (conv, val) in zip(converters, vals)] File "/home/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 659, in floatconv return float(x) ValueError: invalid literal for

Numpy create an empty alpha image

我只是一个虾纸丫 提交于 2021-02-05 08:12:33
问题 I wanted to create a blank alpha image to parse some data from py-opencv and save it on an transparent background png file. I tried : blank_image = np.zeros((H,W,4), np.uint8) and blank_image = np.full((H, W, 4) , (0, 0, 0, 0), np.uint8) (H and W are Height and Width) Both still render a black background instead of a transparent one. how to get a blank alpha transparent image? Thanks in advance :) Edits: as mentioned by Mark Setchell: you need to specify the alpha channel on other colors

Numpy create an empty alpha image

风流意气都作罢 提交于 2021-02-05 08:11:32
问题 I wanted to create a blank alpha image to parse some data from py-opencv and save it on an transparent background png file. I tried : blank_image = np.zeros((H,W,4), np.uint8) and blank_image = np.full((H, W, 4) , (0, 0, 0, 0), np.uint8) (H and W are Height and Width) Both still render a black background instead of a transparent one. how to get a blank alpha transparent image? Thanks in advance :) Edits: as mentioned by Mark Setchell: you need to specify the alpha channel on other colors

Numpy create an empty alpha image

旧时模样 提交于 2021-02-05 08:11:26
问题 I wanted to create a blank alpha image to parse some data from py-opencv and save it on an transparent background png file. I tried : blank_image = np.zeros((H,W,4), np.uint8) and blank_image = np.full((H, W, 4) , (0, 0, 0, 0), np.uint8) (H and W are Height and Width) Both still render a black background instead of a transparent one. how to get a blank alpha transparent image? Thanks in advance :) Edits: as mentioned by Mark Setchell: you need to specify the alpha channel on other colors

Reading a file with Fortran formatted small floats, using numpy

三世轮回 提交于 2021-02-05 08:11:20
问题 I am trying to read a data file written by a Fortran program, in which every once in a while there is a very small float like 0.3299880-104 . The error message is: >np.loadtxt(filename, usecols = (1,)) File "/home/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 928, in loadtxt items = [conv(val) for (conv, val) in zip(converters, vals)] File "/home/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 659, in floatconv return float(x) ValueError: invalid literal for

Replace elements in 2D array based on occurrence in another array

徘徊边缘 提交于 2021-02-05 08:09:00
问题 I need to replace elements in Numpy 2D arrays based on a condition that the element appears in some other replacement array For example: >>> main = np.random.randint(5, size=(3, 4)) >>> main array([[1, 2, 4, 2], [3, 2, 3, 2], [4, 4, 2, 3]]) >>> repl = [2,3] >>> main[main in repl] = -1 I would like to have all values in repl changed to -1, so I expect main to be: [[1, -1, 4, -1], [-1, -1, -1, -1], [4, 4, -1, -1]] However a ValueError is raised while trying to have in inside the condition of