Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

searching for best k values in knn

# Create neighbors
neighbors = np.arange(1, 13)
train_accuracies = {}
test_accuracies = {}

for neighbor in neighbors:
  
  	# Set up a KNN Classifier
  	knn = KNeighborsClassifier(n_neighbors=neighbor)
  
  	# Fit the model
  	knn.fit(X_train, y_train)
  
  	# Compute accuracy
  	train_accuracies[neighbor] = knn.score(X_train, y_train)
  	test_accuracies[neighbor] = knn.score(X_test, y_test)
print(neighbors, '
', train_accuracies, '
', test_accuracies)
# Add a title
plt.title("KNN: Varying Number of Neighbors")

# Plot training accuracies
plt.plot(neighbors, train_accuracies.values(), label="Training Accuracy")

# Plot test accuracies
plt.plot(neighbors, test_accuracies.values(), label="Testing Accuracy")

plt.legend()
plt.xlabel("Number of Neighbors")
plt.ylabel("Accuracy")

# Display the plot
plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: pandas unstring list 
Python :: delete list using slicing 
Python :: python genap ganjil 
Python :: how to set a hyperlink in python 
Python :: python send email from icloud 
Python :: python derivative of mean squared error 
Python :: pascal triangle 
Python :: python docstrings example 
Python :: gui def python 
Python :: enum python print all options 
Python :: 151 - Power Crisis solution 
Python :: Merge 2 or more notebooks into one 
Python :: convert pdf to word doc in python 
Python :: get all subarrays of an array python 
Python :: install pytorch on nvidia jetson nx 
Python :: compiling python code 
Python :: add python to zsh wsl 
Python :: input a number and print even numbers up to that number in python 
Python :: python json nan 
Python :: make array consecutive 2 python 
Python :: docker run python 
Python :: Display shape of the DataFrame 
Python :: python dict add item 
Python :: recall at k calculate python 
Python :: index.py:14: RuntimeWarning: invalid value encountered in true_divide return np.dot(user, user2) / (norm(user) * norm(user2)) 
Python :: print in pytest python 
Python :: dash authentication 
Python :: how to add string in csv in python 
Python :: seaborn documentation x axis range 
Python :: .add_prefix to certain columns python 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =