<jupyter_start><jupyter_text>import Data<jupyter_code>processed_data_path = os.path.join(os.path.pardir,'data','processed')
train_file_path = os.path.join(processed_data_path,'train.csv')
test_file_path= os.path.join(processed_data_path,'test.csv')
train_df = pd.read_csv(train_file_path,index_col='PassengerId')
test_df = pd.read_csv(test_file_path,index_col='PassengerId')
#33個功能中，survived視輸出標籤，32個特徵
train_df.info()
#418個測試項目
test_df.info()<jupyter_output><empty_output><jupyter_text>Data Preperation<jupyter_code>#將訓練數據分成inpute feature and output feature
X=train_df.loc[:,'Age':].values.astype('float')
temp=train_df['Survived'].values
print(temp.shape)
y=train_df['Survived'].ravel()
print(X.shape,y.shape)
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,y_train.shape)
print(X_test.shape,y_test.shape)
#average survival in train and test
#38趴的存活機，62趴的死往機率，存在不平恆
#如果差距太大需要採用不同的方法來建構，所以要常檢查平衡
print('mean survival in train :{0:.3f}'.format(np.mean(y_train)))
print('mean survival in test :{0:.3f}'.format(np.mean(y_test)))<jupyter_output><empty_output><jupyter_text>check scikit-learn version<jupyter_code>import sklearn
sklearn.__version__<jupyter_output><empty_output><jupyter_text>Baseline Model<jupyter_code>from sklearn.dummy import DummyClassifier
#create model，策略指定most_frequent
model_dummy=DummyClassifier(strategy='most_frequent',random_state=0)
model_dummy.fit(X_train,y_train)
print('score for baseline model : {0:.2f}'.format(model_dummy.score(X_test,y_test)))
from sklearn.metrics import accuracy_score,confusion_matrix,precision_score,recall_score
print('accuracy for baseline model: {0:.2f}'.format(accuracy_score(y_test,model_dummy.predict(X_test))))
print('confusion matrix for baseline model: {0}'.format(confusion_matrix(y_test,model_dummy.predict(X_test))))
print('precision for baseline model : {0:.2f}'.format(precision_score(y_test,model_dummy.predict(X_test))))
print('recall_score for baseline model : {0:.2f}'.format(recall_score(y_test,model_dummy.predict(X_test))))<jupyter_output><empty_output><jupyter_text>Making first Kaggle submission<jupyter_code>test_X=test_df.values.astype('float')
#因為這個練習資料沒有真正的labels，必須要提交我們的預測到kaggle
#讓kaggle評估
predictions= model_dummy.predict(test_X)
print(predictions)
df_submission=pd.DataFrame({'PassengerId':test_df.index,'Survived':predictions})
df_submission
submission_data_path = os.path.join(os.path.pardir,'data','external')
submission_file_path = os.path.join(submission_data_path,'01_dummy.csv')
df_submission.to_csv(submission_file_path,index=False)
def get_submission_file(model,filename):
    test_X=test_df.values.astype('float')
    predictions= model.predict(test_X)
    df_submission=pd.DataFrame({'PassengerId':test_df.index,'Survived':predictions})
    submission_data_path = os.path.join(os.path.pardir,'data','external')
    submission_file_path = os.path.join(submission_data_path,filename)
    df_submission.to_csv(submission_file_path,index=False)
    
