Reason of ValueError: negative dimensions are not allowed? - python-3.x

I have been trying to plot the output of the function defined below,but not able to get the output. I tried several things but getting different errors every time. If somebody can help me with what it is that I am doing wrong, I shall be grateful.
import matplotlib.pyplot as plt
%matplotlib inline
import math
import sympy as sym
x = sym.symbols('x',positive = True)
lambd = 4
a= 3
def f(x):
return lambd**a * x**(a-1) * sym.exp(-lambd*x) / math.factorial(a-1)
x1 = np.linspace(0,1,10)
plt.plot(x1,f(x1))
In case I change the x1 as np.linspace(0,1,100) then the error is
"ValueError: sequence too large; cannot be greater than 32"
What can be the reason for that? Some guidance in this will be highly appreciated.

You're passing a numpy array x1 to a function f. The problem that inside this function you have sympy.exp() which does not understand what to do with an array, since it only works on symbols and numbers.
The easiest would be to use numpy.exp instead.
import matplotlib.pyplot as plt
import numpy as np
import math
lambd = 4
a= 3
def f(x):
return lambd**a * x**(a-1) * np.exp(-lambd*x) / math.factorial(a-1)
x1 = np.linspace(0,1,10)
plt.plot(x1,f(x1))
plt.show()
If, for whatever reason, you need to use some function that only works on single numbers and not arrays, you can use numpy.vectorize to convert the function to one that evaluates the input array elementwise.
import matplotlib.pyplot as plt
import sympy as sym
import numpy as np
import math
lambd = 4
a= 3
def f(x):
return lambd**a * x**(a-1) * sym.exp(-lambd*x) / math.factorial(a-1)
fv = np.vectorize(f)
x1 = np.linspace(0,1,10)
plt.plot(x1,fv(x1))
plt.show()

Related

Annotating clustering from DBSCAN to original Pandas DataFrame

I have working code that is utilizing dbscan to find tight groups of sparse spatial data imported with pd.read_csv.
I am maintaining the original spatial data locations and would like to annotate the labels returned by dbscan for each data point to the original dataframe and then write a csv with the same information.
So the code below is doing exactly what I would expect it to at this point, I would just like to extend it to import the label for each row in the original dataframe.
import argparse
import string
import os, subprocess
import pathlib
import glob
import gzip
import re
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from sklearn.cluster import DBSCAN
X = pd.read_csv(tmp_csv_name)
X = X.drop('Name', axis = 1)
X = X.drop('Type', axis = 1)
X = X.drop('SomeValue', axis = 1)
# only columns 'x' and 'y' now remain
db=DBSCAN(eps=EPS, min_samples=minSamples, metric='euclidean', algorithm='auto', leaf_size=30).fit(X)
labels = def_inst_dbsc.labels_
unique_labels = set(labels)
# maxX , maxY are manual inputs temporarily
while sizeX > 16 or sizeY > 16 :
sizeX=sizeX*0.8 ; sizeY=sizeY*0.8
fig, ax = plt.subplots(figsize=(sizeX,sizeY))
plt.xlim(0,maxX)
plt.ylim(0,maxY)
plt.scatter(X['x'], X['y'], c=colors, marker="o", picker=True)
# hackX , hackY are manual inputs temporarily
# which represent the boundaries defined in the original dataset
poly = patches.Polygon(xy=list(zip(hackX,hackY)), fill=False)
ax.add_patch(poly)
plt.show()

f() missing 1 required positional argument: 't'

I just tried to execute this code. but it shows always this error:'f() missing 1 required positional argument:'t'
please can you tell me what should I change?
import numpy as np
def f(y,z,t):
return np.array([2*y+z-t,z+y])
import matplotlib.pyplot as plt
from scipy.integrate import odeint
t=np.linspace(0,2,1000)
sol=odeint(f,[0,1],t)
y,z=sol[:,0],sol[:,1]
plt.plot(t,y,label='y')
plt.plot(t,z,label='z')
plt.show()
Depending on what you are trying to do, you can get around it two ways. To pass in additional arguments other than y and t you need to include them as a constant in the function parameter.
import numpy as np
def f(t,y,z):
return np.array([2*y+z-t,z+y])
import matplotlib.pyplot as plt
from scipy.integrate import odeint
t=np.linspace(0,2,1000)
z = 10.0
sol=odeint(f,[0,1],t, tfirst=True, args=(z, ))
y,z=sol[:,0],sol[:,1]
plt.plot(t,y,label='y')
plt.plot(t,z,label='z')
plt.show()
This will still cause an error. However, if you are also trying to obtain z as result then you should be able to run:
import numpy as np
def f(t,inp):
y, z = inp
return np.array([2*y+z-t,z+y])
import matplotlib.pyplot as plt
from scipy.integrate import odeint
t=np.linspace(0,2,1000)
sol=odeint(f,[0,1],t, tfirst=True)
y,z=sol[:,0],sol[:,1]
plt.plot(t,y,label='y')
plt.plot(t,z,label='z')
plt.show()
This should run without any errors but you may need to double check that this is the result you are expecting.
(The tfirst argument is just for clarity to make ensure the order of arguments provided is correct, you can remove and re-order if you want as well.)
Documentation for odeint function here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html

Statsmodels.api.tsa.seasonal_decompose plot figsize

