/r/pystats

Photograph via snooOG

A place to discuss the use of python for statistical analysis.

Welcome to /r/pystats, a place to discuss the use of python in statistical analysis and machine learning.


Related Subreddits


Where to start

If you're brand new to python, first go and check out the /r/learnpython wiki, or the official Beginner's Guide.

The best way to install python packages is using pip:

pip install <package>

Recommended packages:

  • ipython and the ipython-notebook - Interpreter and sage-style web notebook geared towards exploratory scripting.
  • statsmodels - statistical modelling
  • pandas - data structures and manipulation tools
  • matplotlib - matlab-style plotting
  • bokeh - Protoviz-style plotting
  • pyvttble - Small pivot-table library. Has a few common statistical methods missing from statsmodels.
  • scikit-learn - data mining and machine learning

Some of these packages have dependencies, most require numpy, and some require scipy, check the links for details.

For a good overview of what stats pacakges are available for python, check out http://stats.stackexchange.com/q/1595


/r/pystats

9,710 Subscribers

1

How to use X13-ARIMA-SEATS on python

Im trying to Seasonally Adjust a time series in python using X13-ARIMA-SEATS but I'm not able to use the StatsModels module. So I was trying to find an alternative to it or even another methodology to seasonally adjust time series. It would be amazing if someone could help me with this.

0 Comments
2024/04/02
20:05 UTC

2

Advice on MCMC Sampling

I want to implement a fast MCMC Sampling function. I wanted to know, is there a room for Instruction Level Parallelism in algorithms like Metropolis Hastings? Can I use SIMD low-level Intrinsics to Optimize Metropolis Hastings?

0 Comments
2024/01/24
20:27 UTC

2

Using the delta-method or parametric bootstrap to estimate confidence intervals and prediction intervals in nonlinear regression

Here is a link to a new github repository introducing new Python functions using the delta-method or parametric bootstrap to estimate confidence intervals for predicted values, and prediction intervals for new data, using nonlinear regression.:

https://github.com/gjpelletier/delta_method

These new functions extend the capabilities of the python packages scipy or lmfit to apply the delta-method or parametric bootstrap for confidence intervals and prediction intervals:

The first step is to use either scipy or lmfit to find the optimum parameter values and the variance-covariance matrix of the model parameters. The user may specify any expression for the nonlinear regression model.

The second step is to estimate the confidence intervals and prediction intervals using a new python function that applies either the delta-method or parametric bootstrap.

Three examples are provided:

The user may build any expression for the nonlinear relationship between observed x and y for the nonlinear regression using either scipy.optimize.curve_fit or the ExpressionModel function of lmfit.

To estimate the confidence intervals and prediction intervals, we use a new python functions that apply either the delta-method or parametric bootstrap as described in detail in Section 5 of this MAP566 online lecture by Julien Chiquet from Institut Polytechnique de Paris:

https://jchiquet.github.io/MAP566/docs/regression/map566-lecture-nonlinear-regression.html#confidence-intervals-and-prediction-intervals

0 Comments
2024/01/05
17:36 UTC

2

Getting Started with Pandas Groupby - Guide

The groupby function in Pandas divides a DataFrame into groups based on one or more columns. You can then perform aggregation, transformation, or other operations on these groups. Here’s a step-by-step breakdown of how to use it: Getting Started with Pandas Groupby

  • Split: You specify one or more columns by which you want to group your data. These columns are often referred to as “grouping keys.”
  • Apply: You apply an aggregation function, transformation, or any custom function to each group. Common aggregation functions include sum, mean, count, max, min, and more.
  • Combine: Pandas combines the results of the applied function for each group, giving you a new DataFrame or Series with the summarized data.
2 Comments
2023/11/03
12:29 UTC

0

Pandas Pivot Tables: Guide for Data Science

Pivoting is a neat process in Pandas Python library transforming a DataFrame into a new one by converting selected columns into new columns based on their values. The following guide discusses some of its aspects: Pandas Pivot Tables: A Comprehensive Guide for Data Science

