numpy

Normalizing vector produces nan in Numpy

可紊 提交于 2021-02-19 07:45:05
问题 I'm getting some strange behavior from scipy/numpy that I suspect is a bug but someone may know better? I've got a pair of long arrays which I'm breaking into frames which are of length 2-4 for debugging purposes. I want to normalize each pair of frames and take the dot product. The code that does it (with some debugging output) is: tf = numpy.copy(t_frame) / norm(t_frame) pf = numpy.copy(p_frame) / norm(p_frame) print "OPF:" print p_frame print "PF: " print pf print "TF norm is: " + str(norm

Normalizing vector produces nan in Numpy

允我心安 提交于 2021-02-19 07:44:20
问题 I'm getting some strange behavior from scipy/numpy that I suspect is a bug but someone may know better? I've got a pair of long arrays which I'm breaking into frames which are of length 2-4 for debugging purposes. I want to normalize each pair of frames and take the dot product. The code that does it (with some debugging output) is: tf = numpy.copy(t_frame) / norm(t_frame) pf = numpy.copy(p_frame) / norm(p_frame) print "OPF:" print p_frame print "PF: " print pf print "TF norm is: " + str(norm

Finding squared distances beteen n points to m points in numpy

梦想与她 提交于 2021-02-19 06:23:06
问题 I have 2 numpy arrays(say X and Y) which each row represents a point vector. I want to find the squared euclidean distances(will call this 'dist') between each point in X to each point in Y. I want to the output to be a matrix D where D(i,j) is dist(X(i) , Y(j)). I have the following python code based on : http://nonconditional.com/2014/04/on-the-trick-for-computing-the-squared-euclidian-distances-between-two-sets-of-vectors/ def get_sq_distances(X, Y): a = np.sum(np.square(X),axis=1,keepdims

OpenCV: How to use the convertScaleAbs() function

匆匆过客 提交于 2021-02-19 06:11:06
问题 I am trying to convert an image back to greyscale after applying Sobel filtering on it. I have the following code: import numpy as np import matplotlib.pyplot as plt import cv2 image = cv2.imread("train.jpg") img = np.array(image, dtype=np.uint8) #convert to greyscale img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #remove noise img_smooth = cv2.GaussianBlur(img_grey, (13,13), 0) sobely = cv2.Sobel(img_smooth,cv2.CV_64F,0,1,ksize=9) I want to convert the image sobely back to greyscale using

is there any ceil() function based on abs value like trunc vs floor or round-away-from-zero function?

狂风中的少年 提交于 2021-02-19 05:57:22
问题 numpy.trunc is a floor function based on abs value: a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.floor(a) Out[122]: array([-2., -2., -1., 0., 1., 1., 2.]) np.trunc(a) Out[123]: array([-1., -1., -0., 0., 1., 1., 2.]) The ceil output is this: np.ceil(a) Out[124]: array([-1., -1., -0., 1., 2., 2., 2.]) But I want an output: array([-2., -2., -1., 1., 2., 2., 2.]) What is the numpy function for this? edit: Unfortunately there is no build-in round away from zero function. Based on @Mitch

Create weighted igraph Graph from numpy summetric 2D array as adjacency matrix

陌路散爱 提交于 2021-02-19 05:36:12
问题 I am having a numpy 2D array, with the values representing the weights of edges between nodes. The matrix is symmetric, and I take the diagonal to be zero. I don't find an example of how to convert this matrix into igraph Graph object. I've tried the following approach, but it doesn't work: import numpy as np import igraph def symmetrize(a): return a + a.T - 2*np.diag(a.diagonal()) A = symmetrize(np.random.random((100,100))) G = igraph.Graph.Adjacency(A.tolist()) 回答1: Use Graph.Weighted

C++: matplotlibcpp.h and Python.h linker error

≯℡__Kan透↙ 提交于 2021-02-19 05:30:12
问题 I am finding trouble linking a library in C++. I am trying for the first time to use "matplotlibcpp.h". This is a library that uses "Python.h" My code is not using either of the libraries yet. It gives error just by including "matplotlibcpp". I am using python2.7 and Ubuntu 18.04 and am using Eclipse. The code does not run if I include: #include "matplotlibcpp.h" // programme runs if this is commented out. But I need it to add new features. #include "Python.h" I have added these paths in

C++: matplotlibcpp.h and Python.h linker error

混江龙づ霸主 提交于 2021-02-19 05:28:59
问题 I am finding trouble linking a library in C++. I am trying for the first time to use "matplotlibcpp.h". This is a library that uses "Python.h" My code is not using either of the libraries yet. It gives error just by including "matplotlibcpp". I am using python2.7 and Ubuntu 18.04 and am using Eclipse. The code does not run if I include: #include "matplotlibcpp.h" // programme runs if this is commented out. But I need it to add new features. #include "Python.h" I have added these paths in

Python: Numpy and Pandas Transforming timestamp/data into one-hot-encoding

China☆狼群 提交于 2021-02-19 05:20:52
问题 I have a column of a dataframe that is like this time 0 2017-03-01 15:30:00 1 2017-03-01 16:00:00 2 2017-03-01 16:30:00 3 2017-03-01 17:00:00 4 2017-03-01 17:30:00 5 2017-03-01 18:00:00 6 2017-03-01 18:30:00 7 2017-03-01 19:00:00 8 2017-03-01 19:30:00 9 2017-03-01 20:00:00 10 2017-03-01 20:30:00 11 2017-03-01 21:00:00 12 2017-03-01 21:30:00 13 2017-03-01 22:00:00 . . . I want to "encode" the time of the day. I want to do this by firsly assigning each half an-hour a integer number. Starting

Python: how to convert a string array to a factor list

纵饮孤独 提交于 2021-02-19 04:54:10
问题 Python 2.7, numpy, create levels in the form of a list of factors. I have a data file which list independent variables, the last column indicates the class. For example: 2.34,4.23,0.001, ... ,56.44,2.0,"cloudy with a chance of rain" Using numpy, I read all the numeric columns into a matrix, and the last column into an array which I call "classes". In fact, I don't know the class names in advance, so I do not want to use a dictionary. I also do not want to use Pandas. Here is an example of the