Want to concatenate the random uniformly generated values - python

I have code as following and I want to write a function for it. output should be x as dataframe and y as series, even dataframe having x and y as columns is enough.
x = np.arange(0,50)
x = pd.DataFrame({'x':x})
# just random uniform distributions in differnt range
y1 = np.random.uniform(10,15,10)
y2 = np.random.uniform(20,25,10)
y3 = np.random.uniform(0,5,10)
y4 = np.random.uniform(30,32,10)
y5 = np.random.uniform(13,17,10)
y = np.concatenate((y1,y2,y3,y4,y5))
y = y[:,None]
I tried following but it doesn't return values from y1 to y5.
My code is :
x = np.arange(0,50)
x = pd.DataFrame({'x':x})
def colm(p,q):
s, t = p, q
a = ['y1', 'y2', 'y3', 'y4', 'y5']
for i in a:
i = np.random.uniform(s, t, 10)
s, t = s+10, t+10
return i

Try to modify the function like this:
def colm(p, q, chunk_len=10):
x = np.arange(0,5 * chunk_len)
x = pd.DataFrame({'x':x})
# just random uniform distributions in differnt range
ys = [np.random.uniform(p_, q_, chunk_len) for p_, q_ in zip(p, q)]
y = np.concatenate(ys)
return x, pd.Series(y)
You can now use it as
x, y = colm([10, 20, 0, 30, 13], [15, 25, 5, 13, 17])

I got it after some try what I was looking for:
x = np.arange(0,50)
x = pd.DataFrame({'x':x})
def colm(p,q):
s, t = p, q
z = []
a = ['y1', 'y2', 'y3', 'y4', 'y5']
for i in a:
i = np.random.uniform(s, t, 10)
s, t = s+10, t+10
z.append(i)
y = np.concatenate(z)
return pd.Series(y)
Thanks everyone for showing interest....!

Related

How to generate a polynomial dataset

I am trying to generate a polynomial dataset. I wrote a code
def generate_dataset1():
n = 500
X = 2 - 3 * np.random.normal(0, 1, n)
y = X - 2 * (X ** 2) + 0.5 * ( X ** 3) + np.random.normal(-3, 3, n)
m = np.random.uniform(0.3, 0.5, (n, ))
b = np.random.uniform(5, 10, (n, ))
plt.scatter(X, y, s=10)
plt.show()
Now, if I want to generate a dataset using the given formula (from Wikipedia), could you tell me what I have to change?
y = B_0 + B_1*x, B_2*x2 + B_3*x3 + ... + e
Here, x2 means x (square), x3 means x (cube), so on and e is the unobserved random error with mean zero.
There are many ways to multiply x with B, such as dot product. But I think for loop should be good enough. Just loop through element of B and x:
def generate_dataset(B, n):
# B is beta, n is number of sample
e = np.random.normal(-3, 3, n)
X = 2 - 3 * np.random.normal(0, 1, n)
y = 0
for i in range(len(B)):
y += B[i] * X**i
y += e
return X, y
def plot_dataset(X, y):
#m = np.random.uniform(0.3, 0.5, (n, )) # not sure why you need this
#b = np.random.uniform(5, 10, (n, )) # not sure why you need this
plt.scatter(X, y, s=10)
plt.show()
n = 500
B = [0, 1, -2, 0.5] # [beta0, beta1, beta2, beta3]
X, y = generate_dataset(B, 500)
plot_dataset(X, y)

How to only return the nth arrays?

import numpy as np
def h(x,y):
return x+y
xline = np.linspace(1, 5, 20)
yline = np.linspace(1, 5, 20)
xi = xline
yi = yline
N = 10
for i in range(N):
xOld = xi
yOld = yi
xNew = h(xOld,yOld)
yNew = xOld
xi = xNew
yi = yNew
print(xNew[-1],yNew[-1])
So here is my code, and I was trying to get only the 10th xNew and yNew.
And when I print(xNew[-1],yNew[-1]), I got the 10th element of all 10 arrays we have. What should I do to get only the 10th xi and yi? And xi ,yi should contain 20 elements.

Damping harmonic oscillation code with python

