This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| os.environ["KERAS_BACKEND"] = "plaidml.keras.backend" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data.fillna('missing', inplace=True) | |
| # or, for instance, if your data is numeric | |
| data.fillna(-9999, inplace=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from tqdm import notebook | |
| notebook.tqdm().pandas() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def female_proportion(dataframe): | |
| return (dataframe.Sex=='female').sum() / len(dataframe) | |
| female_proportion(df) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| df.merge( | |
| df.loc[ | |
| df.Ticket.isin( | |
| df.Ticket.value_counts().loc[ | |
| df.Ticket.value_counts()>1 | |
| ].index | |
| ) | |
| ].groupby('Ticket').apply(female_proportion) \ | |
| .reset_index().rename(columns={0:'proportion_female'}), | |
| how='left', on='Ticket' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.impute import SimpleImputer | |
| from sklearn.preprocessing import OneHotEncoder | |
| from sklearn.preprocessing import MinMaxScaler, StandardScaler | |
| from sklearn_pandas import DataFrameMapper | |
| from category_encoders import LeaveOneOutEncoder | |
| imputer_Pclass = SimpleImputer(strategy='most_frequent', add_indicator=True) | |
| imputer_Age = SimpleImputer(strategy='median', add_indicator=True) | |
| imputer_SibSp = SimpleImputer(strategy='constant', fill_value=0, add_indicator=True) | |
| imputer_Parch = SimpleImputer(strategy='constant', fill_value=0, add_indicator=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pd.DataFrame({ | |
| 'variable': variables, | |
| 'coefficient': model.coef_[0] | |
| }) \ | |
| .round(decimals=2) \ | |
| .sort_values('coefficient', ascending=False) \ | |
| .style.bar(color=['grey', 'lightblue'], align='zero') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| group_id, grouped_data = generator.__next__() | |
| print(group_id) | |
| grouped_data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| generator = df.groupby(['identifier']).__iter__() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder | |
| one_hot_encoder_gender = OneHotEncoder(handle_unknown='ignore') | |
| one_hot_encoder_gender.fit(train[['Sex']]) | |
| # For Embarked column, there are some missing values. We need to first fill them then encode them. | |
| imputer_Embarked = SimpleImputer(strategy='most_frequent', add_indicator=True) | |
| imputer_Embarked.fit(train[['Embarked']]) | |
| transformed_Embarked = \ | |
| pd.DataFrame(imputer_Embarked.transform(train[['Embarked']]), |
NewerOlder