While calculate SSE getting Error : 'numpy.float64' object is not iterable - python

I am trying to calculate the Sum of Square Error(SSE), code mentioned below
def SSEadver(tv_train,radio_train,newsppr_train,y_train):
y_train_predct = []
sse_train = 0
y_train_predct_srs = 0
# Calculating the predicted sales values on training data
for i in range(0,len(tv_train)):
y_train_predct.append(2.8769666223179353 + (0.04656457* tv_train.iloc[i])+ (0.17915812*(radio_train.iloc[i])) + (0.00345046*(newsppr_train.iloc[i])))
# ** Here I Convert y_train_predct's type List to Series, but still it is showing type as list**
y_train_predct_srs = pd.Series(y_train_predct)
# *** Due above converting not working here y_train_predct_srs.iloc[j]) is not working***
# Now calculate SSE (sum of Squared Errors)
for j in range (len(y_train)):
sse_train += sum((y_train.iloc[j] - y_train_predct_srs.iloc[j])**2)
return y_train_predct, y_train_predct_srs
sse_train = SSEadver(tv_train,radio_train,newsppr_train, y_train)
While I run this code I am getting error :
TypeError Traceback (most recent call last)
<ipython-input-446-576e1af02461> in <module>()
20 return y_train_predct, y_train_predct_srs
21
---> 22 sse_train = SSEadver(tv_train,radio_train,newsppr_train, y_train)
23
<ipython-input-446-576e1af02461> in SSEadver(tv_train, radio_train, newsppr_train, y_train)
14 # Now calculate SSE (sum of Squared Errors)
15 for j in range (len(y_train)):
---> 16 sse_train += sum((y_train.iloc[j] - y_train_predct_srs.iloc[j])**2)
17
18
TypeError: 'numpy.float64' object is not iterable
why am I getting this error? I am using Python 3.X.X

I can't see all your code, however, it looks like either
y_train.iloc or y_train_predct_srs.iloc
are not lists, but in fact numpy.float64. You should check they are definitely lists, and try again.

Related

How to fix the error of this code: "dirac[:N / 2] = 1"?

I got this python code from the internet, and it's for calculating the modulation spread function (MTF) from an input image. Here is the
full code.
The problem is that the code is not functioning on my PC due to an error in this line :
TypeError Traceback (most recent call last)
<ipython-input-1-035feef9e484> in <module>
54 N = 250
55 dirac = np.zeros(N)
---> 56 dirac[:N / 2] = 1
57
58 # Filter edge
TypeError: slice indices must be integers or None or have an __index__ method
Simply make N/2 an integer again.
dirac[:int(N/2)] = 1

'list' object is not callable for checking score accuracy?

I am creating a model using SVM. I wanted to save the classifier model and the parameters that was used into an excel and .json file, which will then be opened to see the best model out of all the .json files.
However, I got this error when I tried to run the second part of the code:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-9fd85866127d> in <module>
88 for x in func:
89 count=count+1
---> 90 train_val(x[0],x[1],x[2],count)
91 end_time = time.time()
<ipython-input-4-9fd85866127d> in train_val(kernel, c, gamma, count)
43 scoring.append(score(y_test, predictions))
44 else:
---> 45 scoring.append(score(y_test, predictions,average='macro'))
46
47 # saving kernel that is used to the list
TypeError: 'list' object is not callable
I didn't put anything that has the word 'list' so it shouldn't have been overridden. What makes the score list uncallable? Thank you.
You create lists:
accuracy = []
precision = []
recall = []
f1 = []
...
and you define scores to hold these lists:
scores = [accuracy, precision, recall, f1]
Then you iterate over these lists:
for score in scores:
...
But inside that loop you use these lists as if they're functions:
score(y_test, predictions)

Why do I get a type error when trying to set a variable using a loop?

I am trying to set a variable for each row based on a if statements, each row needs to have its own value based on the conditions. The purpose of which is to back test a pricing indicator. I am able to set df_main to only consider the last row which works however I need a result for each row to back test.
The error i receive is:
TypeError: '<' not supported between instances of 'numpy.ndarray' and 'str'
maximum_long=df_main[df_main.columns[i]].max() # find extremes of all positions and traders
minimum_long=df_main[df_main.columns[i]].min()
maximum_short=df_main[df_main.columns[i+1]].min()
minimum_short=df_main[df_main.columns[i+1]].max()
maximum_trader_long=df_main[df_main.columns[i+2]].max()
minimum_trader_long=df_main[df_main.columns[i+2]].min()
maximum_trader_short=df_main[df_main.columns[i+3]].max()
minimum_trader_short=df_main[df_main.columns[i+3]].min()
long_quartile= (maximum_long - minimum_long)/4 #Create quartiles for short and long positions
short_quartile= (maximum_short - minimum_short)/4
trader_long_quartile= (maximum_trader_long - minimum_trader_long)/4
trader_short_quartile= (maximum_trader_short - minimum_trader_short)/4
df_main['dry_long_score'] = 0
df_main['dry_short_score'] = 0
for index, rows in df_main.iterrows():
if df_main.columns[i] > minimum_long + 3*long_quartile:
if df_main.columns[i+2]> minimum_trader_long + 3*trader_long_quartile:
df_main['dry_long_score']=-1
else:
df_main['dry_long_score']=0
elif long_commitment_last < minimum_long + long_quartile:
if df_main.columns[i+2]< minimum_trader_long + trader_long_quartile:
df_main['dry_long_score']=1
else:
df_main['dry_long_score']=0
else:
df_main['dry_long_score']=0
if df_main.columns[i+1] < minimum_short + 3*short_quartile:
if df_main.columns[i+3]> minimum_trader_short + 3*trader_long_quartile:
df_main['dry_short_score']=1
else:
df_main['dry_short_score']=0
elif df_main.columns[i+1] < minimum_short + short_quartile:
if df_main.columns[i+3]< minimum_trader_short + trader_short_quartile:
df_main['dry_short_score']=-1
else:
df_main['dry_short_score']=0
else:
df_main['dry_short_score']=0
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-01e4e50d5411> in <module>()
23 for index, rows in df_main.iterrows():
24
---> 25 if df_main.columns[i] > minimum_long + 3*long_quartile:
26 if df_main.columns[i+2]> minimum_trader_long + 3*trader_long_quartile:
27 df_main['dry_long_score']=-1
TypeError: '<' not supported between instances of 'numpy.ndarray' and 'str'
if df_main.columns[i] > minimum_long + 3*long_quartile:
In this line, the left is a columns of dataframe but the right is a number. So you can't compare between array and number. Therefore, here throw a type error.

