def _knn_property_values(df, bounds): clf = neighbors.KNeighborsRegressor(n_neighbors=10, weights='distance') X = df[['LATITUDE', 'LONGITUDE']].values y = df['PPSQFT'] clf.fit(X, y) h = .0003 # step size in the mesh # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, m_max]x[y_min, y_max]. yy, xx = np.meshgrid(np.arange(bounds[0][0], bounds[1][0], h), np.arange(bounds[0][1], bounds[1][1], h)) clf.fit(X, y) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) MCRASPlotting.leaflet_heatmap( yy=yy, xx=xx, Z=Z, bounds=bounds, map_path='home_value_model.html', legend_text='Estimated value per square foot of home ($)', description_title='Predicting home values in Boston', description_text='Using <a target="_blank" ' 'href="http://scikit-learn.org/stable/modules/generated/' 'sklearn.neighbors.KNeighborsRegressor.html#sklearn.neighbors.' 'KNeighborsRegressor">k-nearest ' 'neighbors regression</a>, we fit a model over ' 'the area of downtown Boston with latitude and longitude as ' 'predictors and home values from 2015 assessors data to ' 'create a spatial model of home prices across the city.')
def _knn_weekday_analysis(df, bounds): X = df[['LATITUDE', 'LONGITUDE']].values y = (1 * df['day_week'].isin(['Friday', 'Saturday', 'Sunday'])).astype(int) neighbors_numbers = np.arange(10, 55, 1) clf = neighbors.KNeighborsClassifier(10, weights='distance') max_score = 0 for n_neighbors in neighbors_numbers: clf = neighbors.KNeighborsClassifier(n_neighbors, weights='distance') clf.fit(X, y) if max_score + .001 > clf.score(X, y): break h = .0003 # step size in the mesh # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, m_max]x[y_min, y_max]. yy, xx = np.meshgrid(np.arange(bounds[0][0], bounds[1][0], h), np.arange(bounds[0][1], bounds[1][1], h)) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) MCRASPlotting.leaflet_heatmap(yy=yy, xx=xx, Z=Z, bounds=bounds, map_path='crime_knn_weekday.html', legend_text='Weekday (0) vs Weekend (1)', tiles='osm_mapnik', description_title='Predicting weekend vs weekday crimes in Boston', description_text='Using <a target="_blank" ' 'href="http://scikit-learn.org/stable/modules/generated' '/sklearn.neighbors.KNeighborsClassifier.html">k-nearest ' 'neighbors classification</a>, we fit a model over ' 'the area of downtown Boston with latitude and longitude as ' 'predictors and all crimes from January to August 2015, ' 'flagged with day of the week they were committed.')
def _knn_property_values(df, bounds): clf = neighbors.KNeighborsRegressor(n_neighbors=10, weights='distance') X = df[['LATITUDE', 'LONGITUDE']].values y = df['PPSQFT'] clf.fit(X, y) h = .0003 # step size in the mesh # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, m_max]x[y_min, y_max]. yy, xx = np.meshgrid(np.arange(bounds[0][0], bounds[1][0], h), np.arange(bounds[0][1], bounds[1][1], h)) clf.fit(X, y) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) MCRASPlotting.leaflet_heatmap(yy=yy, xx=xx, Z=Z, bounds=bounds, map_path='home_value_model.html', legend_text='Estimated value per square foot of home ($)', description_title='Predicting home values in Boston', description_text='Using <a target="_blank" ' 'href="http://scikit-learn.org/stable/modules/generated/' 'sklearn.neighbors.KNeighborsRegressor.html#sklearn.neighbors.' 'KNeighborsRegressor">k-nearest ' 'neighbors regression</a>, we fit a model over ' 'the area of downtown Boston with latitude and longitude as ' 'predictors and home values from 2015 assessors data to ' 'create a spatial model of home prices across the city.')