<jupyter_start><jupyter_text>Based on: machine-learning/examples/bbengfort/traffic/Traffic.ipynbPulled data for one building and modeled electricity usage DATA LOADING<jupyter_code>data = pd.read_csv("0008_0806.csv",
    index_col="DATE_TIME",
    parse_dates=True
)
data.head(24)
def plot_numeric_features(df=data, cols=["NKBTUPERSQFT", "AWND", "CLDD", "HTDD", "SNOW", "TAVG", "TMAX", "TMIN", "WDF2", "WDF5", "WSF2", "WSF5"]):

    #"YEARBUILT", "AWND", "CLDD", "HTDD", "SNOW", "TAVG", "TMAX", "TMIN", "WDF2", "WDF5", "WSF2", "WSF5", "KBTUPERSQFT"
    fig, axes = plt.subplots(nrows=len(cols), sharex=True, figsize=(20,20))
    
    for col, ax in zip(cols, axes) :
        df[col].plot(ax=ax)
        ax.set_title(col)
        
        if ax == axes[-1]:
            ax.set_xlabel("date")
            
#    fig.tight_layout()
    return ax

# Plot features in date range
_ = plot_numeric_features(data.loc["2018-01-01":"2019-12-01"])
#def plot_categorical_features(df=data, cols=['WARD2','RES']):
#    fig, axes = plt.subplots(nrows=len(cols), sharex=True, figsize=(9,18))
    
#    for col, ax in zip(cols, axes):
#        sns.barplot(x='KBTUPERSQFT', y=col, data=df, ax=ax)
#        ax.set_title(col)
        
#        if ax == axes[-1]:
#            ax.set_xlabel('KBTUPERSQFT')
    
    
#    fig.tight_layout()
#    return ax


#_ = plot_categorical_features(df)
from sklearn.model_selection import train_test_split as tts


TARGET = "EKBTUPERSQFT"
COLS = ["NKBTUPERSQFT","DATE_TIME","YEARBUILT", "CLDD", "HTDD", "SNOW","WDF2", "WSF2"]


def make_sklearn_data(df=data, splits=True, train_size=0.8, target=TARGET, cols=COLS):
    # Add the datetime column back to the main dataframe
    df = df.reset_index()
    X, y = df[cols], df[target]
    
    if splits:
        return tts(X, y, train_size=train_size)
    return X, y
from sklearn.base import BaseEstimator, TransformerMixin


class CyclicEncoder(BaseEstimator, TransformerMixin):
    
    def __init__(self, date_extract="month"):
        if date_extract not in {"minute", "hour", "week", "month", "year"}:
            raise ValueError(f"specify correct date component to extract, not {date_extract}")
        self.date_extract = date_extract
    
    def get_date_component(self, x):
        if self.date_extract == "month":
            return x.dt.month
        elif self.date_extract == "year":
            return x.dt.year
        else:
            raise NotImplementedError(f"{self.date_extract} date component not implemented yet")
            
    def fit(self, X, y=None):
        self.cycle_max_ = self.get_date_component(X).max()
        return self

    def transform(self, X,  y=None):
        cols = []
        names = []
        x = self.get_date_component(X)
        xn = 2 * np.pi * x / self.cycle_max_
        cols.append(np.cos(xn))
        names.append(f"{X.name}_cos")
        cols.append(np.sin(xn))
        names.append(f"{X.name}_sin")
        return pd.DataFrame(np.asarray(cols).T, columns=names)

    
ce = CyclicEncoder().fit_transform(data.reset_index()["DATE_TIME"])
ce.plot(x="DATE_TIME_cos", y="DATE_TIME_sin", kind="scatter")
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import FeatureUnion, Pipeline
from sklearn.preprocessing import OneHotEncoder

extraction = [('column_selection', ColumnTransformer([('time_components', FeatureUnion([('month', CyclicEncoder(date_extract="month")),('year', CyclicEncoder(date_extract="year")),]), 'DATE_TIME'),], remainder="passthrough")),]


X, y = make_sklearn_data(splits=False)
Pipeline(extraction).fit_transform(X).shape
from sklearn.ensemble import RandomForestRegressor 


X_train, X_test, y_train, y_test = make_sklearn_data()

extraction.append(("clf", RandomForestRegressor()))
model = Pipeline(extraction)
model.fit(X_train, y_train)
model.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Based on: demos/20201031.ipynbRegression on KBTU for the numeric columns<jupyter_code>data.shape
cols=["NKBTUPERSQFT", "AWND", "CLDD", "HTDD", "SNOW", "TAVG", "WDF2", "WDF5", "WSF2", "WSF5"]

X = data[cols]
y = data["EKBTUPERSQFT"]
from sklearn.linear_model import LinearRegression

model = LinearRegression(fit_intercept=True)
model.fit(X, y)
def describe_model(model):
    print(model.get_params())
    print()
    
    for attr in dir(model):
        if attr.endswith("_") and not attr.startswith("_"):
            print(f"{attr} {getattr(model, attr)}")

            
describe_model(model)
from sklearn.model_selection import train_test_split as tts 


X_train, X_test, y_train, y_test = tts(X, y, train_size=0.8)
lrm = LinearRegression(normalize=True).fit(X_train, y_train)
lrm.score(X_test, y_test)
from sklearn.ensemble import RandomForestRegressor

rfm = RandomForestRegressor(n_estimators=10, max_depth=3).fit(X_train, y_train)
describe_model(rfm)
rfm.score(X_test, y_test)<jupyter_output>{'bootstrap': True, 'ccp_alpha': 0.0, 'criterion': 'mse', 'max_depth': 3, 'max_features': 'auto', 'max_leaf_nodes': None, 'max_samples': None, 'min_impurity_decrease': 0.0, 'min_impurity_split': None, 'min_samples_leaf': 1, 'min_samples_split': 2, 'min_weight_fraction_leaf': 0.0, 'n_estimators': 10, 'n_jobs': None, 'oob_score': False, 'random_state': None, 'verbose': 0, 'warm_start': False}

base_estimator_ DecisionTreeRegressor()
estimators_ [DecisionTreeRegressor(max_depth=3, max_features='auto', random_state=304369059), DecisionTreeRegressor(max_depth=3, max_features='auto', random_state=1692137987), DecisionTreeRegressor(max_depth=3, max_features='auto', random_state=1461427179), DecisionTreeRegressor(max_depth=3, max_features='auto', random_state=2008171853), DecisionTreeRegressor(max_depth=3, max_features='auto', random_state=1036859819), DecisionTreeRegressor(max_depth=3, max_features='auto', random_state=447914895), DecisionTreeRegressor(max_depth=3, max_features='auto', random[...]
{'fraction_non_alphanumeric': 0.1472340425531915, 'fraction_numerical': 0.024340425531914893, 'mean_word_length': 4.699321047526674, 'pattern_counts': {'":': 3, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '62948', 'license': 'MIT', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2121, 'n_words': 403, 'path': 'machine_learning/.ipynb_checkpoints/Single_building_model_test-checkpoint.ipynb', 'repo_name': 'georgetown-analytics/Power-Rangers', 'size': '5875'}