How do I create an array or dataframe to store seedN, clf.score(X_test, y_test)
,n_neighbors
?
JavaScript
x
17
17
1
from sklearn.model_selection import train_test_split
2
3
for seedN in range(1,50,1):
4
X_train, X_test, y_train, y_test = train_test_split(indicators,data2['target'],
5
test_size=0.25, random_state=seedN)
6
7
training_accuracy = []
8
test_accuracy = []
9
10
neighbors_settings = range(1, 70) # try n_neighbors from 1 to 50
11
12
for n_neighbors in neighbors_settings:
13
clf = KNeighborsClassifier(n_neighbors=n_neighbors) # build the model
14
clf.fit(X_train, y_train)
15
training_accuracy.append(clf.score(X_train, y_train)) # record training set accuracy
16
test_accuracy.append(clf.score(X_test, y_test)) # record generalization accuracy
17
Advertisement
Answer
Create a temporary empty list to store the results :
JavaScript
1
2
1
tmp = []
2
For each fit, add a new list with the desired values :
JavaScript
1
6
1
for seedN in range(1, 50, 1):
2
# your code
3
for n_neighbors in neighbors_settings:
4
# your code
5
tmp.append([seedN, clf.score(X_test, y_test), n_neighbors])
6
Finally, create the dataframe with this temporary list :
JavaScript
1
2
1
df = pd.DataFrame(tmp, columns=["seedN", "score", "n_neighbors"])
2