Hướng dẫn heatmap for large dataset python - bản đồ nhiệt cho tập dữ liệu lớn python

Do các ý kiến ​​cho câu trả lời khác của tôi, OP có một câu hỏi khác liên quan đến việc tìm kiếm các cụm 2D. Đây là một số câu trả lời.

Được lấy từ thư viện của tôi EEGPY, tôi sử dụng một phương thức find_clusters. Nó thực hiện một cuộc đi bộ qua một mảng 2D, tìm thấy tất cả các cụm ở trên / dưới một ngưỡng nhất định.

Đây là mã của tôi:

import pylab as plt
import numpy as np
from Queue import Queue


def find_clusters(ar,thres,cmp_type="greater"):
    """For a given 2d-array (test statistic), find all clusters which
are above/below a certain threshold.
"""
    if not cmp_type in ["lower","greater","abs_greater"]:
        raise ValueError("cmp_type must be in [\"lower\",\"greater\",\"abs_greater\"]")
    clusters = []
    if cmp_type=="lower":
        ar_in = (arthres).astype(np.bool)
    else: #cmp_type=="abs_greater":
        ar_in = (abs(ar)>thres).astype(np.bool)

    already_visited = np.zeros(ar_in.shape,np.bool)
    for i_s in range(ar_in.shape[0]): #i_s wie i_sample
        for i_f in range(ar_in.shape[1]):
            if not already_visited[i_s,i_f]:
                if ar_in[i_s,i_f]:
                    #print "Anzahl cluster:", len(clusters)
                    mask = np.zeros(ar_in.shape,np.bool)
                    check_queue = Queue()
                    check_queue.put((i_s,i_f))
                    while not check_queue.empty():
                        pos_x,pos_y = check_queue.get()
                        if not already_visited[pos_x,pos_y]:
                            #print pos_x,pos_y
                            already_visited[pos_x,pos_y] = True
                            if ar_in[pos_x,pos_y]:
                                mask[pos_x,pos_y] = True
                                for coords in [(pos_x-1,pos_y),(pos_x+1,pos_y),(pos_x,pos_y-1),(pos_x,pos_y+1)]: #Direct Neighbors
                                    if 0<=coords[0]

trong đó cung cấp một hình ảnh của biểu mẫu:

Hướng dẫn heatmap for large dataset python - bản đồ nhiệt cho tập dữ liệu lớn python

clusters là danh sách các kết nối 2D boolean (Đúng / Sai). Mỗi mảng đại diện cho một cụm, trong đó mỗi giá trị boolean cho biết liệu một "điểm" cụ thể là một phần của cụm này. Bạn có thể sử dụng nó trong bất kỳ phân tích thêm.

CHỈNH SỬA

Bây giờ với một số niềm vui hơn trên các cụm

import pylab as plt
import numpy as np
from Queue import Queue


def find_clusters(ar,thres,cmp_type="greater"):
    """For a given 2d-array (test statistic), find all clusters which
are above/below a certain threshold.
"""
    if not cmp_type in ["lower","greater","abs_greater"]:
        raise ValueError("cmp_type must be in [\"lower\",\"greater\",\"abs_greater\"]")
    clusters = []
    if cmp_type=="lower":
        ar_in = (arthres).astype(np.bool)
    else: #cmp_type=="abs_greater":
        ar_in = (abs(ar)>thres).astype(np.bool)

    already_visited = np.zeros(ar_in.shape,np.bool)
    for i_s in range(ar_in.shape[0]): #i_s wie i_sample
        for i_f in range(ar_in.shape[1]):
            if not already_visited[i_s,i_f]:
                if ar_in[i_s,i_f]:
                    #print "Anzahl cluster:", len(clusters)
                    mask = np.zeros(ar_in.shape,np.bool)
                    check_queue = Queue()
                    check_queue.put((i_s,i_f))
                    while not check_queue.empty():
                        pos_x,pos_y = check_queue.get()
                        if not already_visited[pos_x,pos_y]:
                            #print pos_x,pos_y
                            already_visited[pos_x,pos_y] = True
                            if ar_in[pos_x,pos_y]:
                                mask[pos_x,pos_y] = True
                                for coords in [(pos_x-1,pos_y),(pos_x+1,pos_y),(pos_x,pos_y-1),(pos_x,pos_y+1)]: #Direct Neighbors
                                    if 0<=coords[0]5, clusters) #Only take clusters with five or more items
