transformation into python class IndexError - python

I'm trying to turn a code into a python class.
my dataframe "URM" looks like :
user_id anime_id user_rating
0 1 20 7.808497
1 3 20 8.000000
2 5 20 6.000000
3 6 20 7.808497
4 10 20 7.808497
I filter only 999users for computation reason.
If i copy paste all that code below without "self" it works fine.
when i'm trying to dive into class
like that
parameters
`class ALS:
def __init__(self, URM, n_factors, lambda_ , n_iterations) :
self.URM = URM
self.n_factors = n_factors
self.lambda_ = lambda_
self.n_iterations = n_iterations
self.n = max(self.URM["anime_id"])
self.m = max(self.URM["user_id"])
functions for normalise df, metrics, and compute training matrix
def normaliseRow(self, x):
return x / sum(x)
def initialiseMatrix(self, n, n_factors):
A = abs(np.random.randn(self.n, self.n_factors))
return np.apply_along_axis(self.normaliseRow, 1, A)
def ratingsPred(X, Y):
return np.dot(X, Y.T)
def MSE(self, ratingsPred, ratingsMatrix):
idx = ratingsMatrix > 0
return sum((ratingsPred[idx] - ratingsMatrix[idx]) ** 2) / np.count_nonzero(ratingsMatrix)
def compute_matrix(self) :
Y = self.initialiseMatrix(self.n, self.n_factors)
X = self.initialiseMatrix(self.m, self.n_factors)
temp = np.zeros((n, 3)) #user_id, anime_id rating_user
for i in range(1,n):
temp[i,] = [m+1,i,0]
COL_NAME = ["user_id","anime_id", "user_rating"]
RM = self.URM.append(pd.DataFrame(temp, columns =COL_NAME))
RM = RM.pivot_table(columns=['anime_id'], index =['user_id'],
values='user_rating', dropna = False)
RM = RM.fillna(0).as_matrix()
ratingsMatrix = RM[0:self.m,0:self.n]
nonZero = ratingsMatrix > 0
reg = lambda_ * np.eye(n_factors,n_factors)
return X, Y, ratingsMatrix, nonZero, reg
training the alternating least square
def train(self) :
X, Y, ratingsMatrix, nonZero, reg = self.compute_matrix()
print("start training ...")
training_process = []
for k in range(1, self.n_iterations):
for i in range(1, self.m):
idx = nonZero[i,:]
a = Y[idx,]
b = np.dot(np.transpose(Y[idx,]), ratingsMatrix[i, idx])
updateX = np.linalg.solve(np.dot(np.transpose(a), a) + reg, b)
X[i,] = updateX
for j in range(1, self.n):
idx = nonZero[:,j]
a = X[idx,]
b = np.dot(np.transpose(X[idx,]), ratingsMatrix[idx, j])
updateY = np.linalg.solve(np.dot(np.transpose(a), a) + reg, b)
Y[j,] = updateY
ratingsP = self.ratingsPred(X, Y)
mse = self.MSE(ratingsP, ratingsMatrix)
training_process.append((k, mse))
if (k+1) % 5 == 0:
print("Iteration: %d ; mse = %.4f" % (k+1, mse))
return training_process
start training
# df, n_factors, lambda, iteration
als_model = ALS(urm, 15, 0.1, 10) `
als_mode.train()
it return an IndexError
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-130-ce2849580784> in <module>()
----> 1 als_model.train()
<ipython-input-129-14f3d633244d> in train(self)
53 for j in range(1, self.n):
54 idx = nonZero[:,j]
---> 55 a = X[idx,]
56 b = np.dot(np.transpose(X[idx,]), ratingsMatrix[idx, j])
57 updateY = np.linalg.solve(np.dot(np.transpose(a), a) + reg, b)
IndexError: boolean index did not match indexed array along dimension 0; dimension is 34240 but corresponding boolean dimension is 999

Related

Heat equation divide by zero issue