get_submission_file(model_dummy,'01_dummy.csv')<jupyter_output><empty_output><jupyter_text>Logistic Regression Model<jupyter_code>from sklearn.linear_model import LogisticRegression
model_lr_l=LogisticRegression(random_state=0)
model_lr_l.fit(X_train,y_train)
print('Score for logistic regression -version 1: {0:.2f}'.format(model_lr_l.score(X_test,y_test)))
print('accuracy for logistic regression model: {0:.2f}'.format(accuracy_score(y_test,model_lr_l.predict(X_test))))
print('precision for logistic regression model : {0:.2f}'.format(precision_score(y_test,model_lr_l.predict(X_test))))
print('recall_score for logistic regression model : {0:.2f}'.format(recall_score(y_test,model_lr_l.predict(X_test))))
#model coefficients，模型權重，模型參數
model_lr_l.coef_
get_submission_file(model_lr_l,'02_lr.csv')<jupyter_output><empty_output><jupyter_text>Hyperparameter Optimization<jupyter_code>model_lr = LogisticRegression(random_state=0)
from sklearn.model_selection import GridSearchCV
#參數字典
parameters = {'C':[1.0,10.0,50.0,100.0,1000.0],'penalty':['l1','l2']}
#cv=3，執行3被交叉驗證
clf = GridSearchCV(model_lr,param_grid=parameters,cv=3)
clf.fit(X_train,y_train)
clf.best_params_
print('best score :{0:.2f}'.format(clf.best_score_))
print('score for logistic regression - version 2: {0:.2f}'.format(clf.score(X_test,y_test)))<jupyter_output><empty_output><jupyter_text>Making Third Submission<jupyter_code>get_submission_file(clf,'03_lr.csv')<jupyter_output><empty_output><jupyter_text>Feature Normalization and Standardization<jupyter_code>from sklearn.preprocessing import MinMaxScaler, StandardScaler
#feature  normalization
scaler= MinMaxScaler()
print(X_train)
X_train_scaled = scaler.fit_transform(X_train)
print(X_train_scaled)
X_train_scaled[:,0].min(),X_train_scaled[:,0].max()
X_test_scale=scaler.fit_transform(X_test)<jupyter_output><empty_output><jupyter_text>Feature Standardization<jupyter_code>scaler = StandardScaler()
X_train_scaled= scaler.fit_transform(X_train)
X_test_scaled= scaler.transform(X_test)
print(X_train_scaled)<jupyter_output>[[ 0.42963418 -0.33167904  3.29646275 ... -1.62827579  0.39477102
  -0.39477102]
 [ 0.12976942 -0.42640542 -0.30335547 ...  0.61414657  0.39477102
  -0.39477102]
 [ 0.12976942  0.10261958 -0.30335547 ... -1.62827579  0.39477102
  -0.39477102]
 ...
 [-0.02016296 -0.48162887 -0.30335547 ... -1.62827579  0.39477102
  -0.39477102]
 [ 0.50460037 -0.28868112 -0.30335547 ...  0.61414657  0.39477102
  -0.39477102]
 [ 2.30378891  0.14245584 -0.30335547 ...  0.61414657  0.39477102
  -0.39477102]]<jupyter_text>Create model after standardization<jupyter_code>model_lr=LogisticRegression()
#參數字典
parameters = {'C':[1.0,10.0,50.0,100.0,1000.0],'penalty':['l1','l2']}
#cv=3，執行3被交叉驗證
clf = GridSearchCV(model_lr,param_grid=parameters,cv=3)
clf.fit(X_train_scaled,y_train)

clf.best_score_
print('score for logistic regression - version 2: {0:.2f}'.format(clf.score(X_test_scaled,y_test)))<jupyter_output>score for logistic regression - version 2: 0.79<jupyter_text>Model Persistence<jupyter_code>import pickle
import os
model_file_path=os.path.join(os.path.pardir,'models','lr_model.pkl')
scaler_file_path=os.path.join(os.path.pardir,'models','lr_scaler.pk1')
#可以用二禁制格式編寫
model_file_pickle=open(model_file_path,'wb')
scaler_file_pickle=open(scaler_file_path,'wb')
pickle.dump(clf,model_file_pickle)
pickle.dump(scaler,scaler_file_pickle)
model_file_pickle.close()
scaler_file_pickle.close()<jupyter_output><empty_output><jupyter_text>load the persisted file<jupyter_code>print(model_file_path)
print(scaler_file_path)
with open(model_file_path, 'rb') as f:
    clf_loaded= pickle.load(f)
    print(clf_loaded)
with open(scaler_file_path, 'rb') as f:
    scaler_loaded=pickle.load(f)
#model_file_pickle = open(model_file_path,'r',encoding="utf-8")
#scaler_file_pickle=open(scaler_file_path,'r',encoding="utf-8")
#clf_loaded= pickle.load(model_file_pickle)
#scaler_loaded=pickle.load(scaler_file_pickle)
#model_file_pickle.close()
#scaler_file_pickle.close()
clf_loaded
scaler_loaded
X_test_scaled= scaler_loaded.transform(X_test)
print('score for peristed logistic regression : {0:.2f}'.format(clf_loaded.score(X_test_scaled,y_test)))<jupyter_output>score for peristed logistic regression : 0.79
{'fraction_non_alphanumeric': 0.13738150844896277, 'fraction_numerical': 0.06003571919219673, 'mean_word_length': 9.963855421686747, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '637287', 'license': 'MIT', 'n_tokens_mistral': 3235, 'n_tokens_neox': 2998, 'n_words': 319, 'path': 'notebooks/2020-05-08-build-oredictive-mode.ipynb', 'repo_name': 'alane506470/jupyter-exploring-processing-data', 'size': '7279'}