import pandas as pd

housing = pd.read_csv('housing.csv')
housing.head()

housing.info()

housing.describe()

"""
Explanation: Here we are using the California Housing dataset to learn more about Machine Learning.
End of explanation
"""


housing['total_rooms'].value_counts()

housing['ocean_proximity'].value_counts()

"""
Explanation: In the meanwhile we are trying to have more information about pandas. In the following sections we are using the value_counts method to have more information about each feature values. This method specify number of different values for given feature.
End of explanation
"""


pd.DataFrame([{'a': 1, 'b': '1'}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}]).iloc[1]

pd.DataFrame([{'a': 1, 'b': '1'}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}]).loc[1]

pd.DataFrame([{'a': 1, 'b': '1'}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}]).loc[1, ['b']]

pd.DataFrame([{'a': 1, 'b': '1'}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}]).loc[[True, True, False]]

"""
Explanation: See the difference between loc and iloc methods in a simple pandas DataFrame.
End of explanation
"""


pd.DataFrame([{'a': 1, 'b': '1'}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}])['a'].apply(lambda a: a > 10)

"""
Explanation: Here we want to see the apply function of pandas for an specific feature.
End of explanation
"""


from zlib import crc32
import numpy as np

def test_set_check(identifier, test_ratio):
    return crc32(np.int64(identifier)) & 0xffffffff < test_ratio * 2**32

def split_train_test_by_id(data, test_ratio, id_column):
    ids = data[id_column]
    in_test_set = ids.apply(lambda _id: test_set_check(_id, test_ratio))
    return data.loc[~in_test_set], data.loc[in_test_set]

housing_with_id = housing.reset_index() # adds an "index" column
train_set, test_set = split_train_test_by_id(housing_with_id, 0.2, 'index')

housing = train_set.copy()
housing.plot(kind="scatter", x="longitude", y="latitude", alpha=0.1)

import matplotlib.pyplot as plt

housing.plot(kind='scatter', x='longitude', y='latitude',
             alpha=0.4, s=housing['population']/100, label='population',
             c='median_house_value', cmap=plt.get_cmap('jet'), colorbar=True,
            )

"""
Explanation: The following function helps to split the given dataset into test and train sets.
End of explanation
"""


