2018-01-11 18:20:25 +01:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2018-01-12 16:55:14 +01:00
|
|
|
|
"""
|
|
|
|
|
** zad. 2 (domowe) **
|
|
|
|
|
Jest to zadanie złożone, składające się z kilku części. Całość będzie opierać się o dane zawarte w pliku *mieszkania.csv* i dotyczą cen mieszkań w Poznaniu kilka lat temu.
|
|
|
|
|
1. Uzupełnił funkcje ``write_plot``, która zapisze do pliku ``filename`` wykres słupkowy przedstawiający liczbę ogłoszeń mieszkań z podziałem na dzielnice.
|
|
|
|
|
1. *(dodatkowe)*: Korzystając z pakietu *sklearn* zbuduj model regresji liniowej, która będzie wyznaczać cenę mieszkania na podstawie wielkości mieszkania i liczby pokoi.
|
|
|
|
|
"""
|
|
|
|
|
import pandas as pd
|
|
|
|
|
import statistics
|
|
|
|
|
|
2018-01-11 18:20:25 +01:00
|
|
|
|
def wczytaj_dane():
|
2018-01-12 16:55:14 +01:00
|
|
|
|
raw_data = pd.read_csv('mieszkania.csv',sep=',')
|
|
|
|
|
data = pd.DataFrame(raw_data)
|
|
|
|
|
return data
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
|
|
|
|
def most_common_room_number(dane):
|
2018-01-12 16:55:14 +01:00
|
|
|
|
rooms=dane['Rooms']
|
|
|
|
|
return(int(statistics.mode(rooms)))
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
|
|
|
|
def cheapest_flats(dane, n):
|
2018-01-12 16:55:14 +01:00
|
|
|
|
cheapest=pd.DataFrame(dane['Expected'])
|
|
|
|
|
cheapest.sort=cheapest.sort_values(by=['Expected'])
|
|
|
|
|
return cheapest.sort[:n]
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
|
|
|
|
def find_borough(desc):
|
|
|
|
|
dzielnice = ['Stare Miasto',
|
|
|
|
|
'Wilda',
|
|
|
|
|
'Jeżyce',
|
|
|
|
|
'Rataje',
|
|
|
|
|
'Piątkowo',
|
|
|
|
|
'Winogrady',
|
|
|
|
|
'Miłostowo',
|
|
|
|
|
'Dębiec']
|
2018-01-12 17:37:46 +01:00
|
|
|
|
for dzielnica in dzielnice:
|
|
|
|
|
if desc.find(dzielnica)!=-1:
|
|
|
|
|
return dzielnica
|
|
|
|
|
return 'Inne'
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_borough(dane):
|
2018-01-12 17:37:46 +01:00
|
|
|
|
borough_list=[]
|
|
|
|
|
for data in dane['Location']:
|
|
|
|
|
borough_list.append(find_borough(data))
|
|
|
|
|
borough_series = pd.Series(borough_list,name='Borough')
|
|
|
|
|
dane['Borough']=borough_series
|
|
|
|
|
return dane
|
|
|
|
|
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
|
|
|
|
def write_plot(dane, filename):
|
2018-01-12 17:54:03 +01:00
|
|
|
|
|
2018-01-11 18:20:25 +01:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def mean_price(dane, room_number):
|
2018-01-12 17:54:03 +01:00
|
|
|
|
data=dane.loc[dane['Rooms'] == room_number]
|
|
|
|
|
return round(statistics.mean(data['Expected']),2)
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
|
|
|
|
def find_13(dane):
|
2018-01-12 17:54:03 +01:00
|
|
|
|
data = add_borough(dane)
|
|
|
|
|
boroughs = data.loc[data['Floor'] == 13]
|
|
|
|
|
return set(boroughs['Borough'])
|
|
|
|
|
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
|
|
|
|
def find_best_flats(dane):
|
2018-01-12 18:03:53 +01:00
|
|
|
|
data = add_borough(dane)
|
|
|
|
|
best_flats = data.loc[(data['Borough'] == 'Winogrady') & (data['Floor'] == 1) & (data['Rooms'] == 3)]
|
|
|
|
|
return best_flats
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
2018-01-12 16:55:14 +01:00
|
|
|
|
|
2018-01-11 18:20:25 +01:00
|
|
|
|
def main():
|
|
|
|
|
dane = wczytaj_dane()
|
2018-01-12 17:54:03 +01:00
|
|
|
|
print(dane[:5])
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
2018-01-12 18:03:53 +01:00
|
|
|
|
print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}"
|
|
|
|
|
.format(most_common_room_number(dane)))
|
|
|
|
|
|
|
|
|
|
print("{} to najłądniejsza dzielnica w Poznaniu."
|
|
|
|
|
.format(find_borough("Grunwald i Jeżyce")))
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
2018-01-12 17:37:46 +01:00
|
|
|
|
|
2018-01-12 18:03:53 +01:00
|
|
|
|
print("Średnia cena mieszkania 3-pokojowego, to: {}"
|
|
|
|
|
.format(mean_price(dane, 3)))
|
2018-01-12 16:55:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-01-11 18:20:25 +01:00
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|