Python3 Learning API Usage
Principal component analysis method for reducing dimension
Using the data set on the network, I have downloaded to the local, can go to my git reference
Git:https://github.com/linyi0604/machinelearning
Code:
1 fromSklearn.svmImportlinearsvc2 fromSklearn.metricsImportClassification_report3 fromSklearn.decompositionImportPCA4 ImportPandas as PD5 ImportNumPy as NP6 " "7 principal component analysis:8 feature to reduce the dimensions of the method. 9 extracting major feature components, with associated features, for operational combinationsTen Discard non-significant feature components while potentially losing meaningful features One implementing reduced feature dimensions A API Usage: - Estimator = PCA (n_components=20) - Pca_x_train = Estimator.fit_transform (x_train) the pca_x_test = Estimator.transform (x_test) - - data re-prediction before and after dimensionality reduction using support vector machine - + The data set originates from the online https://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/ - I downloaded him locally. + Training Sample 3,823, test sample 1797 A the image is represented by a total of 64 dimensions through the 8*8 pixel matrix, and 1 target dimensions represent the number category at - " " - - #1 Preparing Data -Digits_train = Pd.read_csv ("./data/optdigits/optdigits.tra", header=None) -Digits_test = Pd.read_csv ("./data/optdigits/optdigits.tes", header=None) in #extracting 64-dimensional pixel features and 1-dimensional targets from a sample -X_train = Digits_train[np.arange (64)] toY_train = digits_train[64] +X_test = Digits_test[np.arange (64)] -Y_test = digits_test[64] the * #2 dimensionality reduction of image data, 64 dimensions reduced to 20 dimensions $Estimator = PCA (n_components=20)Panax NotoginsengPca_x_train =estimator.fit_transform (X_train) -Pca_x_test =estimator.transform (x_test) the + #3.1 Learning and predicting non-dimensionality data using the default configured support vector machine ASVC =linearsvc () the #Learning + Svc.fit (X_train, Y_train) - #Forecast $Y_predict =svc.predict (x_test) $ - #3.2 Using the default configured support vector machine to learn and predict the data after dimensionality reduction -Pca_svc =linearsvc () the #Learning - Pca_svc.fit (Pca_x_train, Y_train)WuyiPca_y_predict =pca_svc.predict (pca_x_test) the - #4 Model Evaluation Wu Print("accuracy of raw data:", Svc.score (X_test, y_test)) - Print("other ratings: \ n", Classification_report (Y_test, Y_predict, Target_names=np.arange (10). Astype (str ))) About $ Print("data accuracy rate after dimensionality reduction:", Pca_svc.score (Pca_x_test, y_test)) - Print("other ratings: \ n", Classification_report (Y_test, Pca_y_predict, Target_names=np.arange (10). Astype (str ))) - - " " A accuracy of raw data: 0.9165275459098498 + Other ratings: the Precision recall F1-score support - $ 0 0.98 0.98 0.98 178 the 1 0.73 0.99 0.84 182 the 2 0.98 0.97 0.98 177 the 3 0.96 0.88 0.92 183 the 4 0.94 0.95 0.95 181 - 5 0.91 0.96 0.93 182 in 6 0.99 0.96 0.98 181 the 7 0.98 0.92 0.95 179 the 8 0.84 0.79 0.81 174 About 9 0.94 0.76 0.84 the the avg/total 0.92 0.92 0.92 1797 the + data accuracy rate after dimensionality reduction: 0.9220923761825265 - Other ratings: the Precision recall F1-score supportBayi the 0 0.97 0.97 0.97 178 the 1 0.93 0.86 0.89 182 - 2 0.96 0.97 0.96 177 - 3 0.93 0.87 0.90 183 the 4 0.94 0.97 0.96 181 the 5 0.86 0.96 0.91 182 the 6 0.97 0.98 0.98 181 the 7 0.97 0.88 0.92 179 - 8 0.89 0.89 0.89 174 the 9 0.82 0.88 0.85 the the avg/total 0.92 0.92 0.92 179794 " "
The path of machine learning: The main component analysis of the Python feature reduced dimension PCA