control首先,對于基因 G,我想為和條件創建一個 pandas 資料框experimental,其中 0:1 的比率分別為 10% 和 20%。
import pandas as pd
n = 5000
df = pd.DataFrame.from_dict(
{"Cells": (f'Cell{x}' for x in range(1, n 1)), "Control": np.random.choice([1,0], p=[0.1, 0.9], size=n), "Experimental": np.random.choice([1,0], p=[0.1 0.1, 0.9-0.1], size=n)},
orient='columns'
)
df = df.set_index("Cells")
其次,我對基因 G 進行了交叉串列分析。
# Contingency table/array
table = pd.crosstab(df.index, [df["Control"], df["Experimental"]])
table
現在,我想從步驟 1 和 2 中推斷出 1000 個基因的條件,然后執行交叉制表。如何?
uj5u.com熱心網友回復:
有多種方法可以處理它。最簡單的方法之一是:
- 將模擬程序包裝在一個函式中并為每個基因運行它以創建基于基因的結果串列。
- 連接所有基于基因的模擬并運行交叉表函式以在單個資料框中獲取列聯表。
就像是:
n = 100 # no of records per simulation/ gene
nog = 2 # no of genes
gene_list = ["gene_" str(i) for i in range(0,nog)]
# simulation procedure
def generate_gene_df(gene, n):
df = pd.DataFrame.from_dict(
{"Gene" : gene,
"Cells": (f'Cell{x}' for x in range(1, n 1)),
"Control": np.random.choice([1,0], p=[0.1, 0.9], size=n),
"Experimental": np.random.choice([1,0], p=[0.1 0.1, 0.9-0.1], size=n)},
orient='columns'
)
df = df.set_index(["Gene","Cells"])
return df
# create a list of gene based simulations generated using your procedure
gene_df_list = [generate_gene_df(gene, n) for gene in gene_list]
single_genes_df = pd.concat(gene_df_list)
single_genes_df = single_genes_df.reset_index()
table = pd.crosstab([single_genes_df["Gene"], single_genes_df["Cells"]],
[single_genes_df["Control"], single_genes_df["Experimental"]])
table

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/475286.html
