Getting ValueError:Input contains NaN, infinity or a value too large for dtype('float64') even when isnan and isinf are false and dtype=float64
My code is to analyze the PUBG dataset from kaggle and make a model. I have extracted all the features and Standardised them using StandardScaler from sklearn.
//Snippet
X=standardized_data
y=training_features_output
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.30,random_state=42)
print(standardized_data.shape,training_features_output.shape)
[Output]: (4446966, 16) (4446966,)
print(np.all(np.isinf(standardized_data)))
print(np.all(np.isinf(training_features_output)))
print(np.all(np.isnan(standardized_data)))
print(np.all(np.isnan(training_features_output)))
[Output]:
False
False
False
False
print(X.dtype)
print(y.dtype)
[Output]:
dtype('float64')
dtype('float64')
model=LinearRegression()
model.fit(X_train,y_train)
y_train_pred=model.predict(X_train)
y_test_pred=model.predict(X_test)
print('Train r2_accuracy:',r2_score(y_train,y_train_pred))
print('Test r2_accuracy:',r2_score(y_test,y_test_pred))
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
error photo
Full Code
From the above outputs we can see that they are no nan and infinite values in the dataset and also the data is in float64. but how am I getting this error and how to resolve it?
Tried other queries regarding this on stackoverflow all were having nan or something messed up and I dont know where is this code messing up.
1 answer

Your checking point is not correct because you are checking if
all
the data areinf
usingnp.all()
.print(np.all(np.isinf(standardized_data))) ...
Instead, use
np.any()
.Proof:
a = [np.inf, 0, 1] np.all(np.isinf(a)) #False np.any(np.isinf(a)) #True
See also questions close to this topic

how to display contents of text file one line at a time via timer using python on windows?
this is the code.
def wndProc(hWnd, message, wParam, lParam): if message == win32con.WM_PAINT: hdc, paintStruct = win32gui.BeginPaint(hWnd) dpiScale = win32ui.GetDeviceCaps(hdc, win32con.LOGPIXELSX) / 60.0 fontSize = 36 # http://msdn.microsoft.com/enus/library/windows/desktop/dd145037(v=vs.85).aspx lf = win32gui.LOGFONT() lf.lfFaceName = "Times New Roman" lf.lfHeight = int(round(dpiScale * fontSize)) #lf.lfWeight = 150 # Use nonantialiased to remove the white edges around the text. # lf.lfQuality = win32con.NONANTIALIASED_QUALITY hf = win32gui.CreateFontIndirect(lf) win32gui.SelectObject(hdc, hf) rect = win32gui.GetClientRect(hWnd) # http://msdn.microsoft.com/enus/library/windows/desktop/dd162498(v=vs.85).aspx win32gui.DrawText( hdc, **'Glory be to the Father, and to the son and to the Holy Spirit.',** 1, rect, win32con.DT_CENTER  win32con.DT_NOCLIP  win32con.DT_VCENTER ) win32gui.EndPaint(hWnd, paintStruct) return 0
.where it says the "glory be to the father.." prayer I would like that string to actually display a few different prayers on a timer. what I mean is I want to save short prayers to a text file and have the line where it says "glory be.." to change to a new prayer every 60 seconds cycling through a few prayers such as the serenity prayer etc.

How to plot the frequency of my data per day in an histogram?
I want to plot the number occurences of my data per day. y represent the id of my data. x represent the timestamp which I convert to time and day. But I can't make the correct plot. import matplotlib.pyplot as plt plt.style.use('ggplot') import time
y=['5914cce8fad645d1bec2e59e62823617', '1c2067e051734a1d8a75b18267ee4598', 'db6830fffa9c4aa5b71ef6da9333f357', '672cc9d5360e4451bb7c03e3d0bd8f0d', 'fb0f8122fffc47fea87ab2b749df173b', '558e96ca022240c7acc0e444f7663f53', 'c3f86fd5eac348d3a44cb325f30b6139', '21dd849f895f4cf5a16845a4c1a9fbf9', 'e3b4cd56e291467193b6d2226ee82ae7', '01346c48a8c443d1ac021efa33ca0f4e', '23b78b0f85be4ca799f41a5add76c12e', 'b1c036c00c2b4170a1708fd0add0dec2', '74737546e9c34126bcb24d34503421ca', '342991f5ec874c9d83eb9908f3e221aa', '4fdcd83aeb684e26b79b753c5e022a4e', 'b7fbeca9941643c49e909e71acc1eaba', '27c9d358a3ef4c69ba89eac16d8d3bdb', 'ef982c4ba11548a1aef12f672d7f1f00', 'efedede29bb44c5298b18b03070df3fd', 'eb03ae1b4cde409c8d342a16a8be30d2'] x=['1548143296750', '1548183033872', '1548346185194', '1548443373507', '1548446119319', '1548446239441', '1548446068267', '1548445962159', '1548446011209', '1548446259465', '1548446180380', '1548239985290', '1548240060367', '1548240045347', '1547627568993', '1548755333313', '1548673604016','1548673443843', '1548673503914', '1548673563975'] date=[] for i in x: print(i) print() i=i[:10] print(i) readable = time.ctime(int(i)) readable=readable[:10] date.append(readable) print(date) plt.hist(date,y) plt.show()

