forked from tdwojak/Python2017
4197571375
# Conflicts: # labs02/test_task.py
98 lines
3.1 KiB
Python
Executable File
98 lines
3.1 KiB
Python
Executable File
#!/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, col_name):
|
|
from sklearn import linear_model
|
|
from sklearn.metrics import mean_squared_error, r2_score
|
|
d_X = pd.DataFrame(dane[col_name])
|
|
print('Dane z kolumny ', col_name)
|
|
print(d_X.head())
|
|
d_X_train = d_X[4000:]
|
|
d_X_test = d_X[:4000]
|
|
d_y = pd.DataFrame(dane['Expected'])
|
|
d_y_train = d_y[4000:]
|
|
d_y_test = d_y[:4000]
|
|
regr = linear_model.LinearRegression()
|
|
regr.fit(d_X_train, d_y_train)
|
|
y_pred = regr.predict(d_X_test)
|
|
print('MODEL(%s): pred_y = %f * x + %f' % (col_name, regr.coef_[0], regr.intercept_) )
|
|
print('Mean squared error: %.2f' % mean_squared_error(d_y_test, y_pred))
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
plt.clf()
|
|
dataLine, = plt.plot(d_X_test, d_y_test, 'ro', label='collected data')
|
|
predLine, = plt.plot(d_X_test, y_pred, color='blue', linestyle='--', linewidth = 2, label='predictions')
|
|
ax = plt.gca().add_artist(plt.legend(handles=[dataLine], loc=1))
|
|
plt.legend(handles=[predLine], loc=4)
|
|
plt.xticks(())
|
|
plt.yticks(())
|
|
plt.xlabel(col_name)
|
|
plt.ylabel('Price')
|
|
plt.show()
|
|
|
|
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))
|
|
|
|
predict(dane, 'Rooms')
|
|
predict(dane, 'SqrMeters')
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|