Home > Software design >  AttributeError: 'NoneType' object has no attribute 'get', and the knn step is ri
AttributeError: 'NoneType' object has no attribute 'get', and the knn step is ri

Time:05-22

I have a problem with the get() method its not being recognized. I have not found a solution. I want to take the input from the GUI and apply it to the knn algorithm


from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.neighbors import KNeighborsClassifier

from tkinter import * 
top = Tk('300','300')
box= Text(top).grid(row=1,column=1)
p = TfidfVectorizer(sublinear_tf=True, stop_words='english')
p.fit(box.get("1.0",END))
wordOfp = p.transform(p)

x_train,x_test,y_train,y_test = train_test_split(wordOfp,y,random_state = 42, test_size = 0.2)# y is target
model = OneVsRestClassifier(KNeighborsClassifier(n_neighbors=5, metric= 'euclidean'  ))
model.fit(x_train,y_train)
prediction = model.predict(x_test)

this is a error p.fit(box.get("1.0",END))

AttributeError: 'NoneType' object has no attribute 'get'

CodePudding user response:

You need to split box onto two lines:

box= Text(top)
box.grid(row=1,column=1)
p.fit(box.get("1.0",END))

You cannot use the grid function on the same line as your entrypoint otherwise it will return None. This answer has a better explanation if needed:

Tkinter: AttributeError: NoneType object has no attribute <attribute name>

Also you will get an error because END is not defined.

CodePudding user response:

You need to separate Text declaration and grid in order it to work. try this.

box= Text(top)
box.grid(row=1,column=1)
  • Related