The problem here is the number of columns. My df shape is [1428 rows x 3100 columns]
#%%
import pandas as pd
df = pd.read_csv("doc_vector.csv")
#%%
df = df.drop(columns=["document", "keywords"])
#%%
X = df.drop(columns=["category"]).values
y = df.filter(["category"]).values.ravel()
#%%
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.33,
random_state=42
)
#%%
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
'C': [1, 10, 100, 1000]},
{'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]
svc = SVC(gamma='auto')
clf = GridSearchCV(svc, parameters)
clf.fit(X_train, y_train)
sorted(clf.cv_results_.keys())
"""
OUTPUT:
['mean_fit_time',
'mean_score_time',
'mean_test_score',
'param_C',
'param_gamma',
'param_kernel',
'params',
'rank_test_score',
'split0_test_score',
'split1_test_score',
'split2_test_score',
'split3_test_score',
'split4_test_score',
'std_fit_time',
'std_score_time',
'std_test_score']
"""
#%%
clf.cv_results_
"""
OUTPUT:
{'mean_fit_time': array([1.81106772, 2.2082305 , 1.76409402, 1.627356 , 1.75857201,
1.56271949, 1.75669813, 1.56454921, 1.49936023, 1.51295609,
1.53478389, 1.54050641]),
'std_fit_time': array([0.02475751, 0.01825799, 0.03386408, 0.01870898, 0.03761543,
0.01331777, 0.03751674, 0.01426082, 0.01782674, 0.03306172,
0.04617239, 0.03143336]),
'mean_score_time': array([0.33980088, 0.41657252, 0.33987026, 0.32597055, 0.3384655 ,
0.32319117, 0.33914285, 0.32390838, 0.31457753, 0.31692972,
0.32040634, 0.32276688]),
'std_score_time': array([0.00426356, 0.00054301, 0.00069592, 0.00271754, 0.00489743,
0.00426302, 0.00481799, 0.00548289, 0.00591126, 0.00769205,
0.00599371, 0.01014503]),
'param_C': masked_array(data=[1, 1, 10, 10, 100, 100, 1000, 1000, 1, 10, 100, 1000],
mask=[False, False, False, False, False, False, False, False,
False, False, False, False],
fill_value='?',
dtype=object),
'param_gamma': masked_array(data=[0.001, 0.0001, 0.001, 0.0001, 0.001, 0.0001, 0.001,
0.0001, --, --, --, --],
mask=[False, False, False, False, False, False, False, False,
True, True, True, True],
fill_value='?',
dtype=object),
'param_kernel': masked_array(data=['rbf', 'rbf', 'rbf', 'rbf', 'rbf', 'rbf', 'rbf', 'rbf',
'linear', 'linear', 'linear', 'linear'],
mask=[False, False, False, False, False, False, False, False,
False, False, False, False],
fill_value='?',
dtype=object),
'params': [{'C': 1, 'gamma': 0.001, 'kernel': 'rbf'},
{'C': 1, 'gamma': 0.0001, 'kernel': 'rbf'},
{'C': 10, 'gamma': 0.001, 'kernel': 'rbf'},
{'C': 10, 'gamma': 0.0001, 'kernel': 'rbf'},
{'C': 100, 'gamma': 0.001, 'kernel': 'rbf'},
{'C': 100, 'gamma': 0.0001, 'kernel': 'rbf'},
{'C': 1000, 'gamma': 0.001, 'kernel': 'rbf'},
{'C': 1000, 'gamma': 0.0001, 'kernel': 'rbf'},
{'C': 1, 'kernel': 'linear'},
{'C': 10, 'kernel': 'linear'},
{'C': 100, 'kernel': 'linear'},
{'C': 1000, 'kernel': 'linear'}],
'split0_test_score': array([0.78645833, 0.63020833, 0.85416667, 0.80208333, 0.84375 ,
0.84895833, 0.84375 , 0.83854167, 0.83854167, 0.83854167,
0.83854167, 0.83854167]),
'split1_test_score': array([0.79057592, 0.64397906, 0.85863874, 0.80104712, 0.85340314,
0.85340314, 0.85863874, 0.85340314, 0.85863874, 0.85863874,
0.85863874, 0.85863874]),
'split2_test_score': array([0.79057592, 0.64921466, 0.85863874, 0.81151832, 0.85863874,
0.86387435, 0.85863874, 0.85863874, 0.85863874, 0.85863874,
0.85863874, 0.85863874]),
'split3_test_score': array([0.80628272, 0.64397906, 0.86387435, 0.80628272, 0.86387435,
0.86910995, 0.85863874, 0.86387435, 0.86387435, 0.86387435,
0.86387435, 0.86387435]),
'split4_test_score': array([0.77486911, 0.60732984, 0.84816754, 0.78534031, 0.84293194,
0.84816754, 0.84293194, 0.84816754, 0.84293194, 0.84293194,
0.84293194, 0.84293194]),
'mean_test_score': array([0.7897524 , 0.63494219, 0.85669721, 0.80125436, 0.85251963,
0.85670266, 0.85251963, 0.85252509, 0.85252509, 0.85252509,
0.85252509, 0.85252509]),
'std_test_score': array([0.01006947, 0.01517817, 0.00525755, 0.00877064, 0.00819737,
0.00835564, 0.00749881, 0.00873473, 0.00991084, 0.00991084,
0.00991084, 0.00991084]),
'rank_test_score': array([11, 12, 2, 10, 8, 1, 8, 3, 3, 3, 3, 3], dtype=int32)}
"""
#%%
clf.best_params_
"""
OUTPUT:
{'C': 100, 'gamma': 0.0001, 'kernel': 'rbf'}
"""
#%%
clf.best_estimator_
"""
OUTPUT:
SVC(C=100, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma=0.0001, kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
"""
#%%
import m2cgen as m2c
code = m2c.export_to_c(clf.best_estimator_)
print(code)
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
<ipython-input-24-43271f44552b> in <module>
1 import m2cgen as m2c
2
----> 3 code = m2c.export_to_c(clf.best_estimator_)
4
5 print(code)
~/PycharmProjects/jupyter_test/venv/lib/python3.6/site-packages/m2cgen/exporters.py in export_to_c(model, indent)
64 """
65 interpreter = interpreters.CInterpreter(indent=indent)
---> 66 return _export(model, interpreter)
67
68
~/PycharmProjects/jupyter_test/venv/lib/python3.6/site-packages/m2cgen/exporters.py in _export(model, interpreter)
197 def _export(model, interpreter):
198 assembler_cls = assemblers.get_assembler_cls(model)
--> 199 model_ast = assembler_cls(model).assemble()
200 return interpreter.interpret(model_ast)
~/PycharmProjects/jupyter_test/venv/lib/python3.6/site-packages/m2cgen/assemblers/svm.py in assemble(self)
37 def assemble(self):
38 if self._output_size > 1:
---> 39 return self._assemble_multi_class_output()
40 else:
41 return self._assemble_single_output()
~/PycharmProjects/jupyter_test/venv/lib/python3.6/site-packages/m2cgen/assemblers/svm.py in _assemble_multi_class_output(self)
66 n_support_len = len(n_support)
67
---> 68 kernel_exprs = self._apply_kernel(support_vectors, to_reuse=True)
69
70 support_ranges = []
~/PycharmProjects/jupyter_test/venv/lib/python3.6/site-packages/m2cgen/assemblers/svm.py in _apply_kernel(self, support_vectors, to_reuse)
100 kernel_exprs = []
101 for v in support_vectors:
--> 102 kernel = self._kernel_fun(v)
103 kernel_exprs.append(ast.SubroutineExpr(kernel, to_reuse=to_reuse))
104 return kernel_exprs
~/PycharmProjects/jupyter_test/venv/lib/python3.6/site-packages/m2cgen/assemblers/svm.py in _rbf_kernel(self, support_vector)
113 ]
114 kernel = utils.apply_op_to_expressions(ast.BinNumOpType.ADD,
--> 115 *elem_wise)
116 kernel = utils.mul(self._neg_gamma_expr, kernel)
117 return ast.ExpExpr(kernel)
~/PycharmProjects/jupyter_test/venv/lib/python3.6/site-packages/m2cgen/assemblers/utils.py in apply_op_to_expressions(op, to_reuse, *exprs)
55 apply_bin_op(current_expr, rest_exprs[0], op), *rest_exprs[1:])
56
---> 57 result = _inner(apply_bin_op(exprs[0], exprs[1], op), *exprs[2:])
58 result.to_reuse = to_reuse
59 return result
~/PycharmProjects/jupyter_test/venv/lib/python3.6/site-packages/m2cgen/assemblers/utils.py in _inner(current_expr, *rest_exprs)
53
54 return _inner(
---> 55 apply_bin_op(current_expr, rest_exprs[0], op), *rest_exprs[1:])
56
57 result = _inner(apply_bin_op(exprs[0], exprs[1], op), *exprs[2:])
... last 1 frames repeated, from the frame below ...
~/PycharmProjects/jupyter_test/venv/lib/python3.6/site-packages/m2cgen/assemblers/utils.py in _inner(current_expr, *rest_exprs)
53
54 return _inner(
---> 55 apply_bin_op(current_expr, rest_exprs[0], op), *rest_exprs[1:])
56
57 result = _inner(apply_bin_op(exprs[0], exprs[1], op), *exprs[2:])
RecursionError: maximum recursion depth exceeded