I am using statsmodels.api.tsa.seasonal_decompose to do some seasonal analysis of a time series.
I set it up using
decomp_viz = sm.tsa.seasonal_decompose(df_ts['NetConsumption'], period=48*180)
and then try and visualise it using
decomp_viz.plot()
The output was tiny so I tried to use the standard matplotlib command of
decomp_viz.plot(figsize=(20,20))
However, this got the warning:
TypeError: plot() got an unexpected keyword argument 'figsize'
The documentation says that a matplotlib.figure.Figure is returned from DecomposeResult.plot so I am unsure as to why this error is happening.
My version of statsmodels is 0.13.1 and I am aware that the documentation is for 0.14.0, but conda says that that version does not exist and that I cannot update to it.
Any thoughts would be appreciated.
DecomposeResult.plot doesn't pass keyword arguments. You can change the figure size after you create it:
import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt
PERIOD = 48*180
g = np.random.default_rng(20211225)
y = np.cos(2 * np.pi * np.linspace(0, 10.0, 10*PERIOD))
y += g.standard_normal(y.shape)
decomp_viz = sm.tsa.seasonal_decompose(y, period=PERIOD)
fig = decomp_viz.plot()
fig.set_size_inches((16, 9))
# Tight layout to realign things
fig.tight_layout()
plt.show()
Alternatively, you can do the same by altering the MPL rc.
import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt
# Change default figsize
plt.rc("figure",figsize=(20,20))
PERIOD = 48*180
g = np.random.default_rng(20211225)
y = np.cos(2 * np.pi * np.linspace(0, 10.0, 10*PERIOD))
y += g.standard_normal(y.shape)
decomp_viz = sm.tsa.seasonal_decompose(y, period=PERIOD)
decomp_viz.plot()
plt.show()
which produces (cropped as too big for my screen)

How can I make a transparent background?

I have a .csv file which contains some data where x, y, x1, y1 are the coordinate points, and p is the value. My below code is working very well for plotting, but when I am plotting the data, I am getting a background color like the purple color. I don't want any color in the background. I want the background will be Transparent. My ultimate goal is overlying this result over an image. I am new in Python. Any help will be highly appreciated.
Download link of the .csv file here or link-2 or link-3
I am getting below result
My Code
import matplotlib.pyplot as plt
from scipy import ndimage
import numpy as np
import pandas as pd
from skimage import transform
from PIL import Image
import cv2
x_dim=1200
y_dim=1200
# Read CSV
df = pd.read_csv("flower_feature.csv")
# Create numpy array of zeros os same size
array = np.zeros((x_dim, y_dim), dtype=np.uint8)
for index, row in df.iterrows():
x = np.int(row["x"])
y = np.int(row["y"])
x1 = np.int(row["x1"])
y1 = np.int(row["y1"])
p = row["p"]
array[x:x1,y:y1] = p
map = ndimage.filters.gaussian_filter(array, sigma=16)
plt.imshow(map)
plt.show()
As per Ghassen's suggestion I am getting below results. I am still not getting the transparent background.
When Alpha =0
When alpha =0.5
When alpha =1
try with this code :
import matplotlib.pyplot as plt
from scipy import ndimage
import numpy as np
import pandas as pd
x_dim=1200
y_dim=1200
# Read CSV
df = pd.read_csv("/home/rosafi/Downloads/flower_feature.csv")
# Create numpy array of zeros os same size
array = np.ones((x_dim, y_dim), dtype=np.uint8)
for index, row in df.iterrows():
x = np.int(row["x"])
y = np.int(row["y"])
x1 = np.int(row["x1"])
y1 = np.int(row["y1"])
p = row["p"]
array[x:x1,y:y1] = p
map = ndimage.filters.gaussian_filter(array, sigma=16)
map = np.ma.masked_where(map == 0, map)
plt.imshow(map)
plt.show()
output:
I solved this issue by masking out the values where values ==0. The code will be
from mpl_toolkits.axes_grid1 import make_axes_locatable
masked_data = np.ma.masked_where(map == 0, map)

How to improve scipy based script for interpolation. Obtaining unknown x values

Is there a way besides the graphical calltip function to obtain unknown x values from y? How would I go about coding an input() to obtain an unknown x value?
import numpy as np
from scipy import interpolate
x = np.linspace(0, 20, 5)
x = np.array([0. , 5. , 10., 15., 20.])
y = np.linspace(0.422,0.948, 5, endpoint =False)
y = np.array([0.422, 0.5513, 0.66433, 0.83433, 0.948])
f = interpolate.interp1d(x,y)
ynew = np.arange(0,1, 0.1)
import matplotlib.pyplot as plt
plt.figure()
plt.plot(x,y,'o', ynew,f(ynew), '-')
Here is the link to a video by APmonitor which had the solution. See script below:
from numpy import *
x = array([0,5,10,15,20])
y = array ([0.422,0.551333,0.66433,0.834333,0.948])
from scipy.interpolate import *
p1 = polyfit(x,y,1)
from matplotlib.pyplot import *
print(p1)
plot(x,y,'o')
plot(x,polyval(p1,x),'r-')
from scipy import *
slope, intercept, r_value, p_value,std_err = linregress(x,y)
print(pow(r_value,2))
print(p_value)

Resources