<jupyter_start><jupyter_text>PCA<jupyter_code>def my_read_train_data(path_train):
    data_df = pd.read_excel(path_train, encoding='gbk', index_col=[0, 1],engine='openpyxl')  # train_sample_375_v2 train_sample_351_v4
    data_df = data_df.groupby('PATIENT_ID').last()
    # data_df = data_df.iloc[:,1:]
    # data_df = data_df.set_index(['PATIENT_ID'])
    # data_df['年龄'] = data_df['年龄'].apply(lambda x: x.replace('岁', '') if is_number(x.replace('岁', '')) else np.nan).astype(float)
    # data_df['性别'] = data_df['性别'].map({'男': 1, '女': 2})
    # data_df['护理->出院方式'] = data_df['护理->出院方式'].map({'治愈': 0,'好转': 0, '死亡': 1})
    lable = data_df['outcome'].values
    data_df = data_df.drop(['outcome', 'Admission time', 'Discharge time'], axis=1)
    data_df['Type2'] = lable
    data_df = data_df.applymap(lambda x: x.replace('>', '').replace('<', '') if isinstance(x, str) else x)
    data_df = data_df.applymap(lambda x: x if is_number(x) else -1)
    # data_df = data_df.loc[:, data_df.isnull().mean() < 0.2]
    data_df = data_df.astype(float)

    return data_df
my_df=my_read_train_data('./data/time_series_375_prerpocess_en.xlsx')
my_df
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso, LogisticRegression
from sklearn.feature_selection import SelectFromModel
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
imp = IterativeImputer(random_state=0, n_nearest_features=10)
imp_data=imp.fit_transform(my_df)
imp_data
df = pd.DataFrame(data=imp_data, columns=my_df.columns)
df
def my_read_train_data2(path_train):
    data_df = pd.read_excel(path_train, encoding='gbk', index_col=[0, 1],engine='openpyxl')  # train_sample_375_v2 train_sample_351_v4
    data_df = data_df.groupby('PATIENT_ID').last()
    # data_df = data_df.iloc[:,1:]
    # data_df = data_df.set_index(['PATIENT_ID'])
    # data_df['年龄'] = data_df['年龄'].apply(lambda x: x.replace('岁', '') if is_number(x.replace('岁', '')) else np.nan).astype(float)
    # data_df['性别'] = data_df['性别'].map({'男': 1, '女': 2})
    # data_df['护理->出院方式'] = data_df['护理->出院方式'].map({'治愈': 0,'好转': 0, '死亡': 1})
    lable = data_df['outcome'].values
    data_df = data_df.drop(['outcome', 'Admission time', 'Discharge time'], axis=1)
    data_df['Type2'] = lable
    data_df = data_df.applymap(lambda x: x.replace('>', '').replace('<', '') if isinstance(x, str) else x)
    data_df = data_df.applymap(lambda x: x if is_number(x) else -1)
    # data_df = data_df.loc[:, data_df.isnull().mean() < 0.2]
    data_df = data_df.astype(float)

    return lable
x = df.drop(["Type2"], axis=1)
x
from sklearn.preprocessing import StandardScaler
x = StandardScaler().fit_transform(x)
x
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(x)
principalDf = pd.DataFrame(data = principalComponents, columns = ['principal component 1', 'principal component 2'])
y = my_read_train_data2('./data/time_series_375_prerpocess_en.xlsx')
finalDf = pd.concat([principalDf,  pd.Series(y)], axis = 1)
finalDf
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
targets = [0,1]
colors = ['r', 'g', 'b']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf[0] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
               , finalDf.loc[indicesToKeep, 'principal component 2']
               , c = color
               , s = 50)
ax.legend(targets)
ax.plot([-5,15],[-10,40], c='blue')
ax.grid()<jupyter_output>findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.<jupyter_text>Przy 2 składowych głównych mamy niemal liniowo separowalne danedopasowana z palca prosta: y=2.5x+2.5 ma wyniki:<jupyter_code>simple_prediction = []
for i in range(375):
    simple_prediction.append(int(finalDf.iloc[i,1]<(2.5 * finalDf.iloc[i,0] + 2.5)))
print(metrics.classification_report(finalDf.iloc[:,2], simple_prediction))
pca.explained_variance_ratio_
print("Explained Variance ratio with 2 components: {}, sum: {}".format(pca.explained_variance_ratio_, np.sum(pca.explained_variance_ratio_)))
comp = pd.DataFrame(pca.components_,columns=df.columns[0:76],index = ['PC-1','PC-2'])
print(pd.DataFrame(pca.components_,columns=df.columns[0:76],index = ['PC-1','PC-2']))
np.sort(abs(comp.iloc[0,:]))
vals = (abs(comp.iloc[0,:]))>0.15
comp.iloc[0, vals.values]
abs(comp.iloc[0,:]).values
comp.iloc[0, np.argsort(abs(comp.iloc[0,:]))].iloc[66:77]<jupyter_output><empty_output><jupyter_text>Powyżej najważniejsze zmienne, część się pokrywa z tymi co w artykuje, ale np. wieku nie ma  Poniżej model dla kolumn z nowych danych<jupyter_code>new_data = pd.read_csv('data/Yan_reply_First_last_wtime.csv')
x = new_data.iloc[:,2:8]
y = new_data.iloc[:,1]
new_data<jupyter_output><empty_output><jupyter_text>Tutaj bez kolumn typu 6 days 17:33:16<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.pipeline import Pipeline
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.pipeline import Pipeline
from sklearn.ensemble import AdaBoostClassifier
from matplotlib import pyplot as plt
x_train, x_test, y_train, y_test = train_test_split(new_data.iloc[:,2:8], new_data.iloc[:,1], random_state=123)

