Posts

Showing posts with the label scipy

How to define summation type equation as constraints in Python optimization

Image
Clash Royale CLAN TAG #URR8PPP How to define summation type equation as constraints in Python optimization I was trying to solve a optimization problem using scipy package of python. But i don't know how to represent summation type equation as constraints and how to pass those constraint as argumnet to minimize function. The equation is the following : ∑ (Xrij + Xrji) - Yri >= 0,∀r,i j∈V For r=2, V=5 and i=5 I expanded the equation And defined every equation as separate constraint. x[1][1][1]+ x[1][1][1]+ x[1][1][2]+ x[1][2][1]+ x[1][1][3]+ x[1][3][1]+ x[1][1][4]+ x[1][4][1]+ x[1][1][5]+ x[1][5][1] - y[1][1] >= 0 x[1][2][1]+ x[1][1][2]+ x[1][2][2]+ x[1][2][2]+ x[1][2][3]+ x[1][3][2]+ x[1][2][4]+ x[1][4][2]+ x[1][2][5]+ x[1][5][2] - y[1][2] >= 0 x[1][3][1]+ x[1][1][3]+ x[1][3][2]+ x[1][2][3]+ x[1][3][3]+ x[1][3][3]+ x[1][3][4]+ x[1][4][3]+ x[1][3][5]+ x[1][5][3] - y[1][3] >= 0 x[1][4][1]+ x[1][1][4]+ x[1][4][2]+ x[1][2][4]+ x[1][4][3]+ x[1][3][4]+ x[1][4][4]+ x[1][4]...

How to square the individual matrix value using python?

Image
Clash Royale CLAN TAG #URR8PPP How to square the individual matrix value using python? I am trying to implement the Cost function in python. Assume my data have X (loading from txt file) and my theta value is [[0] [0]] For that I have implemented as below: import pandas as pd import numpy as np import matplotlib.pyplot as plt from scipy.linalg import fractional_matrix_power load_data = pd.read_csv('C:python_programex1data1.txt',sep = ",",header = None) feature_vale = load_data[0] y = np.matrix(load_data[1]) m = len(feature_vale) #print(m) #plt.scatter(load_data[0],load_data[1]) df = pd.DataFrame(pd.Series(1,index= range(0,m))) df[1] = load_data[0] X = np.matrix(df) row_theta = np.zeros(2,dtype = int) theta = np.array([row_theta]) # Transpose the array print(theta.T) prediction = np.matmul(X,theta.T) error = (prediction-y) print(error) Output of the error I got as expect...

why scipy.spatial.ckdtree runs slower than scipy.spatial.kdtree

Image
Clash Royale CLAN TAG #URR8PPP why scipy.spatial.ckdtree runs slower than scipy.spatial.kdtree Normally,scipy.spatial.ckdtree runs much faster than scipy.spatial.kdtree. But in my case,scipy.spatial.ckdtree runs slower than scipy.spatial.kdtree. My code is as follows: import numpy as np from laspy.file import File from scipy import spatial from timeit import default_timer as timer inFile = File("Toronto_Strip_01.las") dataset = np.vstack([inFile.x, inFile.y, inFile.z]).transpose() print(dataset.shape) start=timer() tree = spatial.cKDTree(dataset) # balanced_tree = False end=timer() distance,index=tree.query(dataset[100,:],k=5) print(distance,index) print(end-start) start=timer() tree = spatial.KDTree(dataset) end=timer() dis,indices= tree.query(dataset[100,:],k=5) print(dis,indices) print(end-start) dataset.shape is (2727891, 3),dataset.max() is 4834229.32 But, in a test case, scipy.spatial.ckdtree runs much faster than scipy.spatial.kdtree,the code is as follows: import nump...

scipy multivariate_normal(allow_singular=True) in GMM

Image
Clash Royale CLAN TAG #URR8PPP scipy multivariate_normal(allow_singular=True) in GMM During the calculation of a Gaussian Mixture Model I have to calculate the pdf() of the multivariate Gaussian distribution. I do this using the scipy multivariate_normal() method. Now during the calculations I encounter the situation that I get an error which says LinAlgError: singular matrix Setting the allow_singular parameter of the scipy multivariate_normal() = True circumvents this error and I get a quite meaningful result: So how is scipy "allowing" for singularity and what are the consequences for the calculations? --> If there are no consequences, strictly speaking, this parameter could be set to TRUE by default (but it isn't). By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is...