I'm writing a code that solves a heat equation implementing an implicit method. The problem is that the values between first and last layer of the matrix are NaNs. What could be the problem?
From my problem of view, the main issue might be with the 105th line, which represents the convrsion of original function to the one that includes the boundary function.
Boundary functions code:
def func(x, t):
return x*(1 - x)*np.exp(-2*t)
# boundary function for x = 0 and x = 1
def q0(t):
return t*np.exp(-t/0.1)*np.cos(t) # граничное условие при x = 0
def q1(t):
return t*np.exp(-t/0.5)*np.cos(t) # граничное уcловие при x = 1
def derivative(f, x0, step):
return (f(x0+step) - f(x0))/step
# boundary function that for t = 0
def u_x0(x):
return (-x + 1)*x
Function that solves the three-diagonal matrix equation
def solution(a, b):
n = len(a)
x = [0 for k in range(0, n)]
# forward
v = [0 for k in range(0, n)]
u = [0 for k in range(0, n)]
# first string (t = 0)
v[0] = a[0][1] / (-a[0][0])
u[0] = ( - b[0]) / (-a[0][0])
for i in range(1, n - 1):
v[i] = a[i][i+1] / ( -a[i][i] - a[i][i-1]*v[i-1] )
u[i] = ( a[i][i-1]*u[i-1] - b[i] ) / ( -a[i][i] - a[i][i-1]*v[i-1] )
# last string (t = 1)
v[n-1] = 0
u[n-1] = (a[n-1][n-2]*u[n-2] - b[n-1]) / (-a[n-1][n-1] - a[n-1][n-2]*v[n-2])
x[n-1] = u[n-1]
for i in range(n-1, 0, -1):
x[i-1] = v[i-1] * x[i] + u[i-1]
return x
Coefficent matrix values:
A = -t/h**2
B = 1 + 2*t/h**2
C = -t/h**2
Code that actually solves the matrix:
i = 1
X =[]
while i < 99:
X = solution(cool_array, f)
k = 0
while k < len(x_i):
#line-105
X[k] += 0.01*(func(x_i[k], x_i[i]) - (1 - x_i[i])*derivative(q0, x_i[i], 0.01) - (x_i[i])*derivative(q1, x_i[i], 0.01))
k+=1
a = 1
while a < 98:
w_h_t[i][a] = X[a]
a+=1
f = X
f[0] = w_h_t[i][0]
f[99] = w_h_t[i][99]
i+=1
print(w_h_t)
As far as I understand, the algorith solution(a, b) is written properly, so I guess the problem might be with the boundary functions or with the 105th line. The output I expect is at least an array of number, not NaNs.

I am learning how to data fit on python with importance sampling, I am trying to sample x and y values and find their weights but I am getting errors

