<jupyter_start><jupyter_text>Exploratory Data Analysis<jupyter_code>hs_d.isnull().sum()
hs_d.nunique()<jupyter_output><empty_output><jupyter_text>The Cateforical features for the Dataset<jupyter_code>for i in hs_d.columns:
    if hs_d[i].nunique() <= 10:
        print ( 'count value for each category in each feature: {}'.format(hs_d.groupby(i)[i].count()), '\n')

print ('The number of Categorical feautures: {}'.format(len(hs_d.columns)))
hs_d.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 4377 entries, 0 to 4376
Data columns (total 17 columns):
 #   Column          Non-Null Count  Dtype  
---  ------          --------------  -----  
 0   price           4377 non-null   float64
 1   bedrooms        4377 non-null   float64
 2   bathrooms       4377 non-null   float64
 3   toilets         4377 non-null   float64
 4   estate_flag     4377 non-null   float64
 5   terrace_flag    4377 non-null   float64
 6   new_flag        4377 non-null   float64
 7   serviced_flag   4377 non-null   float64
 8   estate_price    4377 non-null   float64
 9   serviced_price  4377 non-null   float64
 10  location_rank   4377 non-null   float64
 11  new_price       4377 non-null   float64
 12  exec_flag       4377 non-null   float64
 13  locationbed     4377 non-null   object 
 14  location        4377 non-null   object 
 15  spec_location   4377 non-null   object 
 16  description     4377 non-null   object 
dtypes: float64(13), object(4)
memory [...]<jupyter_text>Correlation examining the dpendent relationship with the numerical features of the dataset<jupyter_code>corr_hsd = hs_d.corr()
corr_hsd
#.groupby('price').sum()
sns.pairplot(corr_hsd)<jupyter_output><empty_output><jupyter_text>Using the correlated plot and values 1. Finding out the features that have a strong positive correlation with each other2. ...............................................a strong negative correlation with each other From observation The feature columns (estate price, serviced price, location rank, new price) have a high correlation with each other.The feature columns (bedrooms, bathrooms, toilets) have a high correlation with each other. Further realtions can be gottern individually to get a better understanding of the Data set   Possible models and tagert vairables to predictTarget variables1. Price of an Apartment (Regression)2. Location of an Apartment (Classfication)3. Using the Description column to gauge NLPFeature Columns that should be dropped due to lack of correlation or irrelevance for Regression and Classification 1. Description2. Specific Location3. Location Bed4. Serviced price or New price  Preprocessing<jupyter_code>hs_d.drop(['locationbed', 'spec_location', 'description', 'serviced_price'], axis = 1, inplace = True)
from sklearn.preprocessing import LabelEncoder

lbl = LabelEncoder()

hs_d.location = lbl.fit_transform(hs_d.location)
from sklearn.preprocessing import StandardScaler

scl = StandardScaler()

s_hsd = pd.DataFrame( scl.fit_transform(hs_d), columns = hs_d.columns)<jupyter_output><empty_output><jupyter_text>Prediction Model<jupyter_code>X = s_hsd.drop(['price'], axis = 1)
y = s_hsd['price']
#y = np.array(s_hsd['price']).reshape(-1,1)
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 100)
print (x_train.shape, x_test.shape, y_train.shape, y_test.shape)<jupyter_output>(3063, 12) (1314, 12) (3063,) (1314,)<jupyter_text>Models to use1. ElasticNET2. Decisions Tree3. RandomForest<jupyter_code>from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
from sklearn import metrics
from sklearn.linear_model import ElasticNet
from sklearn import model_selection
from sklearn.pipeline import Pipeline
elas = ElasticNet()
pipe = Pipeline
randf = RandomForestRegressor()
dcr = DecisionTreeRegressor()
grd = GridSearchCV
elas_param = {'alpha' : np.arange(0.01, 0.1, 0.01)}

elas_grd = grd(elas, param_grid = elas_param, cv = 10, n_jobs = -1, verbose = True, scoring = 'r2')
elas_grd.fit(x_train, y_train)
randf_param = {'max_depth' : np.arange(100, 500, 50), 'min_samples_leaf': np.arange(10,50,5)}
randf_grd = grd(randf, randf_param, cv = 5, n_jobs = -1,verbose = True, scoring = 'r2')
randf_grd.fit(x_train, y_train)
dcr_param = {'min_samples_split' : np.arange(1,10,1),'min_samples_leaf': np.arange(10,50,5), 'max_depth' : np.arange(10,50,10)}
dcr_grd = grd(dcr, dcr_param,  cv = 5, n_jobs = -1,verbose = True, scoring = 'r2')

dcr_grd.fit(x_train, y_train)
models = [('rand forest', randf_grd.best_estimator_), ('elastice net', elas_grd.best_estimator_), ('decision trees', dcr_grd.best_estimator_)]
outcome = []
model_names  = []


