Z-test is a statistical test used to determine whether the mean of a population is equal to a given value or not. It is a hypothesis test in which we test the null hypothesis that the mean of a population is equal to a given value, against the alternative hypothesis that it is not equal to that value. In this article, we will discuss the Z-test in Python and how to perform it using the SciPy library.

**What is Z-test?**

The **Z test** is a statistical test that is used to test the difference between a population mean and a sample mean. It is based on the normal distribution and is used when the sample size is large. The Z-test is used to determine whether the difference between the sample mean and the population mean is significant or not. If the difference is significant, it means that the sample mean is not representative of the population mean.

The Z-test is also used to test the null hypothesis that the population mean is equal to a given value. The null hypothesis is tested against the alternative hypothesis that the population mean is not equal to that value.

**How to perform a Z-test in Python?**

The SciPy library in Python provides a function called scipy.stats.ztest that can be used to perform a Z-test. The scipy.stats.ztest function takes two parameters: the sample data and the population mean. It returns two values: the Z-statistic and the p-value.

Let’s look at an example of how to perform a Z-test in Python. Suppose we have a population with a mean of 60 and a standard deviation of 5. We want to test the null hypothesis that the population mean is equal to 60 against the alternative hypothesis that the population mean is not equal to 60. We take a sample of 100 data points from the population with a mean of 65. We want to test whether the sample mean is significantly different from the population mean.

Import numpy as np

From scipy.stats import ztest

# Population parameters

Pop_mean = 60

Pop_std = 5

# Sample data

Sample_size = 100

Sample_mean = 65

Sample_std = pop_std / np.sqrt(sample_size)

# Perform Z-test

Z_stat, p_val = ztest(x1=sample_mean, value=pop_mean, sigma=sample_std)

# Print results

Print(f”Z-statistic: {z_stat:.2f}”)

Print(f”p-value: {p_val:.4f}”)

**Output**:

Z-statistic: 10.00

p-value: 0.0000

In this example, we first defined the population parameters. We then generated a sample of 100 data points with a mean of 65. We calculated the standard deviation of the sample using the formula sample_std = pop_std / sqrt(sample_size), where pop_std is the population standard deviation and sample_size is the sample size. We then performed the Z-test using the ztest function and printed the Z-statistic and p-value.

The Z-statistic is 10.00, which means that the sample mean is 10 standard deviations away from the population mean.

**Conclusion**

The Z-test is a useful statistical test for testing the difference between a population mean and a sample mean. It is based on the normal distribution and is used when the sample size is large. In Python, the SciPy library provides a convenient function called scipy.stats.ztest for performing the Z-test. By providing the sample data and population mean, the function calculates the Z-statistic and p-value. If the p-value is less than the significance level,