<jupyter_start><jupyter_text>Valor real de los inmuebles: optimización de recursos provinciales desde un paradigma progresivo.  1. Tareas Preliminares I. Importación de librerías<jupyter_code>import pandas as pd
import statsmodels.formula.api as smf
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as sp
import os
import folium
import pysal
import geopandas as gpd
%matplotlib inline<jupyter_output><empty_output><jupyter_text>II. Importación de datos<jupyter_code>df = pd.read_excel("datos_tesis.xls")
df
df.tail()<jupyter_output><empty_output><jupyter_text>III. Construimos las variables necesarias<jupyter_code># convertimos las variables en floats
df['bsqm'] = df['ConstructionSize'].astype(np.float64, errors='ignore')
df['lat'] = df['Latitude'].astype(np.float64)
df['long'] = df['Longitude'].astype(np.float64)


# generacion de variables dummies segun tipo de inmueble
df['house'] = 0
df['lot'] = 0
df['dept'] = 0
df.loc[(df.PropertyType=="Casas"), 'house'] = 1
df.loc[(df.PropertyType=="Lotes"), 'lot'] = 1
df.loc[(df.PropertyType=="Departamentos"), 'dept'] = 1

df['type'] = df['PropertyType']

# generacion de variables dummies segun distrito (departamento)
df['dcap'] = 0 # capital
df['dsta'] = 0 # santa lucia
df['driv'] = 0 # rivadavia
df['draw'] = 0 # rawson
df['dchi'] = 0 # chimbas
df['dpoc'] = 0 # pocito

df.loc[(df.District=="Capital"), 'dcap'] = 1
df.loc[(df.District=="Santa Lucia"), 'dsta'] = 1
df.loc[(df.District=="Rivadavia"), 'driv'] = 1
df.loc[(df.District=="Rawson"), 'draw'] = 1
df.loc[(df.District=="Pocito"), 'dpoc'] = 1
df.loc[(df.District=="Chimbas"), 'dchi'] = 1

# reemplazo de valores erroneos
#df.loc[(df.Bedrooms=='NaN'), 'Bedrooms'] = 0
#df.loc[(df.Bathrooms=='NaN'), 'Bathrooms'] = 0
#df.loc[(df.CarParkingLots=='NaN'), 'CarParkingLots'] = 0

df['Bedrooms'] = df['Bedrooms'].astype(np.float64, errors='ignore')
df['Bathrooms'] = df['Bathrooms'].astype(np.float64, errors='ignore')
df['CarParkingLots'] = df['CarParkingLots'].astype(np.float64, errors='ignore')


# generacion de variables dummies segun cantidad de banos
df['baths'] = df['Bathrooms']
df['bath1'] = 0
df['bath2'] = 0
df['bath3'] = 0
df['bath4'] = 0
df['bath5'] = 0
df.loc[(df.Bathrooms==1), 'bath1'] = 1
df.loc[(df.Bathrooms==2), 'bath2'] = 1
df.loc[(df.Bathrooms==3), 'bath3'] = 1
df.loc[(df.Bathrooms==4), 'bath4'] = 1
df.loc[(df.Bathrooms==5), 'bath5'] = 1

# generacion de variables dummies segun cantidad de dormitorios
df['dorms'] = df['Bedrooms']
df['dorm1'] = 0
df['dorm2'] = 0
df['dorm3'] = 0
df['dorm4'] = 0
df['dorm5'] = 0
df.loc[(df.Bedrooms==1), 'dorm1'] = 1
df.loc[(df.Bedrooms==2), 'dorm2'] = 1
df.loc[(df.Bedrooms==3), 'dorm3'] = 1
df.loc[(df.Bedrooms==4), 'dorm4'] = 1
df.loc[(df.Bedrooms==5), 'dorm5'] = 1

# generacion de variables dummies segun cantidad de cocheras
df['garages'] = df['CarParkingLots']
df['garage1'] = 0
df['garage2'] = 0
df['garage3'] = 0
df['garage4'] = 0
df['garage5'] = 0
df.loc[(df.CarParkingLots==1), 'garage1'] = 1
df.loc[(df.CarParkingLots==2), 'garage2'] = 1
df.loc[(df.CarParkingLots==3), 'garage3'] = 1
df.loc[(df.CarParkingLots==4), 'garage4'] = 1
df.loc[(df.CarParkingLots==5), 'garage5'] = 1

# precios en pesos y unificados
USDxARS = 43

df['PriceArs'] = df['Price']
df['PriceArs'] = df['PriceArs'].fillna(df.PriceUS*USDxARS)

# unificacion m2
df['tsqm'] = df['LotLandSize']

# forzamos variables a float
df['tsqm'] = pd.to_numeric(df.tsqm, errors='coerce').astype(np.float64, errors='ignore')
df['bsqm'] = pd.to_numeric(df.bsqm, errors='coerce').astype(np.float64, errors='ignore')

# Calculamos el cuadrado de estas variables
df['tsqm2'] = df['tsqm'].apply(np.square)
df['bsqm2'] = df['bsqm'].apply(np.square)


