<jupyter_start><jupyter_text>Training a Non-singleton Fuzzy System with SVM and KBF kernelThis notebook shows how to train a Nonsingleton fuzzy system using regularized kernel methods with the KBF kernel  A datasetWe create a dataset and implement a C-SVM with RBF Gaussian kernel<jupyter_code># we create 40 separable points
X, y = make_blobs(n_samples=10, centers=2, random_state=6)
#X, y = make_classification(n_features=2, n_redundant=0, n_informative=2, random_state=1, n_clusters_per_class=1)
#X, y = make_moons(noise=0.3, random_state=0)
#X, y = make_circles(noise=0.2, factor=0.5, random_state=1)

df=pd.DataFrame(data=X,columns=['x1','x2'])
df['y']=y
print(df)

#plot the dataset
groups = df.groupby('y')

# Plot
fig, ax = plt.subplots()
ax.margins(0.05) # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
    ax.plot(group.x1, group.x2, marker='o', linestyle='', ms=12, label=name)
ax.legend()
plt.title("A toy classification dataset")
plt.show()

# A C-SVM with Gaussian RBF kernel
# fit the model, don't regularize for illustration purposes
clf = SVC(C=1000)
clf.fit(X, y)

print("support vector indices :{}".format(clf.support_))
print("number of support vector per class :{}".format(clf.n_support_))
plt.title("A C-SVM with RBF kernel")
plot_decision_function(X,y,clf)<jupyter_output>x1        x2  y
0   7.058808 -8.355412  1
1   5.370422 -2.447152  0
2   9.496494 -3.790297  0
3  10.488484 -2.758582  0
4   6.535711 -9.036916  1
5   8.075024 -4.259496  0
6   6.500710 -9.321196  1
7   8.984267 -4.874497  0
8   6.088701 -7.928330  1
9   6.779391 -7.353477  1<jupyter_text>Non-singleton fuzzification of the datasetIn this step we perform a nonsingleton fuzzification for each value in the dataset, e.g., in row 0, the value 6.37 is transformed into a Gaussian fuzzy set with μ=6.37 and σ value. :Heuristic for estimation of the individual $\mu_{i,d}, \sigma_{i,d}, i=1,\dots, N, d=1,\dots, D$ values for each data point: * $\mu_{i,d} = x_{i,d}$* $\sigma_{i,d}\sim \mathcal{N}(\rho, \theta_{d,c})$ We used:* $\theta_{d,c}=5\%* std(d,c)$ ,i.e a percentage of the std of dimension $d$ and class $c$* $\rho \sim \mathcal{U}[std(d,c)/10, std(d,c)/2]$<jupyter_code>#nonsingleton fuzzyfication of the dataset
fuzzy_data = FuzzyData(data=df, target='y')
fuzzy_data.non_singleton_fuzzification_classification(constant_std=True,std_proportion=0.5)
#printing std values
ax = sns.heatmap(fuzzy_data.get_std_values(), cmap="coolwarm")


#########
#fuzzy rules
M=X.shape[0] #number of rules
rules_antecedents=fuzzy_data.get_fuzzydata()

elems=np.arange(np.min(X),np.max(X), (np.max(X)-np.min(X))/1000)

fig, list_axes = plt.subplots(nrows=M, figsize=(8, 9))
i=0
for row in rules_antecedents:
    for val in row:
        mu,sigma=val.get_membership_function_params()
        list_axes[i].plot(elems,np.exp(-0.5*np.square(elems - mu)/sigma**2), 'b', linewidth=1.5)
    i=i+1
    
for ax in list_axes:
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.get_xaxis().tick_bottom()
    ax.get_yaxis().tick_left()

plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Kernel Method learning: Training a Non-singleton FS with a C-SVM with KBF kernel<jupyter_code>#computing the kernel matrix
X_train = fuzzy_data.get_fuzzydata()
y_train = fuzzy_data.get_target()

K = gram_matrix_KBF_kernel(X_train, X_train, 10)
sns.heatmap(K)
plt.show()

#training with a C-SVM
svc_p = SVC(kernel='precomputed',C=0.01)
svc_p.fit(K,y_train)

print("indices in training data that became rule-antecedents:{}".format(svc_p.support_))
print("number of rule-antecedents per class :{}".format(svc_p.n_support_))
#print("support vectors class 0 :{}".format(clf.support_vectors_[:, 0]))
#print("support vectors class 1 :{}".format(clf.support_vectors_[:, 1]))
print("dual coefficients : {}".format(svc_p.dual_coef_))

print(svc_p)

print(fuzzy_data.get_data().to_numpy()[:,:-1].shape)
#print(fuzzy_data.get_data().to_numpy()[:,:-1])
plot_decision_function_kernel(X,X_train,y_train,svc_p)<jupyter_output><empty_output><jupyter_text>Kernel Method learning: Training a Non-singleton FS with a nu-SVM with KBF kernel<jupyter_code>#computing the kernel matrix
X_train = fuzzy_data.get_fuzzydata()
y_train = fuzzy_data.get_target()

K = gram_matrix_KBF_kernel(X_train, X_train, 0.01)
sns.heatmap(K)
plt.show()

#training with a nu-SVM
svc_p = NuSVC(kernel='precomputed',nu=0.1)
svc_p.fit(K,y_train)

print("indices in training data that became rule-antecedents:{}".format(svc_p.support_))
print("number of rule-antecedents per class :{}".format(svc_p.n_support_))
#print("support vectors class 0 :{}".format(clf.support_vectors_[:, 0]))
#print("support vectors class 1 :{}".format(clf.support_vectors_[:, 1]))
#print("dual coefficients : {}".format(svc_p.dual_coef_))

