diff --git a/labs06/Pandas.ipynb b/labs06/Pandas.ipynb index 0d9d551..3765ff1 100644 --- a/labs06/Pandas.ipynb +++ b/labs06/Pandas.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -299,30 +299,12 @@ ] }, { - "cell_type": "code", - "execution_count": 16, + "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "count 26.000000\n", - "mean 8.576923\n", - "std 5.375300\n", - "min 1.000000\n", - "25% 4.250000\n", - "50% 8.000000\n", - "75% 12.000000\n", - "max 18.000000\n", - "dtype: float64\n" - ] - } - ], "source": [ "print(dane.describe())" ] diff --git a/labs06/task02.py b/labs06/task02.py index 9d96016..fdbedc8 100755 --- a/labs06/task02.py +++ b/labs06/task02.py @@ -1,14 +1,15 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import pandas as pd def wczytaj_dane(): - pass + return pd.read_csv('mieszkania.csv') def most_common_room_number(dane): - pass + return dane['Rooms'].mode()[0] def cheapest_flats(dane, n): - pass + return dane.sort_values(by=[u'Expected'], na_position='last').head(n) def find_borough(desc): dzielnice = ['Stare Miasto', @@ -19,23 +20,54 @@ def find_borough(desc): 'Winogrady', 'Miłostowo', 'Dębiec'] - pass + 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): - pass + dane['Borough'] = dane.apply( lambda row: find_borough( row['Location']), axis = 1) def write_plot(dane, filename): - pass + dane['Borough'].value_counts().plot.bar().get_figure().savefig(filename) def mean_price(dane, room_number): - pass + return dane.loc[ dane['Rooms'] == room_number ].mean()[1] def find_13(dane): - pass + return dane.loc[dane['Floor'] == 13]['Borough'].unique() def find_best_flats(dane): - pass + 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() @@ -44,11 +76,44 @@ def main(): 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")))) + print("7 najtańszych mieszkań to: ") + print(cheapest_flats(dane, 7)) - print("Średnia cena mieszkania 3-pokojowego, to: {}" - .format(mean_price(dane, 3))) + 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') + + + from sklearn import datasets, linear_model + import numbers as np + from sklearn.metrics import mean_squared_error, r2_score + + + + + # + # diabetes = datasets.load_diabetes() + # diabetes_X = diabetes.data[:, None, 3] + # diabetes_X_train = diabetes_X[:-20] + # diabetes_X_test = diabetes_X[-20:] + # diabetes_y_train = diabetes.target[:-20] + # diabetes_y_test = diabetes.target[-20:] + # regr = linear_model.LinearRegression() + # regr.fit(diabetes_X_train, diabetes_y_train) + # diabetes_y_pred = regr.predict(diabetes_X_test) + # print('Coefficients: \n', regr.coef_) + # print('Mean squared error: %.2f' % mean_squared_error(diabetes_y_test, diabetes_y_pred)) + # + # print( diabetes.data[:, None, 3].shape ) if __name__ == "__main__": main()