mysql.connector.errors.ProgrammingError: Error in SQL Syntax
I'm using the Python MySQL connector to add data to a table by updating the row. A user enters a serial number, and then the row with the serial number is added. I keep getting a SQL syntax error and I can't figure out what it is.
query = ("UPDATE `items` SET salesInfo = %s, shippingDate = %s, warrantyExpiration = %s, item = %s, WHERE serialNum = %s") cursor.execute(query, (info, shipDate, warranty, name, sn, )) conn.commit()
Error:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE serialNum = '1B0000021A974726'' at line 1
"1B0000021A974726" is a serial number inputted by the user and it is already present in the table.

How to merge pandas dataframe into existing reportlab table?
example_df = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]
I want to integrate example_df pandas data frame into an existing Reportlab table  where the number of rows is changing (could be 3 as shown in the example, or it could be 20):
rlab_table(['Mean','Max','Min','TestA','TestB'], ['','','','',''], ['','','','',''], ['','','','','']])
I have tried:
np.array(example_df).tolist()
but I get this error (AttributeError: 'int' object has no attribute 'wrapOn')
I am able to manually add each row into the report lab table by doing:
rlab_table(['Mean','Max','Min','TestA','TestB'], np.array(example_df).tolist()[0], np.array(example_df).tolist()[1], np.array(example_df).tolist()[2]])
However, the issue is that the number of rows in the dataframe is constantly changing, so I am seeking a solution similar to:
rlab_table(['Mean','Max','Min','TestA','TestB'], np.array(example_df).tolist()[0:X])] #Where X is the number of rows in the dataframe
 Pandas python Aggregation and Grouping  I want to show the sum on top of each different type

bins  Categorize column values using bins for ages
I have a .CSV file a snippet of which looks like this:
ID,SN, Age,Gender,Item ID,Item Name, Price 0,Lisim78, 20, Male, 108, Extraction Quickblade, 3.53 1,Lisovynya38, 40, Male, 143, Frenzied Scimitar, 1.56 2,Ithergue48, 24, Male, 92, Final Critic, 4.88 3,Chamassasya86, 24, Male, 100, Blindscythe, 3.27 4,Iskosia90, 23, Male, 131, Fury, 1.44 5,Yalae81, 22, Male, 81, Dreamkiss, 3.61 6,Itheria73, 36, Male, 169, Interrogator, 2.18 7,Iskjaskst81, 20, Male, 162, Abyssal Shard, 2.67 8,Undjask33, 22, Male, 21, Souleater, 1.1 9,Chanosian48, 35, Other, 136, Ghastly Adamantite, 3.58 10,Inguron55, 23, Male, 95, Singed Onyx Warscythe, 4.74
I need to establish bins for the 'Age' column which I have done like so:
bins = [0, 10, 15, 20, 25, 30, 35, 40, 45] names = ['<10', '1014', '1519', '2024', '2529', '3034', '3539', '40+'] df_bins = pd.cut(df['Age'], bins, labels=names)
How do I use the bins to categorize other columns like column 'SN'? I wanna be able to get a count of all players in 'SN' column who are <10, 1014, 1519 years... and so on.
Any help is greatly appreciated!

Word2vec compact models
Tell me if there are any w2v models that do not require a dictionary. So, everything that I found in torchtext first wants to know the dictionary build_vocab. But if I have a huge body of text, I would like to have a model that works at the level of phrases. But I did not find one.

