require 'pycall' pymath = PyCall.import_module('math') pymath.pi pymath.pi.class pymath.sin(30 * Math::PI / 180) Math.sin(30 * Math::PI / 180) unless File.exist?('iris.data') system("curl -sfSLO https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") end require 'pandas' iris = Pandas.read_csv('iris.data', names: %w[SepalLength SepalWidth PetalLength PetalWidth Species]) nil iris.head iris.tail iris.describe # The last `.T` means transpose the dataframe. # The reason why transpose the result is just the readability of the result. iris.groupby('Species').describe.T require 'matplotlib/iruby' Matplotlib::IRuby.activate Matplotlib::Pyplot.ioff sns = PyCall.import_module('seaborn') sns.pairplot(iris, hue: 'Species', markers: %w[o s D], diag_kws: {bins: 25}) iris['SepalRatio'] = iris['SepalLength'] / iris['SepalWidth'] iris['PetalRatio'] = iris['PetalLength'] / iris['PetalWidth'] iris[['SepalRatio', 'PetalRatio', 'Species']].groupby('Species').describe.T sns.pairplot(iris, hue: 'Species', markers: %w[o s D], diag_kws: {bins: 25}) PCA = PyCall.import_module('sklearn.decomposition').PCA pca = PCA.new x_trans = pca.fit_transform(iris.iloc[0..-1, 0..3]) nil iris_trans = Pandas::DataFrame.new(data: x_trans, columns: %w[X0 X1 X2 X3]) iris_trans['Species'] = iris['Species'] sns.pairplot(iris_trans, hue: 'Species', markers: %w[o s D], diag_kws: {bins: 25}) ModelSelection = PyCall.import_module('sklearn.model_selection') x_train, x_test, y_train, y_test = ModelSelection.train_test_split( x_trans, iris['Species'], test_size: 0.4, random_state: 13 ) nil SVC = PyCall.import_module('sklearn.svm').SVC model = SVC.new model.fit(x_train, y_train) model.score(x_test, y_test)