0
$\begingroup$
#creation of data
x,y=make_classification(n_samples=10000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=60)
#splitting of data
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,stratify=y,random_state=42)
train_mean_scores=[]
cv_mean_scores=[]
#function to get optimum k or hyper_parameter
def RandomCVSearch(x_train,y_train,classifier,params,folds):
    for k in params:
        train_fold_score=[]
        cv_fold_score=[]
        #code to assign groups 
        numbers=[]
        for i in range(0,len(x_train)):
            numbers.append(i)
        indices=tuple(numbers)
        split_indices=np.array_split(indices,folds)
        indices_parts=list(map(tuple,split_indices))
        for cv_idx in indices_parts:
            cv_x=x_train[cv_idx]
            cv_y=y_train[cv_idx]
            train_idx= list(set(list(range(1, len(x_train)))) - set(cv_idx))
            x_tr=x_train[train_idx]
            y_tr=y_train[train_idx]
            classifier=KNeighborsClassifier()
            classifier.fit(x_tr,y_tr)
            
            #accuracy score of train data
            y_tr_pred=classifier.predict(x_tr)
            train_fold_score.append(accuracy_score(y_train,y_tr_pred))
            
            #accuracy score of cv data
            y_cv_pred=classifier.predict(cv_x)
            cv_fold_score.append(accuracy_score(cv_y,y_cv_pred))
            
        train_mean_scores.append(np.mean(np.array(train_fold_score)))
        cv_mean_scores.append(np.mean(np.array(cv_fold_score)))
    return train_mean_scores,cv_mean_scores
        
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import random
import warnings
warnings.filterwarnings("ignore")


classifier = KNeighborsClassifier()


params =[i for i in range(1,40)]
folds = 3

train_scores,cv_scores = RandomCVSearch(x_train, y_train, classifier, params, folds)
 

plt.plot(params,train_scores, label='train cruve')
plt.plot(params,cv_scores, label='cv cruve')
plt.title('Hyper-parameter VS accuracy plot')
plt.legend()
plt.show()

OUTPUT:

IndexError                                Traceback (most recent call last)
<ipython-input-21-1fafbc17cd57> in <module>
     13 folds = 3
     14 
---> 15 train_scores,cv_scores = RandomCVSearch(x_train, y_train, classifier, params, folds)
     16 
     17 

<ipython-input-20-17e5d5e8d7bb> in RandomCVSearch(x_train, y_train, classifier, params, folds)
     18         indices_parts=list(map(tuple,split_indices))
     19         for cv_idx in indices_parts:
---> 20             cv_x=x_train[cv_idx]
     21             cv_y=y_train[cv_idx]
     22             train_idx= list(set(list(range(1, len(x_train)))) - set(cv_idx))

IndexError: too many indices for array
$\endgroup$
1
  • 1
    $\begingroup$ X is a multidimensional array so try: cv_x=x_train[cv_idx,:] $\endgroup$ – Julio Jesus Feb 2 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.