Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8977a4f132 | ||
|
|
fc6909a802 | ||
|
|
1ad392fee0 | ||
|
|
b5e3b0242d | ||
|
|
11bc0f7b53 | ||
|
|
a2e4b43e16 | ||
|
|
5bfc480a4d | ||
|
|
b46663289c | ||
|
|
91be71285a |
+10
@@ -0,0 +1,10 @@
|
||||
FROM python
|
||||
|
||||
RUN pip install flask scipy matplotlib
|
||||
# COPY chi_square.py kolmogorov_smirnov.py ./
|
||||
COPY server.py ./
|
||||
COPY templates templates
|
||||
RUN mkdir static
|
||||
|
||||
# ENTRYPOINT ["python", "chi_square.py"]
|
||||
ENTRYPOINT ["flask", "run"]
|
||||
@@ -0,0 +1,8 @@
|
||||
run: build
|
||||
docker compose up
|
||||
|
||||
build:
|
||||
docker compose build
|
||||
|
||||
push:
|
||||
docker compose push
|
||||
+2
-2
@@ -104,11 +104,11 @@ def index():
|
||||
plt.savefig('static/plot.png')
|
||||
|
||||
# Render template with plot image and results
|
||||
return render_template('home.html', plot_path='static/plot.png', chisq_stat=chisq_stat, p_value=p_value)
|
||||
return render_template('chi_square.html', plot_path='static/plot.png', chisq_stat=chisq_stat, p_value=p_value)
|
||||
|
||||
else:
|
||||
# Render initial form
|
||||
return render_template('home.html')
|
||||
return render_template('chi_square.html')
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
version: "3.9"
|
||||
services:
|
||||
analytics_learning:
|
||||
image: marcpartensky/analytics_learning
|
||||
build: .
|
||||
ports:
|
||||
- "5000:5000"
|
||||
environment:
|
||||
FLASK_APP: server.py
|
||||
# FLASK_APP: kolmogorov_smirnov.py
|
||||
# FLASK_APP: chi_square.py
|
||||
FLASK_RUN_HOST: "0.0.0.0"
|
||||
FLASK_RUN_PORT: 5000
|
||||
@@ -103,11 +103,11 @@ def index():
|
||||
plt.savefig('static/plot.png')
|
||||
|
||||
# Render template with plot
|
||||
return render_template('index.html', plot_url='static/plot.png')
|
||||
return render_template('kolmogorov_smirnov.html', plot_url='static/plot.png')
|
||||
else:
|
||||
return render_template('index.html', error=True)
|
||||
return render_template('kolmogorov_smirnov.html', error=True)
|
||||
else:
|
||||
return render_template('index.html')
|
||||
return render_template('kolmogorov_smirnov.html')
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from flask import Flask, render_template, request
|
||||
import numpy as np
|
||||
import scipy.stats as stats
|
||||
from scipy.stats import expon, gamma, norm, poisson
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Define routes
|
||||
@app.route('/chi_square', methods=['GET', 'POST'])
|
||||
def chi_square():
|
||||
if request.method == 'POST':
|
||||
# Get distribution type from form submission
|
||||
distribution_type = request.form['distribution_type']
|
||||
|
||||
# Generate random sample data for selected distribution
|
||||
if distribution_type == 'normal':
|
||||
data = np.random.normal(loc=0, scale=1, size=100)
|
||||
elif distribution_type == 'exponential':
|
||||
data = np.random.exponential(scale=1, size=100)
|
||||
elif distribution_type == 'poisson':
|
||||
data = np.random.poisson(lam=1, size=100)
|
||||
elif distribution_type == 'gamma':
|
||||
data = np.random.gamma(shape=2, scale=1, size=100)
|
||||
|
||||
# Calculate expected values for selected distribution
|
||||
if distribution_type == 'normal':
|
||||
expected = stats.norm(loc=np.mean(data), scale=np.std(data)).pdf(data)
|
||||
elif distribution_type == 'exponential':
|
||||
expected = stats.expon(scale=np.mean(data)).pdf(data)
|
||||
elif distribution_type == 'poisson':
|
||||
expected = stats.poisson(mu=np.mean(data)).pmf(data)
|
||||
elif distribution_type == 'gamma':
|
||||
expected = stats.gamma(a=np.mean(data)**2/np.var(data), scale=np.var(data)/np.mean(data)).pdf(data)
|
||||
|
||||
# Calculate Chi-square statistic and p-value
|
||||
chisq_stat, p_value = stats.chisquare(f_obs=np.histogram(data, bins='auto')[0], f_exp=expected)
|
||||
|
||||
# Create histogram of sample data and plot expected distribution
|
||||
plt.hist(data, bins='auto', density=True, alpha=0.7)
|
||||
plt.plot(data, expected, 'r-', lw=2)
|
||||
plt.title(f'{distribution_type.capitalize()} Distribution')
|
||||
plt.xlabel('Value')
|
||||
plt.ylabel('Density')
|
||||
plt.savefig('static/plot.png')
|
||||
|
||||
# Render template with plot image and results
|
||||
return render_template('chi_square.html', plot_path='static/plot.png', chisq_stat=chisq_stat, p_value=p_value)
|
||||
|
||||
else:
|
||||
# Render initial form
|
||||
return render_template('chi_square.html')
|
||||
|
||||
@app.route('/kolmogorov_smirnov', methods=['GET', 'POST'])
|
||||
def kolmogorov_smirnov():
|
||||
if request.method == 'POST':
|
||||
# Get user input
|
||||
distribution_type = request.form['distribution_type']
|
||||
num_samples = int(request.form['num_samples'])
|
||||
|
||||
# Generate data
|
||||
if distribution_type == 'normal':
|
||||
data = norm.rvs(size=num_samples)
|
||||
elif distribution_type == 'uniform':
|
||||
data = np.random.uniform(size=num_samples)
|
||||
elif distribution_type == 'standard':
|
||||
data = np.random.randn(num_samples)
|
||||
elif distribution_type == 'exponential':
|
||||
data = expon.rvs(size=num_samples)
|
||||
elif distribution_type == 'poisson':
|
||||
data = poisson.rvs(mu=5, size=num_samples)
|
||||
elif distribution_type == 'gamma':
|
||||
data = gamma.rvs(a=5, size=num_samples)
|
||||
else:
|
||||
data = None
|
||||
|
||||
# Generate plot
|
||||
if data is not None:
|
||||
plt.hist(data, bins=30)
|
||||
plt.title(f'{distribution_type.capitalize()} Distribution')
|
||||
plt.xlabel('Value')
|
||||
plt.ylabel('Frequency')
|
||||
plt.savefig('static/plot.png')
|
||||
|
||||
# Render template with plot
|
||||
return render_template('kolmogorov_smirnov.html', plot_url='static/plot.png')
|
||||
else:
|
||||
return render_template('kolmogorov_smirnov.html', error=True)
|
||||
else:
|
||||
return render_template('kolmogorov_smirnov.html')
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
Reference in New Issue
Block a user