I am new to python and have created this tiny class "myclass" which is inside a module called linear_regression_example.py. It prints out a regression summary and a density plot:
import statsmodels.api as sm
import sklearn.datasets as skld
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
class myclass:
def __init__(self, result=1):
self.result = result
def myregression(self):
y_X = skld.load_boston()
y = y_X['target']
X = y_X['data']
n = y_X['feature_names']
y = pd.DataFrame(y)
X = pd.DataFrame(X, columns=n)
X = sm.add_constant(X)
mod = sm.OLS(y, X)
result = mod.fit()
if self.result == 1:
print(result.summary())
pred = mod.predict(result.params)
pred = pd.DataFrame(pred)
errors = y - pred
sns.distplot(errors)
plt.show()
I also have another file, called test.py:
import linear_regression_example as lre
test = lre.myclass()
test.myregression()
Running test.py in pycharm results in the output "Process finished with exit code 0" but no summary or plot is shown. Maybe someone here knows where the problem lies.
Best regards
Dominik
linear_regression_example.py
import statsmodels.api as sm
import sklearn.datasets as skld
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
class myclass:
def __init__(self, result=1):
self.result = result
def myregression(self):
y_X = skld.load_boston()
y = y_X['target']
X = y_X['data']
n = y_X['feature_names']
y = pd.DataFrame(y)
X = pd.DataFrame(X, columns=n)
X = sm.add_constant(X)
mod = sm.OLS(y, X)
result = mod.fit()
if self.result == 1:
print(result.summary())
pred = mod.predict(result.params)
pred = pd.DataFrame(pred)
errors = y - pred
sns.distplot(errors)
plt.show()
if __name__ == '__main__':
test = myclass()
test.myregression()
test.py
import linear_regression_example as lre
test = lre.myclass()
test.myregression()
OUTPUT (from test.py)
Related
I'm trying to make a save button that when clicked will save the plot. When I use the command plt.savefig("SiLorentzFit2.png"), it saves just fine and I can see the plot as a .png. When I try to use a function to execute the same command, the plot saves a blank .png and gives back <Figure size 432x288 with 0 Axes> after pressing the button. How do I get the plot to save normally using the button code?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
def lorentz(x, y0, amp, cen, wid):
return y0 + (2*amp/np.pi)*(wid/(4*(x-cen)**2 + wid**2))
xtest = np.linspace(400,650,250)
y0 = 1000
amp = 10000
cen = 511
wid = 2
ytest = lorentz(xtest,y0,amp,cen, wid)
plt.plot(xtest,ytest)
plt.title("Fitting function")
plt.ylabel("Intenisty")
plt.xlabel("Raman shift (cm$^{-1}$)")
def save_plot(b):
plt.savefig("SiLorentzFit2.png")
print("File Saved!")
saveplot = Button(description="Save Plot")
saveplot.on_click(save_plot)
display(saveplot)
You can do this:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
def lorentz(x, y0, amp, cen, wid):
return y0 + (2*amp/np.pi)*(wid/(4*(x-cen)**2 + wid**2))
xtest = np.linspace(400,650,250)
y0 = 1000
amp = 10000
cen = 511
wid = 2
ytest = lorentz(xtest,y0,amp,cen, wid)
def save_plot(b):
plt.plot(xtest,ytest)
plt.title("Fitting function")
plt.ylabel("Intenisty")
plt.xlabel("Raman shift (cm$^{-1}$)")
plt.savefig(b)
print("File Saved!")
save_plot("SiLorentzFit2.png")
I'm trying to define a model function for MCMC.
The idea is to have a mixture of two distributions controlled with a probability ratio.
One of my attempts would look like this:
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
root = tfd.JointDistributionCoroutine.Root
def model_fn():
rv_p = yield root(tfd.Sample(tfd.Uniform(0.0,1.0),1))
catprobs = tf.stack([rv_p, 1.-rv_p],0)
rv_cat = tfd.Categorical(probs=catprobs)
rv_norm1 = tfd.Sample(tfd.Normal(0.0,1.0),1)
rv_norm2 = tfd.Sample(tfd.Normal(3.0,1.0),1)
rv_mix = yield tfd.Mixture(cat=rv_cat,
components=[
rv_norm1,
rv_norm2,
])
jd = tfd.JointDistributionCoroutine(model_fn)
jd.sample(2)
The code fails with:
ValueError: components[0] batch shape must be compatible with cat shape and other component batch shapes ((2, 2) vs ())
Could you give me an example of how to use Mixture distribution in a way that allows "any" shape of inputs?
I'm using tensorflow 2.4.1 and tensorflow_probability 0.12.1 with python 3.6
I figured it out. For reference here is a sample code:
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import tensorflow as tf
import tensorflow_probability as tfp
import matplotlib.pyplot as plt
tfd = tfp.distributions
tfb = tfp.bijectors
import numpy as np
from time import time
numdata = 10000
data = np.random.normal(0.0,1.0,numdata).astype(np.float32)
data[int(numdata/2):] = 0.0
_=plt.hist(data,30,density=True)
root = tfd.JointDistributionCoroutine.Root
def dist_fn(rv_p,rv_mu):
rv_cat = tfd.Categorical(probs=tf.stack([rv_p, 1.-rv_p],-1))
rv_norm = tfd.Normal(rv_mu,1.0)
rv_zero = tfd.Deterministic(tf.zeros_like(rv_mu))
rv_mix = tfd.Independent(
tfd.Mixture(cat=rv_cat,
components=[rv_norm,rv_zero]),
reinterpreted_batch_ndims=1)
return rv_mix
def model_fn():
rv_p = yield root(tfd.Sample(tfd.Uniform(0.0,1.0),1))
rv_mu = yield root(tfd.Sample(tfd.Uniform(-1.,1. ),1))
rv_mix = yield dist_fn(rv_p,rv_mu)
jd = tfd.JointDistributionCoroutine(model_fn)
unnormalized_posterior_log_prob = lambda *args: jd.log_prob(args + (data,))
n_chains = 1
p_init = [0.3]
p_init = tf.cast(p_init,dtype=tf.float32)
mu_init = 0.1
mu_init = tf.stack([mu_init]*n_chains,axis=0)
initial_chain_state = [
p_init,
mu_init,
]
bijectors = [
tfb.Sigmoid(), # p
tfb.Identity(), # mu
]
step_size = 0.01
num_results = 50000
num_burnin_steps = 50000
kernel=tfp.mcmc.TransformedTransitionKernel(
inner_kernel=tfp.mcmc.HamiltonianMonteCarlo(
target_log_prob_fn=unnormalized_posterior_log_prob,
num_leapfrog_steps=2,
step_size=step_size,
state_gradients_are_stopped=True),
bijector=bijectors)
kernel = tfp.mcmc.SimpleStepSizeAdaptation(
inner_kernel=kernel, num_adaptation_steps=int(num_burnin_steps * 0.8))
#XLA optim
#tf.function(autograph=False, experimental_compile=True)
def graph_sample_chain(*args, **kwargs):
return tfp.mcmc.sample_chain(*args, **kwargs)
st = time()
trace,stats = graph_sample_chain(
num_results=num_results,
num_burnin_steps=num_burnin_steps,
current_state=initial_chain_state,
kernel=kernel)
et = time()
print(et-st)
ptrace, mutrace = trace
plt.subplot(121)
_=plt.hist(ptrace.numpy(),100,density=True)
plt.subplot(122)
_=plt.hist(mutrace.numpy(),100,density=True)
print(np.mean(ptrace),np.mean(mutrace))
I am working on a random project in python with pyautgui,tkinter,matplotlib.pyplot. but when i run my code i get this error: ValueError: x, y, and format string must not be None I am running python 3.7.6 it also seems that this error is coming from either the init in my code or matplotlib.pyplot
My code:
import matplotlib.pyplot as plt
from tkinter import *
import pyautogui
from PIL import Image, ImageTk
import os
x = list(pyautogui.size())[0]
y = list(pyautogui.size())[1]
theme = '#4CAF50'
def init(input1,input2,input3):
sum_list = []
for (item1, item2) in zip(input1, input2):
sum_list.append(item1 + item2)
def chart(init):
plt.plot(init)
plt.savefig('chart.png')
plt.show()
plt.close('all')
return('chart.png')
def delete_file(file):
if os.path.exists(file):
os.remove(file)
else:pass
X = init([31,3,1,2,3,4], [31,3,1,2,3,4], [31,3,1,2,3,4])
app = Tk()
app.state('zoomed')
app.title('')
Frame(app,bg=theme,width=x,height=70).place(x=0,y=0)
top_text = Label(text='Random project',bg=theme,fg='#FFFFFF')
top_text.place(x=x//2-100,y=10)
top_text.config(font=("Helvetica", 30))
#Graph
graph = chart(X)
file2 = Image.open(graph)
new_img = file2.resize((640,480),Image.ANTIALIAS)
graph_img = ImageTk.PhotoImage(new_img)
graph_frame = Label(image=graph_img)
graph_frame.place(x=20,y=90)
close_btn = Button(app,text='X',width=5,height=1,bg='red',fg='white',borderwidth=0,command=app.destroy)
close_btn.place(x=x-41,y=0)
app.bind("<F11>", lambda event: app.attributes("-fullscreen",not app.attributes("-fullscreen")))
app.bind("<Escape>", lambda event: app.attributes("-fullscreen", False))
app.mainloop()
delete_file('chart.png')
I am attempting to look at conglomerated outlier information, utilizing several different SKLearn, HDBScan, and custom outlier detection classes. However, for some reason I am consistently running into an error where any class utilizing HDBScan cannot be iterated over. All other Sklearn and Custom classes can. The issue I am getting seems to consistently occur on the second pass of the HDBScan class and instantly happens upon algorithm.fit(tmp). Upon debugging the script, it looks like the error is thrown before even getting to the first line of the Class.
Any help? Below is the minimum viable reproduction:
import numpy as np
import pandas as pd
import hdbscan
from sklearn.datasets import make_blobs
from sklearn.svm import OneClassSVM
from sklearn.ensemble import IsolationForest
from sklearn.covariance import EllipticEnvelope
class DBClass():
def __init__(self, random = None):
self.random = random
def fit(self, data):
self.train_data = data
cluster = hdbscan.HDBSCAN()
cluster.fit(self.train_data)
self.fit = cluster
def predict(self, data):
self.predict_data = data
if self.train_data.equals(self.predict_data):
return self.fit.probabilities_
def OutlierEnsemble(df, anomaly_algorithms = None, num_slices = 5, num_columns = 7, outliers_fraction = 0.05):
if isinstance(df, np.ndarray):
df = pd.DataFrame(df)
assert isinstance(df, pd.DataFrame)
if not anomaly_algorithms:
anomaly_algorithms = [
("Robust covariance",
EllipticEnvelope(contamination=outliers_fraction)),
("One-Class SVM",
OneClassSVM(nu=outliers_fraction,
kernel="rbf")),
("Isolation Forest",
IsolationForest(contamination=outliers_fraction)),
("HDBScan LOF",
DBClass()),
]
data = []
for i in range(1, num_slices + 1):
data.append(df.sample(n = num_columns, axis = 1, replace = False))
predictions = []
names = []
for tmp in data:
counter = 0
for name, algorithm in anomaly_algorithms:
algorithm.fit(tmp)
predictions.append(algorithm.predict(tmp))
counter += 1
names.append(f"{name}{counter}")
return predictions
blobs, labels = make_blobs(n_samples=3000, n_features=12)
OutlierEnsemble(blobs)
The error provided is not the most helpful.
Traceback (most recent call last):
File "<ipython-input-4-e1d4b63cfccd>", line 75, in <module>
OutlierEnsemble(blobs)
File "<ipython-input-4-e1d4b63cfccd>", line 66, in OutlierEnsemble
algorithm.fit(tmp)
TypeError: 'HDBSCAN' object is not callable
In your DBClass.fit, DBClass.fit is unintentionally redefined.
You could perhaps use something like,
class DBClass():
def __init__(self, random = None):
self.random = random
def fit(self, data):
self.train_data = data
cluster = hdbscan.HDBSCAN()
cluster.fit(self.train_data)
self.myfit = cluster # save calculated cluster
def predict(self, data):
self.predict_data = data
if self.train_data.equals(self.predict_data):
return self.myfit.probabilities_ # use calculated cluster
I am getting the below error when I try to plot a short time energy function, please I need your help solve this problem.
Code:
import os
from tqdm import tqdm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
from python_speech_features import mfcc
from python_speech_features import logfbank
import librosa
def plot_ste(ste):
fig, axes = plt.subplots(nrows=1, ncols=1, sharex=False,
sharey=False, figsize=(400, 50))
fig.suptitle('Short Time Energy', size=100, y=1.02)
i = 0
for x in range(1):
for y in range(1):
data = list(ste.values())[i]
x, win = data[0], data[1]
axes[x,y].set_title(list(ste.keys())[i])
axes[x,y].plot(win, x)
axes[x,y].get_xaxis().set_visible(False)
axes[x,y].get_yaxis().set_visible(False)
i+=1
def ste(x, win):
"""Compute short-time energy."""
if isinstance(win, str):
win = scipy.signal.get_window(win, max(1, len(x) // 8))
win = win / len(win)
return scipy.signal.convolve(x**2, win**2, mode="same")
df = pd.read_csv('/dir/to/a.csv')
df.set_index('fname', inplace=True)
classes = list(np.unique(df.ID))
df.reset_index(inplace=True)
ste = {}
for c in classes:
wav_file = df[df.ID==c].iloc[0, 0]
signal, rate = librosa.load('/dir/to/wav_file')
ste[c] = ste
plot_ste(ste)
plt.show()
Error:
File "/home/Desktop/Program/stft_plot_full_Dir.py", line 35, in plot_ste
x, win = data[0], data[1]
KeyError: 0