supervised learning for parcours
for my school project i got to implement a neural network for a parcours. I know it useless but i want the neural net to learn a simple algorithm:
if front right is bigger than front left > go right, else > go left.
I wanna use supervised learning. I got 2 inputs neurons, 2 hidden neurons and 1 output neuron. The goal is that when the player has to go left the output gives a number under 0.5 and if the player has to go right the nn has to return a number greater that 0.5.
Somehow I made a mistake and the nn always tries to return 0.5. Do so know what i did wrong and what i can do now.
thats how the parcours looks like

Categorical Variables and too many NA for ML model
We have a data set of 250 variables and 50,000 records. One variable is numeric, 248 variables are categorical and one variable is binary (the target variable). Each category variable has more than 3000 levels. We have many NA. Each row is the record of diseases that a patient has suffered. That's why there are so many NAs. Because a patient may have suffered 100 diseases, and another has suffered only one. The objective is to be able to predict if patients can have a specific disease from the information of other diseases they have suffered. How can this data set be handled in machine learning?

ColumnTransformer with TfidfVectorizer produces "empty vocabulary" error
I am running a very simple experiment with
ColumnTransformer
with an intent to transform an array of columns, ["a"] in this example:from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.compose import ColumnTransformer dataset = pd.DataFrame({"a":["word gone wild","gone with wind"],"c":[1,2]}) tfidf = TfidfVectorizer(min_df=0) clmn = ColumnTransformer([("tfidf", tfidf, ["a"])],remainder="passthrough") clmn.fit_transform(dataset)
Which gives me:
ValueError: empty vocabulary; perhaps the documents only contain stop words
Obviously,
TfidfVectorizer
can dofit_transform()
on its own:tfidf.fit_transform(dataset.a) <2x5 sparse matrix of type '<class 'numpy.float64'>' with 6 stored elements in Compressed Sparse Row format>
What could be a reason for such an error and how to correct for it?

Scikit Learn  Excluding predictions
I would like to know if there's a mean already coded in scikitlearn that can incorporate excluding classes prior knowledge on standard sklearn classifiers.
Ex : I create a color classifier with 3 classes (red, green, blue). Works not so well and can mix blue and green. Now when I have more knowledge and I know that I get 3uples with exactly one of each (red, green, blue).
This are my results:
 true red, prediction vector (0.9, 0.05, 0.05) => predicts red
 true green, prediction vector (0.1, 0.7, 0.2) => predicts green
 true blue, prediction vector (0.1, 0.5, 0.4) => predicts green=> False prediction on blue, but if could take into account that green has a higher green score and that I’ve got prior knowledge, the prediction would be accurate.
Would you know how to do that ?
Thanks a bunch in advance !
Best, Arthur

