System of second order ode - system

I am trying to solve an IVP, consisting of two second order ode. I use sympy. No problems to obtain general solution. But I don't understand how to plug in the IC:s.
MWE:
from sympy import *
import numpy as np
import matplotlib.pyplot as plt
x1,x2=symbols('x1,x2',cls=Function)
t,k1,k2,m1,m2=symbols('t k1 k2 m1 m2',real=True)
k1=1.0
k2=1.0
m1=1.0
m2=1.0
eq1=Eq(m1*diff(x1(t),t,2)+k1*x1(t)-k2*(x2(t)-x1(t)),0)
eq2=Eq(m2*diff(x2(t),t,2)+k2*(x2(t)-x1(t)),0)
eqs=[eq1,eq2]
IC1={x1(0):0,x1(t).diff().subs(t,0):0}
IC2={x2(0):1,x2(t).diff().subs(t,0):0}
sol=dsolve(eqs,[x1(t),x2(t)],ics=[IC1,IC2])
print(sol)
Error message:
for funcarg, value in ics.items():
AttributeError: 'list' object has no attribute 'items'

Related

FInding fft gives keyerror :'Aligned ' pandas

I have a time series data
I am trying to find the fft .But it gives keyerror :Aligned when trying to get the value
my data looks like below
this is the code:
import datetime
import numpy as np
import scipy as sp
import scipy.fftpack
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
temp_fft = sp.fftpack.fft(data3)
Looks like your data is a pandas series. fft works with numpy arrays rather than series.
Easy resolution is to convert your series into a numpy array either via
data3.values
or
np.array(data3)
You can then pass that array into fft function. So the end result is:
temp_fft = sp.fftpack.fft(data3.values)
This should work for you now.

can not plot a graph using matplotlib showing error

Exception has occurred: ImportError
dlopen(/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PIL/_imaging.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '_xcb_connect'
File "/Users/showrov/Desktop/Machine learning/Preprosessing/import_dataset.py", line 2, in <module>
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import sys
print(sys.version)
data=pd.read_csv('Data_customer.csv')
print(data)
plt.plot(data[:2],data[:2])
data[:2] will return the first 2 rows. In order to plot, you need to use the columns.
Mention the column name directly like data['columnName'] otherwise use the iloc method.
for example: data.iloc[:, 1:2] in order to access 2nd column.
For more information about indexing operations, please check out this link

Can't figure out what syntax error is when trying to convert Int to Categorical

I have a variable (Bad Indicator) which is also my target. It is currently INT 64 and I am trying to convert to categorical but am getting a syntax error but I can't figure out why.Converting DTI from Object to Float worked. . .
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
project = pd.read_csv('c:/users/Brandon Thomas/Project.csv')
project.dtypes
90+ Perf int64
Bankruptcy Code float64
Bad Indicator int64
RR Downgrade object
Beacon Range object
Product Grouping object
Final Product Grouping object
dtype: object
project["DTI"] = pd.to_numeric(project.DTI, errors='coerce')
project['Bad Indicator']=pd.categorical(project.Bad Indicator)
File "<ipython-input-166-b6f5f0432024>", line 2
project['Bad Indicator']=pd.categorical(project.Bad Indicator)
^
SyntaxError: invalid syntax

The corresponding ctypes type of a numpy.dtype?

If I have a numpy ndarray with a certain dtype, how do I know what is the corresponding ctypes type?
For example, if I have a ndarray, I can do the following to convert it to a shared array:
import multiprocessing as mp
import numpy as np
import ctypes
x_np = np.random.rand(10, 10)
x_mp = mp.Array(ctypes.c_double, x_np)
However, I have to specify c_double here. It works if I don't specify the exact same type, but I would like to keep the type the same. How should I find out the ctypes type of the ndarray x_np automatically, at least for some common elementary data types?
This is now supported by numpy.ctypeslib.as_ctypes_type(dtype):
import numpy as np
x_np = np.random.rand(10, 10)
np.ctypeslib.as_ctypes_type(x_np.dtype)
Gives ctypes.c_double, as expected.
There is actually a way to do this that's built into Numpy:
x_np = np.random.rand(10, 10)
typecodes = np.ctypeslib._get_typecodes()
typecodes[x_np.__array_interface__['typestr']]
Output:
ctypes.c_double
The caveat is that the np.ctypeslib._get_typecodes function is marked as private (ie it's name starts with _). However, it doesn't seem like its implementation has changed in some time, so you can probably use it fairly reliably.
Alternatively, the implementation of _get_typecodes is pretty short, so you could just also copy the whole function over to your own code:
import ctypes
import numpy as np
def get_typecodes():
ct = ctypes
simple_types = [
ct.c_byte, ct.c_short, ct.c_int, ct.c_long, ct.c_longlong,
ct.c_ubyte, ct.c_ushort, ct.c_uint, ct.c_ulong, ct.c_ulonglong,
ct.c_float, ct.c_double,
]
return {np.dtype(ctype).str: ctype for ctype in simple_types}

Dtype work in FROM but not IMPORT

I swear I read almost all the "FROM vs IMPORT" questions before asking this.
While going through the NumPy tutorial I was using:
import numpy as np
but ran into trouble when declaring dtype of a matrix like:
a = np.ones((2,3),dtype=int32)
I kept getting "NameError: name 'int32' is not defined." I am using Python v3.2, and am following the tentative tutorial that goes along with it. I used:
from numpy import *
a = ones((2,3),dtype=int32)
Which works. Any insight as to why this is would be much appreciated.
Thank you in advance!
import numpy as np
#this will work because int32 is defined inside the numpy module
a = np.ones((2,3), dtype=np.int32)
#this also works
b = np.ones((2,3), dtype = 'int32')
#python doesn't know what int32 is because you loaded numpy as np
c = np.ones((2,3), dtype=int32)
back to your example:
from numpy import *
#this will now work because python knows what int32 is because it is loaded with numpy.
d = np.ones((2,3), dtype=int32)
I tend to define the type using strings as in array b