我正在尝试自动化JMP所做的一个过程(分析->;分布,输入列a作为“Y值”,使用后续列作为“权重”值)。在JMP中,一次只能做一列,我想使用Python循环遍历所有列并创建一个数组,显示每列的中值。
例如,如果质量数组为[0,10,20,30],而列1的权重数组为[30,191,9,0],则质量数组的加权中值应为10。然而,我不知道如何得出这个答案。
到目前为止我
我不知道从这里到底该去哪里。基本上,“Y值”是一个质量范围,数组中的所有列表示为每个质量找到的数据点的数量。我需要根据他们被报道的频率找到中间质量。
我不是Python或统计方面的专家,所以如果我遗漏了任何有用的细节,请告诉我!
更新:以下是我目前所做工作的一些代码:
#Boilerplate & Import files
import csv
import scipy as sp
from scipy import stats
from scipy.stats import norm
import numpy as np
from numpy import genfromtxt
import pandas as pd
import matplotlib.pyplot as plt
inputFile = '/Users/cl/prov.csv'
origArray = genfromtxt(inputFile, delimiter = ",")
nArray = np.array(origArray)
dimensions = nArray.shape
shape = np.asarray(dimensions)
#Mask values ==0
maTest = np.ma.masked_equal(nArray,0)
#Create array of masses the same shape as the weights (nArray)
fieldLength = shape[0]
rowLength = shape[1]
for i in range (rowLength):
createArr = np.arange(0, fieldLength*10, 10)
nCreateArr = np.array(createArr)
massArr.append(nCreateArr)
nCreateArr = np.array(massArr)
nmassArr = nCreateArr.transpose()
如果我正确理解你的问题,我们能做什么。就是把观测值加起来,除以2就会得到与中值对应的观测值。从那里我们需要弄清楚这个数字是什么样的观测值。
这里的一个技巧是用np.cumsum计算观测和。这给了我们一个连续的累积和。
示例:
np.cumsum([1,2,3,4]) -> [ 1, 3, 6, 10]
每个元素都是所有先前元素和自身的总和。我们这里有10个观察点。所以平均值是第5次观察。(最后一个元素除以2得到5)。
现在看一下cumsum结果,我们可以很容易地看到,这一定是第二个和第三个元素之间的观测(观测3和6)。
所以我们需要做的就是找出中位数(5)的指数。
np.searchsorted正是我们所需要的。它将找到将元素插入数组的索引,以便保持排序。
这样做的代码如下:
输出为:
分享一些我参与的代码。这允许您在excel电子表格的每一列上运行统计信息。
相关问题 更多 >
编程相关推荐