ROC AUC value is 0
I have trained a binary classifier, but I think that my ROC curve is incorrect.
This is the vector that contains labels:
y_true= [0, 1, 1, 1, 0, 1, 0, 1, 0]
and the second vector is the score vector
y_score= [ 0.43031937, 0.09115553, 0.00650781, 0.02242869, 0.38608587, 0.09407699, 0.40521139, 0.08062053, 0.37445426 ]
When I plot my ROC curve, I get the following:
I think the code is correct, but I don't understand why I'm getting this curve and why the
tpr
,fpr
, andthreshold
lists are of length 4. Why is my AUC is equal to zero?fpr [0. 0.25 1. 1. ] tpr [0. 0. 0. 1.] thershold [1.43031937 0.43031937 0.37445426 0.00650781]
My Code:
import sklearn.metrics as metrics fpr, tpr, threshold = metrics.roc_curve(y_true, y_score) roc_auc = metrics.auc(fpr, tpr) # method I: plt import matplotlib.pyplot as plt plt.title('Receiver Operating Characteristic') plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc) plt.legend(loc = 'lower right') plt.plot([0, 1], [0, 1],'r') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.ylabel('True Positive Rate') plt.xlabel('False Positive Rate') plt.show()
complete code :
from keras.models import Model, Sequential from keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dense, Dropout, Activation from PIL import Image import numpy as np from keras.preprocessing.image import load_img, save_img, img_to_array from keras.applications.imagenet_utils import preprocess_input from keras.preprocessing import image import matplotlib.pyplot as plt # from sup5 import X_test, Y_test from sklearn.metrics import roc_curve, auc model = Sequential() ## 16 couches model.add(ZeroPadding2D((1, 1), input_shape=(256, 256, 3))) model.add(Convolution2D(64, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(Convolution2D(4096, (7, 7), activation='relu')) model.add(Dropout(0.5)) model.add(Convolution2D(4096, (1, 1), activation='relu')) model.add(Dropout(0.5)) model.add(Convolution2D(2622, (1, 1))) model.add(Flatten()) model.add(Activation('softmax')) # you can download the pretrained weights from the following link # https://drive.google.com/file/d/1CPSeum3HpopfomUEK1gybeuIVoeJT_Eo/view?usp=sharing # or you can find the detailed documentation https://sefiks.com/2018/08/06/deepfacerecognitionwithkeras/ from keras.models import model_from_json deep= model.load_weights('C:/Users/PC/PycharmProjects/untitled/vgg_face_weights.h5') vgg_face_descriptor = Model(inputs=model.layers[0].input , outputs=model.layers[2].output) # import pandas as pd # test_x_predictions = deep.predict(X_test) # mse = np.mean(np.power(X_test  test_x_predictions, 2), axis=1) # error_df = pd.DataFrame({'Reconstruction_error': mse, # 'True_class': Y_test}) # error_df.describe() def preprocess_image(image_path): img = load_img(image_path, target_size=(256, 256)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) return img def findCosineSimilarity(source_representation, test_representation): a = np.matmul(np.transpose(source_representation), test_representation) b = np.sum(np.multiply(source_representation, source_representation)) c = np.sum(np.multiply(test_representation, test_representation)) return 1  (a / (np.sqrt(b) * np.sqrt(c))) def findEuclideanDistance(source_representation, test_representation): euclidean_distance = source_representation  test_representation euclidean_distance = np.sum(np.multiply(euclidean_distance, euclidean_distance)) euclidean_distance = np.sqrt(euclidean_distance) return euclidean_distance vgg_face_descriptor = Model(inputs=model.layers[0].input, outputs=model.layers[2].output) epsilon = 0.1 import matplotlib.pyplot as plt import numpy as np # # def verifyFace(img1, img2): # img1_representation = vgg_face_descriptor.predict(preprocess_image(img1))[0, :] # img2_representation = vgg_face_descriptor.predict(preprocess_image(img2))[0, :] # # cosine_similarity = findCosineSimilarity(img1_representation, img2_representation) # euclidean_distance = findEuclideanDistance(img1_representation, img2_representation) # # print("Cosine similarity: ", cosine_similarity) # print("Euclidean distance: ", euclidean_distance) # # if (cosine_similarity < epsilon): # print("verified... they are same person") # return '1',euclidean_distance # else: # print("unverified! they are not same person!") # return '0',euclidean_distance def verifyFace(img1, img2, epsilon): img1_representation = vgg_face_descriptor.predict(preprocess_image(img1))[0, :] img2_representation = vgg_face_descriptor.predict(preprocess_image(img2))[0, :] cosine_similarity = findCosineSimilarity(img1_representation, img2_representation) euclidean_distance = findEuclideanDistance(img1_representation, img2_representation) print("Cosine similarity: ", cosine_similarity) print("Euclidean distance: ", euclidean_distance) if (cosine_similarity < epsilon): print("verified... they are same person") return '1', cosine_similarity else: print("unverified! they are not same person!") return '0', cosine_similarity f = plt.figure() f.add_subplot(1, 2, 1) # name = raw_input("who are you?") # print("hello %s" % (name,)) plt.imshow(image.load_img((img1))) plt.xticks([]); plt.yticks([]) f.add_subplot(1, 2, 2) plt.imshow(image.load_img((img2))) plt.xticks([]); plt.yticks([]) plt.show(block=True) print("") #17.63 i=1 arr=[] arrdist=[] for i in range(1,4): for j in range (1,10): ret_val, euclidean_distance = verifyFace(str(i)+"tst.jpg", str(j)+"train.jpg", epsilon) if ret_val == '0': a=0 print(euclidean_distance) arr.append([a]) arrdist.append([euclidean_distance]) elif ret_val =='1': b=1 arr.append([b]) arrdist.append([euclidean_distance]) arr = np.array(arr) arrdist = np.array(arrdist) print("cosine",euclidean_distance) print(arr) print(arrdist) y_score = arrdist[np.arange(27), 0] y_true = arr[np.arange(27), 0] print("y_score",y_score) print("y_true",y_true) # fpr, tpr, thresholds = roc_curve(y_true, y_score) # roc_auc = auc(fpr, tpr) import sklearn.metrics as metrics # calculate the fpr and tpr for all thresholds of the classification #affichage de y_true et y_score fpr, tpr, threshold = metrics.roc_curve(y_true, y_score,pos_label=0) print("fpr",fpr) print("tpr",tpr) print("thershold",threshold) roc_auc = metrics.auc(fpr, tpr) # method I: plt import matplotlib.pyplot as plt plt.title('Receiver Operating Characteristic') plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc) plt.legend(loc = 'lower right') plt.plot([0, 1], [0, 1],'r') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.ylabel('True Positive Rate') plt.xlabel('False Positive Rate') plt.show()

Plotting points with matplotlib over base map error
I am having trouble overlaying scatter points over my baseman projection of data. I have a gridded set of sea surface temperature data and am trying to plot scatter points over the gridded data but the scatter points are not showing up. I keep getting the error "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" from
pts = m.scatter(x,y, marker='o', s=5, c=ts, cmap='plasma',latlon=True)
the above line in my code. I think it is coming from the latlon=true . Any help would be appreciated! The datasets I am working with are in netCDF file format.
from mpl_toolkits.basemap import Basemap, shiftgrid, cm import numpy as np import matplotlib.pyplot as plt from netCDF4 import Dataset import matplotlib as mpl samosdata = Dataset("name of dataset", "r",format="NETCDF4") d = Dataset("name of datas set", "r",format="NETCDF4") sst = d.variables["analysed_sst"][:][0, ::1, :] lon = d.variables["lon"][:] lat = d.variables["lat"][::1] lats = samosdata.variables['lat'] lons = samosdata.variables['lon'] time = samosdata.variables['time'] ts = samosdata.variables['TS'] ts = np.array(ts) lons = np.array(lons) lats = np.array(lats) fig = plt.figure() ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) m = Basemap(projection='mill', llcrnrlat=80, urcrnrlat=80, llcrnrlon=180, urcrnrlon=180, lat_ts=20, resolution='c') nx = int((m.xmax  m.xmin)/11113.2); ny = int((m.ymax m.ymin)/11113.2) sst = m.transform_scalar(sst, lon, lat, nx, ny) im = m.imshow(sst, interpolation = "none") x, y =m(list(lons), list(lats)) pts = m.scatter(x,y, marker='o', s=5, c=ts, cmap='plasma',latlon=True) plt.colorbar(pts) m.drawcoastlines() parallels = np.arange(90, 90, 30) meridians = np.arange(180, 180, 60) m.drawparallels(parallels, labels = [1, 0, 0, 1]) m.drawmeridians(meridians, labels = [1, 0, 0, 1]) cb = m.colorbar(im, "right", size = "5%", pad = "2%") ax.set_title("SST 2010 01 01") plt.show()

