how to create an undirected graph using a boolean list? - python

I have a list of zero's and one's.
(1's being higher than average and 0's being below average)
I am trying to make an undirected graph but I keep getting
test = (ustest[states] > final).astype(int)
test2 = test.unstack()#this is still a series
test3 = test2.tolist()#this is the series converted to a list
usmap = nx.Graph()
usmap = usmap.add_edges_from(test3)
I keep getting "TypeError: object of type 'int' has no len()" but I am unsure on what I am doing wrong.
I am trying to have to have this list be the edges in the graph.
EDIT
File "//anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "//anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/dshadow/Desktop/usstates.py", line 53, in
usmap = usmap.add_edges_from(test3)
File "//anaconda/lib/python3.6/site-packages/networkx/classes/graph.py", line 857, in add_edges_from
ne = len(e)
TypeError: object of type 'int' has no len()

Related

Converting Python list of lists to a MATLAB sparse matrix

I would like to convert a Python list of lists to a MATLAB sparse matrix. Each element of the Python list is a list with three elements: a row, a column, and a value. My code and the error message are below:
from __future__ import division
import matlab.engine
A = [[1,2,1],[1,3,1],[2,2,1]]
A = matlab.double(A)
eng = matlab.engine.start_matlab()
eng.spconvert(A)
And the error message:
File "testMatlab.py", line 14, in <module>
eng.spconvert(A)
File "/Library/Python/2.7/site-packages/matlab/engine/matlabengine.py", line 71, in __call__
_stderr, feval=True).result()
File "/Library/Python/2.7/site-packages/matlab/engine/futureresult.py", line 67, in result
return self.__future.result(timeout)
File "/Library/Python/2.7/site-packages/matlab/engine/fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
TypeError: unsupported data type returned from MATLAB
The error goes away if we amend the code to, the problem is with Python not being able to handle the returned sparse matrix:
eng.spconvert(A,nargout=0)

Dask array mean throws "setting an array element with a sequence" exception where pandas array mean works

I have a pandas data frame that consist of a single column of numpy arrays. I can use the numpy.mean function to calculate the mean of the arrays.
import numpy
import pandas
f = pandas.DataFrame({"a":[numpy.array([1.0, 2.0]), numpy.array([3.0, 4.0])]})
numpy.mean(f["a"]) # returns array([2., 3.])
I want to do the same thing in Dask.
import dask.dataframe
import dask.array
g = dask.dataframe.from_pandas(f, npartitions=1)
dask.array.mean(g["a"], dtype="float64")
(You have to specify the dtype, otherwise you get a TypeError: unsupported operand type(s) for /: 'NoneType' and 'int' exception.)
The call to dask.array.mean returns the following, which looks correct.
dask.array<mean_agg-aggregate, shape=(), dtype=float64, chunksize=(), chunktype=numpy.ndarray>
However, when I run dask.array.mean(g["a"], dtype="float64").compute() to get the final value I get a ValueError: setting an array element with a sequence. exception. The full stack is as follows.
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<input>", line 1, in <module>
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/base.py", line 165, in compute
(result,) = compute(self, traverse=False, **kwargs)
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/base.py", line 436, in compute
results = schedule(dsk, keys, **kwargs)
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/threaded.py", line 81, in get
**kwargs
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/local.py", line 486, in get_async
raise_exception(exc, tb)
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/local.py", line 316, in reraise
raise exc
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/local.py", line 222, in execute_task
result = _execute_task(task, data)
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/core.py", line 118, in _execute_task
args2 = [_execute_task(a, cache) for a in args]
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/core.py", line 118, in <listcomp>
args2 = [_execute_task(a, cache) for a in args]
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/core.py", line 119, in _execute_task
return func(*args2)
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/optimization.py", line 982, in __call__
return core.get(self.dsk, self.outkey, dict(zip(self.inkeys, args)))
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/core.py", line 149, in get
result = _execute_task(task, cache)
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/core.py", line 119, in _execute_task
return func(*args2)
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/utils.py", line 29, in apply
return func(*args, **kwargs)
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/dask/array/reductions.py", line 539, in mean_chunk
total = sum(x, dtype=dtype, **kwargs)
File "<__array_function__ internals>", line 6, in sum
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 2229, in sum
initial=initial, where=where)
File "/Users/wmcneill/src.private/radius_limit/venv/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 90, in _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: setting an array element with a sequence.
Is it possible to perform the equivalent Dask operation?
It would be good if Dask Dataframe handled this case, but it doesn't today. It's not actually that surprising given the situation.
Your dataframe is a bit odd, in that elements of that dataframe are themselves Numpy arrays.
>>> f
a
0 [1.0, 2.0]
1 [3.0, 4.0]
As a result, Pandas thinks that this is an object dtype dataframe
>>> f.dtypes
a object
dtype: object
Because Dask Dataframe is lazy it doesn't actually keep track of all of the data at any given point, it only knows the dtypes, which in this case are pretty non-informative. Dask Dataframe doesn't really know what do to with a mean computation on these complex elements. It doesn't know if your elements are numpy arrays or strings, or custom Python objects, etc..
So it errs and you need to provide a data type explicitly.
The full solution to this problem is probably for Pandas to develop a much more complex dtype heirarchy, but that's probably unlikely near term.
Ideally Dask Dataframe would give a better error message here encouraging you to specify a dtype manually. If you wanted to raise an issue, that would be welcome.

AttributeError: 'tuple' object has no attribute 'ravel'

I'm trying to solve two simultaneous nonlinear equations using the scipy.optimize.brute function
import numpy as np
import scipy.optimize as so
def root2d(x,a,b):
F1 = np.exp(-np.exp(-(x[0]+x[1]))) - x[1]*(b+x[0]**2)
F2 = x[0]*np.cos(x[1]) + x[1]*np.sin(x[0]) - a
return (F1,F2)
a = 0.5
b = 1
x0 = np.array([-0.1,0.1]) # initial guesses
rranges = (slice(-4,4,0.2),slice(-4,4,0.2))
print(so.brute(root2d,rranges,args=(a,b),finish=so.fmin))
I get an error that I don't understand: AttributeError: 'tuple' object has no attribute 'ravel'. What does this mean and how do I fix my code (if it's possible)?
Edit: full error message
Traceback (most recent call last):
File "<ipython-input-2-29b9507fcb99>", line 1, in <module>
runfile('.../test')
File "C:\WinPython\WinPython-64bit-3.5.2.3\python-3.5.2.amd64\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\WinPython\WinPython-64bit-3.5.2.3\python-3.5.2.amd64\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "../test.py", line 111, in <module>
print(so.brute(root2d,rranges,args=(a,b),finish=so.fmin))
File "C:\WinPython\WinPython-64bit-3.5.2.3\python-3.5.2.amd64\lib\site-packages\scipy\optimize\optimize.py", line 2711, in brute
indx = argmin(Jout.ravel(), axis=-1)
AttributeError: 'tuple' object has no attribute 'ravel'
You return 2 variables F1 and F2 and reveive them using a single variable obj.(say) This is what is called a tuple obj,it is associated with 2 values, the values of F1 and F2. So, use index as you use in a list to get the value you want, in order.

Display a matrix with putting a common factor in sympy

