【问题标题】:How to use for loop for a model如何为模型使用 for 循环
【发布时间】:2022-01-19 02:44:37
【问题描述】:

我知道我必须将这一切都放入一个函数中,然后从 for 循环中调用该函数十次,但我不确定如何。任何帮助将不胜感激。


import random
import matplotlib.pyplot as plt
import statistics as stats
plt.hist(list1, bins=100, alpha = 0.5)
array1 = np.array(list1)
array2 = np.array(list2)
array3 = np.array(list3)
# Run the t-test using scipy library
scipy.stats.ttest_ind(array1,array2)

【问题讨论】:

  • 使用带范围的循环(10

标签: python scipy


【解决方案1】:

使用范围(对于范围(0,10)中的x):

import random
import matplotlib.pyplot as plt
import statistics as stats
import numpy as np
# Library for scientific statistics
import scipy.stats
for x in range(0,10):
  print(x)
  # Create two lists of random numbers that follow a normal ("Gaussian") distribution
  # Start with an empty list named "list1"
  list1 = []
  # Loop that runs 30 times - starts at 1, goes to 30
  for x in range(1,30):
    # Random numbers drawn from pool that has mean of 12 and standard deviation of 5
    value1 = random.gauss(12,5)
    # Add random value to the first list, list1
    list1.append(value1)
  print(list1)
  # Do the same with a second list
  list2 = []
  for x in range(1,30):
    # Random numbers drawn from pool that has mean of 14 and standard deviation of 4
    value2 = random.gauss(14,4)
    list2.append(value2)
  print(list2)
  # Create a histogram of the two lists using matplotlib library
  plt.hist(list1, bins=50, alpha = 0.5)
  plt.hist(list2, bins=50, alpha = 0.5)
  # Run a t-test on the two sets of data
  array1 = np.array(list1)
  array2 = np.array(list2)
  # Run the t-test using scipy library
  scipy.stats.ttest_ind(array1,array2)

【讨论】:

    猜你喜欢
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    相关资源
    最近更新 更多