scipy

Function returns a vector, how to minimize in via NumPy

为君一笑 提交于 2021-02-10 14:50:25
问题 I'm trying to minimize function, that returns a vector of values, and here is an error: setting an array element with a sequence Code: P = np.matrix([[0.3, 0.1, 0.2], [0.01, 0.4, 0.2], [0.0001, 0.3, 0.5]]) Ps = np.array([10,14,5]) def objective(x): x = np.array([x]) res = np.square(Ps - np.dot(x, P)) return res def main(): x = np.array([10, 11, 15]) print minimize(objective, x, method='Nelder-Mead') At these values of P, Ps, x function returns [[ 47.45143225 16.81 44.89 ]] Thank you for any

Double integration of x*np.log(x) in Python using scipy.integrate.dblquad

余生长醉 提交于 2021-02-10 14:33:11
问题 The code below uses double integration with scipy.integrate.dblquad to calculate the differential entropy, c*np.log(c) , of a copula density function c , which has one dependence parameter, theta , usually positive. Formula can be found here. import numpy as np from scipy import integrate def copula_entropy(theta): c = lambda v, u: ((1+theta)*(u*v)**(-1-theta)) * (u**(-theta) + v**(-theta) -1)**(-1/theta-2) return -integrate.dblquad(c*np.log(c), 0, 1, lambda u: 0, lambda u: 1)[0] Calling the

how to load Matlab's struct (saved with v7.3) in Python

谁都会走 提交于 2021-02-10 14:19:35
问题 I created a 1X20 struct in Matlab. This struct has 9 fields. The struct is saved in -v7.3 version because of its dimensions (about 3 Giga). one of the fields contains 4D matrix, other contain cell arrays, meaning it is a complex struct. I would like to know if there is a way to load this struct into Python? 回答1: MATLAB v7.3 uses HDF5 storage; scipy.io.loadmat cannot handle that MATLAB: Differences between .mat versions Instead you have to use numpy plus h5py How to read a v7.3 mat file via

Multi-class Logistic Regression from scratch

这一生的挚爱 提交于 2021-02-10 14:18:14
问题 I am trying to implement from scratch the multiclass logistic regression but my implementation returns bad results. I believe the definition of the gradient function and the cost function is fine. Maybe there is a problem with how these functions are interacting with the minimize function. I have tried it but I could not find out what is wrong. Could you please cast some light? You can add the estimator 'myLR': myLR(**par_dict), with paramters par_dict= {'alpha': 0.1, 'maxit': 2000, 'opt

Multi-class Logistic Regression from scratch

我只是一个虾纸丫 提交于 2021-02-10 14:15:48
问题 I am trying to implement from scratch the multiclass logistic regression but my implementation returns bad results. I believe the definition of the gradient function and the cost function is fine. Maybe there is a problem with how these functions are interacting with the minimize function. I have tried it but I could not find out what is wrong. Could you please cast some light? You can add the estimator 'myLR': myLR(**par_dict), with paramters par_dict= {'alpha': 0.1, 'maxit': 2000, 'opt

ImportError: cannot import name '_ni_support' — Using cx-freeze with scipy

爱⌒轻易说出口 提交于 2021-02-10 14:14:13
问题 I have used cx-freeze to make an executable file from my python 3 script. The issue is that apparently cx-freeze is having a hard time importing scipy scripts. I had to resolve other issues previously, e.g. adding the tcl library files manually. Anyway, my setup files is below: from cx_Freeze import setup, Executable import os os.environ['TCL_LIBRARY'] = "C:\\Users\\Gobryas\\AppData\\Local\\Continuum\\anaconda3\\tcl\\tcl8.6" os.environ['TK_LIBRARY'] = "C:\\Users\\Gobryas\\AppData\\Local\

ImportError: cannot import name '_ni_support' — Using cx-freeze with scipy

走远了吗. 提交于 2021-02-10 14:08:12
问题 I have used cx-freeze to make an executable file from my python 3 script. The issue is that apparently cx-freeze is having a hard time importing scipy scripts. I had to resolve other issues previously, e.g. adding the tcl library files manually. Anyway, my setup files is below: from cx_Freeze import setup, Executable import os os.environ['TCL_LIBRARY'] = "C:\\Users\\Gobryas\\AppData\\Local\\Continuum\\anaconda3\\tcl\\tcl8.6" os.environ['TK_LIBRARY'] = "C:\\Users\\Gobryas\\AppData\\Local\

Return Similarity Matrix From Two Variable-length Arrays of Strings (scipy option?)

大兔子大兔子 提交于 2021-02-10 12:29:06
问题 Say I have two arrays: import numpy as np arr1 = np.array(['faucet', 'faucets', 'bath', 'parts', 'bathroom']) arr2 = np.array(['faucett', 'faucetd', 'bth', 'kichen']) and I want to compute the similarity of the strings in arr2 to the strings in arr1 . arr1 is an array of correctly spelled words. arr2 is an array of words not recognized in a dictionary of words. I want to return a matrix which will then be turned into a pandas DataFrame. My current solution (credit): from scipy.spatial

Volume of 3d shape using numerical integration with scipy

限于喜欢 提交于 2021-02-10 08:45:36
问题 I have written a function for computing volume of intersection of a cube and a half-space and now I'm writing tests for it. I've tried computing the volume numerically like this: integral = scipy.integrate.tplquad(lambda z, y, x: int(Vector(x, y, z).dot(normal) < distance), -0.5, 0.5, lambda x: -0.5, lambda x: 0.5, lambda x, y: -0.5, lambda x, y: 0.5, epsabs=1e-5, epsrel=1e-5) ... basically I integrate over the whole cube and each point gets value 1 or 0 based on if it is inside the half

Checking if a point is in ConvexHull?

天大地大妈咪最大 提交于 2021-02-10 08:44:58
问题 I am having trouble understanding how to compute whether an n-dimensional point is within an n-dimensional ConvexHull. A very similar question (the same) was asked here: What's an efficient way to find if a point lies in the convex hull of a point cloud? However, the answers are confusing me or don't seem to work for me, and I have no idea why. def in_hull(p, hull): """ Copied and from the Top Original answer """ from scipy.spatial import Delaunay if not isinstance(hull,Delaunay): hull =