diff --git a/.gitignore b/.gitignore index 737d0f3..789a590 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ Release/ x64/ PEL208-Special_Learning_Topics/Debug/ PEL208-Special_Learning_Topics/Release/ -PEL208-Special_Learning_Topics/x64/ \ No newline at end of file +PEL208-Special_Learning_Topics/x64/ +PEL208-Special_Learning_Topics/least_squares_data/.idea +PEL208-Special_Learning_Topics/pca_data/.idea +PEL208-Special_Learning_Topics/lda_data/.idea \ No newline at end of file diff --git a/PEL208-Special_Learning_Topics/assignments.cpp b/PEL208-Special_Learning_Topics/assignments.cpp index 087feae..512c449 100644 --- a/PEL208-Special_Learning_Topics/assignments.cpp +++ b/PEL208-Special_Learning_Topics/assignments.cpp @@ -195,8 +195,8 @@ void mhorvath::runLeastSquaresExperiment(const MatrixXd &X, const VectorXd &y, c { unsigned const int n((unsigned int)X.cols()); // Number of features unsigned const int m((unsigned int)X.rows()); // Number of observations - unsigned const int p_m((unsigned int)std::min((unsigned int) 10, m)); // Limit of lines to print - unsigned const int p_n((unsigned int)std::min((unsigned int) 10, n)); // Limit of lines to print + unsigned const int p_m((unsigned int)std::min((unsigned int)10, m)); // Limit of lines to print + unsigned const int p_n((unsigned int)std::min((unsigned int)10, n)); // Limit of lines to print VectorXd Beta(n); // Least squares coefficients FILE *f; // File used filesystem::path out_file; // Used to build output path @@ -237,6 +237,93 @@ void mhorvath::runLeastSquaresExperiment(const MatrixXd &X, const VectorXd &y, c system("CLS"); } +void mhorvath::runLDAExperiment(const MatrixXd &X, const vector &classes, const char * const f_label = "", const char * const out_path = "") +{ + unsigned const int n((unsigned int)X.cols()); // Number of features + unsigned const int m((unsigned int)X.rows()); // Number of observations + unsigned const int p_m((unsigned int)std::min((unsigned int)10, m)); // Limit of lines to print + unsigned const int p_n((unsigned int)std::min((unsigned int)10, n)); // Limit of lines to print + MatrixXd lda_comp(n, n); + FILE *f; // File used + filesystem::path out_file; // Used to build output path + char f_name[256]; // Used to build output file name + + if (strcmp(out_path, "")) { + filesystem::create_directory(out_path); // Create output path + } + + mhorvath::LDA lda(X, classes); + + cout << "Data =" << endl << X.block(0, 0, p_m, p_n) << endl << endl; + cout << "Sb =" << endl << lda.getSb() << endl << endl; + cout << "Sw =" << endl << lda.getSw() << endl << endl; + cout << "Data Mean =" << endl << lda.data_mean() << endl << endl; + cout << "Classes Mean =" << endl << lda.classes_mean() << endl << endl; + cout << "Eigenvalues =" << endl << lda.values().segment(0, p_n) << endl << endl; + cout << "Eigenvectors =" << endl << lda.components().block(0, 0, p_n, p_n) << endl << endl; + cout << "Explained Variance =" << endl << lda.explained_variace_ratio().segment(0, p_n) << endl << endl; + cout << "Sum of Explained Variance =" << endl << lda.explained_variace_ratio().sum() << endl << endl; + + // Vector of matrices to store dimensionality reducted data using from 1 to n principal components + MatrixXd *reduction_D = new MatrixXd[X.cols()]; + + // Compute dimensionality reduction + for (int i = 0; i < X.cols(); i++) { + reduction_D[i] = lda.transform(i + 1); + } + + // Vector of matrices to store rebuilt data using from 1 to n principal components + MatrixXd *rebuilt_D = new MatrixXd[X.cols()]; + + // Compute data rebuilding + for (unsigned int i = 0; i < n; i++) { + rebuilt_D[i] = lda.rebuild(reduction_D[i]); + } + + // Write all results in file + sprintf(f_name, "%s_lda_vectors.csv", f_label); // Build output file name + + cout << "GENERATED FILES: " << endl; + + out_file = filesystem::current_path() / out_path / f_name; + f = fopen(out_file.string().c_str(), "w"); + + lda_comp = lda.components(); + + for (int j = 0; j < lda_comp.rows(); j++) { + fprintf(f, "%f", lda_comp(j, 0)); + for (int i = 1; i < lda_comp.cols(); i++) { + fprintf(f, ",%f", lda_comp(j, i)); + } + fprintf(f, "\n"); + } + + fclose(f); + cout << f_name << endl; + + for (unsigned int i = 0; i < n; i++) { + sprintf(f_name, "%s_rebuilt_lda_%d.csv", f_label, i + 1); + out_file = filesystem::current_path() / out_path / f_name; + + f = fopen(out_file.string().c_str(), "w"); + + for (int l = 0; l < rebuilt_D[i].rows(); l++) { + //fprintf(f, "%f", rebuilt_D[i](l, 0)); + for (int c = 0; c < rebuilt_D[i].cols(); c++) { + fprintf(f, "%f,", rebuilt_D[i](l, c)); + } + fprintf(f, "%s\n", classes[l].c_str()); + } + + fclose(f); + cout << f_name << endl; + } + cout << endl; + + system("PAUSE"); + system("CLS"); +} + void mhorvath::inClassExample() { // In class example: Some Data @@ -445,8 +532,9 @@ void mhorvath::iris() MatrixXd D(m, n); // Data matrix vector classes(m); // Least squares target variable const char * const ex_label = "iris"; // Experiment label + const char * const output_folder = "lda_data"; // Output folder FILE * f; // Used to read dataset file - char data[128]; + char data[128]; // Used to read class variable // Read dataset from file f = fopen("iris.txt", "r"); @@ -459,16 +547,32 @@ void mhorvath::iris() fclose(f); // Close file - mhorvath::PCA pca(mhorvath::lda(D, classes)); + mhorvath::runLDAExperiment(D, classes, ex_label, output_folder); +} - cout << "Data =" << endl << D.block(0, 0, p_m, p_n) << endl << endl; - cout << "Data Mean =" << endl << pca.getOriginalMean().segment(0, p_n) << endl << endl; - cout << "DataAdjust =" << endl << pca.getDataAdjust().block(0, 0, p_n, p_n) << endl << endl; - cout << "Covariance =" << endl << pca.covariance().block(0, 0, p_n, p_n) << endl << endl; - cout << "Eigenvalues =" << endl << pca.values().segment(0, p_n) << endl << endl; - cout << "Eigenvectors =" << endl << pca.components().block(0, 0, p_n, p_n) << endl << endl; - cout << "Explained Variance =" << endl << pca.explained_variace_ratio().segment(0, p_n) << endl << endl; - cout << "Sum of Explained Variance =" << endl << pca.explained_variace_ratio().sum() << endl << endl; +void mhorvath::inClassExampleLDA() +{ + // In class example: Some Data + const unsigned int n(2); // Number of features + const unsigned int m(11); // Number of observations + MatrixXd D(m, n); // Data matrix + vector classes(m); // Least squares target variable + const char * const ex_label = "inClassExample"; // Experiment label + const char * const output_folder = "lda_data"; // Output folder + FILE * f; // Used to read dataset file + char data[128]; - system("PAUSE"); + // Read dataset from file + f = fopen("inClassExample_LDA.txt", "r"); + + // Read line-by-line + for (int i = 0; i < m; i++) { + fscanf(f, "%lf %lf %s\n", &D(i, 0), &D(i, 1), &data); + classes[i] = data; + } + + fclose(f); // Close file + + mhorvath::runLDAExperiment(D, classes, ex_label, output_folder); + mhorvath::runPCAExperimentEx(D, ex_label, output_folder); } diff --git a/PEL208-Special_Learning_Topics/assignments.h b/PEL208-Special_Learning_Topics/assignments.h index f8fd2b2..ce3fb5d 100644 --- a/PEL208-Special_Learning_Topics/assignments.h +++ b/PEL208-Special_Learning_Topics/assignments.h @@ -1,12 +1,15 @@ #pragma once #define _CRT_SECURE_NO_DEPRECATE #include +#include namespace mhorvath { void runPCAExperimentEx(const Eigen::MatrixXd &, const char * const, const char * const); void runPCAExperiment(const Eigen::MatrixXd &, const char * const, const char * const); void runLeastSquaresExperiment(const Eigen::MatrixXd &, const Eigen::VectorXd &, const char * const, const char * const); + + void runLDAExperiment(const Eigen::MatrixXd &, const std::vector &, const char * const, const char * const); void inClassExample(); @@ -19,4 +22,6 @@ namespace mhorvath { void hald(); void iris(); + + void inClassExampleLDA(); } \ No newline at end of file diff --git a/PEL208-Special_Learning_Topics/inClassExample_LDA.txt b/PEL208-Special_Learning_Topics/inClassExample_LDA.txt new file mode 100644 index 0000000..607f115 --- /dev/null +++ b/PEL208-Special_Learning_Topics/inClassExample_LDA.txt @@ -0,0 +1,11 @@ +1 2 1 +2 3 1 +3 3 1 +4 5 1 +5 5 1 +1 0 2 +2 1 2 +3 1 2 +3 2 2 +5 3 2 +6 5 2 \ No newline at end of file diff --git a/PEL208-Special_Learning_Topics/lda.cpp b/PEL208-Special_Learning_Topics/lda.cpp index d5a1113..55a765a 100644 --- a/PEL208-Special_Learning_Topics/lda.cpp +++ b/PEL208-Special_Learning_Topics/lda.cpp @@ -2,37 +2,47 @@ #include #include #include +#include "matrix_inv.h" using namespace Eigen; using namespace std; //typedef pair, int> mypair; -mhorvath::PCA mhorvath::lda(const MatrixXd &M, const vector &class_vec) +bool compEigen2(const tuple &A, const tuple &B) { + return *get<1>(A) > *get<1>(B); +} + +mhorvath::LDA::LDA(const MatrixXd &M, const vector &class_vec) + : DataAdjust(M.rows(), M.cols()), EigenVectors(M.cols(), M.cols()), + EigenValues(M.cols()), ExplainedVariance(M.cols()), DataMean(M.colwise().mean()), + Sb(MatrixXd::Zero(M.cols(), M.cols())), Sw(MatrixXd::Zero(M.cols(), M.cols())) { unsigned const int m((unsigned int)M.rows()); unsigned const int n((unsigned int)M.cols()); - map classes; - map::iterator it_class; - map ids; - map::iterator it_ids; - map X; - map::iterator it_X; - vector class_mean; - VectorXd data_mean(VectorXd::Zero(n)); - MatrixXd Sb(MatrixXd::Zero(n, n)); - MatrixXd Sw(MatrixXd::Zero(n, n)); + + map classes; // Map data structure used to check the number of classes + map::iterator it_class; // Iterator for classes map + map ids; // Map data structure used to split data by class + map::iterator it_ids; // Iterator for ids map + map X; // Map used to store data splited by class + map::iterator it_X; // Iterator for X map + vector> ord; // Vector to support eigen values/vectors sorting + + MatrixXd evecs(n, n); // Unordered eigenvectors + VectorXd evals(n); // Unordered eigenvalues + double evalues_sum(0.0); // Sum of eigenvalues (Used to compute proportional explained variance) + + // Subtract the mean + this->DataAdjust = M.rowwise() - this->DataMean; // Check number of classes for (unsigned int i = 0; i < m; i++) { - it_class = classes.find(class_vec[i]); - if (it_class == classes.end()) { - classes.insert(pair(class_vec[i], 0)); - } + classes.insert(pair(class_vec[i], 0)); } - unsigned const int g((unsigned int) classes.size()); - + unsigned const int g((unsigned int)classes.size()); // Stores number of classes + // Compute number of elements of each class for (unsigned int i = 0; i < m; i++) { it_class = classes.find(class_vec[i]); @@ -50,35 +60,120 @@ mhorvath::PCA mhorvath::lda(const MatrixXd &M, const vector &class_vec) it_X = X.find(c); it_ids = ids.find(c); for (unsigned int j = 0; j < n; j++) { - it_X->second(it_ids->second, j) = M(i, j); + it_X->second(it_ids->second, j) = DataAdjust(i, j); } it_ids->second++; } - class_mean.resize(g); - + this->ClassMean.resize(g); + + // Compute the mean of each class it_X = X.begin(); - for(unsigned int i = 0; it_X != X.end(); it_X++, i++) { - class_mean[i] = VectorXd(it_X->second.colwise().mean()); - data_mean += class_mean[i]; + for (unsigned int i = 0; it_X != X.end(); it_X++, i++) { + this->ClassMean[i] = VectorXd(it_X->second.colwise().mean()); + //data_mean += class_mean[i]; } - data_mean /= g; + //data_mean /= g; - for (it_X = X.begin(); it_X != X.end(); it_X++) { - MatrixXd temp(X.begin()->second.transpose().colwise() - class_mean[0]); + // Compute Sw matrix + it_X = X.begin(); + for (unsigned int i = 0; it_X != X.end(); it_X++, i++) { + MatrixXd temp(it_X->second.transpose().colwise() - this->ClassMean[i].transpose()); Sw += temp * temp.transpose(); } - //cout << "Sw =" << endl << Sw << endl << endl; - + // Compute Sb matrix it_class = classes.begin(); for (unsigned int i = 0; i < g; i++, it_class++) { - VectorXd temp = class_mean[i] - data_mean; - Sb += MatrixXd((temp * temp.transpose()).array() * it_class->second); + //RowVectorXd temp = this->ClassMean[i] - this->DataMean; + RowVectorXd temp = this->ClassMean[i]; + Sb += MatrixXd((temp.transpose() * temp).array() * it_class->second); + } + + // Compute eigenvalues and eigenvectors + EigenSolver es(Sw.inverse() * Sb); + evals = es.eigenvalues().real(); + evecs = es.eigenvectors().real(); + + evalues_sum = evals.sum(); + + // Sort eigenvalues and eigenvectors + for (int i = 0; i < M.cols(); i++) { + ord.push_back(tuple(i, &evals(i))); + } + + std::sort(ord.begin(), ord.end(), compEigen2); + + for (int i = 0; i < M.cols(); i++) { + int ind = get<0>(ord[i]); + this->EigenVectors.col(i) = evecs.col(ind); + this->EigenValues(i) = evals(ind); + this->ExplainedVariance(i) = evals(ind) / evalues_sum; + } +} + +mhorvath::LDA::LDA() {} + +MatrixXd mhorvath::LDA::components() +{ + return this->EigenVectors; +} + +VectorXd mhorvath::LDA::values() +{ + return this->EigenValues; +} + +VectorXd mhorvath::LDA::explained_variace_ratio() +{ + return this->ExplainedVariance; +} + +Eigen::VectorXd mhorvath::LDA::data_mean() +{ + return this->DataMean; +} + +Eigen::MatrixXd mhorvath::LDA::classes_mean() +{ + MatrixXd means(this->ClassMean.size(), this->DataAdjust.cols()); + vector::iterator it(this->ClassMean.begin()); + + for (int i = 0; it != this->ClassMean.end(); i++, it++) { + means.row(i) = *it; } - //cout << "Sb =" << endl << Sb << endl << endl; + return means; +} + +MatrixXd mhorvath::LDA::transform(const unsigned int & n_comp) +{ + return (this->EigenVectors.block(0, 0, EigenVectors.rows(), n_comp).transpose() * + this->DataAdjust.transpose()).transpose(); +} + +MatrixXd mhorvath::LDA::rebuild(const MatrixXd &A) +{ + //MatrixXd temp(MatrixXd::Zero(this->EigenVectors.rows(), this->EigenVectors.cols()).array() + 1.0); + //temp.block(0, 0, temp.rows(), A.cols()) = this->EigenVectors.block(0, 0, temp.rows(), A.cols()); + + //return (temp.transpose().inverse().block(0, 0, temp.rows(), A.cols()) * + // A.transpose()).transpose(); + + //return (this->EigenVectors.transpose().inverse().block(0, 0, this->EigenVectors.rows(), A.cols()) * + // A.transpose()).transpose(); - return mhorvath::PCA(Sw.inverse() * Sb); + return (this->EigenVectors.transpose().inverse().block(0, 0, this->EigenVectors.rows(), A.cols()) * + A.transpose()).transpose().rowwise() + this->DataMean; +} + +MatrixXd mhorvath::LDA::getSb() +{ + return this->Sb; +} + +MatrixXd mhorvath::LDA::getSw() +{ + return this->Sw; } diff --git a/PEL208-Special_Learning_Topics/lda.h b/PEL208-Special_Learning_Topics/lda.h index 4391b1a..94a8076 100644 --- a/PEL208-Special_Learning_Topics/lda.h +++ b/PEL208-Special_Learning_Topics/lda.h @@ -1,8 +1,37 @@ #pragma once -#include "pca.h" #include #include namespace mhorvath { - mhorvath::PCA lda(const Eigen::MatrixXd &, const std::vector &); + class LDA { + private: + Eigen::MatrixXd DataAdjust; + Eigen::MatrixXd EigenVectors; + Eigen::VectorXd EigenValues; + Eigen::VectorXd ExplainedVariance; // Normalized EigenValues + Eigen::RowVectorXd DataMean; + + Eigen::MatrixXd Sb; + Eigen::MatrixXd Sw; + + std::vector ClassMean; // Used to compute the mean of the variables in each class + + LDA(); + public: + LDA(const Eigen::MatrixXd &, const std::vector &); + + Eigen::MatrixXd components(); // Returns the eigenvectors + Eigen::VectorXd values(); // Returns eigenvalues + Eigen::VectorXd explained_variace_ratio(); // Returns the explained variance os each component + Eigen::VectorXd data_mean(); + Eigen::MatrixXd classes_mean(); + + Eigen::MatrixXd transform(const unsigned int &); + Eigen::MatrixXd rebuild(const Eigen::MatrixXd &); + + Eigen::MatrixXd getSb(); + Eigen::MatrixXd getSw(); + }; + + //mhorvath::PCA lda(const Eigen::MatrixXd &, const std::vector &); } \ No newline at end of file diff --git a/PEL208-Special_Learning_Topics/lda_data/inClassExample.py b/PEL208-Special_Learning_Topics/lda_data/inClassExample.py new file mode 100644 index 0000000..2ec55bd --- /dev/null +++ b/PEL208-Special_Learning_Topics/lda_data/inClassExample.py @@ -0,0 +1,127 @@ +import os +import numpy as np +from matplotlib import pyplot as plt +from sklearn.discriminant_analysis import LinearDiscriminantAnalysis + +f = open(os.path.join('..', 'inClassExample_LDA.txt')) +original_data = f.readlines() +f.close() + +f = open('inClassExample_rebuilt_lda_1.csv') +data_lda = f.readlines() +f.close() + +f = open('inClassExample_rebuilt_comp_1.csv') +data_pca = f.readlines() +f.close() + +for i in range(len(data_lda)): + original_data[i] = np.array(original_data[i].split()) + + data_lda[i] = ''.join(data_lda[i].split()) + data_lda[i] = np.array(data_lda[i].split(',')) + + data_pca[i] = ''.join(data_pca[i].split()) + data_pca[i] = np.array(data_pca[i].split(',')) +data_lda = np.array(data_lda) +original_data = np.array(original_data) +data_pca = np.array(data_pca) + +# f = open(os.path.join('inClassExample_lda_vectors.csv')) +# components = f.readlines() +# f.close() + +# for i in range(len(components)): +# components[i] = np.array(components[i].split(','), dtype=np.float) +# components = np.array(components) + +# print components + +X_lda = {} +X_orig = {} +X_pca = {} +X2 = data_lda[:, :2].astype(dtype=np.float) +y = data_lda[:, -1].astype(dtype=str)\ + +for i in range(len(data_lda)): + obs_class = y[i] + if obs_class not in X_lda.keys(): + X_lda[obs_class] = [] + X_orig[obs_class] = [] + X_pca[obs_class] = [] + X_lda[obs_class].append(data_lda[i, :2]) + X_orig[obs_class].append(original_data[i, :2]) + X_pca[obs_class].append(data_pca[i, :2]) + +# components = np.matrix(components) + +# lda = LinearDiscriminantAnalysis(solver='eigen') +# lda.fit(X2, y) +# +# lda_coef = np.array(lda.coef_[0]) +# print lda_coef + +for key in X_lda.keys(): + X_lda[key] = np.matrix(X_lda[key], dtype=np.float) + X_orig[key] = np.matrix(X_orig[key], dtype=np.float) + X_pca[key] = np.matrix(X_pca[key], dtype=np.float) + + # X[key] = components[:, 0].transpose() * X[key].transpose() + # X[key] = (np.linalg.inv(components.transpose())[:, 0] * X[key]).transpose() + # print '{}:\n'.format(key), X[key] + + # X[key] = lda_coef * X[key].transpose() + # X[key] = (np.linalg.inv(lda_coef) * X[key]).transpose() + # print '{}:\n'.format(key), X[key] + +# cp_order = [x for x in range(len(X2[0]))] +cp_order = [x for x in range(len(X2[0])-1, -1, -1)] + + +data_mean = np.array([3.18182, 2.72727]) + + +# comp_X = np.array([-3, 0, 4]) +# comp_Y = [x*components[cp_order[0], 0]/components[cp_order[1], 0] for x in comp_X] +# comp_Y = [x*components[cp_order[0], 0] for x in comp_X] +# comp_Y = [x*components[cp_order[0], 1]/components[cp_order[1], 1] for x in comp_X] +# comp_Y2 = [x*components[cp_order[0], 1] for x in comp_X] + +# comp_Y2 = [x*lda_coef[cp_order[0]]/lda_coef[cp_order[1]] for x in comp_X] + +plt.grid(linestyle=':') +# plt.plot(comp_X + data_mean[0], comp_Y + data_mean[1], 'b') +# plt.plot(comp_X + data_mean[0], comp_Y2 + data_mean[1], 'g') +x = X_orig['1'] +plt.plot(x[:, 0], x[:, 1], 'rx', label='classe 1') +x = X_orig['2'] +plt.plot(x[:, 0], x[:, 1], 'bo', label='classe 2') +plt.legend() +# plt.plot(data_mean[0], data_mean[1], 'kX') + +plt.show() + +plt.grid(linestyle=':') +# plt.plot(comp_X + data_mean[0], comp_Y + data_mean[1], 'b') +# plt.plot(comp_X + data_mean[0], comp_Y2 + data_mean[1], 'g') +x = X_pca['1'] +plt.plot(x[:, 0], x[:, 1], 'rx', label='classe 1') +x = X_pca['2'] +plt.plot(x[:, 0], x[:, 1], 'bo', label='classe 2') +plt.legend() +# plt.plot(data_mean[0], data_mean[1], 'kX') + +plt.show() + +plt.grid(linestyle=':') +# plt.plot(comp_X + data_mean[0], comp_Y + data_mean[1], 'b') +# plt.plot(comp_X + data_mean[0], comp_Y2 + data_mean[1], 'g') +x = X_lda['1'] +plt.plot(x[:, 0], x[:, 1], 'rx', label='classe 1') +x = X_lda['2'] +plt.plot(x[:, 0], x[:, 1], 'bo', label='classe 2') +plt.legend() +# plt.plot(data_mean[0], data_mean[1], 'kX') + +plt.show() + diff --git a/PEL208-Special_Learning_Topics/lda_data/inClassExample_components.csv b/PEL208-Special_Learning_Topics/lda_data/inClassExample_components.csv new file mode 100644 index 0000000..f421b19 --- /dev/null +++ b/PEL208-Special_Learning_Topics/lda_data/inClassExample_components.csv @@ -0,0 +1,2 @@ +-0.686891,-0.726761 +-0.726761,0.686891 diff --git a/PEL208-Special_Learning_Topics/lda_data/inClassExample_lda_vectors.csv b/PEL208-Special_Learning_Topics/lda_data/inClassExample_lda_vectors.csv new file mode 100644 index 0000000..f7be1d3 --- /dev/null +++ b/PEL208-Special_Learning_Topics/lda_data/inClassExample_lda_vectors.csv @@ -0,0 +1,2 @@ +0.665557,-0.978980 +-0.746347,-0.203954 diff --git a/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_comp_1.csv b/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_comp_1.csv new file mode 100644 index 0000000..39e5ca6 --- /dev/null +++ b/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_comp_1.csv @@ -0,0 +1,11 @@ +1.789336,1.253966 +2.760361,2.281352 +3.232180,2.780557 +4.702409,4.336125 +5.174228,4.835330 +0.790926,0.197604 +1.761950,1.224990 +2.233769,1.724196 +2.732975,2.252377 +4.175818,3.778968 +5.646047,5.334535 diff --git a/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_comp_2.csv b/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_comp_2.csv new file mode 100644 index 0000000..75fd623 --- /dev/null +++ b/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_comp_2.csv @@ -0,0 +1,11 @@ +1.000000,2.000000 +2.000000,3.000000 +3.000000,3.000000 +4.000000,5.000000 +5.000000,5.000000 +1.000000,0.000000 +2.000000,1.000000 +3.000000,1.000000 +3.000000,2.000000 +5.000000,3.000000 +6.000000,5.000000 diff --git a/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_lda_1.csv b/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_lda_1.csv new file mode 100644 index 0000000..dcd899c --- /dev/null +++ b/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_lda_1.csv @@ -0,0 +1,11 @@ +2.967759,3.754755,1 +2.948741,3.846042,1 +3.105416,3.094005,1 +2.910705,4.028618,1 +3.067379,3.276580,1 +3.319145,2.068104,2 +3.300127,2.159391,2 +3.456801,1.407354,2 +3.281108,2.250679,2 +3.418765,1.589929,2 +3.224054,2.524543,2 diff --git a/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_lda_2.csv b/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_lda_2.csv new file mode 100644 index 0000000..fe795ab --- /dev/null +++ b/PEL208-Special_Learning_Topics/lda_data/inClassExample_rebuilt_lda_2.csv @@ -0,0 +1,11 @@ +1.000000,2.000000,1 +2.000000,3.000000,1 +3.000000,3.000000,1 +4.000000,5.000000,1 +5.000000,5.000000,1 +1.000000,0.000000,2 +2.000000,1.000000,2 +3.000000,1.000000,2 +3.000000,2.000000,2 +5.000000,3.000000,2 +6.000000,5.000000,2 diff --git a/PEL208-Special_Learning_Topics/lda_data/plots/inClassExample_1comp_plot.png b/PEL208-Special_Learning_Topics/lda_data/plots/inClassExample_1comp_plot.png new file mode 100644 index 0000000..e66ccdc Binary files /dev/null and b/PEL208-Special_Learning_Topics/lda_data/plots/inClassExample_1comp_plot.png differ diff --git a/PEL208-Special_Learning_Topics/lda_data/plots/inClassExample_2comp_plot.png b/PEL208-Special_Learning_Topics/lda_data/plots/inClassExample_2comp_plot.png new file mode 100644 index 0000000..e66ccdc Binary files /dev/null and b/PEL208-Special_Learning_Topics/lda_data/plots/inClassExample_2comp_plot.png differ diff --git a/PEL208-Special_Learning_Topics/lda_data/plots/inClassExample_PCAxLSM_plot.png b/PEL208-Special_Learning_Topics/lda_data/plots/inClassExample_PCAxLSM_plot.png new file mode 100644 index 0000000..bdfcf13 Binary files /dev/null and b/PEL208-Special_Learning_Topics/lda_data/plots/inClassExample_PCAxLSM_plot.png differ diff --git a/PEL208-Special_Learning_Topics/main.cpp b/PEL208-Special_Learning_Topics/main.cpp index f2632af..ddad27e 100644 --- a/PEL208-Special_Learning_Topics/main.cpp +++ b/PEL208-Special_Learning_Topics/main.cpp @@ -5,7 +5,8 @@ using namespace std; int main() { - mhorvath::iris(); + mhorvath::inClassExampleLDA(); + //mhorvath::iris(); return 0; } \ No newline at end of file diff --git a/PEL208-Special_Learning_Topics/pca.cpp b/PEL208-Special_Learning_Topics/pca.cpp index 0de71a7..4b868ca 100644 --- a/PEL208-Special_Learning_Topics/pca.cpp +++ b/PEL208-Special_Learning_Topics/pca.cpp @@ -20,7 +20,7 @@ mhorvath::PCA::PCA(const MatrixXd &A) MatrixXd evecs(A.cols(), A.cols()); // Unordered eigenvectors VectorXd evals(A.cols()); // Unordered eigenvalues vector> ord; // Vector to support eigen values/vectors sorting - double evalues_sum(0.0); // Sum of eigenvalues (Used to compute proportional ) + double evalues_sum(0.0); // Sum of eigenvalues (Used to compute proportional explained variance) // Compute the mean value of each column this->OriginalMean = A.colwise().mean(); diff --git a/PEL208-Special_Learning_Topics/pca.h b/PEL208-Special_Learning_Topics/pca.h index 05d3f16..bcc28f8 100644 --- a/PEL208-Special_Learning_Topics/pca.h +++ b/PEL208-Special_Learning_Topics/pca.h @@ -29,7 +29,6 @@ namespace mhorvath { Eigen::RowVectorXd getOriginalMean(); Eigen::MatrixXd transform(const int &); - Eigen::MatrixXd rebuild(const Eigen::MatrixXd &); }; }