The guide shows hads-on what is pivoting, and why do you need it, as well as how to use pivot and pivot table in Pandas restructure your data to make it more easier to analyze.

0 Comments
2023/10/30
19:38 UTC

1

Flask SQLAlchemy - Tutorial

Flask SQLAlchemy is a popular ORM tool tailored for Flask apps. It simplifies database interactions and provides a robust platform to define data structures (models), execute queries, and manage database updates (migrations).

The tutorial shows how Flask combined with SQLAlchemy offers a potent blend for web devs aiming to seamlessly integrate relational databases into their apps: Flask SQLAlchemy - Tutorial

It explains setting up a conducive development environment, architecting a Flask application, and leveraging SQLAlchemy for efficient database management to streamline the database-driven web application development process.

0 Comments
2023/10/24
12:27 UTC

3

Python List Comprehension - Guide

The article explores list comprehension, along with the definitions, syntax, advantages, some use cases as well as how to nest them - for easier creation process and avoiding the complexities of traditional list-generating methods: Python List Comprehension | CodiumAI

0 Comments
2023/10/18
15:20 UTC

1

My library says that it has 1k downloads, is this at least somewhat true?

I just published a python library, chess-analytica, that aims to make data analytics of chess games a lot easier. It's pretty niche, so I didn't expect much to come of it, but I've checked pystats and another site that check pip downloads and they say I have anywhere between 1k-3k. What should I expect is actually true? Is it actually like 200?

2 Comments
2023/09/09
05:05 UTC

3

Pandas Pivot Tables: A Guide for Data Science

For the Pandas library in Python, pivoting is a neat process that transforms a DataFrame into a new one by converting selected columns into new columns based on their values. The following guide discusses some of its aspects: Pandas Pivot Tables: A Comprehensive Guide for Data Science

  • What is pivoting, and why do you need it?
  • How to use pivot and pivot table in Pandas
  • When to choose pivot vs. pivot table
  • Using melt() in Pandas

The guide shows hads-on, how, with these functions, you can restructure your data to make it more easier to analyze.

2 Comments
2023/07/31
06:55 UTC

9

Statistical Modeling with Python: How-to & Top Libraries Compared (NumPy and Pandas, Matplotlib and Seaborn, Statsmodels)

The short guide discusses the advantages of utilizing Python for statistical modeling as well as three most popular Python libraries for this and checks several examples of their utilization: Statistical Modeling with Python: How-to & Top Libraries

These libraries can be used together to perform a wide range of statistical modeling tasks, from basic data analysis to advanced machine learning and Bayesian modeling - that's why Python has become a popular language for statistical modeling and data analysis.

1 Comment
2023/07/12
08:01 UTC

2

`AnalytiXHero` : A New Python Library

I'm thrilled to share with you my latest creation - 'AnalytiXHero,' a cutting-edge Python3 library. With just a few lines of code, this library simplifies exploratory data analysis and preprocessing. It covers all aspects of data preprocessing, including outlier handling, minimizing skewness/kurtosis, handling null spaces, plotting outliers, calculating variance, and performing various transformations. This library comes equipped with pre-defined state-of-the-art features to make your data preprocessing tasks a breeze.

To get started, simply install 'AnalytiXHero' in either Python's global environment or a virtual environment by executing the following command in your terminal: `pip install analytixhero`. For those interested in diving into the source code, you can find it at this link: https://github.com/thesahibnanda/AnalytiXHero

To explore the library's documentation, visit: https://github.com/thesahibnanda/AnalytiXHero/blob/main/DOCUMENTATION/0.%20Documentation%20Index.md

If you're interested in contributing, please refer to the contribution guidelines found here: https://github.com/thesahibnanda/AnalytiXHero/blob/main/CONTRIBUTION%20GUIDELINES.md

Official PyPI Link: https://pypi.org/project/analytixhero/

2 Comments
2023/07/08
08:54 UTC

6

Python library to access italian data

italy-geopop

I created this library that can be useful to anyone analyzing Italian data. It gives you access to Italian administrative, geographic and demographic data, taken from the Italian Institute of Statistics (2022), allowing you to easily draw geographic graphs (docs here).

