Python unhashable type: numpy.ndarray

2024/10/18 13:04:10

I worked on making functions for K Nearest Neighbors. I have tested each function separately and they all work well. However whenever I put them together and run KNN_method, it shows unhashable type: 'numpy.ndarray'. Here is my code:

def distance(p,point):import numpy as npvalue = np.sqrt(sum(np.power((p-point),2)))return(value)def find_neighbors(p,list_of_points, k = 3):import numpy as npdistances = np.zeros(list_of_points.shape[0])for i in range(list_of_points.shape[0]):distances[i]= distance(p,list_of_points[i])ind = np.argsort(distances)return(ind[0:k])def majority_votes(votes):import randomvote_result = {}for key in votes:if key in vote_result:vote_result[key] += 1else:vote_result[key] = 1final_list = []for (number, vote) in vote_result.items():if vote == max(vote_result.values()):final_list.append(number)Winner = random.choice(final_list)return(Winner)def KNN_method(p , list_of_points , outcomes , k = 3):ind = find_neighbors(p , list_of_points , k) Final = majority_votes(outcomes[ind])return(Final)
Answer

Convert to tuple first.

hash(tuple(np.array([1,2,3,4])))
https://en.xdnf.cn/q/72718.html

Related Q&A

Efficient way to generate Lime explanations for full dataset

Am working on a binary classification problem with 1000 rows and 15 features. Currently am using Lime to explain the predictions of each instance. I use the below code to generate explanations for full…

how to handle javascript alerts in selenium using python

So I there is this button I want to click and if its the first time youve clicked it. A javascript alert popup will appear. Ive been using firebug and just cant find where that javascript is located an…

testing.postgresql command not found: initdb inside docker

Hi im trying to make a unittest with postgresql database that use sqlalchemy and alembicAlso im running it on docker postgresqlIm following the docs of testing.postgresql(docs) to set up a temporary po…

Recommended approach for loading CouchDB design documents in Python?

Im very new to couch, but Im trying to use it on a new Python project, and Id like to use python to write the design documents (views), also. Ive already configured Couch to use the couchpy view server…

Error when import matplotlib.pyplot as plt

I did not have any problem to use "plt", but it suddenly shows an error message and does not work, when I import it. Please see the below. >>> import matplotlib >>> import m…

Python NtQueryDirectoryFile (File information structure)

Ive written a simple (test) script to list files in a selected directory. Not using FindFirstFile; only native API. When I execute the script and watch, Win32API monitor tells me STATUS_SUCCESS. My Fil…

returning A DNS record in dnspython

I am using dnspython to get the A record and return the result (IP address for a given domain).I have this simple testing python script:import dns.resolverdef resolveDNS():domain = "google.com&quo…

IO completion port key confusion

Im writing an IO completion port based server (source code here) using the Windows DLL API in Python using the ctypes module. But this is a pretty direct usage of the API and this question is directed…

PySpark reversing StringIndexer in nested array

Im using PySpark to do collaborative filtering using ALS. My original user and item ids are strings, so I used StringIndexer to convert them to numeric indices (PySparks ALS model obliges us to do so).…

Numba np.convolve really slow

Im trying to speed up a piece of code convolving a 1D array (filter) over each column of a 2D array. Somehow, when I run it with numbas njit, I get a 7x slow down. My thoughts:Maybe column indexing is …