<jupyter_start><jupyter_text>输入一个pandas dataframe（t-sne数据），看聚类效果  导入所需包<jupyter_code>import json
from pyecharts import Scatter3D<jupyter_output><empty_output><jupyter_text>画图方法<jupyter_code>def echart3D_tsne(pddata,label,x_title,y_title,z_title):
    scatter3D = Scatter3D("T-SNE", width=1200, height=600)
    for lab in set(pddata[label]):
        data=pddata[pddata[label]==lab]
        jsondata = json.loads(data[[x_title,y_title,z_title]].to_json(orient='values'))
        scatter3D.add(str(lab),
                      jsondata,
                      xaxis3d_name='TSNE1',
                      yaxis3d_name='TSNE2',
                      zaxis3d_name='TSNE3',)
    return scatter3D<jupyter_output><empty_output><jupyter_text>一个例子<jupyter_code>import pandas as pd
import random
data = [
    [random.randint(0, 2),
    random.randint(0, 100),
    random.randint(0, 100),
    random.randint(0, 100)] for _ in range(10)
]
pddata = pd.DataFrame(data,columns=['label','t1','t2','t3'])
print(pddata)

s3d=echart3D_tsne(pddata,'label','t1','t2','t3')
s3d<jupyter_output>label  t1  t2  t3
0      1  53  68   9
1      1  76   1  99
2      2  56  28  17
3      0   5  23  85
4      1  29  65  87
5      0  54  38  75
6      1  58  90  94
7      1  60  59  91
8      1  60   6  20
9      2  36  98  38<jupyter_text>GC test<jupyter_code>def one_sample_depth(file):
    '''
    :param file:
    :return:
    '''
    data = []
    with open(file, 'r') as f:
        line = f.readline()
        line = f.readline()
        while line:
            cells = line.strip('\n').split(',')
            name_eles = cells[0].split('-')
            data.append(['{}:{}-{}'.format(*name_eles), int(cells[1])])
            line = f.readline()
    pd_data = pd.DataFrame(data, columns=['loc', 'depth'])
    return pd_data

def get_name(file):
    return (os.path.basename(file).split('.'))[0]

def echart3D(e3d,pddata,label,x_title,y_title,z_title):
#     print(label)
    jsondata = json.loads(pddata[[x_title,y_title,z_title]].to_json(orient='values'))
    e3d.add(str(label),
                  jsondata,
                  xaxis3d_name='GC',
                  yaxis3d_name='LENGTH',
                  zaxis3d_name='DEPTH',)

import os
import pandas as pd


files=['demo_data/controls/FH_1923.stat.csv',
       'demo_data/controls/FH_1925.stat.csv',
       'demo_data/controls/FH_1926.stat.csv',
#        'demo_data/controls/FH_2128.stat.csv',
#        'demo_data/controls/FH_2182.stat.csv',
#        'demo_data/controls/FH_2250.stat.csv'
      ]
gc_length_file = 'demo_data/controls/bed_gc.csv'

glf=pd.read_csv(gc_length_file)
scatter3D = Scatter3D("GC", width=1200, height=600)
for file in files:
    depth = one_sample_depth(file)
    depth['depth'] = depth['depth']/depth['depth'].mean()
    test = pd.merge(glf,depth,how='outer',on='loc',left_index=True)
    label = get_name(file)
#     print(test.head())
    echart3D(scatter3D,test,label,'gc','length','depth')
scatter3D
depth = one_sample_depth(files[0])
test=pd.merge(glf,depth,how='outer',on='loc',left_index=True)
glf.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15061409179056237, 'fraction_numerical': 0.0546218487394958, 'mean_word_length': 3.014267185473411, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '500959', 'license': 'MIT', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1235, 'n_words': 185, 'path': 'pyechart-test.ipynb', 'repo_name': 'btrspg/python-lib-test', 'size': '3094'}