<commit_before># Principal Component Analysis (PCA)

[Principal component analysis](http://setosa.io/ev/principal-component-analysis/) in Ruby. Uses [GSL](http://www.gnu.org/software/gsl/) for calculations.

PCA can be used to map data to a lower dimensional space while minimizing information loss. 
It's useful for data visualization, where you're limited to 2-D and 3-D plots.

For example, here's a plot of the 4-D iris flower dataset mapped to 2-D via PCA:

![iris](https://raw.githubusercontent.com/gbuesing/pca/master/examples/data/iris_small.png)

PCA is also used to compress the features of a dataset before feeding it into a machine learning algorithm,
potentially speeding up training time with a minimal loss of data detail.


## Install

**GSL must be installed first**. On OS X it can be installed via homebrew: ```brew install gsl```

    gem install pca


## Example Usage

```ruby
require 'pca'

pca = PCA.new components: 1

data_2d = [ 
  [2.5, 2.4], [0.5, 0.7], [2.2, 2.9], [1.9, 2.2], [3.1, 3.0],
  [2.3, 2.7], [2.0, 1.6], [1.0, 1.1], [1.5, 1.6], [1.1, 0.9]
]

data_1d = pca.fit_transform data_2d

# Transforms 2d data into 1d:
# data_1d ~= [
#   [-0.8], [1.8], [-1.0], [-0.3], [-1.7],
#   [-0.9], [0.1], [1.1], [0.4], [1.2]
# ]

more_data_1d = pca.transform [ [3.1, 2.9] ]

# Transforms new data into previously fitted 1d space:
# more_data_1d ~= [ [-1.6] ]

reconstructed_2d = pca.inverse_transform data_1d

# Reconstructs original data (approximate, b/c data compression):
# reconstructed_2d ~= [
#   [2.4, 2.5], [0.6, 0.6], [2.5, 2.6], [2.0, 2.1], [2.9, 3.1]
#   [2.4, 2.6], [1.7, 1.8], [1.0, 1.1], [1.5, 1.6], [1.0, 1.0]
# ]
```

See [examples](examples/) for more. Also, peruse the [source code](lib/pca.rb) (~ 100 loc.)


### Options

The following options can be passed in to ```PCA.new```:

option | default | description
------ | ------- | -----------
:components | nil | number of components to extract. If nil, will just rotate data onto first principal component
:scale_data | false | scales features before running PCA by dividing each feature by its standard deviation.

<commit_msg>Show #explained_variance_ratio example in README<commit_after># Principal Component Analysis (PCA)

[Principal component analysis](http://setosa.io/ev/principal-component-analysis/) in Ruby. Uses [GSL](http://www.gnu.org/software/gsl/) for calculations.

PCA can be used to map data to a lower dimensional space while minimizing information loss. 
It's useful for data visualization, where you're limited to 2-D and 3-D plots.

For example, here's a plot of the 4-D iris flower dataset mapped to 2-D via PCA:

![iris](https://raw.githubusercontent.com/gbuesing/pca/master/examples/data/iris_small.png)

PCA is also used to compress the features of a dataset before feeding it into a machine learning algorithm,
potentially speeding up training time with a minimal loss of data detail.


## Install

**GSL must be installed first**. On OS X it can be installed via homebrew: ```brew install gsl```

    gem install pca


## Example Usage

```ruby
require 'pca'

pca = PCA.new components: 1

data_2d = [ 
  [2.5, 2.4], [0.5, 0.7], [2.2, 2.9], [1.9, 2.2], [3.1, 3.0],
  [2.3, 2.7], [2.0, 1.6], [1.0, 1.1], [1.5, 1.6], [1.1, 0.9]
]

data_1d = pca.fit_transform data_2d

# Transforms 2d data into 1d:
# data_1d ~= [
#   [-0.8], [1.8], [-1.0], [-0.3], [-1.7],
#   [-0.9], [0.1], [1.1], [0.4], [1.2]
# ]

more_data_1d = pca.transform [ [3.1, 2.9] ]

# Transforms new data into previously fitted 1d space:
# more_data_1d ~= [ [-1.6] ]

reconstructed_2d = pca.inverse_transform data_1d

# Reconstructs original data (approximate, b/c data compression):
# reconstructed_2d ~= [
#   [2.4, 2.5], [0.6, 0.6], [2.5, 2.6], [2.0, 2.1], [2.9, 3.1]
#   [2.4, 2.6], [1.7, 1.8], [1.0, 1.1], [1.5, 1.6], [1.0, 1.0]
# ]

evr = pca.explained_variance_ratio

# Proportion of data variance explained by each component
# Here, the first component explains 99.85% of the data variance:
# evr ~= [0.99854]
```

See [examples](examples/) for more. Also, peruse the [source code](lib/pca.rb) (~ 100 loc.)


### Options

The following options can be passed in to ```PCA.new```:

option | default | description
------ | ------- | -----------
:components | nil | number of components to extract. If nil, will just rotate data onto first principal component
:scale_data | false | scales features before running PCA by dividing each feature by its standard deviation.


{'fraction_non_alphanumeric': 0.1686503480799461, 'fraction_numerical': 0.05883673927689198, 'mean_word_length': 3.4318407960199004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3446108', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1643, 'n_words': 550}