Last active
August 29, 2015 14:21
-
-
Save binary10/815c6eb3aee9812b4b5e to your computer and use it in GitHub Desktop.
IronPython, Accord.NET, and LINQ expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import C# CLR interface and system | |
import clr, sys | |
# Add C# Library Paths and add references via CLR module | |
sys.path.append('C:\\Program Files (x86)\\Accord.NET\\Framework\\Release\\net40') | |
sys.path.append('C:\\Program Files (x86)\\AForge.NET\\Framework\\Release') | |
clr.AddReference('System.Core') | |
clr.AddReference('AForge.Math') | |
clr.AddReference('Accord.Statistics') | |
clr.AddReference('System.Linq') | |
# Import previously referenced C# libraries like first-class Python modules | |
import System | |
import AForge.Math # Accord.NET depends on this module to work correctly | |
import System.Linq | |
import Accord.Statistics as s | |
clr.ImportExtensions(System.Linq) # Import LINQ extension methods (to enable "fluent syntax") | |
d = s.Distributions.Univariate # Easy reference for distributions | |
# SAMPLE | |
# Generate a random sample from a Standard Normal distribution | |
sample_size = 10000 | |
sample = d.NormalDistribution.Standard.Generate(sample_size) | |
# QUERY | |
# Query the sample using LINQ expressions! | |
# This calculation takes the average of the square of observations greater than zero | |
sample.Where(lambda n: n > 0).Select(lambda n: n**2).Cast[float]().Average() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment