{"id":7566,"date":"2023-11-02T10:40:36","date_gmt":"2023-11-02T05:10:36","guid":{"rendered":"https:\/\/python-programs.com\/?p=7566"},"modified":"2023-11-10T12:13:48","modified_gmt":"2023-11-10T06:43:48","slug":"python-program-to-calculate-the-standard-deviation","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-calculate-the-standard-deviation\/","title":{"rendered":"Python Program to Calculate the Standard Deviation"},"content":{"rendered":"
Standard Deviation:<\/strong><\/p>\n In statistics, the standard deviation is a measure of spread. It’s a metric for quantifying the spread or variance of a group of data values. It is quite similar to variance in that it delivers the deviation measure, whereas variance offers the squared value.<\/p>\n A low Standard Deviation value implies that the data are more evenly distributed, whereas a high number suggests that the data in a set are dispersed from their mean average values. The standard deviation has the advantage of being expressed in the same units as the data, unlike the variance.<\/p>\n Given a list of numbers , the task is to calculate the standard deviation of the given list in python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to calculate the standard deviation in python some of them are:<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In Python, the statistics package has a function called stdev() that can be used to determine the standard deviation. The stdev() function estimates standard deviation from a sample of data instead of the complete population. Parameters:<\/strong><\/p>\n Return type: <\/strong><\/p>\n Exceptions :<\/strong><\/p>\n We’ve built a list and applied the standard deviation operation to the data values in the following example:<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The NumPy module provides us with a number of functions for dealing with and manipulating numeric data items.<\/p>\n Python’s numpy package includes a function named numpy.std() that computes the standard deviation along the provided axis. This function returns the array items’ standard deviation. The standard deviation is defined as the square root of the average square deviation (calculated from the mean). The standard deviation for the flattened array is calculated by default. The average square deviation is generally calculated using x.sum()\/N, where N=len (x).<\/p>\n The standard deviation for a range of values can be calculated using the numpy.std() function, as demonstrated below.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The Pandas module allows us to work with a bigger number of datasets and gives us with a variety of functions to apply to them.<\/p>\n We may conduct different statistics operations on the data values using the Pandas module, one of which is standard deviation, as shown below<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n In this example, we built a list and then used the pandas.dataframe() function to convert the list into a data frame. Using the std() function, we calculated the standard deviation of the values in the data frame. Standard Deviation: In statistics, the standard deviation is a measure of spread. It’s a metric for quantifying the spread or variance of a group of data values. It is quite similar to variance in that it delivers the deviation measure, whereas variance offers the squared value. A low Standard Deviation value implies that the data …<\/p>\ngiven list = [34, 14, 7, 13, 26, 22, 12, 19,29, 33, 31, 30, 20, 10, 9, 27, 31, 24]<\/pre>\n
The given list of numbers : \r\n34 14 7 13 26 22 12 19 29 33 31 30 20 10 9 27 31 24 \r\nStandard deviation of the given list = 9.008529001520827<\/pre>\n
given list = [4, 7, 1, 2, 17, 19, 11, 19, 18, 13, 5, 3, 17]<\/pre>\n
The given list of numbers : \r\n4 7 1 2 17 19 11 19 18 13 5 3 17 \r\nStandard deviation of the given list = 7.042908781360447<\/pre>\n
given list = [12, 1, 4, 5, 19, 11, 13, 11, 12, 5, 5, 6, 7, 10, 15, 14]<\/pre>\n
The given list of numbers : \r\n12 1 4 5 19 11 13 11 12 5 5 6 7 10 15 14 \r\nStandard deviation of the given list = 4.8425200051213<\/pre>\n
Python Program to Calculate standard deviation in Python<\/h2>\n
\n
Method #1:Using stdev() function in statistics package<\/h3>\n
\nstdev\u00a0( [data-set], xbar )<\/p>\n[data] : An iterable with real-valued numbers as parameters.\r\nxbar (Optional): Uses the data set's actual mean as a value.<\/pre>\n
Returns the real standard deviation of the values supplied as parameters.<\/pre>\n
\n
# importing statistics\r\nimport statistics\r\n# given list\r\ngiven_list = [34, 14, 7, 13, 26, 22, 12, 19,\r\n 29, 33, 31, 30, 20, 10, 9, 27, 31, 24]\r\n# using stdev() fun to calculate standard devaition\r\nstandarddevList = statistics.stdev(given_list)\r\n# print the given list and standard deviation\r\nprint(\"The given list of numbers : \")\r\nfor i in given_list:\r\n print(i, end=\" \")\r\n# printing new empty line\r\nprint()\r\n# printing the standard deviation of the given list of numbers\r\nprint(\"Standard deviation of the given list =\", standarddevList)\r\n<\/pre>\n
The given list of numbers : \r\n34 14 7 13 26 22 12 19 29 33 31 30 20 10 9 27 31 24 \r\nStandard deviation of the given list = 9.008529001520827<\/pre>\n
Method #2:Using std() function in NumPy module<\/h3>\n
# importing numpy\r\nimport numpy as np\r\n# given list\r\ngiven_list = [34, 14, 7, 13, 26, 22, 12, 19,\r\n 29, 33, 31, 30, 20, 10, 9, 27, 31, 24]\r\n# using std() fun to calculate standard devaition\r\nstandarddevList = np.std(given_list)\r\n# print the given list and standard deviation\r\nprint(\"The given list of numbers : \")\r\nfor i in given_list:\r\n print(i, end=\" \")\r\n# printing new empty line\r\nprint()\r\n# printing the standard deviation of the given list of numbers\r\nprint(\"Standard deviation of the given list =\", standarddevList)\r\n<\/pre>\n
The given list of numbers : \r\n34 14 7 13 26 22 12 19 29 33 31 30 20 10 9 27 31 24 \r\nStandard deviation of the given list = 8.754716541864452<\/pre>\n
Method #3:Using std() in pandas Module<\/h3>\n
# importing pandas\r\nimport pandas as pd\r\n# given list\r\ngiven_list = [34, 14, 7, 13, 26, 22, 12, 19,\r\n 29, 33, 31, 30, 20, 10, 9, 27, 31, 24]\r\n# converting the given_list to DataFrame\r\ndataList = pd.DataFrame(given_list)\r\n# calculating the standard devaition using std() function\r\nstandarddevList = dataList.std()\r\n\r\n# print the given list and standard deviation\r\nprint(\"The given list of numbers : \")\r\nfor i in given_list:\r\n print(i, end=\" \")\r\n# printing new empty line\r\nprint()\r\n# printing the standard deviation of the given list of numbers\r\nprint(\"Standard deviation of the given list =\", standarddevList)\r\n<\/pre>\n
The given list of numbers : \r\n34 14 7 13 26 22 12 19 29 33 31 30 20 10 9 27 31 24 \r\nStandard deviation of the given list = 0 9.008529\r\ndtype: float64<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n