Memuat
Memuat
k-Nearest Neighbors for Psychology
0 views
Classify test anxiety level with k-Nearest Neighbors based on test score and study hours and choose the best k. k-Nearest Neighbors classifies a point by the majority class among its k nearest neighbours. It suits as a simple baseline for small to medium data with irregular class boundaries.

Classify test anxiety level with k-Nearest Neighbors based on test score and study hours and choose the best k.
| Role | Variable | Unit |
|---|---|---|
| Main | Test anxiety level | - |
| Supporting 1 | Test score | skor |
| Supporting 2 | Study hours | jam/minggu |
The k-NN model classifies Test anxiety level (low/high) from Test score and Study hours with test accuracy 0.939, F1 0.941 and AUC 0.974. Best k = 3.
Computed from illustrative synthetic data, not from a real study.
k-Nearest Neighbors classifies a point by the majority class among its k nearest neighbours. It suits as a simple baseline for small to medium data with irregular class boundaries.
Report the chosen k with the reason for choosing it, and the test metrics. Note that prediction is slow on large data because the whole training set is stored.
A short starting script with this project's variable names. The delivered version is adapted to your data and fully commented.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.metrics import accuracy_score, f1_score
rng = np.random.default_rng(51)
n = 300
test_score = np.r_[rng.normal(0, 1, n // 2), rng.normal(2, 1, n // 2)]
study_hours = np.r_[rng.normal(0, 1, n // 2), rng.normal(2, 1, n // 2)]
test_anxiety_level = np.r_[np.zeros(n // 2), np.ones(n // 2)].astype(int)
X = np.column_stack([test_score, study_hours])
X_tr, X_te, y_tr, y_te = train_test_split(X, test_anxiety_level, test_size=0.25, stratify=test_anxiety_level, random_state=51)
ks = range(1, 26, 2)
cv = [cross_val_score(make_pipeline(StandardScaler(), KNeighborsClassifier(k)), X_tr, y_tr, cv=5).mean()
for k in ks]
best_k = list(ks)[int(np.argmax(cv))]
clf = make_pipeline(StandardScaler(), KNeighborsClassifier(best_k)).fit(X_tr, y_tr)
pred = clf.predict(X_te)
print("Best k:", best_k)
print("Accuracy:", round(accuracy_score(y_te, pred), 3), "F1:", round(f1_score(y_te, pred), 3))
plt.plot(list(ks), cv, marker="o")
plt.xlabel("k"); plt.ylabel("CV accuracy")
plt.show()Choose how much help you need. Each higher package includes everything in the lower ones. Prices exclude 11% VAT.
Package 1
US$19
excl. 11% VAT
Duration: 3 working days
Output: Python files + result figures
Deliverables
Package 2 · most chosen
US$50
excl. 11% VAT
Duration: 7 working days
Output: Code + analysis + interpretation notes
Includes all of Package 1, plus
Package 3
US$113
excl. 11% VAT
Duration: 12-15 working days
Output: Code + analysis + guidance until ready to present
Includes all of Packages 1 & 2, plus
Yes. k-Nearest Neighbors is commonly used at this level in Psychology. We adapt the analysis to your research question, your own data and your supervisor's requirements.
No. The sheet is computed from illustrative synthetic data so you can see the kind of output Python gives. For your order we run the same method on your own data.
That is the point of the packages. Every script is commented, and Package 3 includes two coaching sessions plus practice questions so you can explain the method and the results at your seminar and defence.
Each project has three packages: Package 1 (code and explanation) US$19, Package 2 (your data analysis and interpretation) US$50 and Package 3 (full guidance) US$113, excluding 11% VAT. Larger datasets, extra methods or tight deadlines adjust the quote after a short consultation.
The result sheet uses illustrative synthetic data. Python is a trademark of the Python Software Foundation.
Back to Python portfolio