How to fix 'ValueError: The truth value of an array with more than one element is ambiguous.' when comparing objects in dictionary?
I'm trying to check if a specific class object, here referred to as
new_state
, is present as a key in a dictionary. However, when I run the commandif new_state not in dictionary:
, I get the following error:ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
More specifically, I want to store a bunch of unique objects (called 'states') as keys in a dictionary (here initialized as
self.Q = dict()
), and to each key store a corresponding list as the dictionary value. The states are objects of theclass State:
, whose attributes are inputs of matrices, and are defined as:class State: """Defines the current state of the agent.""" def __init__(self, grid): self.grid = grid def __eq__(self, other): """Override the default Equals behaviour.""" if isinstance(other, self.__class__): return self.grid == other.grid return False def __ne__(self, other): """Override the default Unequal behaviour.""" return self.grid != other.grid
At some point in the program, I want to check if the object
new_state' is in the dictionary
self.Q, and if it isn't, then a list of random numbers is to be added to the dictionary with
new_state`as key:new_state = State(cur_env) if new_state not in self.Q: self.Q[new_state] = np.random.rand(len(ACTIONS)) # Add list of random numbers return new_state, a
And here is where the error occurs. What's going on here? I don't understand what the error means and I haven't been able to find i similar post that covers this. This code is part of a simple implementation of a Reinforcement Learning problem (I'll include the full code below). An agent is supposed to move from point A to B on a grid using a Qlearning algorithm, which is saved as a matrix in the agent's current state. My code is loosely based on the one found in this article https://medium.com/@curiousily/solvinganmdpwithqlearningfromscratchdeepreinforcementlearningforhackerspart145d1d360c120, however, they don't seem to run into this problem and I cannot see what the difference would be.
# Reinforcement learning import numpy as np import random as rnd from copy import deepcopy grid_size = 4 m_A = 0 # Start coordinate in matrix n_A = 0 # Start coordinate in matrix m_B = grid_size  1 # End coordinate n_B = grid_size  1 # End coordinate ACTIONS = ['Right', 'Left', 'Up', 'Down'] eps = 0.1 gamma = 0.7 alpha = 1 class State: """Defines the current state of the agent.""" def __init__(self, grid): self.grid = grid def __eq__(self, other): """Override the default Equals behaviour.""" if isinstance(other, self.__class__): return self.grid == other.grid return False def __ne__(self, other): """Override the default Unequal behaviour.""" return self.grid != other.grid def __hash__(self): return hash(str(self.grid)) terminal_grid = np.zeros((grid_size, grid_size), dtype = int) terminal_grid[m_B][n_B] terminal_state = State(terminal_grid) class Robot: """Implements agent. """ def __init__(self, row = m_A, col = n_A, cargo = False): self.m = row # Robot position in grid (row) self.n = col # Robot position in grid (col) self.carry = cargo # True if robot carries cargo, False if not self.Q = dict() self.Q[terminal_state] = [0, 0, 0, 0] def move_robot(self, state): """Moves the robot according to the given action.""" m = self.m # Current row n = self.n # Current col p = [] # Probability distribution for i in range(len(ACTIONS)): p.append(eps/4) if self.carry is False: # If the robot is moving from A to B Qmax = max(self.Q[state]) for i in range(len(p)): if self.Q[state][i] == Qmax: p[i] = 1  eps + eps/4 break # Use if number of episodes is large cur_env = deepcopy(state.grid) # cur_env = state.grid cur_env[m][n] = 0 action = choose_action(p) if action == 'Right': if n + 1 >= grid_size or cur_env[m][n+1] == 1: Rew = 5 # Reward 5 if we move into wall or another agent else: n += 1 Rew = 1 # Reward 1 otherwise a = 0 # Action number elif action == 'Left': if n  1 < 0 or cur_env[m][n1] == 1: Rew = 5 else: n = 1 Rew = 1 a = 1 elif action == 'Up': if m  1 < 0 or cur_env[m1][n] == 1: Rew = 5 else: m = 1 Rew = 1 a = 2 elif action == 'Down': if m + 1 >= grid_size or cur_env[m+1][n] == 1: Rew = 5 else: m += 1 Rew = 1 a = 3 m = m % grid_size n = n % grid_size self.m = m self.n = n cur_env[m][n] = 1 # print(cur_env) new_state = State(cur_env) if new_state not in self.Q: self.Q[new_state] = np.random.rand(len(ACTIONS)) # Add list of random numbers return new_state, a def choose_action(prob): # Given a probability distribution, chooses an action! """Defines policy to follow.""" action = np.random.choice(ACTIONS, p = prob) # Chooses an action at random return action def episode(robot): """Simulation of one episode.""" # Initialize E, S E = np.zeros((grid_size, grid_size), dtype = int) # Initializes the environment, E E[m_A][n_A] = 1 # Initializes position of robot S = State(E) # Initializes state of robot robot.Q[S] = np.random.rand(len(ACTIONS)) # print(S.grid) # print(robot.Q[S]) count = 0 while robot.carry is False: print('Carry == False') S_new, action_number = robot.move_robot(S) print('New state: ', S_new.grid) m_new = robot.m n_new = robot.n if m_new != m_B or n_new != n_B: R = 1 else: R = 5 robot.carry = True # Picks up cargo robot.Q[S][action_number] += alpha*(R + gamma*max(robot.Q[S_new])  robot.Q[S][action_number]) S = S_new # print(E) # print() count += 1 return count nepisodes = [] step_list = [] def simulation(): """Iterates through all episodes.""" r1 = Robot() for i in range(400): nsteps = episode(r1) nepisodes.append(i+1) step_list.append(nsteps) r1.m = m_A r1.n = n_A print("End of episode!") print(nsteps) simulation() plt.plot(nepisodes, step_list, '.') plt.show()
Any help would be greatly appreciated! Thanks in advance.

How to fix "The truth value of an array with more than one element is ambiguous" error when finding objects in dictionary?
I'm trying to implement a simple Reinforcement Learning algorithm. Basically, the agent is supposed to move from point A of a square grid to point B using Qlearning. I've gotten this to work previously using a simpler model, but now I need to refine it a bit. Basically, I want to store the Qvalues generated by the algorithm in a dictionary called (self.)Q, where each key is a state of the agent and each dictionary value is a list with Qvalues corresponding to that state. The states are objects of the class State, which has the grid matrix as attribute. However, when I want to check if a state (new_state) is already in the dictionary self.Q (see code below), I get the following error:
The truth value of an array with more than one element is ambiguous. Use >a.any() or a.all()
Why does this happen? I'm basing my code on this article https://medium.com/@curiousily/solvinganmdpwithqlearningfromscratchdeepreinforcementlearningforhackerspart145d1d360c120, in which they don't seem to run into this problem. If think this has something to do with the fact that the states are separate obejcts, but I do not know how to solve this.
import numpy as np import random as rnd from copy import deepcopy grid_size = 4 m_A = 0 # Start coordinate in matrix n_A = 0 # Start coordinate in matrix m_B = grid_size  1 # End coordinate n_B = grid_size  1 # End coordinate ACTIONS = ['Right', 'Left', 'Up', 'Down'] eps = 0.1 gamma = 0.7 alpha = 1 class State: """Defines the current state of the agent.""" def __init__(self, grid): self.grid = grid def __eq__(self, other): return isinstance(other, State) and self.grid == other.grid def __hash__(self): return hash(str(self.grid)) terminal_grid = np.zeros((grid_size, grid_size)) terminal_grid[m_B][n_B] terminal_state = State(terminal_grid) class Robot: """Implements agent. """ def __init__(self, row = m_A, col = n_A, cargo = False): self.m = row # Robot position in grid (row) self.n = col # Robot position in grid (col) self.carry = cargo # True if robot carries cargo, False if not self.Q = dict() self.Q[terminal_state] = [0, 0, 0, 0] def move_robot(self, state): """Moves the robot according to the given action.""" m = self.m # Current row n = self.n # Current col p = [] # Probability distribution for i in range(len(ACTIONS)): p.append(eps/4) if self.carry is False: # If the robot is moving from A to B Qmax = max(self.Q[state]) for i in range(len(p)): if self.Q[state][i] == Qmax: p[i] = 1  eps + eps/4 break # Use if number of episodes is large cur_env = deepcopy(state.grid) # cur_env = state.grid cur_env[m][n] = 0 action = choose_action(p) if action == 'Right': if n + 1 >= grid_size or cur_env[m][n+1] == 1: Rew = 5 # Reward 5 if we move into wall or another agent else: n += 1 Rew = 1 # Reward 1 otherwise a = 0 # Action number elif action == 'Left': if n  1 < 0 or cur_env[m][n1] == 1: Rew = 5 else: n = 1 Rew = 1 a = 1 elif action == 'Up': if m  1 < 0 or cur_env[m1][n] == 1: Rew = 5 else: m = 1 Rew = 1 a = 2 elif action == 'Down': if m + 1 >= grid_size or cur_env[m+1][n] == 1: Rew = 5 else: m += 1 Rew = 1 a = 3 m = m % grid_size n = n % grid_size self.m = m self.n = n cur_env[m][n] = 1 # print(cur_env) new_state = State(cur_env) if new_state not in self.Q: # Cheack if state is in dictionary self.Q[new_state] = np.random.rand(len(ACTIONS)) return new_state, a def choose_action(prob): """Defines policy to follow.""" action = np.random.choice(ACTIONS, p = prob) return action def episode(robot): """Simulation of one episode.""" # Initialize E, S E = np.zeros((grid_size, grid_size), dtype = int) E[m_A][n_A] = 1 # Initializes position of robot S = State(E) # Initializes state of robot robot.Q[S] = np.random.rand(len(ACTIONS)) count = 0 while robot.carry is False: S_new, action_number = robot.move_robot(S) m_new = robot.m n_new = robot.n if m_new != m_B or n_new != n_B: R = 1 else: R = 5 robot.carry = True # Picks up cargo robot.Q[S][action_number] += alpha*(R + gamma*max(robot.Q[S_new]) robot.Q[S][action_number]) S = S_new # print(E) # print() count += 1 return count nepisodes = [] step_list = [] def simulation(): """Iterates through all episodes.""" r1 = Robot() for i in range(400): nsteps = episode(r1) nepisodes.append(i+1) step_list.append(nsteps) r1.m = m_A r1.n = n_A print("End of episode!") print(nsteps) simulation()