Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How can python tell if the bread is getting lighter?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "python how to judge whether the bread is lighter or not". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to judge whether the bread is lighter or not" by python.

Case review

Is the bread getting lighter?

The bakery claims that each piece of bread is 400g.

The old shopkeeper retired and his son took over the bakery

There are complaints from customers that the weight of bread is lighter than before

Based on the statistics of the weight of 30 pieces of bread, it is found that the average weight of bread is 397g. (question: handmade bread can't be exactly 400g each. according to the data, has the weight of bread become lighter? )

Simulation experiment and analysis

The data is stored in csv format, where each observed object (individual bread) occupies a row, and the measured variables (purchase date and bread weight) are arranged in a row. Import data into python.

Import pandas as pd

Breads = pd.read_csv ('breads.csv',encoding='utf-8')

Breads.head ()

The pandas package provides the dataframe data structure, and it is very convenient to import csv data in python with pandas. Use head () to view the first few rows of data (default is the first five) as follows:

Dateweight02015/1/7386.712015/1/9396.722015/1/10409.832015/1/12384.542015/1/14394.3

Calculate the mean and standard deviation of bread weight:

Import numpy as np

Mean = round (np.mean (breads.weight), 4)

Std = round (np.std (breads.weight), 4)

Import sys

Sys.stdout.write ('mean =' + str (mean) +'\ n' + 'std =' + str (std))

The result is:

Mean = 397.1267std = 10.7371

Assuming that the weight of the bread accords with the normal distribution, then to test whether the bread is lighter, it is necessary to use the standard deviation of the sample to check whether there is a contradiction between the sample average and the overall average, that is, the mean difference test.

Mean difference test (t test)

The difference between the zero hypothesis sample mean and the population mean is within the error range, that is, the bread is not lighter.

The difference between the sample mean and the population mean is beyond the error range, that is, the bread becomes lighter.

The probability is not significant (5%) negates the zero hypothesis, that is, the bread is indeed lighter.

The probability equals or exceeds the significance level (5%) and retains the zero assumption, that is, the weight of bread has not changed.

From scipy.stats import ttest_rel # ttest_rel: independent mean test ttest_rel (breads.weight, * 30)

The result is:

Ttest_relResult (statistic=-1.4411172599973978, pvalue=0.16026297018603147)

Pvalue stands for probability, in which the probability is 0.1603, which exceeds the significant level, so there is not enough evidence to suspect that the average weight of bread in the bakery has decreased.

A few small concepts

Normal distribution: a left-right symmetrical discrete distribution centered on the average value. 95% of the data is concentrated in the range of 1.96 times (about 2 times) standard deviation from the average.

Mean difference test: this example analyzes whether there is a substantial difference between the average weight of 30 pieces of bread and the average weight advertised by the bakery. If the probability is less than the significance level (generally 5%), there is a significant difference.

At this point, I believe you have a deeper understanding of "python how to judge whether the bread is lighter or not". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report