def performance_for_features(feat_idxs): # train classifier knn = KNeighborsClassifier(n_neighbors=3) knn.fit(f_train_norm[:, feat_idxs], c_train) # predict and evaluate performance prediction = knn.predict(f_test_norm[:, feat_idxs]) return performance(prediction, c_test)
X_train = X_train[:, s_clean] # Paso 3-Training: Normalizacion # > Training: 211 x 387 X_train, a, b = normalize(X_train) # Paso 4-Training: SFS # > Training: 211 x 40 s_sfs = sfs(X_train, d_train, n_features=40, method="fisher", show=True) X_train = X_train[:, s_sfs] # Paso 5-Training: PCA # > Training: 211 x 10 X_train, _, A, Xm, _ = pca(X_train, n_components=10) # *** DEFINCION DE DATOS PARA EL TESTING *** X_test = X_test[:, s_clean] # Paso 2: clean X_test = X_test * a + b # Paso 3: normalizacion X_test = X_test[:, s_sfs] # Paso 4: SFS X_test = np.matmul(X_test - Xm, A) # Paso 5: PCA # *** ENTRENAMIENTO CON DATOS DE TRAINING Y PRUEBA CON DATOS DE TESTING *** knn = KNN(n_neighbors=1) knn.fit(X_train, d_train) Y_pred = knn.predict(X_test) accuracy = performance(Y_pred, d_test) print("Accuracy = " + str(accuracy))
# Leemos los datos. mat1 = loadmat('DATOS1.mat') mat2 = loadmat('DATOS2.mat') # Cantidad de features. N_FEATURES = 15 # Obtenemos X,Y X, Y = mat2["X"], mat2["Y"].squeeze() # Selección de features selected_feats = sfs(X, Y, n_features=N_FEATURES, method="fisher", show=False) # Separamos los datos de entrenamiento y testing. Xtrain, Ytrain = mat1["Xtrain"], mat1["Ytrain"].squeeze() Xtest, Ytest = mat1["Xtest"], mat1["Ytest"].squeeze() # Se definen Xtrain_new,Xtest_new con las features seleccionadas. Xtrain_new, Xtest_new = Xtrain[:, selected_feats], Xtest[:, selected_feats] # Se define el modelo y se entrena. knn = KNeighborsClassifier(n_neighbors=1) knn.fit(Xtrain_new, Ytrain) # Se obtiene la predicción Ypred_new = knn.predict(Xtest_new) # Computamos el accuracy. result = performance(Ypred_new, Ytest) print(result)
(c) Solución proporcionada por: Germán Leandro Contreras Sagredo, Daniel Vives Santiago de Chile, 04 de Abril de 2019 Universidad Católica de Chile ''' # Librerías a utilizar from scipy.io import loadmat # Lectura de archivos. from sklearn.neighbors import KNeighborsClassifier # Modelo KNN from pybalu.performance_eval import performance # Cómputo del accuracy. # Leemos los datos. mat = loadmat('DATOS1.mat') # Separamos los datos de entrenamiento y testing. Xtrain, Ytrain = mat["Xtrain"], mat["Ytrain"].squeeze() Xtest, Ytest = mat["Xtest"], mat["Ytest"].squeeze() # Se define el modelo y se entrena. knn = KNeighborsClassifier(n_neighbors=1) knn.fit(Xtrain, Ytrain) # Se obtiene la predicción Ypred = knn.predict(Xtest) # Computamos el accuracy. result = performance(Ypred, Ytest) print(result)