numpy TypeError: ufunc 'invert' not supported for the input types, and the inputs

For the code below:
def makePrediction(mytheta, myx):
# -----------------------------------------------------------------
pr = sigmoid(np.dot(myx, mytheta))
pr[pr < 0.5] =0
pr[pr >= 0.5] = 1
return pr
# -----------------------------------------------------------------
# Compute the percentage of samples I got correct:
pos_correct = float(np.sum(makePrediction(theta,pos)))
neg_correct = float(np.sum(np.invert(makePrediction(theta,neg))))
tot = len(pos)+len(neg)
prcnt_correct = float(pos_correct+neg_correct)/tot
print("Fraction of training samples correctly predicted: %f." % prcnt_correct)
I get this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-f0c91286cd02> in <module>()
13 # Compute the percentage of samples I got correct:
14 pos_correct = float(np.sum(makePrediction(theta,pos)))
---> 15 neg_correct = float(np.sum(np.invert(makePrediction(theta,neg))))
16 tot = len(pos)+len(neg)
17 prcnt_correct = float(pos_correct+neg_correct)/tot
TypeError: ufunc 'invert' not supported for the input types, and the inputs
Why is it happening and how can I fix it?
np.invert requires ints or bools, use the method np.linalg.inv instead.
From the documentation:
Parameters:
x : array_like.
Only integer and boolean types are handled."
Your original array is floating point type (the return value of sigmoid()); setting values in it to 0 and 1 won't change the type. You need to use astype(np.int):
neg_correct = float(np.sum(np.invert(makePrediction(theta,neg).astype(np.int))))
should do it (untested).
Doing that, the float() cast you have also makes more sense. Though I would just remove the cast, and rely on Python doing the right thing.
In case you are still using Python 2 (but please use Python 3), just add
from __future__ import division
to let Python do the right thing (it won't hurt if you do it in Python 3; it just doesn't do anything). With that (or in Python 3 anyway), you can remove numerous other float() casts you have elsewhere in your code, improving readability.

AttributeError: 'str' object has no attribute 'search_nodes' - Python

I've built a tree using ete2 package. Now I'm trying to write a piece of code that takes the data from the tree and a csv file and does some data analysis through the function fre.
Here is an example of the csv file I've used:
PID Code Value
1 A1... 6
1 A2... 5
2 A.... 4
2 D.... 1
2 A1... 2
3 D.... 5
3 D1... 3
3 D2... 5
Here is a simplified version of the code
from ete2 import Tree
import pandas as pd
t= Tree("((A1...,A2...)A...., (D1..., D2...)D....).....;", format=1)
data= pd.read_csv('/data_2.csv', names=['PID','Code', 'Value'])
code_count = data.groupby('Code').sum()
total_patients= len(list (set(data['PID'])))
del code_count['PID']
############
def fre(code1,code2):
code1_ancestors=[]
code2_ancestors=[]
for i in t.search_nodes(name=code1)[0].get_ancestors():
code1_ancestors.append(i.name)
for i in t.search_nodes(name=code2)[0].get_ancestors():
code2_ancestors.append(i.name)
common_ancestors = []
for i in code1_ancestors:
for j in code2_ancestors:
if i==j:
common_ancestors.append(i)
print common_ancestors
####
for i in patients_list:
a= list (data.Code[data.PID==patients_list[i-1]])
#print a
for j in patients_list:
b= list (data.Code[data.PID==patients_list[j-1]])
for k in a:
for t in b:
fre (k,t)
However, an error is raising which is:
AttributeError Traceback (most recent call last)
<ipython-input-12-f9b47fcec010> in <module>()
38 for k in a:
39 for t in b:
---> 40 fre (k,t)
<ipython-input-12-f9b47fcec010> in fre(code1, code2)
12 code1_ancestors=[]
13 code2_ancestors=[]
---> 14 for i in t.search_nodes(name=code1)[0].get_ancestors():
15 code1_ancestors.append(i.name)
16 for i in t.search_nodes(name=code2)[0].get_ancestors():
AttributeError: 'str' object has no attribute 'search_nodes'
I've tried to manually pass all possible values to the function and it works! However, When I'm using the last section of the code, it raises the error.
You're changing your global variable 't' with your for loop.
If you print out its value before each call to your function, you will find that you have assigned it to a string at some point.

Categories

Resources