I don't know that how make the code the three graph in damping harmonic oscillation model,
[X - t(time)], [V(velocity) - t(time)], [a(acceleration) - t(time)] graph
i can make the [X - t(time)] graph
but i don`t know how to make another graphs..
import numpy as np
from matplotlib import pyplot as plt
# mx'' = - bx' - kx
x_0 = 3
v_0 = 0
y_0 = np.array([x_0,v_0]) # first array
def Euler_Method(f,a,b,y0,step):
t = np.linspace(a,b,step)
h = t[1] - t[0]
Y = [y0]
N = len(t)
n = 0
y = y0
for n in range(0,N-1) :
y = y + h*f(y,t[n])
Y.append(y)
n = n+1
Y = np.array(Y)
return Y, t
def harmonic(y,t) :
k = 50
m = 200
b = 20 # drag coefficient
a = (-1*k/m)*y[0] - (b/m)*y[1] # x'' = a, y[0] : first position
v = y[1] # v = first velocity : y[1]
f = np.array([v,a])
return f
a = Euler_Method(harmonic, 0, 100, y_0, 100000)
X = a[0][:,0]
t = a[1]
plt.plot(t,X)
plt.show()
Why can't you just take the derivative of X to get V and A?
V = np.diff(X)
A = np.diff(V)
fig, (ax1, ax2, ax3) = plt.subplots(3)
fig.suptitle('Vertically stacked subplots')
ax1.plot(t, X)
ax2.plot(t[1:], V)
ax3.plot(t[2:], A)
plt.show()
Gives,

Python list of object extraction into sub list

Say I have a class called 'points' with attributes 'x' and 'y'
and I create a list of points p = [p1,p2,p3,p4]
where p1.x = x1, py.y = y1 ...
How do I easily extract all the x and y values as a list?
e.g.
x = [x1,x2,x3,x4]
y = [y1,y2,y3,y4]
can this be done simply in one line of code in python?
You can do something like this:
p = [p1,p2,p3,p4]
x = [point.x for point in p]
y = [point.y for point in p]
x = [point.x for point in p]
y = [point.y for point in p]
could work but this will iterate the p twice, which is unnecessary ,You could try:
p = [p1, p2, p3, p4]
x, y = list(zip(*[(point.x, point.y) for point in p]))
Here you are
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
points = [Point(1,2), Point(3,4), Point(5,6), Point(7,8)]
x = [p.x for p in points]
y = [p.y for p in points]
print(x)
print(y)
Output :
[1, 3, 5, 7]
[2, 4, 6, 8]
Try it here
You could do it like this
x, y = [point.x for point in p],[point.y for point in p]

How do I convert this ECDF representation function from MATLAB into Python?

I have to convert the following MATLAB code:
% calculate ECDF from D at n points
function X = ECDF_representation(D, n)
m = mean(D); X = [];
for d=1:size(D,2),
[f, x] = ecdf(D(:,d)+
randn(size(D(:,d)))*0.01);
ll=interp1(f,x,linspace(0,1,n),'cubic');
X=[X ll];
end
X= [X m];
end
into Python. Here's what I've tried so far:
import numpy as np
from scipy import interpolate
from statsmodels.distributions.empirical_distribution import ECDF
def ecdf_representation(D, n):
"""calculate ECDF from D at n points"""
m = np.mean(D)
X = []
for d in xrange(D.shape[1] + 1):
f, x = ECDF([D[:, d] + np.random.randn(np.shape(D[:, d])) * 0.01])
ll = interpolate.interp1d(f, x, np.linspace(0, 1, n), 'cubic')
X = [X, ll]
X = [X, m]
return X
I get the error:
in ecdf_representation
for d in xrange(D.shape[1] + 1):
IndexError: tuple index out of range
If I switch the line for d in xrange(D.shape[1] + 1): to for d in xrange(D.shape[0] + 1):
I resolve the original error but get a new one that is:
line 25, in ecdf_representation
f, x = ECDF([D[:, d] + np.random.randn(np.shape(D[:, d])) * 0.01])
IndexError: too many indices for array
EDIT:
D is a 1-d array because of the value being returned in the following function where data is a pandas data frame.
I think I should be returning something else into the ecdf_representation function, but I'm not sure what.
def segment_energy(data, th):
mag = np.linalg.norm(data.loc[:, ['x', 'y', 'z']], axis=1)
mag = np.array(mag)
mag -= np.mean(mag)
above = np.where(mag >= th * np.std(mag))
indicator = np.zeros(mag.shape)
indicator[above] = 1
plt.plot(mag)
plt.plot(indicator * 1000, 'r')
plt.show()
return indicator

Categories