2017-12-15 14:24:17 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-01-20 18:18:03 +01:00
|
|
|
import numpy as np
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
import matplotlib
|
|
|
|
|
|
|
|
matplotlib.style.use('ggplot')
|
|
|
|
|
|
|
|
plt.rcParams['figure.figsize'] = (10, 7)
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
from sklearn import linear_model
|
|
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
from mpl_toolkits.mplot3d import Axes3D
|
|
|
|
from sklearn.metrics import mean_squared_error, r2_score
|
|
|
|
|
|
|
|
# funkcja, która wczyta zestaw danych z pliku *mieszkania.csv* i zwróci obiekt typu *DataFrame*.
|
2017-12-15 14:24:17 +01:00
|
|
|
def wczytaj_dane():
|
2018-01-20 18:18:03 +01:00
|
|
|
dane = pd.read_csv('mieszkania.csv', # ścieżka do pliku
|
|
|
|
sep=',', # separator
|
|
|
|
index_col='Id') # ustawienie indeksu na kolumnę Id
|
|
|
|
return (dane)
|
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
def most_common_room_number(dane):
|
2018-01-20 18:18:03 +01:00
|
|
|
return (dane['Rooms'].mode()[0])
|
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
def cheapest_flats(dane, n):
|
2018-01-20 18:18:03 +01:00
|
|
|
return (dane.nsmallest(n, 'Expected'))
|
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
def find_borough(desc):
|
|
|
|
dzielnice = ['Stare Miasto',
|
|
|
|
'Wilda',
|
|
|
|
'Jeżyce',
|
|
|
|
'Rataje',
|
|
|
|
'Piątkowo',
|
|
|
|
'Winogrady',
|
|
|
|
'Miłostowo',
|
|
|
|
'Dębiec']
|
2018-01-20 18:18:03 +01:00
|
|
|
for i in dzielnice:
|
|
|
|
if desc.find(i) + 1:
|
|
|
|
return (i)
|
|
|
|
return ('Inne')
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def add_borough(dane):
|
2018-01-20 18:18:03 +01:00
|
|
|
dane['Borough'] = dane['Location'].apply(find_borough)
|
|
|
|
return (dane)
|
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
def write_plot(dane, filename):
|
2018-01-20 18:18:03 +01:00
|
|
|
fig = plt.figure()
|
|
|
|
dane.plot(kind='bar')
|
|
|
|
plt.savefig(filename)
|
|
|
|
plt.close(fig)
|
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
def mean_price(dane, room_number):
|
2018-01-20 18:18:03 +01:00
|
|
|
return (dane.groupby(['Rooms']).mean()['Expected'][room_number])
|
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
def find_13(dane):
|
2018-01-20 18:18:03 +01:00
|
|
|
return (dane[dane['Floor'] == 13]['Borough'].tolist())
|
|
|
|
|
|
|
|
|
|
|
|
def find_best_flats(dane, borough='Winogrady', rooms=3, floor=1):
|
|
|
|
return (dane[(dane['Borough'] == borough) & (dane['Rooms'] == rooms) & (dane['Floor'] == floor)])
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
dane = wczytaj_dane()
|
|
|
|
print(dane[:5])
|
|
|
|
|
2018-01-20 18:18:03 +01:00
|
|
|
print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}."
|
2017-12-15 14:24:17 +01:00
|
|
|
.format(most_common_room_number(dane)))
|
|
|
|
|
2018-01-20 18:18:03 +01:00
|
|
|
print('Pięć najtańszych ofert sprzedaży mieszkań: \n{}'
|
|
|
|
.format(cheapest_flats(dane, 5)))
|
|
|
|
|
|
|
|
print("{} to najładniejsza dzielnica w Poznaniu."
|
|
|
|
.format(find_borough("Grunwald i Jeżyce")))
|
|
|
|
|
|
|
|
# dane['Borough'] = dane['Location'].apply(find_borough)
|
|
|
|
|
|
|
|
dane = add_borough(dane)
|
|
|
|
|
|
|
|
write_plot(dane.groupby(['Borough']).count()['Expected'], 'filename')
|
|
|
|
|
|
|
|
print('Dzielnice, które zawierają ofertę mieszkanie na 13 piętrze to: {}.'.format(', '.join(find_13(dane))))
|
|
|
|
|
|
|
|
print('Średnia cena mieszkania {0}-pokojowego z ofert sprzedaży mieszkań wynosi: {1:.0f} zł.'
|
|
|
|
.format(3, mean_price(dane, 3)))
|
|
|
|
|
|
|
|
# print(find_best_flats(dane))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Dzielimy dane na dwie części - jedną do uczenia 80% oraz drugą do testowania 20%
|
|
|
|
|
|
|
|
dane_X = dane[['SqrMeters', 'Rooms']]
|
|
|
|
dane_Y = dane[['Expected']]
|
|
|
|
dane_X_train, dane_X_test, dane_Y_train, dane_Y_test = train_test_split(dane_X,
|
|
|
|
dane_Y,
|
|
|
|
test_size=0.2,
|
|
|
|
random_state=2)
|
|
|
|
|
|
|
|
# Tworzymy model regresji wielorakiej, zmienną wyjaśnianą będzie cena
|
|
|
|
# mieszkania, zmiennymi wyjaśniającymi będą: wielkość mieszkania i liczba
|
|
|
|
# pokoi
|
|
|
|
|
|
|
|
regr = linear_model.LinearRegression()
|
|
|
|
|
|
|
|
# Uczymy model korzystając ze zbioru uczącego
|
|
|
|
regr.fit(dane_X_train, dane_Y_train)
|
|
|
|
|
|
|
|
# Współczynniki modelu
|
|
|
|
print('Współczynniki: {0:.4f} (SqrMeters) {1:.4f} (Rooms)'.format(regr.coef_[0,0],
|
|
|
|
regr.coef_[0,1]))
|
|
|
|
# Dokonujemy predykcji korzystając ze zbioru testującego
|
|
|
|
dane_Y_pred = regr.predict(dane_X_test)
|
|
|
|
|
|
|
|
# Test
|
|
|
|
print('Dopasowanie modelu: {:.4f}'.format(regr.score(dane_X_test, dane_Y_test)))
|
|
|
|
|
|
|
|
|
|
|
|
# Błąd średniokwadratowy
|
|
|
|
print('Błąd średniokwadratowy: {:.4f}'.format(mean_squared_error(dane_Y_test, dane_Y_pred)))
|
|
|
|
|
|
|
|
# Explained variance score: 1 is perfect prediction
|
|
|
|
print('Variance score: {:.4f}'.format(r2_score(dane_Y_test, dane_Y_pred)))
|
|
|
|
|
2017-12-15 14:24:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|