<jupyter_start><jupyter_text>Observations* Missing values in n_hos_beds* bus_ter provides no additional info* n_hot_rooms and rainfall have outliers<jupyter_code>np.percentile(df.n_hot_rooms,[99])
np.percentile(df.n_hot_rooms,[99])[0]
uv = np.percentile(df.n_hot_rooms,[99])[0]
df[(df.n_hot_rooms>uv)]
df.n_hot_rooms[(df.n_hot_rooms> 3*uv)]= 3*uv
 np.percentile(df.rainfall,[1])[0]
lv =  np.percentile(df.rainfall,[1])[0]
df[ (df.rainfall<lv)]
df.rainfall[(df.rainfall<0.3*lv)] = 0.3*lv
df.describe()
df.info()
df.Time_taken = df.Time_taken.fillna(df.Time_taken.mean())
df.info()
df['avg_dist'] = (df.dist1+df.dist2+df.dist3+df.dist4)/4
df.describe()
del df['dist1']
del df['dist2']
del df['dist3']
del df['dist4']
df.describe()
del df['bus_ter']
df.head()
df = pd.get_dummies(df)
df.head()
del df['3D_available_NO']
del df['Genre_Action']
df.head()<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>X = df[['price']]
y= df['Sold']
X.head()
y.head()
from sklearn.linear_model import LogisticRegression
clf_lrs = LogisticRegression()
clf_lrs.fit(X,y)
clf_lrs.coef_
clf_lrs.intercept_
import statsmodels.api as sn
X_cons = sn.add_constant(X)
X_cons.head()
import statsmodels.discrete.discrete_model as sm
logit = sm.Logit(y,X_cons).fit()
logit.summary()<jupyter_output><empty_output><jupyter_text>Logistic with Multiple predictor<jupyter_code>X = df.loc[:, df.columns != 'Start_Tech_Oscar']
y = df['Start_Tech_Oscar']
clf_lr = LogisticRegression()
clf_lr.fit(X,y)
clf_lr.coef_
clf_lr.intercept_
X_cons = sn.add_constant(X)
logit = sm.Logit(y,X_cons).fit()
logit.summary()<jupyter_output><empty_output><jupyter_text>Predicting and confusion matrix<jupyter_code>clf_lr.predict_proba(X)
y_pred = clf_lr.predict(X)
y_pred
y_pred_03 = (clf_lr.predict_proba(X)[:,1] >= 0.3)
y_pred_03
from sklearn.metrics import confusion_matrix
confusion_matrix(y, y_pred)
confusion_matrix(y, y_pred_03)<jupyter_output><empty_output><jupyter_text>Linear Discriminant Analysis<jupyter_code>from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
clf_lda = LinearDiscriminantAnalysis()
clf_lda.fit(X, y)
y_pred_lda = clf_lda.predict(X) 
y_pred_lda
confusion_matrix(y, y_pred_lda)<jupyter_output><empty_output><jupyter_text>Performance metrics* Precision$$\textrm{precision} = \frac{\textrm{True Positives}}{\textrm{True Positives} + \textrm{False Positives}}$$* Recall$$\textrm{recall} = \frac{\textrm{True Positives}}{\textrm{True Positives} + \textrm{False Negatives}}$$* AUC (ROC)<jupyter_code>from sklearn.metrics import precision_score, recall_score
precision_score(y, y_pred)
recall_score(y, y_pred)
from sklearn.metrics import roc_auc_score
roc_auc_score(y, y_pred)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
print (X_train.shape,X_test.shape,y_train.shape, y_test.shape)
clf_LR = LogisticRegression()
clf_LR.fit(X_train,y_train)
y_test_pred = clf_LR.predict(X_test)
from sklearn.metrics import accuracy_score, confusion_matrix
confusion_matrix(y_test, y_test_pred)
accuracy_score(y_test, y_test_pred)<jupyter_output><empty_output><jupyter_text>K-Nearest Neighbors<jupyter_code>from sklearn import preprocessing
scaler = preprocessing.StandardScaler().fit(X_train)
X_train_s= scaler.transform(X_train)
scaler = preprocessing.StandardScaler().fit(X_test)
X_test_s= scaler.transform(X_test)
X_test_s
from sklearn.neighbors import KNeighborsClassifier
clf_knn_1 = KNeighborsClassifier(n_neighbors=1)
clf_knn_1.fit(X_train_s, y_train)
confusion_matrix(y_test, clf_knn_1.predict(X_test_s))
accuracy_score(y_test, clf_knn_1.predict(X_test_s))
clf_knn_3 = KNeighborsClassifier(n_neighbors=3)
clf_knn_3.fit(X_train_s, y_train)
accuracy_score(y_test, clf_knn_3.predict(X_test_s))
from sklearn.model_selection import GridSearchCV
params = {'n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20,30]}
grid_search_cv = GridSearchCV(KNeighborsClassifier(), params)
grid_search_cv.fit(X_train_s, y_train)
grid_search_cv.best_params_
optimised_KNN = grid_search_cv.best_estimator_
y_test_pred = optimised_KNN.predict(X_test_s) 
confusion_matrix(y_test, y_test_pred)
accuracy_score(y_test, y_test_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13167848699763593, 'fraction_numerical': 0.015130023640661938, 'mean_word_length': 9.498759305210918, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 32, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '162519', 'license': 'MIT', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1752, 'n_words': 256, 'path': 'estudos_python/Machine_Learning/Others/2. ST Academy - Classification models resource files/Python Code/Classification.ipynb', 'repo_name': 'robertoweller/python', 'size': '4230'}