<jupyter_start><jupyter_text><jupyter_code>import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC #For SVM
sns.set(style="ticks")
np.random.seed(1)
margin = True
support_vector = True

sns.set(style="ticks")
df = sns.load_dataset("penguins")
sns.pairplot(df, hue='species')
df.to_csv('to_csv_out.csv')
df = df.copy().dropna()

#dfa[["species"]]
df.loc[df["species"] == 'Adelie',["species"]] = 0
df.loc[df["species"] == 'Chinstrap',["species"]] = 0
df.loc[df["species"] == 'Gentoo',["species"]] = 1
#dfa["species"][dfa["species"] == 'Adelie'] = 0
#dfa["species"][dfa["species"] == 'Chinstrap'] = 0
#dfa["species"][dfa["species"] == 'Gentoo'] = 1
y = df["species"].astype('int8').values
x1label = "bill_depth_mm"
x2label = "flipper_length_mm"
X1 = df[[x1label]].values
X2 = df[[x2label]].values
X = df[[x1label, x2label]].values
# X1 = np.random.randn(100,2) + 1
# X2 = np.random.randn(100,2) + 6
# X = np.vstack((X1,X2))
# y = np.vstack((np.ones((100,1)), -1*np.ones((100,1)))).ravel()

fig, ax = plt.subplots(figsize=(8,8))
cnt = 0
datalen = len(y)
for cnt in range(datalen):
    if y[cnt] == 0:
       ax.scatter(X1[cnt],X2[cnt], marker='o', s=50, c="red")
    else:
       ax.scatter(X1[cnt],X2[cnt], marker='o', s=50, c="green")
ax.grid()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# create grid to evaluate model
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
XX, YY = np.meshgrid(xx, yy)
xy = np.vstack([XX.ravel(), YY.ravel()]).T

# clf is our classifier. Ignore the variable C and kernel for now
clf = SVC(C=10000, kernel='linear')
# fitting the model to our data
clf.fit(X,y)
#plotting the decision boundary
Z = clf.decision_function(xy).reshape(XX.shape)

# plot decision boundary and margins
if margin:
    ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
                linestyles=['--', '-', '--'])
else:
    ax.contour(XX, YY, Z, colors='k', levels=[0], alpha=0.5,
                linestyles=['-'])
    
# plot support vectors
if support_vector:
    ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], 
                s=140, linewidth=1, 
                facecolors='none', edgecolors='k')

plt.xlabel(x1label, fontsize=15)
plt.ylabel(x2label, fontsize=15)
plt.figsize=(8,8)

plt.show()
# 保存先のファイル名(PNG形式)
fname = "cubic.png"

# ファイルを保存
fig.savefig(fname, dpi = 512,
            facecolor = "white", tight_layout = True)
X
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

sns.set(style="ticks")
df = sns.load_dataset("penguins")
sns.pairplot(df, hue='species')
df.to_csv('to_csv_out.csv')
df.columns
dfa = df.dropna()
#dfa[["species"]]
dfa["species"][dfa["species"] == 'Adelie'] = 0
dfa["species"][dfa["species"] == 'Chinstrap'] = 0
dfa["species"][dfa["species"] == 'Gentoo'] = 1
y = dfa["species"].astype('int8').values
x1label = "bill_depth_mm"
x2label = "flipper_length_mm"
x = dfa[[x1label, x2label]].values
y
#data = df[["species"]]
#y = data[:,0].astype(int)
#x = data[:,1:3]
from sklearn import svm
from sklearn.metrics import confusion_matrix

clf = svm.SVC(kernel='linear')
# 学習させる
clf.fit(x, y)


print('予測した結果',clf.predict(x))
x[:, 0]
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt

plot_decision_regions(x,y, clf=clf,  res=0.02)
plt.xlabel(x1label)
plt.ylabel(x2label)
plt.show()
data_test = np.loadtxt('data_test.csv', delimiter=',') 
test_y = data_test[:,0].astype(int)
test_x = data_test[:,1:3]

print('正解',test_y)
# 学習したデータと比較して推測する
print('予測した結果',clf.predict(test_x))
print('予測した結果の正解率',clf.score(test_x, test_y))

pyplot.style.use('ggplot')

x_bind = np.vstack((test_x,x))
y_bind = np.hstack((test_y,y))

plot_decision_regions(x_bind, y_bind, clf=clf,  res=0.02)
pyplot.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19864794591783672, 'fraction_numerical': 0.029121164846593862, 'mean_word_length': 5.096671949286846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '351370', 'license': 'Apache-2.0', 'n_tokens_mistral': 1763, 'n_tokens_neox': 1679, 'n_words': 313, 'path': 'penguins002.ipynb', 'repo_name': 'tamtak38/ibc', 'size': '3846'}