Original: http://www.cnblogs.com/leonwen/p/5158947.html The algorithm is ported by MATLAB (see the previous blog post for details). But the final output is inconsistent with MATLAB, it is found that in the invocation of the internal function Eig to solve eigenvalues and eigenvectors, both eigenvalues are consistent, but the eigenvectors are different.But, theoretically, it makes sense, because eigenvectors are inherently non-unique. The most puzzling
follows:>>> A = np. ones (2, 2 ))>>> B = np. eye (2)>>> Print np. vstack (a, B ))[[1. 1.][1. 1.][1. 0.][0. 1.]>>> Print np. hstack (a, B ))[[1. 1. 1. 0.][1. 1. 0. 1.]
Check whether the two functions involve the shortest copy problem:Copy codeThe Code is as follows:>>> C = np. hstack (a, B ))>>> Print c[[1. 1. 1. 0.][1. 1. 0. 1.]>>> A [1, 1] = 5>>> B [1, 1] = 5>>> Print c[[1. 1. 1. 0.][1. 1. 0. 1.]We can see that the change of elements in a and B does not affect c.
Deep copy Array
The array obje
matrix matrices, and the column represents the feature, where the percentage represents the variance ratio of the number of features required before taking the default to 0.9" "defPCA (datamat,percentage=0.9): #averaging for each column, because the mean value is subtracted from the calculation of the covarianceMeanvals=mean (datamat,axis=0) meanremoved=datamat-meanvals#CoV () Calculating varianceCovmat=cov (meanremoved,rowvar=0)#using the Eig ()
the inner area width of the first matched element (including padding, excluding borders).Here is an example:CSS code block style type="Text/css"> . Div{ width: px ; Height: + px ; background-color: Green ; padding: tenpx ; Border: px solid #009999 ; margin: px ;} style>jquery code block $(function(){ console.log($(‘div‘).width()+‘+‘+$(‘div‘).innerWidth()+‘+‘+$(‘div‘).outerWidth()); var k = $(‘div‘).height()+‘+‘+ $(‘div‘).innerHeight()+‘+‘+$(‘div‘).outerHeight()+
#define NSLOG (FORMAT, ...) fprintf (stderr, "%s\n", [[NSString Stringwithformat:format, # #__VA_ARGS__] utf8string]);#import int main (int argc, const char * argv[]) {@autoreleasepool {Variable groups inherit immutable groups1, create. Set the number of elements to createNsmutablearray *arr=[nsmutablearray Arraywithcapacity:7];2. Add an element to the arrayNsarray *[email protected][@ "Mon", @ "Tue", @ "Wed", @ "Thu", @ "Fri", @ "sat", @ "Sun"];//immutable variable groupNsmutablearray *arr2=[ns
#import int Main (int argc, const Char * argv[]) { @autoreleasepool { // variable array inheritance immutable variable group //1. Create A set number of elements to create nsmutablearray *mutarr=[nsmutablearray arraywithcapacity:7]; //2. add an element to an array nsarray *arr=@[@ "MON",@ "TUE",@ "WED",@ "THU",@ "FRI", @ "SAT",@ "SUN"]; nsmutablearray *mutarray=[nsmutablearray arraywitharray: arr]; [Mutarray addobject:@ "EIG
#import int main (int argc, const char * argv[]) {@autoreleasepool {Variable groups inherit immutable groups1. Create a set number of elementsNsmutablearray *mutarr=[nsmutablearray Arraywithcapacity:7];2. Add an element to the arrayNsarray *[email protected][@ "MON", @ "TUE", @ "WED", @ "THU", @ "FRI", @ "SAT", @ "SUN"];Nsmutablearray *mutarray=[nsmutablearray Arraywitharray:arr];[Mutarray addobject:@ "EIG"];3. Insert the element according to the spec
The Python operation can find the pixel point in an image. For example, a white pixel with black pixels will be found. import cv2.cv as cv # load imagefilename = ".. /Video/cat.jpg "image = cv. loadImage (filename) # create one windowwin_name = "test" cv. namedWindow (win_name) win2_name = "test2" cv. namedWindow (win2_name) cv. showImage (win2_name, image) # set created imagesize = cv. getSize (image) # (100,100) depth = 8 channels = 1 # gray operategrey = cv. createImage (size, depth, channels
-meanvals3) find out the covariance matrix of the Matrix after averaging>>> Covmat=np.cov (meanremove, rowvar=0)4) the eigenvalues and eigenvectors of the covariance matrix are obtained, and the Eig function in the NumPy library is Used.>>> Eigvals,eigvects=np.linalg.eig (np.mat (covmat))>>> eigvalsArray ([1.20374494e+01, 3.44539806e+00, 1.01715252e+00,-1.59662646e-16, 1.21625562e-16])>>> eigvectsMatrix ([[[0.20502268, 0.21893499,-0.80686681, 0.450186
function [V,s,e]=princa (x) [M,n]=size (x); The% calculates the row m and column n% of the matrix-------------the first step: the normalized matrix-----------------%mv=mean (X); % calculates the mean value of each variable st=std (X); % calculates the standard deviation of each variable x= (X-repmat (mv,m,1))./repmat (st,m,1); % normalized matrix x-------------Second step: Calculate the correlation coefficient matrix-----------------percent r1=x ' *x/(m-1); % method One: Covariance matrix calcul
The next problem is that when the model is symmetric, the result is expected, but when the model is asymmetrical, the result is wrong, as follows:Input: Vertex: 233Output:What the hell is this? , Where's my horse!!!There seems to be a logical error.Note that the debug information for the C + + output is as follows:The error message is: Input to EIG must not contain NaN, then a bunch of hot hot ...There is also a hint: Matrix is close to singular or ba
product of all elements
Methods for arrays of Boolean types#True直接当1计算In [24]: (arr2dSort
Np.sort () This will copy a copy.
Arr2d.sort () is the operation on the source data
5. Input and output for array filesTo save an array to disk in binary form
Np.save ()
Np.load ()
accessing text files
Np.loadtext ()
Np.savetext ()
6. Linear algebra is not found when the Numpy.linalg
Note: Transpose arr. T
Np.dot (ARR1,ARR2) The produc
(X)
print (pca.explained_ Variance_ratio_)
Test and test with a dataset of your own makingThe program extracts a characteristic value to reduce the dimensionality of two-dimensional data
Using PCA algorithm to reduce dimension of testSet.txt data set
Import NumPy as NP import Matplotlib.pyplot as Plt def loaddataset (filename, delim= ' \ t '): FR = open (filename) S
Tringarr = [Line.strip (). Split (Delim) for line in Fr.readlines ()] Datarr = [Map (float, line) for line in Stringarr] Ret
converted to C + + code, and the command line parameters are as follows:Generate C File: Mcc–t–l C x.mGenerate C + + files: mcc–t–l Cpp x.mGenerate dynamic link library functions: mcc–t–w lib:y–t link.lib x.m (Y for generated link file name)4. To import a link library:
#pragma comment (lib, "libmatlb.lib") #pragma comment (lib, "libmx.lib") #pragma comment (lib, "libmatpm.lib") // c++ maths library
5. The reference code is as follows:
double d[] = { 1, 2, 3, 4 }; mwArray A(2, 2, d); mwArray
(i,i)) * sqrt (d (j,j)))
;
End end% calculates eigenvalue eigenvectors [eigvectors,eigvalues] = Eig (L);
% selected before K maximum eigenvalue [eigvalues, Ind] = sort (diag (eigvalues), ' descend ');
Neigvec =eigvectors (:, Ind (1:K));
% constructs a normalized matrix U from the obtained eigenvector U=zeros (size (neigvec,1), k);
For I=1:size (neigvec,1) n = sqrt (sum (Neigvec (i,:). ^2)); U (i,:) = Neigvec (i,:).
N
End End Functio
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.