print(svc_p)

print(fuzzy_data.get_data().to_numpy()[:,:-1].shape)
#print(fuzzy_data.get_data().to_numpy()[:,:-1])
plot_decision_function_kernel(X,X_train,y_train,svc_p)

#plot kernel matrix with only the support vectors
sns.heatmap(K[svc_p.support_[:,None], svc_p.support_[None,:]])
plt.show()<jupyter_output><empty_output><jupyter_text>Setting the fuzzy rule-antecedents<jupyter_code>#fuzzy rules
M=np.sum(svc_p.n_support_) #number of rules
rules_antecedents=X_train[svc_p.support_,:]

elems=np.arange(np.min(X),np.max(X), (np.max(X)-np.min(X))/1000)

fig, list_axes = plt.subplots(nrows=M, figsize=(8, 9))
i=0
for row in rules_antecedents:
    for val in row:
        mu,sigma=val.get_membership_function_params()
        list_axes[i].plot(elems,np.exp(-0.5*np.square(elems - mu)/sigma**2), 'b', linewidth=1.5)
    i=i+1
    
for ax in list_axes:
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.get_xaxis().tick_bottom()
    ax.get_yaxis().tick_left()

plt.tight_layout()
plt.show()

#plot all the MFs per variable
D=X_train.shape[1]  #number of dimensions
#fuzzy rules
M=np.sum(svc_p.n_support_) #number of rules
rules_antecedents=X_train[svc_p.support_,:]

#fig, list_axes = plt.subplots(nrows=D, figsize=(8, 9))

i=0
for d in range(0,D):
    params=[fuzzySet.get_membership_function_params()  for fuzzySet in rules_antecedents[:,d]]
    elems=np.arange(np.min(X[:,d]),np.max(X[:,d]), (np.max(X[:,d])-np.min(X[:,d]))/1000)
    [plt.plot(elems,np.exp(-0.5*np.square(elems - mu)/sigma**2), 'b', linewidth=1.5) for mu, sigma in params]
    plt.title("dimension : {}".format(d))
    plt.show()


#plt.tight_layout()


#############################
#############################
#including the kernel parameter within the sigma of the antecedents
i=0
for d in range(0,D):
    params=[fuzzySet.get_membership_function_params()  for fuzzySet in rules_antecedents[:,d]]
    elems=np.arange(np.min(X[:,d]),np.max(X[:,d]), (np.max(X[:,d])-np.min(X[:,d]))/1000)
    [plt.plot(elems,np.exp(-0.5*np.square(elems - mu)/sigma**2), 'b', linewidth=1.5) for mu, sigma in params]
    plt.title("dimension : {}".format(d))
    plt.show()<jupyter_output><empty_output><jupyter_text>Kernel Method learning: Training a Non-singleton FS with a nu-SVM with KBF kernel and  cross-validation<jupyter_code>X_train = fuzzy_data.get_fuzzydata()
y_train = fuzzy_data.get_target()

#pipe
#nuParam=0.1
pipe = Pipeline([
    ('KBF', KBFkernel()),
    ('svm', NuSVC())])

#grid search over KBF kernel parameter   
cv_params = dict([
    ('KBF__KBF_param', 2.0**np.arange(-5,3)),
    ('svm__nu', np.arange(0.3,0.4,0.1)),
    ('svm__kernel', ['precomputed'])
    # ('svm__C', 10.0**np.arange(-3,3)),
])

model = GridSearchCV(pipe, cv_params, cv=5, verbose=1, n_jobs=-1)
model.fit(X_train, y_train)
    
#training the model with the best parameters
print(model.best_params_)
best_params=model.best_params_
print("best model: ", model.best_estimator_)
print("best model: ", model.best_estimator_['svm'])

clf=model.best_estimator_['svm']
    
#clf = SVC(kernel='precomputed',C=best_params['svm__C'])
#clf = NuSVC(kernel='precomputed',nu=best_params['svm__nu'])
#K = gram_matrix_KBF_kernel(X_train, X_train, best_params['KBF__KBF_param'])
#clf.fit(K, y_train)

print(clf.support_)
print(clf.n_support_)
print(clf.dual_coef_)
print(clf)

print(fuzzy_data.get_data().to_numpy()[:,:-1].shape)
plot_decision_function_kernel(X,X_train,y_train,clf)
svc_p=clf
#fuzzy rules
M=np.sum(svc_p.n_support_) #number of rules
rules_antecedents=X_train[svc_p.support_,:]

elems=np.arange(np.min(X),np.max(X), (np.max(X)-np.min(X))/100)

fig, list_axes = plt.subplots(nrows=M, figsize=(8, M))
i=0
for row in rules_antecedents:
    for val in row:
        mu,sigma=val.get_membership_function_params()
        list_axes[i].plot(elems,np.exp(-0.5*np.square(elems - mu)/sigma**2), 'b', linewidth=1.5)
    i=i+1
    
for ax in list_axes:
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.get_xaxis().tick_bottom()
    ax.get_yaxis().tick_left()

plt.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1703272075302555, 'fraction_numerical': 0.035522187359928285, 'mean_word_length': 5.625835189309577, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '329766', 'license': 'MIT', 'n_tokens_mistral': 3824, 'n_tokens_neox': 3596, 'n_words': 692, 'path': 'notebooks/9-(old) Training a Non-singleton fuzzy system with a SVM with KBF kernel for Classification.ipynb', 'repo_name': 'jorjasso/fuzzy-kernel-machines', 'size': '8924'}