It can also be used as a pandas accessor.

I'd love to hear from anyone who tries it any suggestions or ideas for improvement.

If anyone would like to contribute they would be welcome.

0 Comments
2023/06/06
09:07 UTC

1

newbie question - df.method() vs method(df)

Hi All,

I'm not new to stats, but I am new to python. Something I'm struggling with is when to use the syntax df.method() versus the syntax method(df).

For example, I see I can get the length of a dataframe with len(df) but not df.len() . I'm sure there's a reason, but I haven't come across it yet! In contrast, I can see the first five lines of a dataframe with df.head() but not head(df) .

What am I missing? I'm using Codecademy, and they totally glossed over this. I've searched for similar posts and didn't see any.

Thanks for your help!

7 Comments
2023/04/30
19:21 UTC

3

Multi Curve Fit Shading

https://preview.redd.it/6duau3x0cfpa1.png?width=1081&format=png&auto=webp&s=908648b681e64c81c447d93eb30f0673cc4fcc4a

Hi Everyone. I wrote a python script to fit a curve for preorders. You can see by the dots that as the release date gets closer the preorders increase significantly. The problem is I can't figure out why I can't shade the second curve. I believe the issue is with the params_upper and params_lower where the sigma is applied. For some reason it just returns zero when passing it through. How can I fix this? Any help would be greatly appreciated

# Define the exponential function
def exponential(x, a, b, c):
    return a * np.exp(b * (x-c))

#Define a function to fit the curve to
def polynomial(x, a, b, c):
    return a*x**2 + b*x + c
# Define the combined function
def combined(x, a1, b1, c1, a2, b2, c2):
    polynomial_range = (x >= 0) & (x <= 27)
    exponential_range = (x > 27) & (x <= 37)
    y = np.zeros_like(x)
    y[polynomial_range] = polynomial(x[polynomial_range], a1, b1, c1)
    y[exponential_range] = exponential(x[exponential_range], a2, b2, c2)
    return y
   

# Load data from a Pandas dataframe
x_data = preorders_AF['rank'].values
y_data = preorders_AF['running_total'].values

# Fit the curve using the defined function and the x and y data
params, covariance = curve_fit(combined, x_data, y_data)
# Fit the combined function to the data


# Calculate the 5 sigma interval
sigma = np.sqrt(np.diag(covariance))
params_upper = params + 1*sigma
params_lower = params - 1*sigma


# Generate the curve using the fitted parameters
x_curve = np.linspace(min(x_data), max(x_data) + 6, 37)

y_curve = combined(x_curve, *params)
y_upper = combined(x_curve,*params_upper)
y_lower = combined(x_curve,*params_lower)

fig, ax = plt.subplots()
# Plot the data points and the curve
ax.plot(x_data, y_data, 'o', label='Data')
ax.plot(x_curve, y_curve, label='Curve')
ax.fill_between(x_curve, y_upper, y_lower, alpha=0.2, label='Range')

# Add labels for the last data points
last_y1 = y_curve[-1].astype(int)
last_y2 = y_upper[-1].astype(int)
last_y3 = y_lower[-1].astype(int)

ax.annotate(f'{last_y1}', xy=(x_curve[-1], y_curve[-1]), xytext=(x_curve[-1]+0.5, y_curve[-1]), fontsize=12, color='orange')
ax.annotate(f'{last_y2}', xy=(x_curve[-1], y_upper[-1]), xytext=(x_curve[-1]+0.5, y_upper[-1]), fontsize=12, color='lightblue')
ax.annotate(f'{last_y3}', xy=(x_curve[-1], y_lower[-1]), xytext=(x_curve[-1]+0.5, y_lower[-1]), fontsize=12, color='lightblue')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.legend(loc='center right')
fig = plt.gcf()
fig.set_size_inches(13, 10)
plt.ylim(bottom=0)

plt.show()
0 Comments
2023/03/23
05:19 UTC

4

statsmodels OLS for multiple linear regression with error on input data

hello all

