<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
from sklearn import svm
from sklearn.metrics import accuracy_score, confusion_matrix
import joblib
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
########################################################################################

###############################数据种类为 float64 ###############################
# 故障数据集
abnormal_data=pd.read_csv('/content/abnormal_data.csv', sep=',',header=0)
#abnormal_data_label=pd.read_csv('/content/abnormal_data_label.csv', sep=',',header=0)

#正常数据集
normal_data=pd.read_csv('/content/normal_data.csv', sep=',',header=0)
#normal_data_label=pd.read_csv('/content/normal_data_label.csv', sep=',',header=0)


###############################数据种类为 float64 ###############################


####################### 标签的数据种类为 float64 #################################

#正常数据集标签
normal_data_label=np.zeros(434)

#故障数据集标签
abnormal_data_label=np.ones(440)

####################### 标签的数据种类为 float64#################################

#正常的数据集与数据集标签先等比例划分
x=normal_data
y=normal_data_label
#print(x)
#print(y)

normal_trainset, normal_testset, normal_trainset_label, normal_testset_label = train_test_split(x, y, test_size=0.5, random_state=42)
#print(normal_trainset)
#print(normal_testset)
#print(normal_trainset_label)
#print(normalnormal_testset_label)

#异常的数据集与数据集标签先等比例划分

a=abnormal_data
b=abnormal_data_label
#print(a)
#print(b)

abnormal_trainset, abnormal_testset, abnormal_trainset_label, abnormal_testset_label = train_test_split(a, b, test_size=0.5, random_state=42)


#print(abnormal_trainset)
#print(abnormal_testset)
#print(abnormal_trainset_label)
#print(abnormalnormal_testset_label)
#print(abnormal_data_label.shape)
#abnormal_data.shape

##############合并并生成 train ， test, train label, test label #################

train_dataset = np.concatenate((normal_trainset,abnormal_trainset), axis=0, out=None)
#print(train_dataset)

train_dataset_label = np.concatenate((normal_trainset_label,abnormal_trainset_label), axis=0, out=None)
#print(train_dataset_label)

test_dataset = np.concatenate((normal_testset,abnormal_testset), axis=0, out=None)
#print(test_dataset)

test_dataset_label = np.concatenate((normal_testset_label,abnormal_testset_label), axis=0, out=None)
#print(test_dataset_label)
#print(abnormal_testset.dtypes)
#print(train_dataset_label)

#########################################################################导入模型###############################################################################################


model = svm.SVC(kernel='rbf',degree=2)
model.fit(train_dataset,train_dataset_label)

test_set_predict=model.predict(test_dataset)

accuracy=accuracy_score(test_dataset_label,test_set_predict)
cfm=confusion_matrix(test_dataset_label,test_set_predict)

print("2nd degree polynomial kernel\nAccuracy(normalized):" + str(accuracy)+'\n')
print('confusion matrix:\n',cfm)
from google.colab import drive
drive.mount('/content/drive')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.26526592252133946, 'fraction_numerical': 0.010505581089954037, 'mean_word_length': 13.04147465437788, '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': '642137', 'license': 'MIT', 'n_tokens_mistral': 1065, 'n_tokens_neox': 1047, 'n_words': 133, 'path': 'data_preprocessing_&_SVM.ipynb', 'repo_name': 'shangzechen/write-by-myself', 'size': '3046'}