np.array question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: np.array question (/thread-26037.html) |
np.array question - hissonrr - Apr-19-2020 Morning all, I am a python beginner and was playing around. I ran into a roadblock when the original np.array was set up to use a 9 varriable array like below... adjacent_facies = np.array([[1], [0,2], [1], [4], [3,5], [4,6,7], [5,7], [5,6,8], [6,7]])and I need to convert it for a 6 variable breakdown and get an index error... IndexError: index 6 is out of bounds for axis 0 with size 6 Any help would be appreciated as I do not really understand the np.array functionality fully yet. Thanks, -RH RE: np.array question - buran - Apr-19-2020 you don't show your code (always do, in python tags) indexes are 0 based, so for array of size 6 max index is 5 RE: np.array question - hissonrr - Apr-19-2020 (Apr-19-2020, 01:51 PM)buran Wrote: you don't show your code (always do, in python tags) Apologies here is the code # define our accuracy def accuracy(conf): total_correct = 0. nb_classes = conf.shape[0] for i in np.arange(0,nb_classes): total_correct += conf[i][i] acc = total_correct/sum(sum(conf)) return acc # Define error within 'adjacent facies' #This needs to be updated for 6 facies model ,np.array -RH adjacent_facies = np.array([[1], [0,2], [1], [4], [3,5], [4,6,7], [5,7], [5,6,8], [6,7]]) def accuracy_adjacent(conf, adjacent_facies): nb_classes = conf.shape[0] total_correct = 0. for i in np.arange(0,nb_classes): total_correct += conf[i][i] for j in adjacent_facies[i]: total_correct += conf[i][j] return total_correct / sum(sum(conf)) #display accuracy print('Facies classification accuracy = %f' % accuracy(conf)) print('Adjacent facies classification accuracy = %f' % accuracy_adjacent(conf, adjacent_facies)) RE: np.array question - hissonrr - Apr-20-2020 Solved after some research, here is what I got working. adjacent_facies = np.array([[1], [0,3], [1], [0,3], [1], [1], [1], [5], [3]]) |