import sys sys.path.append("E:/utils") import classification_utils as cutils from sklearn import preprocessing import numpy as np X, y = cutils.generate_nonlinear_synthetic_data_classification2(n_samples=1000, noise=0.1) X, y = cutils.generate_nonlinear_synthetic_data_classification3(n_samples=1000, noise=0.1) cutils.plot_data_2d_classification(X, y) #guassian basis transformation tmp = np.exp(-(X**2).sum(1)) X_3d = np.c_[X, tmp] cutils.plot_data_3d_classification( X_3d, y, new_window=True, title="Linearly separable data in 3D with basis change") #polynomial basis transformation poly_features = preprocessing.PolynomialFeatures() X_poly1 = poly_features.fit_transform(X) poly_features = preprocessing.PolynomialFeatures(degree=3) X_poly2 = poly_features.fit_transform(X)
import sys path = 'J://utils' sys.path.append(path) from sklearn import cluster, manifold import common_utils as utils import clustering_utils as cl_utils import classification_utils as cutils X, _ = cutils.generate_nonlinear_synthetic_data_classification2(n_samples=300) utils.plot_data_2d(X) X, _ = cutils.generate_nonlinear_synthetic_data_classification3(n_samples=300) utils.plot_data_2d(X) tsne = manifold.TSNE() X_tsne = tsne.fit_transform(X) utils.plot_data_2d(X_tsne) scoring = 's_score' kmeans_estimator = cluster.KMeans() kmeans_grid = {'n_clusters': list(range(2, 7))} kmeans_final_model = cl_utils.grid_search_best_model_clustering( kmeans_estimator, kmeans_grid, X, scoring=scoring) print(kmeans_final_model.labels_) print(kmeans_final_model.cluster_centers_) cl_utils.plot_model_2d_clustering(kmeans_final_model, X)
import sys sys.path.append("E:/") import classification_utils as cutils from sklearn import model_selection, neighbors, metrics import numpy as np X_train, y_train = cutils.generate_nonlinear_synthetic_data_classification3( n_samples=500, noise=0.25) cutils.plot_data_2d_classification(X_train, y_train) #underfitted learning in knn knn_estimator = neighbors.KNeighborsClassifier(n_neighbors=400) knn_estimator.fit(X_train, y_train) cv_scores = model_selection.cross_val_score(knn_estimator, X_train, y_train, cv=10) print(np.mean(cv_scores)) train_score = knn_estimator.score(X_train, y_train) print(train_score) cutils.plot_model_2d_classification(knn_estimator, X_train, y_train) #underfitted learning in knn knn_estimator = neighbors.KNeighborsClassifier(n_neighbors=1) knn_estimator.fit(X_train, y_train) cv_scores = model_selection.cross_val_score(knn_estimator, X_train, y_train, cv=10) print(np.mean(cv_scores))