# logaritmo natural del precios
#df['p'] = df['PriceArs']
#df['lp'] = df['p'].apply(np.log)
df['lprice'] = df['PriceArs'].apply(np.log)

# ratio de metros construidos
df['b_ratio'] = df['bsqm']/df['tsqm']

df['pm2'] = df['PriceArs']/df['tsqm']<jupyter_output><empty_output><jupyter_text>IV. Filtramos el dataframe a las variables homogeneizadas que nos interesan<jupyter_code>df = df.filter(['PriceArs','lprice', 'tsqm', 'tsqm2', 'bsqm', 'bsqm2', 'house', 'dept', 'lot', 'lat', 'long',
                'bath1', 'bath2', 'bath3', 'bath4', 'bath5', 'garages', 'garage1', 'garage2', 'garage3',
                'garage4', 'garage5', 'dorm1', 'dorm2', 'dorm3', 'dorm4', 'dorm5', 'mratio', 'baths', 'dorms', 'p', 'lp',
                'type', 'dcap', 'dsta', 'driv', 'draw', 'dpoc', 'dchi', 'pm2', 'b_ratio', 'District'])

df<jupyter_output><empty_output><jupyter_text>2. Análisis exploratorio de datos<jupyter_code>sns.pairplot(df, vars=['PriceArs'], hue='type', height=7, markers='type' );
'Distribución de precios de los inmuebles: \
Casas en Azul, Departamentos en Naranja y Lotes en Verde'
sns.pairplot(df, vars=['lprice'], hue='type', height=7, markers='type' );
'Distribución del logaritmo de precios de los inmuebles: \
Casas en Azul, Departamentos en Naranja y Lotes en Verde'
sns.pairplot(df, vars=['pm2'], hue='type', height=7, markers='type' );
'Distribución del logaritmo de precios de los inmuebles: \
Casas en Azul, Departamentos en Naranja y Lotes en Verde'
df.pivot_table('pm2', columns='type', aggfunc=['mean', np.std])<jupyter_output><empty_output><jupyter_text>3. Feature Selection I. Modelo lineal inicial para Casas<jupyter_code>model1 = smf.ols("lprice ~ tsqm + tsqm2 + bsqm + bsqm2 + dorm1 + dorm2 + dorm3 + dorm4 + dorm5 + bath1 + bath2 + bath3 + bath4 + bath5 + garage1 + garage2 + garage3 + garage4 + garage5 + house + dept + lot ", data=df )
model1_reg = model1.fit()
model1_reg.summary()<jupyter_output><empty_output><jupyter_text>II. Modelo lineal para Departamentos<jupyter_code>model2 = smf.ols("lprice ~ bsqm + dorm1 + dorm2 + dorm3 + dorm4 + dorm5 + bath1 + bath2 + bath3 + bath4 + bath5 + garages + house + dept", data=df )
model2_reg = model2.fit()
model2_reg.summary2()
df.loc[(df.lot==1)]<jupyter_output><empty_output><jupyter_text>III. Modelo lineal para Lotes<jupyter_code>model2 = smf.ols("lprice ~ tsqm + tsqm2 + bsqm + dorm1 + dorm2 + dorm3 + dorm4 + dorm5 + bath1 + bath2 + bath3 + bath4 + bath5 + garages + house + dept", data=df.loc[(df.lot==1)] )
model2_reg = model2.fit()
model2_reg.summary2()<jupyter_output><empty_output><jupyter_text>4. Modelo modificado I. Modelo lineal para Casas<jupyter_code>model1 = smf.ols("lprice ~ tsqm + bsqm + dorm1 + dorm2 + dorm3 + dorm4 + dorm5 + bath1 + bath2 + bath3 + bath4 + bath5 + house + dept + lot + C(District)-1", data=df )
model1_reg = model1.fit()
model1_reg.summary()<jupyter_output>E:\Miniconda3\envs\py36\lib\site-packages\statsmodels\regression\linear_model.py:1471: RuntimeWarning: divide by zero encountered in double_scalars
  return np.sqrt(eigvals[0]/eigvals[-1])
E:\Miniconda3\envs\py36\lib\site-packages\statsmodels\base\model.py:1036: RuntimeWarning: invalid value encountered in true_divide
  return self.params / self.bse
E:\Miniconda3\envs\py36\lib\site-packages\scipy\stats\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in greater
  return (self.a < x) & (x < self.b)
E:\Miniconda3\envs\py36\lib\site-packages\scipy\stats\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in less
  return (self.a < x) & (x < self.b)
E:\Miniconda3\envs\py36\lib\site-packages\scipy\stats\_distn_infrastructure.py:1821: RuntimeWarning: invalid value encountered in less_equal
  cond2 = cond0 & (x <= self.a)<jupyter_text>2. Modelo lineal para departamentos<jupyter_code>model2m = smf.ols("dfd.lprice ~  dfd.tsqm + dfd.tsqm2 + dfd.bsqm + dfd.bsqm2 + dfd.dorms + dfd.baths + dfd.garages", data=dfd )
