使用numpy“where”功能的问题

问题描述 投票:0回答:1

所以我想学习python和一些统计数据。在一个关于F测试的示例代码中,出现了这一点。

np.random.seed(12)

# Generate random data

voter_race = np.random.choice(a= races, p = [0.05, 0.15 ,0.25, 0.05, 0.5], size=1000)



# Use a different distribution for white ages

white_ages = stats.poisson.rvs(loc=18, mu=32, size=1000)

voter_age = stats.poisson.rvs(loc=18, mu=30, size=1000)

voter_age = np.where(voter_race=="white", white_ages, voter_age)


# Group age data by race

voter_frame = pd.DataFrame({"race":voter_race,"age":voter_age})

groups = voter_frame.groupby("race").groups


# Extract individual groups

asian = voter_age[groups["asian"]]

black = voter_age[groups["black"]]

hispanic = voter_age[groups["hispanic"]]

other = voter_age[groups["other"]]

white = voter_age[groups["white"]]


# Perform the ANOVA

stats.f_oneway(asian, black, hispanic, other, white)

在粗体代码中,为什么voter_race出现两次,为什么white_ages也在np.where代码?

python numpy where
1个回答
0
投票

np.where返回x或y,(第二个或第三个参数),具体取决于第一个参数条件是否为真。如果条件为真,则返回x,否则返回y。关于该功能的documentation非常好。

因此,如果voter_race为“white”,则此处的np.where语句将返回white_ages,否则将返回voter_age。

你的代码也没有出现粗体,仅供参考。

© www.soinside.com 2019 - 2024. All rights reserved.