large_clusters = sorted(large_clusters, key=lambda cl: -cl.sum())

plot_data = np.ma.masked_equal(data[:,:], 0)

plt.subplots_adjust(left=0.1, bottom=0.15, right=0.99, top=0.95)
plt.imshow(plot_data, cmap=plt.cm.get_cmap("Reds"), interpolation="nearest", aspect = "auto", 
           vmin=0, extent=[-0.5,plot_data.shape[1]-0.5, plot_data.shape[0] - 0.5, -0.5])
plt.colorbar()

for cl in large_clusters:
    plt.contour(cl.astype(np.int),[.5], colors="k", lw=2)
plt.xticks(np.arange(0, len(labels)+1), labels, rotation=90, va="top", ha="center")

print "Summary of all large clusters:\n"
print "#\tSize\tIn regions"
for i, cl in enumerate(large_clusters):
    print "%i\t%i\t" % (i, cl.sum()),
    regions_in_cluster = np.where(np.any(cl, axis=0))[0]
    min_region = labels[min(regions_in_cluster)]
    max_region = labels[max(regions_in_cluster)]
    print "%s to %s" % (min_region, max_region)

plt.xlim(-0.5,plot_data.shape[1]-0.5)

plt.show()

Tôi lọc tất cả các cụm có hơn năm điểm bao gồm. Tôi chỉ vẽ những thứ này. Bạn có thể sử dụng tổng số data bên trong mỗi cụm. Sau đó tôi sắp xếp các cụm lớn này theo kích thước của chúng, giảm dần.

Cuối cùng, tôi in một bản tóm tắt của tất cả các cụm lớn, bao gồm tên của tất cả các cụm mà chúng đi qua.

Hướng dẫn heatmap for large dataset python - bản đồ nhiệt cho tập dữ liệu lớn python

Những gì có thể được sử dụng thay vì bản đồ nhiệt?

Không giống như các nhà cung cấp phần mềm HeatMap thay thế như FullStory, HotJar hoặc biên độ, SmartLook cho phép người dùng xác định lại các sự kiện, phễu và nhiệt-cung cấp trí thông minh kinh doanh thời gian thực tốt hơn.FullStory, Hotjar, or Amplitude, Smartlook allows users to retroactively define events, funnels, and heatmaps — providing better real-time business intelligence.

Có thể sử dụng bản đồ nhiệt cho dữ liệu phân loại?

Nếu chúng ta muốn xem các biến phân loại tương tác với nhau như thế nào, các bản đồ nhiệt là một cách rất hữu ích để làm như vậy.Mặc dù bạn có thể sử dụng bản đồ nhiệt để hình dung mối quan hệ giữa bất kỳ hai biến phân loại nào, nhưng việc sử dụng các bản đồ nhiệt theo các kích thước của thời gian là khá phổ biến.heatmaps are a very useful way to do so. While you can use a heatmap to visualize the relationship between any two categorical variables, it's quite common to use heatmaps across dimensions of time.

Annot HeatMap là gì?

Annot: Nếu đúng, hãy viết giá trị dữ liệu trong mỗi ô.FMT: Mã định dạng chuỗi để sử dụng khi thêm chú thích.Độ rộng của dòng: Chiều rộng của các dòng sẽ phân chia từng ô.LineColor: Màu của các dòng sẽ phân chia từng ô.

Loại đầu vào nào là cần thiết cho bản đồ nhiệt?

Ba loại đầu vào chính tồn tại để vẽ một bản đồ nhiệt: định dạng rộng, ma trận tương quan và định dạng dài.wide format, correlation matrix, and long format.