model2m_reg = model2m.fit()
model2m_reg.summary2()
puntos = df[['long', 'lat']].values
triangulation = sp.spatial.Delaunay(puntos)
tri_2d = sp.spatial.delaunay_plot_2d(triangulation)
puntos_dept = dfd[['long', 'lat']].values
tri_dept = sp.spatial.Delaunay(puntos_dept)
tri_dept_2d = sp.spatial.delaunay_plot_2d(tri_dept)<jupyter_output><empty_output><jupyter_text>A continuacion para hacer pruebas tomamos 10 observaciones unicamente<jupyter_code># Prueba de matriz de delaunay
dfp = df.iloc[205:235]

points_p = dfp[['long', 'lat']].values
tp = sp.spatial.Delaunay(points_p)
sp.spatial.delaunay_plot_2d(tp)<jupyter_output><empty_output><jupyter_text>NO BORRAR TODAVÍA. QUEDA REVISAR EL CODIGO DE ABAJO QUE PUEDE DAR COMO RESULTADO UN LISTADO DE VECINOS def find_neighbors(pindex, tp):    neighbors = list()    for simplex in tp.vertices:        if pindex in simplex:            neighbors.extend([simplex[i] for i in range(len(simplex)) if simplex[i] != pindex])    now we just have to strip out all the dulicate indices and return the neighbors list:    return list(set(neighbors))  Vecinos DelaunayEl siguiente código busca un elemento del listado de puntos (_pindex_) y averigua cuáles son los vecinos.Nota: en este ejemplo estamos utilizando únicamente los *deptos* ya que el numero de obs es el menor.<jupyter_code>import scipy.spatial
import numpy
import pylab

pindex = 5
points_d = dfp[['lat', 'long']].values
points_x = dfp['lat'].values
points_y = dfp['long'].values

td = sp.spatial.Delaunay(points_d)
neighbor_indices = td.neighbors[pindex]




pylab.plot(points_x, points_y, 'b.')
pylab.plot(points_x[pindex], points_y[pindex], 'dg')
pylab.plot([points_x[i] for i in neighbor_indices],
           [points_y[i] for i in neighbor_indices], 'ro')    

pylab.show()<jupyter_output><empty_output><jupyter_text>¿Qué más hace falta?Necesitamos armar la matriz ponderada NxN de vecinos de Delaunay, donde los vecinos toman valor 1.  MapaEl siguiente mapa muestra los inmuebles con el precio relativo por m2 representado por el radio del círculo.- Casas en color Azul- Departamentos en color Rojo- Lotes en color Verde Falta graficar la triangulación de Delaunay para que se vean todos los triángulos que se generan.<jupyter_code>mapa = folium.Map(location=[-31.539828,-68.514519], zoom_start=11)

# agregamos casas en color azul
for i in range(0,len(dfh)):
    folium.Circle(
        location = [dfh.iloc[i]['lat'], dfh.iloc[i]['long']],
        popup = dfh.iloc[i]['Type']+', '+str(dfh.iloc[i]['PriceArs'])+', '+str(dfh.iloc[i]['tsqm'])+', '+str(dfh.iloc[i]['bsqm']),
        radius = dfh.iloc[i]['pm2']/500,
        color = 'blue',
        fill = True,
        opacity = 0.89,
        fill_color = 'blue',
        stroke = True,
        weight = 1
        ).add_to(mapa)

# agregamos deptos en color rojo
for i in range(0,len(dfd)):
    folium.Circle(
        location = [dfd.iloc[i]['lat'], dfd.iloc[i]['long']],
        popup = dfd.iloc[i]['Type']+', '+str(dfd.iloc[i]['PriceArs'])+', '+str(dfd.iloc[i]['tsqm'])+', '+str(dfd.iloc[i]['bsqm']),
        radius = dfd.iloc[i]['pm2']/500,
        color = 'red',
        fill = True,
        opacity = 0.89,
        fill_color = 'red',
        stroke = True,
        weight = 1
        ).add_to(mapa)

# agregamos lotes en color verde
for i in range(0,len(dfl)):
    folium.Circle(
        location = [dfl.iloc[i]['lat'], dfl.iloc[i]['long']],
        popup = dfl.iloc[i]['Type']+', '+str(dfl.iloc[i]['PriceArs'])+', '+str(dfl.iloc[i]['tsqm'])+', '+str(dfl.iloc[i]['bsqm']),
        radius = dfl.iloc[i]['pm2']/500,
        color = 'green',
        fill = True,
        opacity = 0.89,
        fill_color = 'green',
        stroke = True,
        weight = 1
        ).add_to(mapa)

mapa<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17132346500832094, 'fraction_numerical': 0.028028378733467637, 'mean_word_length': 5.047669491525424, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '218737', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 4785, 'n_tokens_neox': 4572, 'n_words': 965, 'path': '.ipynb_checkpoints/Apendice computacional-checkpoint.ipynb', 'repo_name': 'francodegio/DigitalHouse', 'size': '11417'}