adsense728x90


data set으로부터 inverse function의 해를 구하기 python numpy Python


먼저 dataset을 interpolation해서 function으로 만들기

One-dimensional linear interpolation으로 충분하다면,
>>> xp = [1, 2, 3]>>> fp = [3, 2, 0]>>> np.interp(2.5, xp, fp)1.0
interpolation하고 나면, f(x)를 구할 수 있다.
하지만, 원하는 것은 f^{-1}(x) 이므로,



scipy 방정식 해를 구하기

fsolve로 되겠다.

y1=x1^2y2=x2+1

[des]activar nros. de línea
   1 from scipy.optimize import fsolve   2    3 import numpy as np   4    5 def f(xy):   6    x, y = xy   7    z = np.array([y - x**2, y - x - 1.0])   8    return z   9   10 fsolve(f, [1.0, 2.0])

The result of this should be:

[des]activar nros. de línea
   1 array([ 1.61803399,  2.61803399])

두 방정식을 만족하는 해를 찾되,
x, y = 1.0, 2.0에서 출발해서 optimization으로 찾는 것.

fsolve의 예제가 많은 곳

꼭 x, y 식 2개가 있을 필요가 있는 건 아니었다.


또는 굳이 함수를 특정하고 싶다면, polynomial 로 만드는 것도 한 방법이겠다.

numpy.polyfit

아니면,
방법이 정석인가? 잘 모르겠다.






import matplotlib.pyplot as plot

import numpy

from scipy.optimize import fsolve


n_0_hist_1000_39901 = []

n_1_hist_1000_39901 = []

n_people_ranges = []


execfile("blur_hist_1000_39901.py")


n_slots = 1000

n_iterations = 100000

n_people_range = range(n_slots*2, 40000, n_iterations/1000)

x_ranges = n_people_range

n_1_hist = n_1_hist_1000_39901


def n_1_slots_n_people(x, n_people):

global x_ranges

global n_1_hist

#x, y = xy

#print x, numpy.interp(x, x_ranges, n_1_hist), n_people

return (numpy.interp(x, x_ranges, n_1_hist) - n_people)

#return numpy.array([y - numpy.interp(x, x_ranges, n_1_hist), y - n_people])


yinterp = numpy.interp(x_ranges, x_ranges, n_1_hist)

#print range(len(x_ranges)), x_ranges, yinterp


#plot.plot(x_ranges, n_1_hist, 'o')

#plot.plot(x_ranges, yinterp, '-x')

#plot.show()


intervals = [4459,76,14,25,346]

prices = [0]

expected_people = []

densities = []


cumulative_price = 0

for interval in intervals:

cumulative_price += interval

prices.append(cumulative_price)

n_spaces = n_slots / interval

#[n_slots - n_spaces, n_spaces]

x, = fsolve(n_1_slots_n_people, 20000.0, args=(n_spaces,))#, full_output=True)

density = x / n_slots

expected_people.append(density * interval)

densities.append(density)

print "solution for ", interval, n_spaces, x, density, density * interval

expected_people.append(expected_people[-1])

densities.append(densities[-1])

print sum(expected_people)


plot.plot(prices, densities, '-x')

#plot.plot(prices, expected_people, 'o')

plot.show()  



덧글

댓글 입력 영역