I am trying to perform multiple linear regression using statsmodels.OLS (in python); ie my goal is to fit a set of measured data points to a linear combination of two or more predefined sets of values. The measured data has a measurement error on it, but I can't find anywhere how to include this uncertainty in the model, and I need to do so in order to have correct errors on the regression coefficients. Is there any way to do this?

0 Comments
2023/03/11
12:39 UTC

2

CDF and PMF of binomial function not same with extreme values

Hello,
I wanted to calculate the chance that I inhale at least one molecule of Ceasars words (see here). I thought to calculate the chance of inhaling zero molecules and distract this value from 1 [1-(binom(0,n,p)]

I used this code

from scipy.stats import binom
def calculate(n, p, r):
    print (f"{n=} {p=} {r=}")
    print  (f"PMF  The chance that you inhale {r} molecules {binom.pmf(r, n, p)}")
    print  (f"CDF The chance that you inhale {r} molecules {binom.cdf(r, n, p)}")
n = 25.0*10**21
p = 1.0*10**-21
r = 0
calculate(n, p, r)

My output is

PMF The chance that you inhale 0 molecules 1.0

CDF The chance that you inhale 0 molecules 1.388794386496407e-11

When I do normal values my output is the same

n=10 p=0.1 r=0

PMF The chance that you inhale 0 molecules 0.3486784401000001

CDF The chance that you inhale 0 molecules 0.34867844009999993

How is this possible?

4 Comments
2023/01/17
09:46 UTC

7

Want to learn Bayesian Modeling in Python? - Join the Scicloj Online Book Club starting Saturday January 7th 2023 12:00 EST

0 Comments
2023/01/03
20:14 UTC

4

SEC API-python

Anyone know if there is a documentation for the SEC Edgar api? There doesn’t seem to be any information available. Please help!!

2 Comments
2022/12/24
23:49 UTC

1 Comment
2022/11/10
14:51 UTC

2

Text generation using my own dataset of titles/content?

I have a csv file containing article titles and article content. I'm trying to find a way to take a new title as input and use the training model to generate content. I've found a bunch of resources on how to use GPT2 or transformer pipelines to do complete sentences, etc. but I'd like to be able to provide my own data/model instead of using something from e.g. HuggingFace.

Can anyone point me in the right direction?

2 Comments
2022/08/02
18:50 UTC

5

Python libraries or ideas on how you would go about solving this?

So there's this dating show where there are 12 guys and 12 girls. Each person has a "perfect pair" and they're supposed to try to find out who it is. So every trial they match up with someone and then we find out how many of those pairs are correct (but not which ones they are). Also one of the pairs is randomly chosen, and we find out if they are a pair or not.

I basically want to build a python app using that data, and show how many possible combinations there are after each trial.

I've only done one intro to stats course in college, so I don't really know where to begin. I know this is a super broad question, but can anyone give me any advice on how to start? Maybe some formulas or concepts I should look into? Thanks!

1 Comment
2022/07/28
03:44 UTC

6

Create Choropleth map in Python plotly easily for data analysis

0 Comments
2022/03/09
06:11 UTC

9

Financial stock analysis using the python3 programming, Jupyter Notebook and Yahoo Finance Library

0 Comments
2022/02/06
13:03 UTC

0

Open source alternative to JSON, NumPy, Pandas

Hey everyone, if you're looking for a data structure for unstructured data, you should check out DocArray. I've made a walkthrough of how it works in this video.

Feel free to check it out on https://docarray.jina.ai/get-started/what-is/#comparing-to-alternatives

https://reddit.com/link/sl5c1h/video/27d2gk0i70g81/player

1 Comment
2022/02/05
11:57 UTC

3

Statsmodels OLS Confidence Intervals

How do I set the confidence level of get_prediction?

It has a default upper and lower interval, but the documentation for the method doesn’t tell how to change it.

0 Comments
2022/01/30
21:49 UTC

7

Financial Stock Analysis using the Python programming language and the Yahoo Finance Python library.

0 Comments
2022/01/24
21:20 UTC

Back To Top