for model_name, model in models:
    k_fold = model_selection.KFold (n_splits = 10, shuffle = True,  random_state = 100)
    results = model_selection.cross_val_score(model, x_train, y_train, cv=k_fold, scoring = 'r2')
    outcome.append(results)
    model_names.append(model_name)
    output_message = "%s| Mean=%f STD=%f" % (model_name, results.mean(), results.std())
    print (output_message)<jupyter_output>rand forest| Mean=0.921948 STD=0.013495
elastice net| Mean=0.816061 STD=0.018882
decision trees| Mean=0.917857 STD=0.013715<jupyter_text>Classification Model  Preprocessing<jupyter_code>from sklearn.preprocessing import OneHotEncoder
#lbl.inverse_transform(s_hsd.location)
x1= s_hsd.drop('location', axis = 1)
y1= hs_d['location']<jupyter_output><empty_output><jupyter_text>Models to use1. RandomForestClasssfier2. K Nearest Nieghbours3. Decison trees Classsifer4. Extra tree classifier<jupyter_code>from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, classification_report
dcc = DecisionTreeClassifier(random_state = 100)
extr = ExtraTreeClassifier(random_state = 100)
knn = KNeighborsClassifier()
ranfc = RandomForestClassifier(random_state = 100)
x_tr, x_te, y_tr, y_te = train_test_split(x1, y1, test_size = 0.3, random_state = 100)
import sklearn
class_outcome = []
class_model_names= []

class_models = [('Decison Tree Classifier', dcc), ('Extra Tree Classifier', extr), ('K Nearest', knn), ('Random Forest Classifier', ranfc)]


for model_name, model in class_models:
    k_fold = sklearn.model_selection.KFold (n_splits = 10, shuffle = True , random_state = 100)
    results = sklearn.model_selection.cross_val_score(model, x1, y1, cv = k_fold, scoring = 'f1_macro')
    class_outcome.append(results)
    class_model_names.append(model_name)
    
    output_message =  ' {}  {}'.format(model_name, results.mean())
    print(output_message)
dcc_param = {'min_samples_split' : np.arange(0,10,2),'min_samples_leaf': np.arange(0,10,1), 'max_depth' : np.arange(5,15,1) }
dcc_grd = grd(dcc, dcc_param, cv = 5, n_jobs = -1, verbose = True, scoring = 'f1_macro')

dcc_grd.fit(x1, y1)
dcc_grd.best_estimator_
extr_params = {'min_samples_split' : np.arange(0,6,1),'min_samples_leaf': np.arange(0,10,1), 'max_depth' : np.arange(16,26,2)}
extr_grd = grd(extr, extr_params, cv = 5, n_jobs = -1, verbose = True, scoring = 'f1_macro')

extr_grd.fit(x1, y1)
extr_grd.best_estimator_
knn_params = {'n_neighbors' : np.arange(1,10,1), 'weights' : ['uniform', 'distance']}
knn_grd = grd(knn, knn_params, cv = 5, n_jobs = -1, verbose = True, scoring = 'f1_macro')

knn_grd.fit(x1, y1)
y_pred = knn_grd.predict(x_te)

print(classification_report(y_te, y_pred, digits = 4, target_names= list(lbl.inverse_transform(list(np.arange(0,10,1))))))
knn_grd.best_estimator_
ranfc_params =  {'min_samples_split' : np.arange(0,10,1),'min_samples_leaf': np.arange(0,10,1), 'max_depth' : np.arange(20,40,10), 'n_estimators' : np.arange(30,70,10)}
ranfc_grd =grd(ranfc, ranfc_params, cv = 2, n_jobs = 3 , verbose = True, scoring = 'f1_macro')

ranfc_grd.fit(x1, y1)
ranfc_grd.best_estimator_
class_outcome_one = []
class_model_names_one = []

class_models = [('Decison Tree Classifier', dcc_grd.best_estimator_),
                ('Extra Tree Classifier', extr_grd.best_estimator_),
                ('K Nearest', knn_grd.best_estimator_),
                ('Random Forest Classifier', ranfc_grd.best_estimator_)]


for model_name, model in class_models:
    k_fold = sklearn.model_selection.KFold (n_splits = 10, shuffle = True , random_state = 100)
    results = sklearn.model_selection.cross_val_score(model, x1, y1, cv = k_fold, scoring = 'f1_macro')
    class_outcome_one.append(results)
    class_model_names_one.append(model_name)
    
    output_message =  ' {}  {}'.format(model_name, results.mean())
    print(output_message)<jupyter_output>Decison Tree Classifier  0.9625395453238372
 Extra Tree Classifier  0.9379588686624192
 K Nearest  0.8032979726023909
 Random Forest Classifier  0.9537808125779526
{'fraction_non_alphanumeric': 0.11835775813849424, 'fraction_numerical': 0.05045307081329008, 'mean_word_length': 4.920529801324503, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '6796', 'license': 'MIT', 'n_tokens_mistral': 3497, 'n_tokens_neox': 3206, 'n_words': 750, 'path': 'Housing Prediction/housing EDA by Daye.ipynb', 'repo_name': 'daye-oa/Data-Science-Projects', 'size': '8939'}