I want to display a matrix with putting an extracted common factor on outside of the matrix after matrix calculation in sympy.
I wrote below code.
from sympy import *
a = symbols("a")
b = symbols("b")
A = Matrix([exp(I*a),exp(I*a)*exp(I*b)])
print simplify(A)
I got below output.
Matrix([
[ exp(I*a)],
[exp(I*(a + b))]])
However, I want to get below output.
exp(I*a)*Matrix([
[ 1],
[exp(I*b)]])
I tried collect(A,exp(I*a)) and got follow error.
Traceback (most recent call last):
File "<ipython-input-65-834f4c326df4>", line 1, in <module>
runfile('C:/Anaconda2/Programs/test/untitled44.py', wdir='C:/Anaconda2/Programs/test')
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Anaconda2/Programs/test/untitled44.py", line 14, in <module>
collect(A,exp(I*a))
File "C:\Anaconda2\lib\site-packages\sympy\simplify\simplify.py", line 451, in collect
if expr.is_Mul:
File "C:\Anaconda2\lib\site-packages\sympy\matrices\matrices.py", line 3084, in __getattr__
"%s has no attribute %s." % (self.__class__.__name__, attr))
AttributeError: MutableDenseMatrix has no attribute is_Mul.
I know a way to extract a common factor for a element of a matrix like follow link.
https://github.com/sympy/sympy/issues/8442
But it's not my desire.
How should I do?
I do not think Sympy provides a function for the task you want. However, you can do this manually, as per the method proposed in the accepted answer of a similar question asked in the Mathematica SE (link).
The idea is to extract the common factor of the polynomial elements via gcd and then use MatMul with the evaluate=False option in order to restrict Sympy from performing the scalar-matrix multiplication.
import sympy as sp
a, b = sp.symbols('a, b')
A = sp.Matrix([sp.exp(sp.I * a), sp.exp(sp.I * a) * sp.exp(sp.I * b)])
g = sp.gcd(tuple(A))
A_v2 = sp.MatMul(g,(A/g),evaluate = False)
print(A_v2)
exp(I*a)*Matrix([
[ 1],
[exp(I*b)]])

covariance of each key in a dictionary

I have a list, which is a set of tickers. For each ticker, I get the the daily return going back six months. I then want to compute the covariance between each ticker. I am having trouble with np.cov, here is my code to test COV:
newStockDict = {}
for i in newList_of_index:
a = Share(i)
dataB = a.get_historical(look_back_date, end_date)
stockData = pd.DataFrame(dataB)
stockData['Daily Return'] = ""
yList = []
for y in range(0,len(stockData)-1):
stockData['Daily Return'][y] = np.log(float(stockData['Adj_Close'][y])/float(stockData['Adj_Close'][y+1]))
yList = stockData['Daily Return'].values.tolist()
newStockDict[stockData['Symbol'][0]] = yList
g = (np.cov(pd.Series((newStockDict[newList_of_index[0]]))), pd.Series(((newStockDict[newList_of_index[1]]))))
return g
My error is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Udaya\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile
execfile(filename, namespace)
File "C:/Users/Udaya/Documents/Python Scripts/SR_YahooFinanceRead.py", line 150, in <module>
print CumReturnStdDev(stock_list)
File "C:/Users/Udaya/Documents/Python Scripts/SR_YahooFinanceRead.py", line 132, in CumReturnStdDev
g = (np.cov(pd.Series((newStockDict[newList_of_index[0]]))), pd.Series(((newStockDict[newList_of_index[1]]))))
File "C:\Users\Udaya\Anaconda\lib\site-packages\numpy\lib\function_base.py", line 1885, in cov
X -= X.mean(axis=1-axis, keepdims=True)
File "C:\Users\Udaya\Anaconda\lib\site-packages\numpy\core\_methods.py", line 66, in _mean
ret = umr_sum(arr, axis, dtype, out, keepdims)
TypeError: unsupported operand type(s) for +: 'numpy.float64' and 'str'
>>> TypeError: unsupported operand type(s) for +: 'numpy.float64' and 'str'
I've tried using pd.cov on a dataframe, then np.cov. Nothing works. Here I am actually appending the daily returns to a list, then to a dictionary, before I manually calculate an n by n covariance matrix. But I am unable to get np.cov to work.
Please help. The idea is I can easily construct a dataframe of N tickers, with each row being a daily return. but am unable to compute cov with said dataframe, thus this df-->list-->dict process.

Categories

Resources