0

I have used python to create spatial Markov matrix but I got this error (AttributeError: 'DataFrame' object has no attribute 'flatten' ). I am not familiar with python so, I hope to help me to solve this problem? the codes is

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)

enter image description here

yousif
  • 23
  • 1
  • 4

1 Answers1

0

Seems like ps.Spatial_Markov wants a numpy array, while you pass a pandas data frame. Try extracting the array from the data frame by modifying this line

pci = f[list(map(str,range(1, 1096)))].as_matrix()

Update

Indeed, the docs are quit specific about that:

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. ...

Eli Korvigo
  • 10,265
  • 6
  • 47
  • 73
  • Thanks a lot, Dear Eli Korvigo for your asnwer. I have used your code but I also get error module 'pysal' has no attribute 'Quantiles' – yousif Apr 09 '19 at 13:08
  • @yousif this looks like a separate issue: I don't see any potentiat source in your question. Are you trying to access this attribute somewhere else in your code? – Eli Korvigo Apr 10 '19 at 13:59
  • May I have your email please, I will send to you all codes maybe you can find the mistake? this is my email p90499@siswa.ukm.edu.my...regards – yousif Apr 15 '19 at 04:42
  • import libpysal #f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv")) f = pd.read_csv("Last37API.csv") pci = np.array([f.by_col[str(y)] for y in range(1,1096)]) print(pci.shape) I got this error AttributeError: 'DataFrame' object has no attribute 'by_col' – yousif Apr 16 '19 at 13:39
  • Once again, the error message is pretty clear: you are trying to access attribute `by_col` of a `DataFrame` object `f` (created here `f = pd.read_csv("Last37API.csv")`), though `DataFrame` objects don't have this attribute. This is the second time you try to ask a question that's not related to your original question, and this is not how this site works. If you have any further questions, ask them separately. That being said, I urge you to read error messages and tracebacks yourself before starting a question. – Eli Korvigo Apr 16 '19 at 15:53