float() argument must be a string or a number, not 'map' - python

enter image description here
between the codes there are the following codes:
def regTreeEval(model, inDat):
if model is not None:
return float(model)
def modelTreeEval(model, inDat):
n = shape(inDat)[1]
X = mat(ones((1, n+1)))
X[:, 1: n+1] = inDat
# print X, model
xmodel = X*model
if xmodel is not None:
return float(X * model)

<pre><code>def modelErr(dataSet):
ws, X, Y = linearSolve(dataSet)
yHat = X * ws
# print corrcoef(yHat, Y, rowvar=0)
return sum(power(Y - yHat, 2))
def linearSolve(dataSet):
m,n = shape(dataSet)
X = mat(ones((m,n)))
Y = mat(ones((m,1)))
X[:,1:n] = dataSet[:,0:n-1]
Y = dataSet[:,-1]
xTx = X.T*X
if linalg.det(xTx) ==0.0:
raiseNameError("This matrix is singular, cannot do inverse")
ws = X.T*X.I*(X.T*Y)
return ws,X,Y` def modelErr(dataSet):
ws, X, Y = linearSolve(dataSet)
yHat = X * ws
# print corrcoef(yHat, Y, rowvar=0)
return sum(power(Y - yHat, 2))
def linearSolve(dataSet):
m,n = shape(dataSet)
X = mat(ones((m,n)))
Y = mat(ones((m,1)))
X[:,1:n] = dataSet[:,0:n-1]
Y = dataSet[:,-1]
xTx = X.T*X
if linalg.det(xTx) ==0.0:
raiseNameError("This matrix is singular, cannot do inverse")
ws = X.T*X.I*(X.T*Y)
return ws,X,Y
<pre><code>def modelTreeEval(model,inDat):
n=shape(inDat)[1]
X=mat(ones((1,n+1)))
X[:,1:n+1] = inDat
print('X',X,'\n','model',model)
return float(X*model)
---------------------------------------------------------------------------
<pre><code>
TypeError Traceback (most recent call last)
<ipython-input-50-c482daea3463> in <module>()
116 myDat = loadDataSet('F:/data4.txt')
117 myMat = mat(myDat)
--> 118 myTree = createTree(myMat, modelLeaf, modelErr)
119 print(myTree)
120
<ipython-input-42-60c716a5beb4> in createTree(dataSet, leafType, errType, ops)
12 """
13 #
---> 14 feat,val = chooseBestSplit(dataSet,leafType,errType,ops)
15 #
16 '''
<ipython-input-41-4305f5a748bb> in chooseBestSplit(dataSet, leafType, errType, ops)
76 s = set(dataSet[:,-1].T.tolist()[0])
77 if len(s) ==1:
---> 78 return None,leafType(dataSet)
79 m,n = shape(dataSet)
80 #
<ipython-input-43-6a297b0e048d> in modelLeaf(dataSet)
9 ###
10 """
---> 11 ws,X,Y = linearSolve(dataSet)
12 return ws
13
<ipython-input-43-6a297b0e048d> in linearSolve(dataSet)
42 X = mat(ones((m,n)))
43 Y = mat(ones((m,1)))
---> 44 X[:,1:n] = dataSet[:,0:n-1]
45 Y = dataSet[:,-1]
46 xTx = X.T*X
TypeError: float() argument must be a string or a number, not 'map'

Related

TypeError: Singleton array array(22) cannot be considered a valid collection

I'm trying to make a classification prediction using the Random Forest code I wrote, but I get this error. Can anyone solve the error I'm having?
This is the code random forest classifier that I made
class DecisionTreeClassifier(object):
def __init__(self, max_features=lambda x: x, max_depth=10,
min_samples_split=2):
self.max_features = max_features
self.max_depth = max_depth
self.min_samples_split = min_samples_split
def fit(self, X, y):
n_features = X.shape[1]
n_sub_features = int(self.max_features(n_features))
feature_indices = random.sample(range(n_features), n_sub_features)
self.trunk = self.build_tree(X, y, feature_indices, 0)
def predict(self, X):
num_samples = X.shape[0]
y = np.empty(num_samples)
for j in range(num_samples):
node = self.trunk
while isinstance(node, Node):
if X[j][node.feature_index] <= node.threshold:
node = node.branch_true
else:
node = node.branch_false
y[j] = node
return y
def build_tree(self, X, y, feature_indices, depth):
if depth == self.max_depth or len(y) < self.min_samples_split or entropy(y) == 0:
return mode(y)[0][0]
feature_index, threshold = find_split(X, y, feature_indices)
X_true, y_true, X_false, y_false = train_test_split(X, y, feature_index, threshold)
if y_true.shape[0] == 0 or y_false.shape[0] == 0:
return mode(y)[0][0]
branch_true = self.build_tree(X_true, y_true, feature_indices, depth + 1)
branch_false = self.build_tree(X_false, y_false, feature_indices, depth + 1)
return Node(feature_index, threshold, branch_true, branch_false)
def find_split(X, y, feature_indices):
num_features = X.shape[1]
best_gain = 0
best_feature_index = 0
best_threshold = 0
for feature_index in feature_indices:
values = sorted(set(X[:, feature_index])) ### better way
for j in range(len(values) - 1):
threshold = (values[j] + values[j+1])/2
X_true, y_true, X_false, y_false = train_test_split(X, y, feature_index, threshold)
gain = information_gain(y, y_true, y_false)
if gain > best_gain:
best_gain = gain
best_feature_index = feature_index
best_threshold = threshold
return best_feature_index, best_threshold
class Node(object):
def __init__(self, feature_index, threshold, branch_true, branch_false):
self.feature_index = feature_index
self.threshold = threshold
self.branch_true = branch_true
self.branch_false = branch_false
def split(X, y, feature_index, threshold):
X_true = []
y_true = []
X_false = []
y_false = []
for j in range(len(y)):
if X[j][feature_index] <= threshold:
X_true.append(X[j])
y_true.append(y[j])
else:
X_false.append(X[j])
y_false.append(y[j])
X_true = np.array(X_true)
y_true = np.array(y_true)
X_false = np.array(X_false)
y_false = np.array(y_false)
return X_true, y_true, X_false, y_false
class RandomForestClassifier(object):
def __init__(self, n_estimators=32, max_features=np.sqrt, max_depth=10,
min_samples_split=2, bootstrap=0.9):
self.n_estimators = n_estimators
self.max_features = max_features
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.bootstrap = bootstrap
self.forest = []
def fit(self, X, y):
self.forest = []
n_samples = len(y)
n_sub_samples = round(n_samples*self.bootstrap)
for i in range(self.n_estimators):
shuffle_in_unison(X, y)
X_subset = X[:n_sub_samples]
y_subset = y[:n_sub_samples]
tree = DecisionTreeClassifier(self.max_features, self.max_depth,
self.min_samples_split)
tree.fit(X_subset, y_subset)
self.forest.append(tree)
def predict(self, X):
n_samples = X.shape[0]
n_trees = len(self.forest)
predictions = np.empty([n_trees, n_samples])
for i in range(n_trees):
predictions[i] = self.forest[i].predict(X)
return mode(predictions)[0][0]
def score(self, X, y):
y_predict = self.predict(X)
n_samples = len(y)
correct = 0
for i in range(n_samples):
if y_predict[i] == y[i]:
correct = correct + 1
accuracy = correct/n_samples
return accuracy
When I run the model for prediction like below:
forest = RandomForestClassifier()
forest.fit(xTrain, yTrain)
accuracy = forest.score(xTest, yTest)
print('The accuracy was', 100*accuracy, '% on the test data.')
The error i got:
TypeError Traceback (most recent call last)
<ipython-input-64-afbb01cc03d7> in <module>
1 forest = RandomForestClassifier()
----> 2 forest.fit(xTrain, yTrain)
3
4 accuracy = forest.score(xTest, yTest)
5 print('The accuracy was', 100*accuracy, '% on the test data.')
-------------------------------------------------------------------------------
/usr/local/lib/python3.8/dist-packages/sklearn/utils/validation.py in _num_samples(x)
267 if hasattr(x, "shape") and x.shape is not None:
268 if len(x.shape) == 0:
--> 269 raise TypeError(
270 "Singleton array %r cannot be considered a valid collection." % x
271 )
TypeError: Singleton array array(22) cannot be considered a valid collection.
array(22) in error always changes (the number) every time I try to rerun the prediction model.

Theano TypeError during model building with celerite2 (pymc3)

I recently switched from celerite to celerite2 for modelling stellar light curves. I have followed the celerite 2 tutorial closely, adapting it to my needs. My model consists of a flat mean, a jitter term, a granulation term and an SHO term to model nonradial oscillations.
Data input:
The observations are converted from a lightkurve.KeplerLightCurve object in the following way:
import numpy as np
import lightkurve as lk
import pymc3 as pm
import theano.tensor as tt
import theano
import celerite2
from celerite2.theano import terms
datalist = lk.search_lightcurvefile('KIC005002732', cadence='long')
data = datalist[2:3].download_all()
lc = data.stitch().remove_outliers().remove_nans()
lc = lc[0:2001] # cut lightcurve
# Convert to ppm
lc.flux = (lc.flux - 1) * 1e6
lc.flux_err *= 1e6
#Find numax prior:
pg = lc.to_periodogram(method='lombscargle', normalization='psd',
minimum_frequency=50, maximum_frequency=300, oversample_factor = 3)
snr = pg.flatten()
seis = snr.to_seismology()
numax_guess = seis.estimate_numax(window_width = 1)
time = np.ascontiguousarray(lc.time, dtype=np.float64)
flux = np.ascontiguousarray(lc.flux, dtype=np.float64)
flux_err = np.ascontiguousarray(lc.flux_err, dtype=np.float64)
Priors:
The priors (prior_*_m and prior_*_sd) exclusively are numpy float64 scalars.
# MEAN FUNCTION
prior_mean_m = np.float64(0.0)
prior_mean_sd = np.float64(np.std(flux))
# JITTER TERM
prior_jitter_m = np.float64(10)
prior_jitter_sd = np.float64(5)
# GRANULATION TERM(S)
prior_w_gran_m = np.float64([2*np.pi*numax_guess.value/2])
prior_w_gran_sd = np.float64(100.0)
prior_a_gran_m = np.float64([np.var(flux)])
prior_a_gran_sd = np.float64(np.var(flux)/100*50)
prior_Q_gran_m = np.float64(1/np.sqrt(2))
prior_Q_gran_sd = np.float64(4)
prior_S_gran_m = prior_a_gran_m/(prior_w_gran_m * prior_Q_gran_m)
prior_S_gran_sd= np.abs(prior_Q_gran_sd/(prior_w_gran_m*prior_Q_gran_m)
- (prior_a_gran_m *prior_w_gran_sd)/(prior_w_gran_m**2*prior_Q_gran_m)
- (prior_a_gran_m*prior_Q_gran_sd)/(prior_w_gran_m*prior_Q_gran_m**2))
# NONRADIAL OSCILLATION TERM(S)
prior_rho_osc_m = np.float64(1/numax_guess.value)
prior_rho_osc_sd = prior_rho_osc_m/100*10
prior_tau_osc_m = np.float64(2*4/(2*np.pi*numax_guess.value)) # deterministic result for Q = 4, numax = numax_guess
prior_tau_osc_sd = prior_tau_osc_m/100*50
prior_sig_osc_m = np.float64(np.sqrt(np.var(flux)))
prior_sig_osc_sd = prior_sig_osc_m/100*50
Model building:
I have included a shape parameter for most of the distributions because I want my code to be able to process vectors as inputs in case I need more than one granulation/oscillation term.
with pm.Model() as gp_model:
# MEAN FUNCTION ----------------------------------------------------------------------------------------------------------------
mean_function = pm.Normal("mean", mu=prior_mean_m, sd=prior_mean_sd)
# JITTER TERM ------------------------------------------------------------------------------------------------------------------
jitter = pm.Lognormal("jitter", mu=prior_jitter_m, sigma=prior_jitter_sd)
# GRANULATION TERM(S) ----------------------------------------------------------------------------------------------------------
num_gran_terms = np.size(prior_w_gran_m) # used later on
#Set pymc3 priors for parameters
Q_gran = prior_Q_gran_m
a_gran = pm.Lognormal("a_gran", mu = np.log(prior_a_gran_m), sigma = prior_a_gran_sd, shape = np.size(prior_a_gran_m)) #parameter a of SHO term. Used to link w0 and S0.
w_gran = pm.Lognormal("w_gran", mu = np.log(prior_w_gran_m), sigma = prior_w_gran_sd, shape = np.size(prior_w_gran_m))
S_gran = pm.Deterministic("S_gran", a_gran/(w_gran * Q_gran)) #S0 = a/(w0*Q)
# Build terms
if np.size(prior_w_gran_m) is 1:
gran_terms = terms.SHOTerm(S0=S_gran, w0=w_gran, Q=Q_gran)
else:
gran_terms = terms.SHOTerm(S0=S_gran[0], w0=w_gran[0], Q=Q_gran)
for i in range(1,np.size(prior_a_gran_m)):
gran_terms += terms.SHOTerm(S0=S_gran[i], w0=w_gran[i], Q=Q_gran)
# NONRADIAL OSCILLATION TERM(S) ------------------------------------------------------------------------------------------------
num_osc_terms = np.size(prior_rho_osc_m) # used later on
#Set pymc3 priors for parameters
tau_osc = pm.Lognormal("tau_osc", mu = np.log(prior_tau_osc_m), sigma = prior_tau_osc_sd, shape = np.size(prior_tau_osc_m))
rho_osc = pm.Lognormal("rho_osc", mu = np.log(prior_rho_osc_m), sigma = prior_rho_osc_sd, shape = np.size(prior_rho_osc_m))
sig_osc = pm.Lognormal("sig_osc", mu = np.log(prior_sig_osc_m), sigma = prior_sig_osc_sd, shape = np.size(prior_sig_osc_m))
# Build terms
if np.size(prior_rho_osc_m) is 1:
osc_terms = terms.SHOTerm(rho=rho_osc, tau=tau_osc, sigma=sig_osc)
else:
osc_terms = terms.SHOTerm(rho=rho_osc[0], tau=tau_osc[0], sigma=sig_osc[0])
for i in range(1,np.size(prior_rho_osc_m)):
osc_terms += terms.SHOTerm(rho=rho_osc[i], tau=tau_osc[i], sigma=sig_osc[i])
# BUILD GP ---------------------------------------------------------------------------------------------------------------------
kernel = terms.TermSum(gran_terms, osc_terms)
gp = celerite2.theano.GaussianProcess(kernel, mean = mean_function)
gp.compute(time, diag=flux_err ** 2 + jitter, check_sorted = True) # Compute cholesky factorisation of covariance matrix
gp.marginal("obs", observed=flux) # Add marginal likelihood to model (observations)
print("Initial log likelihood: {0}".format(gp.log_likelihood(flux)))
pm.Potential("loglike", gp.log_likelihood(flux - mean_function)) # Define Potential to be used for optimization
Now when I run this code, I get the following error:
TypeError: The two branches should have identical types, but they are TensorType(float64, vector) and TensorType(float64, (True, True)) respectively. This error could be raised if for example you provided a one element list on the `then` branch but a tensor on the `else` branch.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-b80a96105cf6> in <module>
16 # Build terms
17 if np.size(prior_w_gran_m) is 1:
---> 18 gran_terms = terms.SHOTerm(S0=S_gran, w0=w_gran, Q=Q_gran)
19 else:
20 gran_terms = terms.SHOTerm(S0=S_gran[0], w0=w_gran[0], Q=Q_gran)
~\Anaconda3\lib\site-packages\celerite2\terms.py in wrapped(target, *args, **kwargs)
604 break
605
--> 606 return to_wrap(target, *args, **kwargs)
607
608 return wrapped
~\Anaconda3\lib\site-packages\celerite2\theano\terms.py in __init__(self, eps, **kwargs)
444 def __init__(self, *, eps=1e-5, **kwargs):
445 self.eps = tt.as_tensor_variable(eps).astype("float64")
--> 446 super().__init__(**kwargs)
447
448 def overdamped(self):
~\Anaconda3\lib\site-packages\celerite2\theano\terms.py in __init__(self, dtype)
27 def __init__(self, *, dtype="float64"):
28 self.dtype = dtype
---> 29 self.coefficients = self.get_coefficients()
30
31 def __add__(self, b):
~\Anaconda3\lib\site-packages\celerite2\theano\terms.py in get_coefficients(self)
480 def get_coefficients(self):
481 m = self.Q < 0.5
--> 482 return [
483 ifelse(m, a, b)
484 for a, b in zip(self.overdamped(), self.underdamped())
~\Anaconda3\lib\site-packages\celerite2\theano\terms.py in <listcomp>(.0)
481 m = self.Q < 0.5
482 return [
--> 483 ifelse(m, a, b)
484 for a, b in zip(self.overdamped(), self.underdamped())
485 ]
~\Anaconda3\lib\site-packages\theano\ifelse.py in ifelse(condition, then_branch, else_branch, name)
367 if then_branch_elem.type != else_branch_elem.type:
368 # If the types still don't match, there is a problem.
--> 369 raise TypeError(
370 'The two branches should have identical types, but '
371 'they are %s and %s respectively. This error could be '
TypeError: The two branches should have identical types, but they are TensorType(float64, vector) and TensorType(float64, (True, True)) respectively. This error could be raised if for example you provided a one element list on the `then` branch but a tensor on the `else` branch.
This makes no sense to me (as most theano error messages do, to be honest). Why is one of the branches boolean?
What I've tried
I have already tried omitting the shape parameter in the code, as I've had problems with that before.
a_gran = pm.Lognormal("a_gran", mu = np.log(prior_a_gran_m), sigma = prior_a_gran_sd)
...
This results in the following error:
TypeError: For compute_test_value, one input test value does not have the requested type.
The error when converting the test value to that variable type:
Wrong number of dimensions: expected 0, got 1 with shape (1,).
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-70-d0ae027fe60a> in <module>
10 num_gran_terms = np.size(prior_w_gran_m)
11 Q_gran = prior_Q_gran_m
---> 12 a_gran = pm.Lognormal("a_gran", mu = np.log(prior_a_gran_m), sigma = prior_a_gran_sd) #parameter a of SHO term. Used to link w0 and S0.
13 w_gran = pm.Lognormal("w_gran", mu = np.log(prior_w_gran_m), sigma = prior_w_gran_sd)
14 S_gran = pm.Deterministic("S_gran", a_gran/(w_gran * Q_gran)) #S0 = a/(w0*Q)
~\Anaconda3\lib\site-packages\pymc3\distributions\distribution.py in __new__(cls, name, *args, **kwargs)
45 total_size = kwargs.pop('total_size', None)
46 dist = cls.dist(*args, **kwargs)
---> 47 return model.Var(name, dist, data, total_size)
48 else:
49 raise TypeError("Name needs to be a string but got: {}".format(name))
~\Anaconda3\lib\site-packages\pymc3\model.py in Var(self, name, dist, data, total_size)
924 else:
925 with self:
--> 926 var = TransformedRV(name=name, distribution=dist,
927 transform=dist.transform,
928 total_size=total_size,
~\Anaconda3\lib\site-packages\pymc3\model.py in __init__(self, type, owner, index, name, distribution, model, transform, total_size)
1654
1655 self.transformed = model.Var(
-> 1656 transformed_name, transform.apply(distribution), total_size=total_size)
1657
1658 normalRV = transform.backward(self.transformed)
~\Anaconda3\lib\site-packages\pymc3\distributions\transforms.py in apply(self, dist)
110 def apply(self, dist):
111 # avoid circular import
--> 112 return TransformedDistribution.dist(dist, self)
113
114 def __str__(self):
~\Anaconda3\lib\site-packages\pymc3\distributions\distribution.py in dist(cls, *args, **kwargs)
55 def dist(cls, *args, **kwargs):
56 dist = object.__new__(cls)
---> 57 dist.__init__(*args, **kwargs)
58 return dist
59
~\Anaconda3\lib\site-packages\pymc3\distributions\transforms.py in __init__(self, dist, transform, *args, **kwargs)
139 self.dist = dist
140 self.transform_used = transform
--> 141 v = forward(FreeRV(name="v", distribution=dist))
142 self.type = v.type
143
~\Anaconda3\lib\site-packages\pymc3\model.py in __init__(self, type, owner, index, name, distribution, total_size, model)
1368 self.tag.test_value = np.ones(
1369 distribution.shape, distribution.dtype) * distribution.default()
-> 1370 self.logp_elemwiset = distribution.logp(self)
1371 # The logp might need scaling in minibatches.
1372 # This is done in `Factor`.
~\Anaconda3\lib\site-packages\pymc3\distributions\continuous.py in logp(self, value)
1832 mu = self.mu
1833 tau = self.tau
-> 1834 return bound(-0.5 * tau * (tt.log(value) - mu)**2
1835 + 0.5 * tt.log(tau / (2. * np.pi))
1836 - tt.log(value),
~\Anaconda3\lib\site-packages\theano\gof\op.py in __call__(self, *inputs, **kwargs)
623 for i, ins in enumerate(node.inputs):
624 try:
--> 625 storage_map[ins] = [self._get_test_value(ins)]
626 compute_map[ins] = [True]
627 except AttributeError:
~\Anaconda3\lib\site-packages\theano\gof\op.py in _get_test_value(cls, v)
560 # ensure that the test value is correct
561 try:
--> 562 ret = v.type.filter(v.tag.test_value)
563 except Exception as e:
564 # Better error message.
~\Anaconda3\lib\site-packages\theano\tensor\type.py in filter(self, data, strict, allow_downcast)
174
175 if self.ndim != data.ndim:
--> 176 raise TypeError("Wrong number of dimensions: expected %s,"
177 " got %s with shape %s." % (self.ndim, data.ndim,
178 data.shape))
TypeError: For compute_test_value, one input test value does not have the requested type.
The error when converting the test value to that variable type:
Wrong number of dimensions: expected 0, got 1 with shape (1,).
I have also tried setting a test value manually, which yields the same error.
a_gran = pm.Lognormal("a_gran", mu = np.log(prior_a_gran_m), sigma = prior_a_gran_sd, testval = np.log(prior_a_gran_m))
...
This is my first time posting here, so sorry if I forgot any crucial information.
Thanks in advance for your help!

Multiple variables in curve_fit ,sigma has incorrect shape?

I have a tried curve_fit function for multiple variables. I have encountered a problem with "sigma has incorrect shape". I tried the following code. Could anyone explain why I get this error?
Here x and y are my independent variables and p,q,r are parameters I want to fit
xdata = [214.737191559, -5.64912101538e-36, 36.1372453686, 189.459700978, 233.562136902, 201.230228832, -5.59364882619e-36, -36.3232002416, -188.192199081, -212.837139143, -232.342545403, -200.699429716]
ydata = [-5.88273617837e-37, -211.536123799, -186.67108047, -35.9497006815, 200.282998159, 232.085860035, 213.44274878, 187.945919272, 35.7227474297, -6.00785257974e-37, -199.746844708, -230.856058666]
xdata = np.array(xdata)
ydata = np.array(ydata)
def func1(X,a,b,c):
x,y = X
n = 8
# % A = ydata
# % B = -xdata
# % C = xdata. - ydata
# % H = zdata
g = np.subtract(x,y)
I_0 = np.subtract(x,y) # x-y = C
I_1 = np.multiply(I_0,c) # c(x-y) = cC
I_2 = np.multiply(b,-x) #b(-x) = bB
I_3 = np.multiply(a,y) # aA
I3_0 = np.subtract(I_1,I_2) # cC-bB
I3_1 = np.subtract(I_3,I_1) # aA-cC
I3_2 = np.subtract(I_2,I_3) # bB-aA
I3_00 = np.multiply(I3_0,I3_1) # (cC-bB)(aA-cC)
I3_01 = np.multiply(I3_00,I3_2) # (cC-bB)(aA-cC)(bB-aA)
I3 = np.divide(I3_01,54) # (cC-bB)(aA-cC)(bB-aA)/54
I2_0 = np.power((I3_1),2) # (aA-cC)^2
I2_1 = np.power((I3_0),2) # (cC-bB)^2
I2_2 = np.power((I3_2),2) # (bB-aA)^2
I2_00 = np.add(I2_0,I2_1) # (aA-cC)^2 + (cC-bB)^2
I2_01 = np.add(I2_00,I2_2) # (aA-cC)^2 + (cC-bB)^2 + (bB-aA)^2
I2 = np.divide(I2_01,54) # ((aA-cC)^2 + (cC-bB)^2 + (bB-aA)^2)/54
th_0 = np.divide(I3,(np.power(I2,(3/2)))) # I3/(I2^(3/2))
th = np.arccos(np.clip((th_0),-1,1)) # arccos(I3/(I2^(3/2)))
ans_0 = np.divide(np.add((2*th),(np.pi)),6) # (2*th + pi)/6
ans_1 = np.divide(np.add((2*th),(3*np.pi)),6) # (2*th + 3*pi)/6
ans_2 = np.divide(np.add((2*th),(5*np.pi)),6) # (2*th + 5*pi)/6
ans_00 = np.multiply(np.cos(ans_0),2) # 2*cos((2*th + pi)/6)
ans_11 = np.multiply(np.cos(ans_1),2) # 2*cos((2*th + 3*pi)/6)
ans_22 = np.multiply(np.cos(ans_2),2) # 2*cos((2*th + 5*pi)/6)
ans_000 = np.power(np.absolute(ans_00),n) # (abs(2*cos((2*th + pi)/6)))^n
ans_111 = np.power(np.absolute(ans_11),n) # (abs(2*cos((2*th + 3*pi)/6)))^n
ans_222 = np.power(np.absolute(ans_22),n) # (abs(2*cos((2*th + 5*pi)/6)))^n
ans_0000 = np.add((np.power(np.absolute(ans_00),n)),(np.power(np.absolute(ans_11),n))) # (abs(2*cos((2*th + pi)/6)))^n + (abs(2*cos((2*th + 3*pi)/6)))^n
ans_1111 = np.add((ans_0000),(np.power(np.absolute(ans_22),n))) # (abs(2*cos((2*th + pi)/6)))^n + (abs(2*cos((2*th + 3*pi)/6)))^n + (abs(2*cos((2*th + 5*pi)/6)))^n
sna_0 = np.power(np.multiply(3,I2),(n/2)) # (3*I2)^(n/2) !!
sna_1 = 2*(np.power(190,n)) # 2*(sigma^n) !!
sna_00 = np.multiply(sna_0,ans_1111)
sna_11 = np.subtract(sna_00,sna_1)
return sna_11
a, b, c = 10., 4., 6.
z = func1((xdata,ydata), a, b, c) * 1 + np.random.random(12) / 100
# initial guesses for a,b,c:
a, b, c = 1, 1, 1
p0 = np.array([a, b, c])
# p0 = 8., 2., 7.
popt,pcov = (curve_fit(func1, (xdata,ydata),z, p0))
popt
When I run this I go the following error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-13-484bc542850b> in <module>()
6 p0 = np.array([a, b, c])
7 # p0 = 8., 2., 7.
----> 8 popt,pcov = (curve_fit(func1, (xdata,ydata), p0))
9 popt
~/.conda/envs/ML/lib/python3.6/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
749 # Remove full_output from kwargs, otherwise we're passing it in twice.
750 return_full = kwargs.pop('full_output', False)
--> 751 res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
752 popt, pcov, infodict, errmsg, ier = res
753 cost = np.sum(infodict['fvec'] ** 2)
~/.conda/envs/ML/lib/python3.6/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
381 if not isinstance(args, tuple):
382 args = (args,)
--> 383 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
384 m = shape[0]
385 if n > m:
~/.conda/envs/ML/lib/python3.6/site-packages/scipy/optimize/minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
25 def _check_func(checker, argname, thefunc, x0, args, numinputs,
26 output_shape=None):
---> 27 res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
28 if (output_shape is not None) and (shape(res) != output_shape):
29 if (output_shape[0] != 1):
~/.conda/envs/ML/lib/python3.6/site-packages/scipy/optimize/minpack.py in func_wrapped(params)
461 if transform is None:
462 def func_wrapped(params):
--> 463 return func(xdata, *params) - ydata
464 elif transform.ndim == 1:
465 def func_wrapped(params):
ValueError: operands could not be broadcast together with shapes (12,) (3,)
The error you are receiving ValueError: ``sigma`` has incorrect shape. is related to the incorrect call of curve_fit and the difference between what the function is expecting and what are you feeding it. Here is an example of a correct call:
p, q, r = 1, 1, 1
p0 = np.array([p, q, r])
cfit = curve_fit(func, xdata, ydata, p0)
print(cfit)
Unfortunately, that is not the only thing porblematic in your code. Your func1 will require from you some editing. You can refer to this post on how to use curve_fit.
Update:
I shortened your code and optimized some lines plus -as mentioned in the comments- you need an output variable so I generated some custom zdata that you can later replace with your data.
import numpy as np
from scipy.optimize import curve_fit
xdata = [214.737, -5.649e-36, 36.137, 189.459, 233.562, 201.230, -5.593e-36, -36.323, -188.192, -212.837, -232.342, -200.699]
ydata = [-5.882e-37, -211.536, -186.671, -35.949, 200.282, 232.085, 213.442, 187.945, 35.722, -6.007, -199.746, -230.856]
def func(X, p, q, r):
x = np.array(X[0])
y = np.array(X[1])
n = 8
a1 = (p * y) - (r * (x-y))
b1 = (q * -1 * x) - (p * y)
c1 = (r * (x - y)) - (q * -1 * x)
I3 = (a1 * b1 * c1) / 54
I2 = (a1**2 + b1**2 + c1**2) / 54
th = np.arccos( I3 / (I2**(3/2)) )
an1 = (np.abs(2 * np.cos((2 * th + 1 * np.pi) /6)))**n
an2 = (np.abs(2 * np.cos((2 * th + 3 * np.pi) /6)))**n
an3 = (np.abs(2 * np.cos((2 * th + 5 * np.pi) /6)))**n
res = ( (3 * I2)**(n/2) ) * (an1 + an2 + an3) - (2 * (189.32)**8)
return res
# init
p, q, r = 1, 1, 1
p0 = np.array([p, q, r])
# artificial zdata
zdata = func((xdata, ydata), p, q, r) + np.random.random(np.array(xdata).shape)
cfit = curve_fit(func, (xdata, ydata), zdata, p0)
# print output
print(cfit)
I still don't exactly get what you have inside of func which is causing a RuntimeWarning: due to invalid value encountered in arccos and that is why I edited the data you provided too.

zero-size array to reduction operation maximum which has no identity

Here is a runnable snippet of code which generates the error on my machine.
I have this error (zero-size array to reduction operation maximum which has no identity).
reader = csv.reader(open(BASE_PATH + 'labels.csv'))
# skip the header
next(reader)
X = []
y = []
for row in reader:
label = row[2]
if len(label) > 0 and label.find(',') == -1:
filename = get_filename_for_index(row[1])
img_file = cv2.imread(BASE_PATH + filename)
if img_file is not None:
img_file = scipy.misc.imresize(arr=img_file, size=(120, 160, 3))
img_arr = np.asarray(img_file)
img_arr = apply_color_mask(img_arr)
X.append(img_arr)
y.append(label)
X = np.asarray(X)
y = np.asarray(y)
encoder = LabelEncoder()
encoder.fit(y)
encoded_y = encoder.transform(y)
y = np_utils.to_categorical(encoded_y)
**Error**
ValueError Traceback (most recent call last)
<ipython-input-7-49c49b99292b> in <module>()
26 encoded_y = encoder.transform(y)
27
---> 28 y = np_utils.to_categorical(encoded_y)
~/.local/lib/python3.5/site-packages/keras/utils/np_utils.py in to_categorical(y, num_classes)
20 y = np.array(y, dtype='int').ravel()
21 if not num_classes:
---> 22 num_classes = np.max(y) + 1
23 n = y.shape[0]
24 categorical = np.zeros((n, num_classes))
~/.local/lib/python3.5/site-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims)
2270
2271 return _methods._amax(a, axis=axis,
-> 2272 out=out, **kwargs)
2273
2274
~/.local/lib/python3.5/site-packages/numpy/core/_methods.py in _amax(a, axis, out, keepdims)
24 # small reductions
25 def _amax(a, axis=None, out=None, keepdims=False):
---> 26 return umr_maximum(a, axis, None, out, keepdims)
27
28 def _amin(a, axis=None, out=None, keepdims=False):
Value Error: zero-size array to reduction operation maximum which has no identity
so please any one can help me.
You can try to put the failing operation inside some kind guard:
if np.any(encoded_y):
y = np_utils.to_categorical(encoded_y)

Python: UnboundLocalError: local variable 'mostfrequent' referenced before assignment (KNeighborsClassifier)

I am trying to run KNeighborsClassifer for multiple "k" as follows, but get an error on line where I am doing the "predictions". When I run the same code with k = 1, even multiple times, it works fine. There must be something I don't understand about the sklearn objects here. What is the problem with the code here? Thank you.
My code:
accuracy = []
f1score = []
predictions = []
for n in range(10):
vectorizer = CountVectorizer()
output = vectorizer.fit_transform(train_data)
output_dev = vectorizer.transform(dev_data)
neighbor = KNeighborsClassifier(n_neighbors = n)
neighbor.fit(output, train_labels)
predictions = neighbor.predict(output_dev)
accuracy.append(round(sum(predictions == dev_labels) * 1.0 / len(predictions), 2))
f1score.append(round(metrics.f1_score(dev_labels, predictions), 2))
print accuracy
print f1score
Error message:
UnboundLocalError Traceback (most recent call last)
<ipython-input-99-c3eaa2d9dd70> in <module>()
17 print f1score
18
---> 19 P3()
<ipython-input-99-c3eaa2d9dd70> in P3()
10 neighbor = KNeighborsClassifier(n_neighbors = n)
11 neighbor.fit(output, train_labels)
---> 12 predictions = neighbor.predict(output_dev)
13
14 accuracy.append(round(sum(predictions == dev_labels) * 1.0 / len(predictions), 2))
/Library/Python/2.7/site-packages/sklearn/neighbors/classification.pyc in predict(self, X)
160 for k, classes_k in enumerate(classes_):
161 if weights is None:
--> 162 mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
163 else:
164 mode, _ = weighted_mode(_y[neigh_ind, k], weights, axis=1)
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/stats/stats.pyc in mode(a, axis)
658 oldcounts = np.maximum(counts, oldcounts)
659 oldmostfreq = mostfrequent
--> 660 return mostfrequent, oldcounts
661
662 def mask_to_limits(a, limits, inclusive):
UnboundLocalError: local variable 'mostfrequent' referenced before assignment

Categories

Resources