pycall 1.0.1-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.travis.yml +41 -0
- data/CHANGES.md +39 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +91 -0
- data/Rakefile +29 -0
- data/appveyor.yml +138 -0
- data/bin/console +10 -0
- data/bin/guard +17 -0
- data/bin/rspec +17 -0
- data/bin/runner +6 -0
- data/bin/setup +8 -0
- data/config/Guardfile +30 -0
- data/docker/Dockerfile +191 -0
- data/docker/Gemfile +12 -0
- data/docker/README.md +22 -0
- data/examples/classifier_comparison.rb +135 -0
- data/examples/datascience_rb_20170519.ipynb +4836 -0
- data/examples/hist.rb +32 -0
- data/examples/notebooks/classifier_comparison.ipynb +226 -0
- data/examples/notebooks/forest_importances.ipynb +238 -0
- data/examples/notebooks/iruby_integration.ipynb +183 -0
- data/examples/notebooks/lorenz_attractor.ipynb +214 -0
- data/examples/notebooks/polar_axes.ipynb +209 -0
- data/examples/notebooks/sum_benchmarking.ipynb +374 -0
- data/examples/notebooks/xkcd_style.ipynb +149 -0
- data/examples/plot_forest_importances_faces.rb +46 -0
- data/examples/sum_benchmarking.rb +49 -0
- data/ext/pycall/extconf.rb +3 -0
- data/ext/pycall/gc.c +74 -0
- data/ext/pycall/libpython.c +217 -0
- data/ext/pycall/pycall.c +2184 -0
- data/ext/pycall/pycall_internal.h +700 -0
- data/ext/pycall/range.c +69 -0
- data/ext/pycall/ruby_wrapper.c +432 -0
- data/lib/2.1/pycall.so +0 -0
- data/lib/2.2/pycall.so +0 -0
- data/lib/2.3/pycall.so +0 -0
- data/lib/2.4/pycall.so +0 -0
- data/lib/pycall/conversion.rb +173 -0
- data/lib/pycall/dict.rb +48 -0
- data/lib/pycall/error.rb +10 -0
- data/lib/pycall/gc_guard.rb +84 -0
- data/lib/pycall/import.rb +120 -0
- data/lib/pycall/init.rb +55 -0
- data/lib/pycall/iruby_helper.rb +40 -0
- data/lib/pycall/libpython/finder.rb +170 -0
- data/lib/pycall/libpython/pyobject_struct.rb +30 -0
- data/lib/pycall/libpython/pytypeobject_struct.rb +273 -0
- data/lib/pycall/libpython.rb +12 -0
- data/lib/pycall/list.rb +45 -0
- data/lib/pycall/pretty_print.rb +9 -0
- data/lib/pycall/pyerror.rb +30 -0
- data/lib/pycall/pyobject_wrapper.rb +212 -0
- data/lib/pycall/python/PyCall/__init__.py +1 -0
- data/lib/pycall/python/PyCall/six.py +23 -0
- data/lib/pycall/python/investigator.py +7 -0
- data/lib/pycall/pytypeobject_wrapper.rb +90 -0
- data/lib/pycall/set.rb +19 -0
- data/lib/pycall/slice.rb +8 -0
- data/lib/pycall/tuple.rb +46 -0
- data/lib/pycall/version.rb +3 -0
- data/lib/pycall/wrapper_object_cache.rb +61 -0
- data/lib/pycall.rb +91 -0
- data/pycall.gemspec +40 -0
- data/tasks/docker.rake +21 -0
- data/tasks/pycall.rake +7 -0
- metadata +228 -0
data/docker/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# IRuby dependencies
|
4
|
+
gem 'pry'
|
5
|
+
gem 'pry-doc'
|
6
|
+
gem 'awesome_print'
|
7
|
+
gem 'cztop', '< 0.3.0'
|
8
|
+
gem 'iruby', github: 'sciruby/iruby'
|
9
|
+
|
10
|
+
# PyCall related gems
|
11
|
+
gem 'pycall', '>= 0.1.0.alpha.20170426'
|
12
|
+
gem 'matplotlib', '>= 0.1.0.alpha.20170426'
|
data/docker/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# How to use and build docker image
|
2
|
+
|
3
|
+
## Use
|
4
|
+
|
5
|
+
Execute the following command.
|
6
|
+
|
7
|
+
```
|
8
|
+
rake docker:run [port=<PORT>] [attach_local=<DIRECTORY>]
|
9
|
+
```
|
10
|
+
|
11
|
+
The `port` option is for specifying the port number connecting to iruby notebook.
|
12
|
+
|
13
|
+
You can access to a local directory from jupyter notebook in the container by attaching the local directory to `/notebooks/local` in the container using `attach_local` option. The default value is the current directory, that should be pycall directory.
|
14
|
+
|
15
|
+
## Build
|
16
|
+
|
17
|
+
You can build your own docker image if you modify pycall.
|
18
|
+
Execute the following command to build it.
|
19
|
+
|
20
|
+
```
|
21
|
+
rake docker:build
|
22
|
+
```
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'pycall/import'
|
2
|
+
include PyCall::Import
|
3
|
+
|
4
|
+
pyimport 'numpy', as: :np
|
5
|
+
pyfrom 'sklearn.cross_validation', import: :train_test_split
|
6
|
+
pyfrom 'sklearn.preprocessing', import: :StandardScaler
|
7
|
+
pyfrom 'sklearn.datasets', import: %i(make_moons make_circles make_classification)
|
8
|
+
pyfrom 'sklearn.neighbors', import: :KNeighborsClassifier
|
9
|
+
pyfrom 'sklearn.svm', import: :SVC
|
10
|
+
pyfrom 'sklearn.tree', import: :DecisionTreeClassifier
|
11
|
+
pyfrom 'sklearn.ensemble', import: %i(RandomForestClassifier AdaBoostClassifier)
|
12
|
+
pyfrom 'sklearn.naive_bayes', import: :GaussianNB
|
13
|
+
pyfrom 'sklearn.discriminant_analysis', import: %i(LinearDiscriminantAnalysis QuadraticDiscriminantAnalysis)
|
14
|
+
|
15
|
+
# FIXME: MacOSX backend is not usable through pycall. I want to fix this issue but the reason is unclear.
|
16
|
+
pyimport 'matplotlib', as: :mp
|
17
|
+
mp.rcParams[:backend] = 'TkAgg' if mp.rcParams[:backend] == 'MacOSX'
|
18
|
+
|
19
|
+
pyimport 'matplotlib.pyplot', as: :plt
|
20
|
+
pyimport 'matplotlib.colors', as: :mplc
|
21
|
+
|
22
|
+
h = 0.02 # step size in the mesh
|
23
|
+
|
24
|
+
names = [
|
25
|
+
'Nearest Neighbors',
|
26
|
+
'Linear SVM',
|
27
|
+
'RBF SVM',
|
28
|
+
'Decision Tree',
|
29
|
+
'Random Forest',
|
30
|
+
'AdaBoost',
|
31
|
+
'Naive Bayes',
|
32
|
+
'Linear Discriminant Analysis',
|
33
|
+
'Quadratic Discriminant Analysis'
|
34
|
+
]
|
35
|
+
|
36
|
+
classifiers = [
|
37
|
+
KNeighborsClassifier.new(3),
|
38
|
+
SVC.new(kernel: 'linear', C: 0.025),
|
39
|
+
SVC.new(gamma: 2, C: 1),
|
40
|
+
DecisionTreeClassifier.new(max_depth: 5),
|
41
|
+
RandomForestClassifier.new(max_depth: 5, n_estimators: 10, max_features: 1),
|
42
|
+
AdaBoostClassifier.new(),
|
43
|
+
GaussianNB.new(),
|
44
|
+
LinearDiscriminantAnalysis.new(),
|
45
|
+
QuadraticDiscriminantAnalysis.new()
|
46
|
+
]
|
47
|
+
|
48
|
+
x, y = *make_classification(
|
49
|
+
n_features: 2,
|
50
|
+
n_redundant: 0,
|
51
|
+
n_informative: 2,
|
52
|
+
random_state: 1,
|
53
|
+
n_clusters_per_class: 1
|
54
|
+
)
|
55
|
+
|
56
|
+
np.random.seed(42)
|
57
|
+
x += 2 * np.random.random_sample(x.shape)
|
58
|
+
linearly_separable = PyCall.tuple([x, y]) # FIXME: allow PyCall.tuple(x, y)
|
59
|
+
|
60
|
+
datasets = [
|
61
|
+
make_moons(noise: 0.3, random_state: 0),
|
62
|
+
make_circles(noise: 0.2, factor: 0.5, random_state: 1),
|
63
|
+
linearly_separable
|
64
|
+
]
|
65
|
+
|
66
|
+
fig = plt.figure(figsize: [27, 9])
|
67
|
+
i = 1
|
68
|
+
all = 0..-1
|
69
|
+
datasets.each do |ds|
|
70
|
+
x, y = *ds
|
71
|
+
x = StandardScaler.new.fit_transform(x)
|
72
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size: 0.4)
|
73
|
+
|
74
|
+
x_min, x_max = np.min(x[all, 0]) - 0.5, np.max(x[all, 0]) + 0.5
|
75
|
+
y_min, y_max = np.min(x[all, 1]) - 0.5, np.max(x[all, 1]) + 0.5
|
76
|
+
|
77
|
+
xx, yy = np.meshgrid(
|
78
|
+
np.linspace(x_min, x_max, ((x_max - x_min)/h).round),
|
79
|
+
np.linspace(y_min, y_max, ((y_max - y_min)/h).round),
|
80
|
+
)
|
81
|
+
mesh_points = np.dstack(PyCall.tuple([xx.ravel(), yy.ravel()]))[0, all, all]
|
82
|
+
|
83
|
+
# just plot the dataset first
|
84
|
+
cm = plt.cm.__dict__[:RdBu]
|
85
|
+
cm_bright = mplc.ListedColormap.new(["#FF0000", "#0000FF"])
|
86
|
+
ax = plt.subplot(datasets.length, classifiers.length + 1, i)
|
87
|
+
# plot the training points
|
88
|
+
ax.scatter(x_train[all, 0], x_train[all, 1], c: y_train, cmap: cm_bright)
|
89
|
+
# and testing points
|
90
|
+
ax.scatter(x_test[all, 0], x_test[all, 1], c: y_test, cmap: cm_bright, alpha: 0.6)
|
91
|
+
|
92
|
+
ax.set_xlim(np.min(xx), np.max(xx))
|
93
|
+
ax.set_ylim(np.min(yy), np.max(yy))
|
94
|
+
ax.set_xticks(PyCall.tuple())
|
95
|
+
ax.set_yticks(PyCall.tuple())
|
96
|
+
i += 1
|
97
|
+
|
98
|
+
# iterate over classifiers
|
99
|
+
names.zip(classifiers).each do |name, clf|
|
100
|
+
ax = plt.subplot(datasets.length, classifiers.length + 1, i)
|
101
|
+
clf.fit(x_train, y_train)
|
102
|
+
scor = clf.score(x_test, y_test)
|
103
|
+
|
104
|
+
# Plot the decision boundary. For that, we will assign a color to each
|
105
|
+
# point in the mesh [x_min, x_max]x[y_min, y_max]
|
106
|
+
begin
|
107
|
+
# not implemented for some
|
108
|
+
z = clf.decision_function(mesh_points)
|
109
|
+
rescue
|
110
|
+
z = clf.predict_proba(mesh_points)[all, 1]
|
111
|
+
end
|
112
|
+
|
113
|
+
# Put the result into a color plot
|
114
|
+
z = z.reshape(xx.shape)
|
115
|
+
ax.contourf(xx, yy, z, cmap: cm, alpha: 0.8)
|
116
|
+
|
117
|
+
# Plot also the training points
|
118
|
+
ax.scatter(x_train[all, 0], x_train[all, 1], c: y_train, cmap: cm_bright)
|
119
|
+
# and testing points
|
120
|
+
ax.scatter(x_test[all, 0], x_test[all, 1], c: y_test, cmap: cm_bright, alpha: 0.6)
|
121
|
+
|
122
|
+
ax.set_xlim(np.min(xx), np.max(xx))
|
123
|
+
ax.set_ylim(np.min(yy), np.max(yy))
|
124
|
+
ax.set_xticks(PyCall.tuple())
|
125
|
+
ax.set_yticks(PyCall.tuple())
|
126
|
+
ax.set_title(name)
|
127
|
+
|
128
|
+
ax.text(np.max(xx) - 0.3, np.min(yy) + 0.3, "%.2f" % scor, size: 15, horizontalalignment: 'right')
|
129
|
+
|
130
|
+
i += 1
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
fig.subplots_adjust(left: 0.02, right: 0.98)
|
135
|
+
plt.show()
|