#!/usr/bin/env python # -*- coding: utf-8 -*- import pandas as pd def wczytaj_dane(): return pd.read_csv('mieszkania.csv') def most_common_room_number(dane): return dane['Rooms'].mode()[0] def cheapest_flats(dane, n): return dane.sort_values(by=[u'Expected'], na_position='last').head(n) def find_borough(desc): dzielnice = ['Stare Miasto', 'Wilda', 'Jeżyce', 'Rataje', 'Piątkowo', 'Winogrady', 'Miłostowo', 'Dębiec'] histogram = {districtName: desc.find(districtName) for districtName in dzielnice if desc.find(districtName) != -1} return 'Inne' if not histogram else min(histogram, key=histogram.get) def add_borough(dane): dane['Borough'] = dane.apply( lambda row: find_borough( row['Location']), axis = 1) def write_plot(dane, filename): dane['Borough'].value_counts().plot.bar().get_figure().savefig(filename) def mean_price(dane, room_number): return dane.loc[ dane['Rooms'] == room_number ].mean()[1] def find_13(dane): return dane.loc[dane['Floor'] == 13]['Borough'].unique() def find_best_flats(dane): return dane.loc[(dane['Borough'] == 'Winogrady') & (dane['Rooms'] == 3) & (dane['Floor'] == 1)] def predict(dane, rooms, sqrMeters): from sklearn import linear_model import numpy as np data = dane df = pd.DataFrame(data, columns=np.array(['Rooms','SqrMeters'])) target = pd.DataFrame(data, columns=["Expected"]) X = df y = target["Expected"] lm = linear_model.LinearRegression() model = lm.fit(X, y) inData = pd.DataFrame.from_records([(rooms, sqrMeters)], columns=['Rooms', 'SqrMeters']) return lm.predict(inData)[0] def main(): dane = wczytaj_dane() print(dane[:5]) print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}" .format(most_common_room_number(dane))) print("7 najtańszych mieszkań to: ") print(cheapest_flats(dane, 7)) print("{} to najłądniejsza dzielnica w Poznaniu.".format(find_borough("Grunwald i Jeżyce"))) add_borough(dane) write_plot(dane, 'tmp_borough_hist.png') for i in dane['Rooms'].unique(): print("Średnia cena mieszkania {}-pokojowego, to: {}".format(i, mean_price(dane, i))) print('Dzielnice z mieszkaniami na 13 piętrze: {}'.format(find_13(dane))) print('"Najlepsze" mieszkania: ') print(find_best_flats(dane)) print('Predicted price(actual 146000): ', predict(dane,1,31.21)) if __name__ == "__main__": main()