<jupyter_start><jupyter_text>NH3 Categorical Regressors AnalysisREGRESSOR: Aree in classi con popolazione<jupyter_code>import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np
agricolture_classes_shp = gpd.read_file('../../resources/analisi_spaziale/Analisi_spaziale_8classi.shp')
agricolture_classes_shp.to_crs(epsg=4326, inplace=True)
agricolture_classes_shp.head()
fig, ax = plt.subplots(1, figsize=(5,5))

agricolture_classes_shp.plot(column='DESCR_CLAS', legend=True, ax=ax, legend_kwds={'loc': 'right', 'bbox_to_anchor': (2, 0.5)})
ax.set_title('Spatial Analysis Classes')
ax.set_xlabel('longitude')
ax.set_ylabel('latiitude')
plt.show()
population_shp = gpd.read_file('../../resources/residenti/residenti_2017.shp')
population_shp.to_crs(epsg=4326, inplace=True)
population_shp.head()
fig, ax = plt.subplots(1, figsize=(5,5))

population_shp.plot(column='NUM_ABIT_S', legend=True, ax=ax)

ax.set_title('Population Density 2017')
ax.set_xlabel('longitude')
ax.set_ylabel('latiitude')

plt.show()
comuni = gpd.read_file('../../resources/comuni/coordinate/comuni_shp/features-points.shp')
comuni.head()
import pandas as pd

agricolture_classes = pd.DataFrame(columns=['name', 'class' ])
agricolture_classes['name'] = agricolture_classes_shp['NOME_COMUN']
agricolture_classes['class'] = agricolture_classes_shp['DESCR_CLAS']
agricolture_classes.head()
pop_density = pd.DataFrame(columns=['name', 'pop_density' ])
pop_density['name'] = population_shp['NOME_COM']
pop_density['pop_density'] = population_shp['NUM_ABIT_S']
pop_density.head()
agricolture_classes['name'] = agricolture_classes['name'].apply(lambda name: name.upper())
pop_density['name'] = pop_density['name'].apply(lambda name: name.upper())
table = pd.merge(agricolture_classes, pop_density, how='inner', on=['name'])
table.head()
comuni_shp = gpd.read_file('../../resources/comuni/coordinate/comuni_shp/features-points.shp')
comuni_shp.to_crs(epsg=4326, inplace=True)
comuni_shp.head()
fig, ax = plt.subplots(1, figsize=(5,5))

comuni_shp.plot(ax=ax)

ax.set_title('Comuni Lombardia')
ax.set_xlabel('longitude')
ax.set_ylabel('latiitude')

plt.show()

comuni_coords = pd.DataFrame(columns=['name', 'latitude', 'longitude'])
comuni_coords['name'] = comuni_shp['Name']
comuni_coords['point'] = comuni_shp['geometry']
comuni_coords.head()
comuni_coords['longitude'] = comuni_coords['point'].apply(lambda point: round(float(point.bounds[0]), 2))
comuni_coords['latitude'] = comuni_coords['point'].apply(lambda point: round(float(point.bounds[1]), 2))
comuni_coords.drop(columns='point', inplace=True)
comuni_coords.head()
comuni_coords['name'] = comuni_coords['name'].apply(lambda name: name.upper())
table = pd.merge(table, comuni_coords, how='inner', on=['name'])
table.head()
population_shp['class'] = np.nan
population_shp['weighted_class'] = np.nan
for i in range(len(population_shp)):

    name = population_shp['NOME_COM'][i]
    agr_class = table[table['name'] == name]['class']

    if len(agr_class) > 0:
        
        population_shp['class'][i] = agr_class.item()  
        population_shp['weighted_class'][i] = population_shp['NUM_ABIT_S'][i] if agr_class.item().lower() == 'aree rurali' else np.nan
fig, ax = plt.subplots(1, figsize=(5,5))

population_shp.plot(column='weighted_class', legend=True, ax=ax)

ax.set_title('Population Density in Rural Areas')
ax.set_xlabel('longitude')
ax.set_ylabel('latiitude')

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16956521739130434, 'fraction_numerical': 0.01217391304347826, 'mean_word_length': 8.05774278215223, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '125094', 'license': 'MIT', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1325, 'n_words': 186, 'path': 'notebooks/preliminar-analysis/nh3-categorical-regressors-analysis.ipynb', 'repo_name': 'samuelexferri/unibg-statistic2', 'size': '3450'}