clf = GradientBoostingClassifier(n_estimators=75, learning_rate=.05,
    max_depth=3, random_state=123)

clf.fit(x_train,y_train)
prediction_test=clf.predict(x_test)
print(prediction_test)
clf.score(x_test,y_test)<jupyter_output>[0 1 0 0 0 1 0 0 1 0 1 1 0 0 0 0 0 1 1 0 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0
 0 0 0 1 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 1 1 0 0 1 0 0 0 1 0
 0 0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0
 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 0 0 1 0
 0 0 1 1 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1
 0 0 0 0 1 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0
 1]<jupyter_text>A tutaj biorąc tylko dni z tych kolumn<jupyter_code>def days_to_num(test_str):
    test_str = str(test_str)
    for char in test_str:
        if not char.isdigit() and char not in ['.','-']:
            temp = test_str.index(char)
            return float(str(test_str[0 : temp]))
    return float(test_str)
def df_change_date(data):
    features = ['Last_time_LDH', 'Last_time_CRP', 'Last_time_Lymph', 
                'First_time_LDH', 'First_time_CRP', 'First_time_Lymph']
    for feature in features:
        data[feature] = data[feature].apply(days_to_num)
    return data
df_change_date(new_data)
x_train2, x_test2, y_train2, y_test2 = train_test_split(new_data.iloc[:,2:14], new_data.iloc[:,1], random_state=123)

clf = GradientBoostingClassifier(n_estimators=75, learning_rate=.05,
    max_depth=3, random_state=123)


clf.fit(x_train2,y_train2)
prediction_test2=clf.predict(x_test2)
print(prediction_test2)
clf.score(x_test2,y_test2)
print(metrics.classification_report(y_test2, prediction_test2))<jupyter_output>precision    recall  f1-score   support

           0       0.86      0.95      0.90       175
           1       0.87      0.68      0.76        85

    accuracy                           0.86       260
   macro avg       0.86      0.82      0.83       260
weighted avg       0.86      0.86      0.86       260<jupyter_text>Accuracy skoczyło z 0.8 do 0.86<jupyter_code>from sklearn.model_selection import cross_validate
print('Kroswalidacja dla GradientBoosting')
print(np.mean(cross_validate(clf, x_test2, y_test2, cv=20, scoring='accuracy').get('test_score')))<jupyter_output>Kroswalidacja dla GradientBoosting
0.8307692307692308<jupyter_text>Adaboost<jupyter_code>clf1 = AdaBoostClassifier(random_state=1,learning_rate=0.15,algorithm ='SAMME.R')
clf1.fit(x_train2,y_train2)
prediction_test_ada=clf1.predict(x_test2)
print(prediction_test_ada)
clf1.score(x_test2,y_test2)
from sklearn.model_selection import cross_validate
print('Kroswalidacja dla AdaBoosting')
print(np.mean(cross_validate(clf1, x_test2, y_test2, cv=30, scoring='accuracy').get('test_score')))<jupyter_output>Kroswalidacja dla AdaBoosting
0.8430555555555554<jupyter_text>histogramy zmiennych<jupyter_code>new_data
new_data[new_data.columns.tolist()[2:]].hist(figsize=(18, 12), bins=30)
plt.show()
def sqrt_data(data = new_data, features=new_data.columns.tolist()[2:]):
    for feature in features:
        data[feature] = np.sqrt(np.abs(data[feature])) * np.sign(data[feature]) 
    return data
sqrt_data()
new_data[new_data.columns.tolist()[2:]].hist(figsize=(18, 12), bins=30)
plt.show()
x_train_s, x_test_s, y_train_s, y_test_s = train_test_split(new_data.iloc[:,2:14], new_data.iloc[:,1], random_state=123)

clf_s = AdaBoostClassifier(random_state=123,learning_rate=0.15,algorithm ='SAMME.R')

clf_s.fit(x_train_s,y_train_s)
prediction_test_s=clf.predict(x_test_s)
print(prediction_test_s)
clf.score(x_test_s,y_test_s)
print("Wyniki z pierwiastkowanymi danymi:")
print(metrics.classification_report(y_test_s, prediction_test_s))
print('Wyniki przed pierwiastkowaniem danych:')
print(metrics.classification_report(y_test2, prediction_test2))<jupyter_output>Wyniki przed pierwiastkowaniem danych:
              precision    recall  f1-score   support

           0       0.86      0.95      0.90       175
           1       0.87      0.68      0.76        85

    accuracy                           0.86       260
   macro avg       0.86      0.82      0.83       260
weighted avg       0.86      0.86      0.86       260
{'fraction_non_alphanumeric': 0.1327344302008681, 'fraction_numerical': 0.06480266478247704, 'mean_word_length': 4.332615715823466, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '631999', 'license': 'MIT', 'n_tokens_mistral': 4399, 'n_tokens_neox': 3972, 'n_words': 890, 'path': 'Covid-19_ML_Research/WB_PCA.ipynb', 'repo_name': 'HubertR21/Hubert-Ruczynski-Projects', 'size': '9907'}