<jupyter_start><jupyter_text>导入数据及转为array形式<jupyter_code>import pandas as pd
import numpy as np

LassoClassificationFeature = pd.read_excel('./data/分类数据集.xlsx')
RandomForestLogisticsFeature = pd.read_excel('./data/回归数据集.xlsx')

tempArray_Lasso = np.array(LassoClassificationFeature)
tempArray_RandomForest = np.array(RandomForestLogisticsFeature)<jupyter_output><empty_output><jupyter_text>半监督学习——分类<jupyter_code># coding:utf-8
import random
import numpy as np
import sklearn.svm as svm
from sklearn.datasets.samples_generator import make_classification
from sklearn.externals import joblib
import warnings; warnings.filterwarnings(action='ignore')

class TSVM(object):
    def __init__(self, kernel='linear'):
        self.Cl, self.Cu = 1.5, 0.001
        self.kernel = kernel
        self.clf = svm.SVC(C=1.5, kernel=self.kernel)

    def train(self, X1, Y1, X2):
        N = len(X1) + len(X2)
        # 样本权值初始化
        sample_weight = np.ones(N)
        sample_weight[len(X1):] = self.Cu

        # 用已标注部分训练出一个初始SVM
        self.clf.fit(X1, Y1)
        
        # 对未标记样本进行标记
        Y2 = self.clf.predict(X2)
        Y2 = Y2.reshape(-1,1)
        
        X = np.vstack([X1, X2])
        Y = np.vstack([Y1, Y2])
        
        # 未标记样本的序号
        Y2_id = np.arange(len(X2))
        
        while self.Cu < self.Cl:
            # 重新训练SVM, 之后再寻找易出错样本不断调整
            self.clf.fit(X, Y, sample_weight=sample_weight)
            while True:
                # print(X2)
                print(type(X2))
                Y2_decision = self.clf.decision_function(X2)   # 参数实例到决策超平面的距离
                Y2 = Y2.reshape(-1)
                print(len(Y2))
                print(Y2_decision)
                epsilon = 1 - Y2 * Y2_decision
                negative_max_id = Y2_id[epsilon==min(epsilon)]

                print(epsilon[negative_max_id][0])
                if epsilon[negative_max_id][0] > 0:
                    # 寻找很可能错误的未标记样本，改变它的标记成其他标记
                    pool = list(set(np.unique(Y1))-set(Y2[negative_max_id]))
                    Y2[negative_max_id] = random.choice(pool)
                    Y2 = Y2.reshape(-1, 1)
                    Y = np.vstack([Y1, Y2])
                    
                    self.clf.fit(X, Y, sample_weight=sample_weight)
                else:
                    break
            self.Cu = min(2*self.Cu, self.Cl)
            sample_weight[len(X1):] = self.Cu

    def score(self, X, Y):
        return self.clf.score(X, Y)

    def predict(self, X):
        return self.clf.predict(X)

    def save(self, path='./TSVM.model'):
        joblib.dump(self.clf, path)

    def load(self, model_path='./TSVM.model'):
        self.clf = joblib.load(model_path)

    def plot_confusion_matrix(self, X, Y):
        plot_confusion_matrix(self.clf,X,Y)

if __name__ == '__main__':
    tempArray = np.array(tempArray_Lasso)[:,:]

    #43很好
    split_num = 92

    #18效果还可以25：f1_score: 0.7692307692307692
    test_num = 25

    train_data = tempArray[test_num:split_num,5:]
    train_labels = np.array(np.array(tempArray)[test_num:split_num,4].astype(int)).reshape(-1,1)

    test_data = tempArray[:test_num,5:]
    test_labels = np.array(np.array(tempArray)[:test_num,4].astype(int)).reshape(-1,1)

    X2 = tempArray[split_num:,5:]
    Y2_labeled = np.array(np.array(tempArray)[split_num:,4].astype(int)).reshape(-1,1)

    model = TSVM()
    model.train(train_data, train_labels, X2)

    # Y2_hat = model.predict(X2)
    accuracy = model.score(test_data, test_labels)
    print(accuracy)
    
    pred = np.array(model.predict(test_data)).reshape(-1,1)
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score

# print(test_labels)
# print(pred)

print("accuracy_score:",precision_score(test_labels, pred))
print("precision_score:",precision_score(test_labels, pred))
print("recall_score:",recall_score(test_labels, pred))
print("f1_score:",f1_score(test_labels, pred))
model.plot_confusion_matrix(test_data,test_labels)<jupyter_output><empty_output><jupyter_text>半监督学习——回归任务<jupyter_code>import numpy as np
import random
from sklearn.metrics import *
from sklearn import datasets
from sklearn.metrics import confusion_matrix
from sklearn.semi_supervised import LabelPropagation
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt


tempArray = np.array(tempArray_Lasso)[:,:]
print(len(tempArray))
data = tempArray[:,5:]

labels = np.array(tempArray)[:,3].astype(int)


split_num = 19

train_data = tempArray[split_num:,5:]
# np.save('X.npy',data)
# np.save('y.npy',labels)
train_labels = np.array(tempArray)[split_num:,3].astype(int)

# print(data)
masked_labels = np.array(tempArray)[:,3].astype(int)

# train_X,test_X,train_y,test_y = train_test_split(data,labels,test_size=0.2,random_state=4)

#RUN THE MODEL
model = LabelPropagation()
# model.fit(train_X, train_y)
model.fit(train_data,train_labels)
pred = np.array(model.predict(data))

# print(labels[masked_labels] == -1)

#SEPARATE PREDICTED SAMPLES
# labels = np.array(labels[masked_labels != -1]).astype(int)
# pred = pred[masked_labels != -1]

labels = np.array(tempArray)[:split_num,3].astype(int)
pred = pred[0:split_num]

print(len(pred))
print(labels)
print(pred)

print('Mean squared error: %.2f'
    % mean_squared_error(labels, pred))
print('Coefficient of determination: %.2f'
    % r2_score(labels, pred))<jupyter_output>138
19
[18 16 16 20 13 15 11 17 25 22 17 17 16 17 16  8 18 15  8]
[19 12 18 20 10 11  7 21 16 25 12 12 16 11 21 12 15 15 12]
Mean squared error: 16.84
Coefficient of determination: -0.04
{'fraction_non_alphanumeric': 0.12300017202821263, 'fraction_numerical': 0.035953896439015996, 'mean_word_length': 2.8811748998664886, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '270351', 'license': 'MIT', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2187, 'n_words': 358, 'path': 'Project-2/Semi_supervised_learning.ipynb', 'repo_name': 'JasonCZH4/SCNU-CS-2018-DataMining', 'size': '5813'}