How can I pass the AttributeError: 'DataFrame' object has no attribute 'flatten in Python?
How can I pass the AttributeError: 'DataFrame' object has no attribute 'flatten in Python?
我已经使用 python 创建了空间马尔可夫矩阵但是我得到了这个错误(AttributeError: 'DataFrame' object has no attribute 'flatten' )。我对python不熟悉所以,希望能帮我解决这个问题?
代码是
import numpy as np
import pysal`enter code here`
import pandas as pd
import pysal as ps
f = pd.read_csv("C:\Users\Yousif\Desktop\Spatial.MC\updated_testdata1-36-Copy.csv")
f
pci = f[list(map(str,range(1, 1096)))]
#f = ps.open(ps.examples.get_path("usjoin36.csv"))
#f
#pci = np.array([f.by_col[str(y)] for y in range(1,1096)])
pci = pci.transpose()
rpci = pci/(pci.mean(axis=0))
#w = ps.open(ps.examples.get_path("states36.gal")).read()
w = ps.open("C:/Users/Yousif/Desktop/Spatial.MC/states-36-Copy.gal").read( )
w.transform = 'r'
sm = ps.Spatial_Markov(rpci, w, fixed=True, k=5, variable_name='rpci')
for p in sm.P:
print(p)
似乎 ps.Spatial_Markov
想要一个 numpy 数组,而您传递的是 pandas 数据框。尝试通过修改此行
从数据框中提取数组
pci = f[list(map(str,range(1, 1096)))].as_matrix()
更新
的确,docs 是关于这一点的:
pysal.spatial_dynamics.markov.Spatial_Markov(y, w, k=4, permutations=0, fixed=False, variable_name=None)
Parameters:
- y (array) – (n,t), one row per observation, one column per state of each observation, with as many columns as time periods.
...
我已经使用 python 创建了空间马尔可夫矩阵但是我得到了这个错误(AttributeError: 'DataFrame' object has no attribute 'flatten' )。我对python不熟悉所以,希望能帮我解决这个问题? 代码是
import numpy as np
import pysal`enter code here`
import pandas as pd
import pysal as ps
f = pd.read_csv("C:\Users\Yousif\Desktop\Spatial.MC\updated_testdata1-36-Copy.csv")
f
pci = f[list(map(str,range(1, 1096)))]
#f = ps.open(ps.examples.get_path("usjoin36.csv"))
#f
#pci = np.array([f.by_col[str(y)] for y in range(1,1096)])
pci = pci.transpose()
rpci = pci/(pci.mean(axis=0))
#w = ps.open(ps.examples.get_path("states36.gal")).read()
w = ps.open("C:/Users/Yousif/Desktop/Spatial.MC/states-36-Copy.gal").read( )
w.transform = 'r'
sm = ps.Spatial_Markov(rpci, w, fixed=True, k=5, variable_name='rpci')
for p in sm.P:
print(p)
似乎 ps.Spatial_Markov
想要一个 numpy 数组,而您传递的是 pandas 数据框。尝试通过修改此行
pci = f[list(map(str,range(1, 1096)))].as_matrix()
更新
的确,docs 是关于这一点的:
pysal.spatial_dynamics.markov.Spatial_Markov(y, w, k=4, permutations=0, fixed=False, variable_name=None)
Parameters:
- y (array) – (n,t), one row per observation, one column per state of each observation, with as many columns as time periods. ...