Basically, I am trying to sample x and y from a proposal distribution function and then find the weights for the sample, and create 10 different sets of n = 10,000 samples. I was given an array of 55 values for radius and mu and sigma, and I have 10,000 values of x and y as samples.
here is my code:
import numpy as np
import matplotlib.pyplot as plt
GN = 4.302**-6
A = 8445.264
mu = np.array([1,1])
C = [[1,0],[0,1]]
num = 10**4
def chi_sq(x,y):
r = np.zeros(len(radius))
for s in range(len(radius)):
f_xy = np.sqrt( GN * 10**y * radius[s] / (radius[s] + 10**x )**3 )
chi_sqtotal = np.sum((f_xy - mu[s] )**2 / (sigma[s])**2)
r += chi_sqtotal
return r
def P(x,y):
return A*np.exp(-0.5*chi_sq(x,y))
for i in range(10):
def chi_sq(x,y):
r = np.zeros(len(radius))
for s in range(len(radius)):
f_xy = np.sqrt( GN * 10**y * radius[s] / (radius[s] + 10**x )**3 )
chi_sqtotal = np.sum((f_xy - mu[s] )**2 / (sigma[s])**2)
r += chi_sqtotal
return r
def P(x,y):
return A*np.exp(-0.5*chi_sq(x,y))
detC = np.linalg.det(C)
Cinv = np.linalg.inv(C)
def Q(x,y):
r = np.array([x,y]) - mu
prefactor = 1/(2*np.pi*np.sqrt(detC))
exponent = 0.5*(r # Cinv # r)
return prefactor*np.exp(-exponent)
r_samples = np.random.multivariate_normal(mu,C,num)
x_samples = r_samples[:,0]
y_samples = r_samples[:,1]
w = np.zeros(num)
for j in range(num):
x, y = r_samples[j]
w[j] = P(x,y)/Q(x,y)
Here is the error I am getting :
IndexError Traceback (most recent call last)
<ipython-input-114-638cc1a1b2e2> in <module>()
41 x, y = r_samples[j]
42
---> 43 w[j] = P(x,y)/Q(x,y)
1 frames
<ipython-input-114-638cc1a1b2e2> in chi_sq(x, y)
22 for s in range(len(radius)):
23 f_xy = np.sqrt( GN * 10**y * radius[s] / (radius[s] + 10**x )**3 )
---> 24 chi_sqtotal = np.sum((f_xy - mu[s] )**2 / (sigma[s])**2)
25 r += chi_sqtotal
26 return r
IndexError: index 2 is out of bounds for axis 0 with size 2
The error is very descriptive:
IndexError: index 2 is out of bounds for axis 0 with size 2
If I have a numpy.ndarray, let's call it x, then the axes are as follows:
x[axis_0][axis_1]...
They are saying that the size of the axis is 2, and you're indexing it with 2, which is out of bounds. Notice
mu = np.array([1,1])
and the line with mu has the error. In this chunk:
for s in range(len(radius)):
f_xy = np.sqrt( GN * 10**y * radius[s] / (radius[s] + 10**x )**3 )
chi_sqtotal = np.sum((f_xy - mu[s] )**2 / (sigma[s])**2)
r += chi_sqtotal
s goes above 1, causing the error. You need to make a correction somewhere and not loop as high as len(radius), because mu only has 2 elements.

Invalid index to scalar variable. How to fix this?

So, I checked other questions and answers about this and I didn't understand the reason, could you please help me with this, please
I'm learning linear regression and I implemented the code for Linear Regression with two variables.
This implementation should predict the sum of two numbers but it's giving an error
Implemented code here:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([[1,1],[1,2],[2,3],[3,4],[4,5],[5,6]])
y = np.array([2,3,5,7,9,11])
#hypothesis
def hypothesis(theta, x):
return theta[0] + theta[1]*x[0] + theta[2]*x[1]
#cost function J(t0, t1, t2)
def cost(theta, x, y):
m = x.shape[0]
error = 0
for i in range(m):
d = x[i]
hx = hypothesis(theta, d)
error = error + (hx - y[i])**2
return error
#differentiation of cost function
def diffGradient(theta, x, y):
grad = np.zeros((3,))
m = x.shape[0]
for i in range(m):
hx = hypothesis(theta, x)
grad[0] = grad[0] + (hx - y[i])
grad[1] = grad[1] + (hx - y[i])*x[0]
grad[2] = grad[2] + (hx - y[i])*x[1]
return 0
#gradient descent funtion
def gradientDescent(x, y, learning_rate = 0.001):
theta = [-2.0,0.0,1.0]
iter = 100
error_list = []
theta_list = []
for i in range(iter):
d = x[i]
grad = diffGradient(theta, d, y)
e = cost(theta, d, y)
error_list.append(e)
theta_list.append((theta[0],theta[1],theta[2]))
#simultaneous update
theta[0] = theta[0] - learning_rate*grad[0]
theta[1] = theta[1] - learning_rate*grad[1]
theta[2] = theta[2] - learning_rate*grad[2]
return theta, theta_list, error_list
final_theta, theta_list, error_list = gradientDescent(x,y)
After the above line, I'm getting this error
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-56-bda7687d0af9> in <module>
----> 1 final_theta, theta_list, error_list = gradientDescent(x,y)
<ipython-input-55-033133fbfbd5> in gradientDescent(x, y, learning_rate)
8 d = x[i]
9 grad = diffGradient(theta, d, y)
---> 10 e = cost(theta, d, y)
11 error_list.append(e)
12 theta_list.append((theta[0],theta[1],theta[2]))
<ipython-input-41-6a07f4b81c9c> in cost(theta, x, y)
5 for i in range(m):
6 d = x[i]
----> 7 hx = hypothesis(theta, d)
8 error = error + (hx - y[i])**2
9 return error
<ipython-input-27-43ce9d7c567b> in hypothesis(theta, x)
1 #hypothesis
2 def hypothesis(theta, x):
----> 3 return theta[0] + theta[1]*x[0] + theta[2]*x[1]
IndexError: invalid index to scalar variable.
I don't know what I'm doing wrong. Any help would be appreciated.
What's the x that you pass to
gradientDescent(x,y)
Look at the traceback and try to figure out what variable has the problem - the problem is that you can't index a scalar, a number. It has to be a list or array. Trace the problem variable back up through your code.
In the traceback:
in the problem line you use x[0] and x[1]. What's x at this point?
In the calling function it's d, which is set with d = x[i]
In gradientDescent the passed variable is again called d, and set as d = x[i]
So you have 3 levels of indexing. Does the original x support that?
You have to understand the problem before you try a fix.

Python trapezoidal method graphing

I'm trying to plot the result of a trapezoidal method into a graph, I figured out that to get the exact value of the integral you can use integrate.trapz(f(x)) from the scypy libary. This is my second month learning comp math, not really sure about libraries usage yet.
Here is my code
from scipy import integrate
def f(x):
return 1 - x - 4*x**3 + 2*x**5
def trapezoid(a, b, n):
h = (b - a) / n
s = (f(a) + f(b))
i = 1
while i < n:
s += 2 * f(a + i * h)
i += 1
area = ((h / 2) * s)
return area
def graphTrapezoid():
val = []
err = []
exact_sum = integrate.trapz(f(b))-integrate.trapz(f(a))
for i in range (2,100):
val.append(trapezoid(a,b,i))
errVal = abs((trapezoid(a,b,i) - exact_sum)/exact_sum)
err.append(errVal)
plt.plot(val, err)
a = 1
b = 5
n = 1
graphTrapezoid()
Below is the error statement that I got
IndexError Traceback (most recent call last)
<ipython-input-134-504c0acc46bf> in <module>
39
40
---> 41 graphTrapezoid()
<ipython-input-134-504c0acc46bf> in graphTrapezoid()
29 val = []
30 err = []
---> 31 exact_sum = integrate.trapz(f(b))-integrate.trapz(f(a))
32 for i in range (2,100):
33 val.append(trapezoid(a,b,i))
~\Anaconda3\lib\site-packages\numpy\lib\function_base.py in trapz(y, x, dx, axis)
4059 slice1 = [slice(None)]*nd
4060 slice2 = [slice(None)]*nd
-> 4061 slice1[axis] = slice(1, None)
4062 slice2[axis] = slice(None, -1)
4063 try:
IndexError: list assignment index out of range
From the scipy.integrate.trapz documentation:
The function definition is:
scipy.integrate.trapz(y, x=None, dx=1.0, axis=-1)
Where y should be "array_like". However, you are passing two integer values to the trapz function.
If you want to integrate f betweeen b and a, maybe you could first define a list of x values:
a = 1
b = 5
step = 0.1 # A smaller step will give you a more precise result
x = []
for i in range(int((b-a)/step + 1)):
x.append(a + i*step)
Then, sample your function:
y = [f(val) for val in x]
And lastly, you could integrate your samples:
result = integrate.trapz(y, x, dx=step)

Visual for masses on a string using ivisual

Hi I am trying to create a visual for a physics problem using ivisual. The best visual I could find to describe the problem is below:
The problem I am having is I found an example in a text book of the same problem. The textbook visualizes the answers using visual module, however from the research I have been doing I believe that the visual module is now called ivisual. I can get the answer numerically when I hash out the function call and any other code that uses the ivisual module. In my specific example the masses are 10 and 20 and the length of each rope segment is 3, 4, and 4, and the distance between a and a is 8. I tried fixing the visual code, but I cannot get it work, it is probably something stupid.
My code is below with the error I am currently getting:
from ivisual import *
from numpy.linalg import solve
import numpy as np
import math as m
scene = display(x=0,y=0,width=500,height=500,
title='String and masses configuration')
tempe = curve(x=range(0,500),color=color.black)
n = 9
eps = 1*10**(-6)
deriv = np.zeros( (n, n), float)
f = np.zeros( (n), float)
x = np.array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1., 1., 1.])
def plotconfig():
for obj in scene.objects:
obj.visible=0 # to erase the previous configuration
L1 = 3.0
L2 = 4.0
L3 = 4.0
xa = L1*x[3] # L1*cos(th1)
ya = L1*x[0] # L1 sin(th1)
xb = xa+L2*x[4] # L1*cos(th1)+L2*cos(th2)
yb = ya+L2*x[1] # L1*sin(th1)+L2*sen(th2)
xc = xb+L3*x[5] # L1*cos(th1)+L2*cos(th2)+L3*cos(th3)
yc = yb-L3*x[2] # L1*sin(th1)+L2*sen(th2)-L3*sin(th3)
mx = 100.0 # for linear coordinate transformation
bx = -500.0 # from 0=< x =<10
my = -100.0 # to -500 =<x_window=>500
by = 400.0 # same transformation for y
xap = mx*xa+bx # to keep aspect ratio
yap = my*ya+by
ball1 = sphere(pos=(xap,yap), color=color.cyan,radius=15)
xbp = mx*xb+bx
ybp = my*yb+by
ball2 = sphere(pos=(xbp,ybp), color=color.cyan,radius=25)
xcp = mx*xc+bx
ycp = my*yc+by
x0 = mx*0+bx
y0 = my*0+by
line1 = curve(pos=[(x0,y0),(xap,yap)], color=color.yellow,radius=4)
line2 = curve(pos=[(xap,yap),(xbp,ybp)], color=color.yellow,radius=4)
line3 = curve(pos=[(xbp,ybp),(xcp,ycp)], color=color.yellow,radius=4)
topline = curve(pos=[(x0,y0),(xcp,ycp)], color=color.red,radius=4)
def F(x, f): # Define F function
f[0] = 3*x[3] + 4*x[4] + 4*x[5] - 8.0
f[1] = 3*x[0] + 4*x[1] - 4*x[2]
f[2] = x[6]*x[0] - x[7]*x[1] - 10.0
f[3] = x[6]*x[3] - x[7]*x[4]
f[4] = x[7]*x[1] + x[8]*x[2] - 20.0
f[5] = x[7]*x[4] - x[8]*x[5]
f[6] = pow(x[0], 2) + pow(x[3], 2) - 1.0
f[7] = pow(x[1], 2) + pow(x[4], 2) - 1.0
f[8] = pow(x[2], 2) + pow(x[5], 2) - 1.0
def dFi_dXj(x, deriv, n): # Define derivative function
h = 1*10**(-4)
for j in range(0, n):
temp = x[j]
x[j] = x[j] + h/2.
F(x, f)
for i in range(0, n): deriv[i, j] = f[i]
x[j] = temp
for j in range(0, n):
temp = x[j]
x[j] = x[j] - h/2.
F(x, f)
for i in range(0, n): deriv[i, j] = (deriv[i, j] - f[i])/h
x[j] = temp
for it in range(1, 100):
rate(1) # 1 second between graphs
F(x, f)
dFi_dXj(x, deriv, n)
B = np.array([[-f[0]], [-f[1]], [-f[2]], [-f[3]], [-f[4]], [-f[5]],[-f[6]], [-f[7]], [-f[8]]])
sol = solve(deriv, B)
dx = np.take(sol, (0, ), 1) # take the first column of matrix sol
for i in range(0, n):
x[i] = x[i] + dx[i]
plotconfig()
errX = errF = errXi = 0.0
for i in range(0, n):
if ( x[i] != 0.): errXi = abs(dx[i]/x[i])
else: errXi = abs(dx[i])
if ( errXi > errX): errX = errXi
if ( abs(f[i]) > errF ): errF = abs(f[i])
if ( (errX <= eps) and (errF <= eps) ): break
print('Number of iterations = ', it)
print('Solution:')
for i in range(0, n):
print('x[', i, '] = ', x[i])
AttributeError Traceback (most recent call last)
<ipython-input-5-78050c1f23ab> in <module>()
76 for i in range(0, n):
77 x[i] = x[i] + dx[i]
---> 78 plotconfig()
79 errX = errF = errXi = 0.0
80
<ipython-input-5-78050c1f23ab> in plotconfig()
10
11 def plotconfig():
---> 12 for obj in scene.objects:
13 obj.visible=0 # to erase the previous configuration
14 L1 = 3.0
AttributeError: 'NoneType' object has no attribute 'objects'
I tried your example : It works like it is, but first install vpython this way
pip install vpython (after to upgrade : --upgrade)
pip install ivisual --upgrade
If you work system wide
sudo -H pip.....

Categories