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
| # Note: make sure `s3fs` is installed in order to make Pandas use S3. | |
| # Credentials for AWS in the normal location ~/.aws/credentials | |
| def _write_dataframe_to_parquet_on_s3(dataframe, filename): | |
| """ Write a dataframe to a Parquet on S3 """ | |
| print("Writing {} records to {}".format(len(dataframe), filename)) | |
| output_file = f"s3://{DESTINATION}/{filename}/data.parquet" | |
| dataframe.to_parquet(output_file) |
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 pyspark.sql.functions import array, col, explode, lit, struct | |
| from pyspark.sql import DataFrame | |
| from typing import Iterable | |
| def melt( | |
| df: DataFrame, | |
| id_vars: Iterable[str], value_vars: Iterable[str], | |
| var_name: str="variable", value_name: str="value") -> DataFrame: | |
| """Convert :class:`DataFrame` from wide to long format.""" |
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
| import pandas as pd | |
| def compare_two_dfs(input_df_1, input_df_2): | |
| df_1, df_2 = input_df_1.copy(), input_df_2.copy() | |
| ne_stacked = (df_1 != df_2).stack() | |
| changed = ne_stacked[ne_stacked] | |
| changed.index.names = ['id', 'col'] | |
| difference_locations = np.where(df_1 != df_2) | |
| changed_from = df_1.values[difference_locations] |
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
| # Create Dataframe with fake data | |
| df = pd.util.testing.makeMissingDataframe() | |
| df['index1'] = df.index # create a text column by replicating index | |
| df['A'] = 0 # create a zero column | |
| # Helper function | |
| def check_df_sanity(df, verbose=False): | |
| """Perform usual types and values checks on columns of a pandas.DataFrame""" | |
| for col in 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
| def sanity_check_sum(model, dataframe, cols, delta=1): | |
| '''Calculates success rate on basic sanity check. A "delta" value is added | |
| to columns in a dataframe and the newly predicted house price should be higher | |
| than the existing prediction since the addition is supposed to be an added feature | |
| to the house such as bigger area or better condition or view etc. | |
| Args: | |
| model: sklearn or other model with predict() method | |
| dataframe: pandas dataframe with dataset to be test | |
| cols: column or list of columns in dataframe to be incremented by delta parameter |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
| import datetime | |
| def get_easter_for_year(year): | |
| a = year % 19 | |
| b, c = divmod(year, 100) | |
| d, e = divmod(b, 4) | |
| g = (8 * b +13)//25 | |
| h = (19 * a + b - d - g + 15)% 30 | |
| j, k = divmod(c, 4) |
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
| #notebook-container{ | |
| box-shadow: none !important; | |
| } | |
| .container { | |
| width: 80% !important; | |
| } | |
| .notebook_app { | |
| background: #fff !important; |
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
| {% extends "bootstrap/base.html" %} | |
| {% import "bootstrap/wtf.html" as wtf %} | |
| {% block title %}test{% endblock %} | |
| {% block content %} | |
| <div class="col-md-4"> | |
| <form action="" method="post" novalidata> | |
| {{ form.hidden_tag() }} | |
| <p> | |
| {{ form.example }}<br> | |
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 flask import Flask | |
| from flask import json, make_response, render_template_string, request | |
| from flask_wtf import Form | |
| from wtforms import IntegerField, SelectField | |
| from wtforms.validators import DataRequired | |
| template = """ | |
| <html> | |
| <head> | |
| <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> |
NewerOlder