I am working on the file "HousePrediction.ipynb".
The first task is:
Choose the index
To simply train the model, I pick up 3 aspect of the index.
Area related : GrLivArea, GarageArea, TotalBsmtSF
Number/Rank related: OverallQual(don't know how it compute), FullBath, Fireplaces
Time related : YearBuilt, YearRemodAdd
Can you write Python code for it?

#missing data
total = df_train.isnull().sum().sort_values(ascending = False)
percent = (df_train.isnull().sum()/df_train.isnull().count()).sort_values(ascending = False)
missing_data = pd.concat([total,percent],axis=1,keys=['Total','Percent'])
missing_data.head(25)

# In the search for normality
sns.distplot(df_train['SalePrice'],fit=norm)
fig = plt.figure()
res = stats.probplot(df_train['SalePrice'],plot=plt)

df_train['SalePrice'] = np.log(df_train['SalePrice'])
sns.distplot(df_train['SalePrice'],fit=norm)
fig = plt.figure()
res = stats.probplot(df_train['SalePrice'],plot=plt)

sns.distplot(df_train['GrLivArea'],fit=norm)
fig = plt.figure()
res = stats.probplot(df_train['GrLivArea'],plot=plt)

# do log transformation
df_train['GrLivArea'] = np.log(df_train['GrLivArea'])
sns.distplot(df_train['GrLivArea'],fit=norm)
fig = plt.figure()
res = stats.probplot(df_train['GrLivArea'],plot=plt)

sns.distplot(df_train['GarageArea'],fit=norm)
fig = plt.figure()
res = stats.probplot(df_train['GarageArea'],plot=plt)

sns.distplot(df_train['TotalBsmtSF'],fit=norm)
fig = plt.figure()
res = stats.probplot(df_train['TotalBsmtSF'],plot=plt)

sns.distplot(df_train['OverallQual'])

sns.distplot(df_train['FullBath'])

sns.distplot(df_train['Fireplaces'])

sns.distplot(df_train['YearBuilt'],fit=norm)
fig = plt.figure()
res = stats.probplot(df_train['YearBuilt'],plot=plt)

sns.distplot(df_train['YearRemodAdd'],fit=norm)
fig = plt.figure()
res = stats.probplot(df_train['YearRemodAdd'],plot=plt)


I am working on the file "HousePrediction.ipynb".
The first task is:
How about using Xgboost?
Can you write Python code for it?

import xgboost as xgb

regr = xgb.XGBRegressor(
                 colsample_bytree=0.8,
                 gamma=0.0,
                 learning_rate=0.001,
                 max_depth=4,
                 min_child_weight=1.5,
                 n_estimators=10000,
                 reg_alpha=0.9,
                 reg_lambda=0.6,
                 subsample=0.8,
                 seed=42,
                 silent=False)

regr.fit(X_train,y_train)
y_pred = regr.predict(X_test)
print("XGBoost score on training set: ", rmse(y_test, y_pred))

#create prediction csv
df_test = pd.read_csv('test.csv')
#transfer data
df_test['GrLivArea'] = np.log(df_train['GrLivArea'])
X_data = pd.concat([df_test['GrLivArea'],df_test['GarageArea'],df_test['TotalBsmtSF'],df_test['OverallQual'],df_test['FullBath'],df_test['Fireplaces'],df_test['YearBuilt'],df_test['YearRemodAdd']],axis=1)

y_pred_data = regr.predict(X_data)
y_pred_data = np.exp(y_pred_data)

pd.DataFrame({'Id': df_test['Id'], 'SalePrice':y_pred_data}).to_csv('result.csv', index =False)


I am working on the file "Household+Electricity+Consumption+1990-2014.ipynb".
The first task is:
Electricity consumption between 1990 and 2014 in the household market in Central & SOuth America is led by Brazil frollowed by Argentina & Venezuela. Although it was expected Chile to be between the first three due to its economic development, its in the 5 place after Colombia. Compared to Brazil (first place) households consumption in Argentina (second place) is about 4 times less.
Can you write Python code for it?

#Plotting all the figures together for comparison.
#North America has a different scale that Europe & "Central & South America"

plt.figure(figsize=(20, 7))

plt.subplot(1, 3, 1)
df10.plot.bar()
plt.ylabel('GWh')
plt.ylim(0,1200000)
plt.title('Av. Elect. Cons. in N. America: Households 1990-2014')

plt.subplot(1, 3, 2)
df6.plot.bar()
plt.ylabel('GWh')
plt.ylim(0,140000)
plt.title('Av. Elect. Cons. in Europe: Households 1990-2014')

plt.subplot(1, 3, 3)
df8.plot.bar()
plt.ylabel('GWh')
plt.ylim(0,140000)
plt.title('Av. Elect. Cons. in Central & South America: Households 1990-2014')

#plt.legend(loc='lower right',prop={'size':14})
plt.tight_layout()
plt.show()

#Correct the problem of skewness when the 3 graphs are represented together by normalizing with Log the data.

plt.figure(figsize=(20, 7))

plt.subplot(1, 3, 1)
np.log(df10).plot.bar()
plt.ylabel('Log(GWh)')
plt.ylim(0,14)
plt.title('Av. Elect. Cons. in N. America: Households 1990-2014')

plt.subplot(1, 3, 2)
np.log(df6).plot.bar()
plt.ylabel('Log(GWh)')
plt.ylim(0,14)
plt.title('Av. Elect. Cons. in Europe: Households 1990-2014')

plt.subplot(1, 3, 3)
np.log(df8).plot.bar()
plt.ylabel('Log(GWh)')
plt.ylim(0,14)
plt.title('Av. Elect. Cons. in Central & South America: Households 1990-2014')

#plt.legend(loc='lower right',prop={'size':14})
plt.tight_layout()
plt.show()


I am working on the file "Household+Electricity+Consumption+1990-2014.ipynb".
The first task is:
The comparison between North America, Europe and Central & South America shows that average eletricity consumption in North America is 8.5 times bigger than the one in Europe (comparing the best in breed in each case). Europe compared to Central & South America has an average consumption 1.8 bigger. Within each regions variations are high concentrating most of the region´s consumption in less than 10 contries.
Can you write Python code for it?

#Histograms showing consumption in the World 1990-2014
plt.figure(figsize=(20, 5))

plt.subplot(1, 2, 1)
plt.xlabel("Electricity Consumption")
plt.ylabel("Frequency")
plt.hist(df3['Quantity (GWh)'], bins=5000 ,facecolor='green', alpha=0.5)
plt.axis([0, 20000, 0, 2000])
plt.ylabel('frequency')
plt.xlabel('Year')
plt.title('Distribution of Electricity Consumption in the World 1990-2014')

plt.subplot(1, 2, 2)
plt.xlabel("Electricity Consumption")
plt.ylabel("Frequency")
plt.hist(df3['Quantity (GWh)'], bins=5000 ,facecolor='red', normed=1, cumulative=1, alpha=0.5)
plt.axis([0, 80000, 0, 1])
plt.ylabel('frequency')
plt.xlabel('Year')
plt.title('Cumulative distribution of Electricity Consumption in the World')

plt.tight_layout()
plt.show()


I am working on the file "Household+Electricity+Consumption+1990-2014.ipynb".
The first task is:
The electricity consumption experiments a moderate growth from 1990 to 2015. There is a higher growth between 1990 and 2005 than from 2005 onwards. In the last 10 years of the period under analysis, the UK´s electricity consumption in the household segment has decreased. At the end of the period electricity consumption levels have fallen to those in the year 2000.
Can you write Python code for it?

#Dynamic Comparison of the Electricity consumption between The UK & Spain
plt.figure(figsize=(20, 5))

plt.subplot(1, 3, 1)
plt.scatter(x = df1["Year"], y = df1['Quantity'], color = 'purple', marker = 'o', s = 30)
plt.ylabel('GWh')
plt.xlabel('Year')
plt.ylim([20000, 160000])
plt.title('Electricity consumption in Spain by household 1990-2014')

plt.subplot(1, 3, 2)
plt.scatter(x = df2["Year"], y = df2['Quantity'], color = 'green', marker = 'x', s = 30)
plt.ylabel('GWn')
plt.xlabel('Year')
plt.ylim([20000, 160000])
plt.title('Electricity consumption in UK by household 1990-2014')

plt.subplot(1, 3, 3)
plt.scatter(x = df1["Year"], y = df1['Quantity'], color = 'purple', marker= "o", s= 30, label="Spain")
plt.scatter(x = df2["Year"], y = df2['Quantity'], color = 'green', marker ="x", s= 30, label="UK")
plt.ylabel('GWh')
plt.xlabel('Year')
plt.ylim([20000, 160000])
plt.title('Electricity consumption in Spain, UK by household 1990-2014')

plt.legend(loc='lower right',prop={'size':14})
plt.tight_layout()
plt.show()


I am working on the file "Household+Electricity+Consumption+1990-2014.ipynb".
The first task is:
United States carries most of the weight of the total electricity consumption in the household market in N. America in the perdio 1990-2014. US is followed in consumption by Canada and Mexico. The average consumption in the US is about 6 times more than in Canada and about 10 times the one in Mexico.
Can you write Python code for it?

#Europe
df5 = df4.loc[df4.index.isin(['Austria', 'Belgium', 'Bulgaria','Croatia', 'Cyprus', 'Czechia','Denmark', 'Estonia','Finland','France','Germany','Greece','Hungary','Ireland','Italy','Latvia','Lithuania','Luxembourg','Malta','Netherlands','Poland','Portugal','Romania','Slovakia', 'Slovenia','Spain', 'Sweden', 'United Kingdom'])]
df6= df5.sort_values(ascending=[False])
plt.figure(figsize=(10, 5))
plt.ylabel('GWh')
plt.title('Average Electricity Consumption in Europe: Household Market 1990-2014')
df6.plot.bar()


I am working on the file "Housing Prices.ipynb".
The first task is:
Decision Tree & Ada Boost Regressor
Can you write Python code for it?

#Initialize and fit the model

rf = DecisionTreeRegressor()
rfA = AdaBoostRegressor()

#Create range of values to fit parameters

k1  =['best']
k2 = ['mse','friedman_mse']
k3 = [100, 150, 200]
k4 = ['auto','sqrt']

parameters = {'criterion': k2,
              'splitter': k1,
             'max_features': k4
             }
parameters2 = {'n_estimators':k3}

#Fit parameters

rf1 = GridSearchCV(rf, param_grid=parameters, cv=kf)
rf2 = GridSearchCV(rfA, param_grid=parameters2, cv=kf)

rf1.fit(X_train,y_train)
rf2.fit(X_train,y_train)

#The best hyper parameters set
print("Best Hyper Parameters:", rf1.best_params_)
print("Best Hyper Parameters:", rf2.best_params_)

#Predict the value of y
y_pred = rf1.predict(X_test)
y_pred2 = rf2.predict(X_test)

#Mean squared error of the regressor
dt_mse = mean_squared_error(y_pred, y_test)
dt_rmse = np.sqrt(dt_mse)
ada_mse2 = mean_squared_error(y_pred2, y_test)
ada_rmse2 = np.sqrt(ada_mse2)

#Accuracy of the regressor
accdt = cross_val_score(rf1, X_test, y_test, cv=kf).mean()
accada = cross_val_score(rf2, X_test, y_test, cv=kf).mean()

#Accuracy of the regressor
accdtpca = cross_val_score(rf1, XPCA, Y, cv=kf).mean()
accadapca = cross_val_score(rf2, XPCA, Y, cv=kf).mean()

print('RMSE: %.4f' % dt_rmse)
print('RMSE: %.4f' % ada_rmse2)

print('Decision Tree Accuracy:  %.4f' % accdt)
print('Decision Tree Accuracy PCA:  %.4f' % accdtpca)
print('Ada Boost Accuracy:  %.4f' % accada)
print('Ada Boost Accuracy PCA:  %.4f' % accadapca)


I am working on the file "Housing Prices.ipynb".
The first task is:
Feature Importance with Random Forest
Can you write Python code for it?

#Calculate Feature Importance using Random Forest

rf = RandomForestRegressor()
rf.fit(X1, Y)

#Define feature importance
feature_importance = rf.feature_importances_

# Make importances relative to max importance.
feature_importance = 100.0 * (feature_importance / feature_importance.max())
sorted_idx = np.argsort(feature_importance)
pos = np.arange(sorted_idx.shape[0]) + .5
plt.figure(figsize=(20, 5))
plt.barh(pos, feature_importance[sorted_idx], align='center')
plt.yticks(pos, X1.columns[sorted_idx])
plt.xlabel('Relative Importance')
plt.title('Features')
plt.show()

#Feature Selection. Scores for the most relevant features (should we start with the one that has more explanatory power)
from sklearn.feature_selection import SelectKBest

# feature extraction
test = SelectKBest()
fit = test.fit(X1, Y)

#Identify features with highest score from a predictive perspective (for all programs)
names2 = X1.columns
Bestfeatures = pd.DataFrame(fit.scores_, index = names2)
Bestfeatures.columns = ['Best Features']
Bestfeatures.sort_values(by=['Best Features'], ascending=False)

#Build new set of features

#Initial features
names = ['Rooms', 'Type', 'Distance', 'Bedroom2', 'Bathroom', 'Car',
       'Landsize', 'BuildingArea', 'Regionname', 'Price_per_buildingarea',
       'Neighbourhood', 'Age']

X0 = X1[names]

#Recursive Feature Elimination

namesRFE = ['Rooms','Bathroom','Car', 'Type', 'Distance', 'Regionname', 'Neighbourhood','Age']

XRFE = X1[namesRFE]

#Best Select Features
namesBS = ['Rooms', 'Bathroom', 'Price_per_buildingarea', 'Neighbourhood']

XBS = X1[namesBS]

#Feature Importance (Random Forest)
namesFIRF = ['Rooms', 'Type', 'BuildingArea', 'Price_per_buildingarea', 'Neighbourhood']

XFIRF = X1[namesFIRF]


I am working on the file "Housing Prices.ipynb".
The first task is:
Feature Selection using Lasso
Can you write Python code for it?

#Feature Selection using Lasso

plt.figure(figsize=(20, 5))
sns.set_style("whitegrid")

plt.subplot(1, 4, 1)
for lambd in [x * 0.01 for x in range(1, 100)]:
    lasso = Lasso(alpha=lambd)
    lasso_coef = lasso.fit(X0, Y).coef_
    plt.xticks(range(len(names)), names, rotation=90)
    plt.ylabel('Coefficients')
    plt.plot(range(len(names)), lasso_coef)
    plt.title('Lasso (All features)')

plt.subplot(1, 4, 2)
for lambd in [x * 0.01 for x in range(1, 100)]:
    lasso = Lasso(alpha=lambd)
    lasso_coef = lasso.fit(XRFE, Y).coef_
    plt.xticks(range(len(namesRFE)), namesRFE, rotation=90)
    plt.ylabel('Coefficients')
    plt.plot(range(len(namesRFE)), lasso_coef)
    plt.title('Lasso RFE')

plt.subplot(1, 4, 3)
for lambd in [x * 0.01 for x in range(1, 100)]:
    lasso = Lasso(alpha=lambd)
    lasso_coef = lasso.fit(XBS, Y).coef_
    plt.xticks(range(len(namesBS)), namesBS, rotation=90)
    plt.ylabel('Coefficients')
    plt.plot(range(len(namesBS)), lasso_coef)
    plt.title('Lasso (Best Selector)')

plt.subplot(1, 4, 4)
for lambd in [x * 0.01 for x in range(1, 100)]:
    lasso = Lasso(alpha=lambd)
    lasso_coef = lasso.fit(XFIRF, Y).coef_
    plt.xticks(range(len(namesFIRF)), namesFIRF, rotation=90)
    plt.ylabel('Coefficients')
    plt.plot(range(len(namesFIRF)), lasso_coef)
    plt.title('Lasso on Random Forest')

plt.tight_layout()
plt.show()

#New set of features based on results obtained in with Lasso, Random Forest and RFE

Xl = X1[['Rooms', 'BuildingArea', 'Regionname','Distance', 'Neighbourhood']]

#Split the data into training and testing datasets. Split: 70/30; train/test

X_train, X_test, y_train, y_test = train_test_split(Xl,Y, test_size=0.3, random_state=0)

#Initiating the cross validation generator, N splits = 5
kf = KFold(5)


I am working on the file "Housing Prices.ipynb".
The first task is:
Gradient Boosting
Can you write Python code for it?

# Train model
GBR = GradientBoostingRegressor()

k2 = ['ls','huber']
k4 = [200,300, 350]

parameters = { 'loss': k2,
             'n_estimators': k4}

#Fit parameters

GBR1 = GridSearchCV(GBR, param_grid=parameters, cv=kf)

GBR1.fit(X_train, y_train)

#The best hyper parameters set
print("Best Hyper Parameters:", GBR1.best_params_)

#Predict the value of y
y_pred = GBR1.predict(X_test)

#Mean squared error of the regressor
gb_mse = mean_squared_error(y_pred, y_test)
gb_rmse = np.sqrt(gb_mse)

#Accuracy of the regressor
accgd = cross_val_score(GBR1, X_test, y_test, cv=kf).mean()
accgdpca = cross_val_score(GBR1, XPCA, Y, cv=kf).mean()

print('RMSE: %.4f' % gb_rmse)
print('Gradient Boosting Regressor Accuracy: %.4f' % accgd)
print('Gradient Boosting Regressor Accuracy PCA: %.4f' % accgdpca)


I am working on the file "Housing Prices.ipynb".
The first task is:
KNN Regressor
Can you write Python code for it?

#Initialize and fit the model

KNR = KNeighborsRegressor(n_jobs=-1)

#Create range of values to fit parameters

#Create range of values to fit parameters

k1 = [17,19,21,23,25]
k3 = ['distance']

parameters = {'n_neighbors': k1, 'weights':k3}

#Fit parameters

KNR1 = GridSearchCV(KNR, param_grid=parameters, cv=kf)

KNR1.fit(X_train,y_train)

#The best hyper parameters set
print("Best Hyper Parameters:", KNR1.best_params_)

#Predict the value of y
y_pred = KNR1.predict(X_test)

#Mean squred error of the regressor
KNR_mse = mean_squared_error(y_pred, y_test)
KNR_rmse = np.sqrt(KNR_mse)

#Accuracy of the regressor
accKNR1 = cross_val_score(KNR1, X_test, y_test, cv=kf).mean()
accKNR1pca = cross_val_score(KNR1, XPCA, Y, cv=kf).mean()

print('RMSE: %.4f' % KNR_rmse)
print('KN Regressor Accuracy: %.4f' % accKNR1)
print('KN Regressor Accuracy PCA: %.4f' % accKNR1pca)


I am working on the file "Housing Prices.ipynb".
The first task is:
Linear Regression
Can you write Python code for it?

#Fit the Linear Regression model with the PCA features
lr = LinearRegression()

lr.fit(X_train, y_train)

print(lr.coef_)

#Predict the value of y
y_pred = lr.predict(X_test)

#Mean squared error of the regressor
lin_mse = mean_squared_error(y_pred, y_test)
lin_rmse = np.sqrt(lin_mse)

#Accuracy of the regressor
accln = cross_val_score(lr, X_test, y_test, cv=kf).mean()
acclnpca = cross_val_score(lr, XPCA, Y, cv=kf).mean()

print('RMSE: %.4f' % lin_rmse)
print('Linear Regression Accuracy: %.4f' % accln)
print('Linear Regression Accuracy PCA: %.4f' % acclnpca)


I am working on the file "Housing Prices.ipynb".
The first task is:
PCA Analysis
Can you write Python code for it?

#Eigenvectores & Eigenvalues

eig_vals, eig_vecs = np.linalg.eig(correlation_matrix)

sklearn_pca = PCA(n_components=len(Z.columns))
Y_sklearn = sklearn_pca.fit_transform(correlation_matrix)

print(
    'The percentage of total variance in the dataset explained by each',
    'component from Sklearn PCA.\n',
    sklearn_pca.explained_variance_ratio_
)

#From the Scree plot.

plt.plot(eig_vals)
plt.show()

#PCA Analysis

# Create a scaler object
sc = StandardScaler()

# Fit the scaler to the features and transform
X_std = sc.fit_transform(X1)

# Create a PCA object
pca = decomposition.PCA(n_components=6)

# Fit the PCA and transform the data
X_std_pca = pca.fit_transform(X_std)

# View the new feature data's shape
X_std_pca.shape

# Create a new dataframe with the new features

XPCA = pd.DataFrame(X_std_pca)
XPCA.head()


I am working on the file "Housing Prices.ipynb".
The first task is:
Random Forest
Can you write Python code for it?

#Initialize and fit the model

abc = RandomForestRegressor(n_jobs=-1)

#Create range of values to fit parameters

k2 = ['mse','mae']
k4 = ['auto','log2']
k3 = [150, 200,250, 300]

parameters = { 'criterion': k2,
              'n_estimators': k3,
             'max_features': k4
}

#Fit parameters

clf = GridSearchCV(abc, param_grid=parameters, cv=kf)

clf.fit(X_train,y_train)

#The best hyper parameters set
print("Best Hyper Parameters:", clf.best_params_)

#Predict the value of y
y_pred = clf.predict(X_test)

#Mean squared error of the regressor
rf_mse = mean_squared_error(y_pred, y_test)
rf_rmse = np.sqrt(rf_mse)

#Accuracy of the regressor
accrf = cross_val_score(clf, X_test, y_test, cv=kf).mean()
accrfpca = cross_val_score(clf, XPCA, Y, cv=kf).mean()

print('RMSE: %.4f' % rf_rmse)
print('Random Forest Accuracy:  %.4f' % accrf)
print('Random Forest Accuracy PCA:  %.4f' % accrfpca)


I am working on the file "Housing Prices.ipynb".
The first task is:
Recursive Feature Elimination & PCA
Can you write Python code for it?

# create the RFE model and select features

lr = LinearRegression()
nfeatures = 4
rfe = RFE(lr,nfeatures)
fit = rfe.fit(X1,Y)

# summarize the selection of the features

result_RFE = pd.DataFrame(list(zip(X1.head(0), rfe.ranking_, rfe.support_)),columns=['Features','Ranking','Support'] )
result_RFE.sort_values('Ranking')


I am working on the file "Housing Prices.ipynb".
The first task is:
Ridge Regression
Can you write Python code for it?

# Testing the Ridge Regression.

ridgeregr = Ridge()

k1 = np.arange(50)+1

parameters = {'alpha': k1}

#Fit parameters

ridgeregr1 = GridSearchCV(ridgeregr, param_grid=parameters, cv=kf)

#Fit the tunned classifier in the traiing space
ridgeregr1.fit(X_train, y_train)

#Print the best parameters
print(ridgeregr1.best_params_)

# Fit the model.
ridgeregr1.fit(X_test, y_test)

#Predict the value of y
y_pred = ridgeregr1.predict(X_test)

#Mean squared error of the regressor
rid_mse = mean_squared_error(y_pred, y_test)
rid_rmse = np.sqrt(rid_mse)

#Accuracy of the regressor
accridg = cross_val_score(ridgeregr1, X_test, y_test, cv=kf).mean()
accridgpca = cross_val_score(ridgeregr1, XPCA, Y, cv=kf).mean()

print('RMSE: %.4f' % rid_rmse)
print('Ridge Regression Accuracy: %.4f' % accridg)
print('Ridge Regression Accuracy PCA: %.4f' % accridgpca)


I am working on the file "Housing Prices/Housing Prices Regression.ipynb".
The first task is:
Optimizing out model using Bayesian Optimization!
It's not reflected in this workbook, but my workflow for Keras has recently transitioned to primarily using Hyperas for hypermarameter optimization.
Can you write Python code for it?

model, history = keras_model(X_train, X_test, y_train, y_test)

predicted = model.model.predict(X_test)

plt.plot(y_test - predicted)

test_data = preprocess_data(test_data=True)


I am working on the file "Houston_J/Houston_J-sub1.ipynb".
The first task is:
#There are 4149 elements, and PE has a significant amount of missing values
Can you write Python code for it?

well_PE_Miss = train.loc[train["PE"].isnull(),"Well Name"].unique()
well_PE_Miss

train.loc[train["Well Name"] == well_PE_Miss[0]].count()
train.loc[train["Well Name"] == well_PE_Miss[1]].count()


I am working on the file "Houston_J/Houston_J-sub1.ipynb".
The first task is:
The PE of all wells have no strong variance; For now, fillin the Missing value of median

Fancy visualization from forum
Can you write Python code for it?

features = ['GR', 'ILD_log10', 'DeltaPHI',
    'PHIND','PE','NM_M', 'RELPOS']
feature_vectors = train[features]
facies_labels = train['Facies']
## 1=sandstone  2=c_siltstone   3=f_siltstone
## 4=marine_silt_shale 5=mudstone 6=wackestone 7=dolomite
## 8=packstone 9=bafflestone
facies_colors = ['#F4D03F', '#F5B041','#DC7633','#6E2C00',
       '#1B4F72','#2E86C1', '#AED6F1', '#A569BD', '#196F3D']

facies_labels = ['SS', 'CSiS', 'FSiS', 'SiSh', 'MS',
                 'WS', 'D','PS', 'BS']
#facies_color_map is a dictionary that maps facies labels
#to their respective colors
facies_color_map = {}
for ind, label in enumerate(facies_labels):
    facies_color_map[label] = facies_colors[ind]

def label_facies(row, labels):
    return labels[ row['Facies'] -1]

train.loc[:,'FaciesLabels'] = train.apply(lambda row: label_facies(row, facies_labels), axis=1)
#

def make_facies_log_plot(logs, facies_colors):
    #make sure logs are sorted by depth
    logs = logs.sort_values(by='Depth')
    cmap_facies = colors.ListedColormap(
            facies_colors[0:len(facies_colors)], 'indexed')

    ztop=logs.Depth.min(); zbot=logs.Depth.max()

    cluster=np.repeat(np.expand_dims(logs['Facies'].values,1), 100, 1)

    f, ax = plt.subplots(nrows=1, ncols=6, figsize=(8, 12))
    ax[0].plot(logs.GR, logs.Depth, '-g')
    ax[1].plot(logs.ILD_log10, logs.Depth, '-')
    ax[2].plot(logs.DeltaPHI, logs.Depth, '-', color='0.5')
    ax[3].plot(logs.PHIND, logs.Depth, '-', color='r')
    ax[4].plot(logs.PE, logs.Depth, '-', color='black')
    im=ax[5].imshow(cluster, interpolation='none', aspect='auto',
                    cmap=cmap_facies,vmin=1,vmax=9)

    divider = make_axes_locatable(ax[5])
    cax = divider.append_axes("right", size="20%", pad=0.05)
    cbar=plt.colorbar(im, cax=cax)
    cbar.set_label((17*' ').join([' SS ', 'CSiS', 'FSiS',
                                'SiSh', ' MS ', ' WS ', ' D  ',
                                ' PS ', ' BS ']))
    cbar.set_ticks(range(0,1)); cbar.set_ticklabels('')
   ...


I am working on the file "How many Australian Open players need photos in Wikipedia?.ipynb".
The first task is:
(Another task is try and resolve the disambiguation page to the correct page, but I'll tackle that later.)
Can you write Python code for it?

needspage = []
disambigs = []
hasimage = []
needsimage = []

with open(PLAYERSFILE) as players:
    for player in players:
        forwardname = normaliseName(player)
        page = getPage(forwardname)
        if not page:
            needspage.append(forwardname)
            continue

        if isDisambiguation(page):
            disambigs.append(page.name)
        elif hasImage(page):
            hasimage.append(page.name)
        else:
            needsimage.append(page.name)

print("Needs page:", needspage)
print("Disambig:", disambigs)
print("Has image:", hasimage)
print("Needs image:", needsimage)


I am working on the file "How many Australian Open players need photos in Wikipedia?.ipynb".
The first task is:
Disambiguation pages
Disambiguation or "dab" pages are what I would call part of the Wikipedia API. They're built on editing community conventions rather than technical capabilities of MediaWiki. But I need to deal with them otherwise the results will be nonsense.
So let's look at the full content of the Brian Baker page and see what there is to play with:
Can you write Python code for it?

page = site.Pages['Brian Baker']
print(page.text())


I am working on the file "How many Australian Open players need photos in Wikipedia?.ipynb".
The first task is:
Hmm ok...kind of not that useful. If I look at the page on Wikipedia, I can see there is a bit more structure that is not evident in the page wikitext:
<img src="blog/brianbakerdisambig.png" />
At the bottom there is a category which seems pretty definitive in terms of identifying a disambiguation page. Categories are part of the MediaWiki API:
Can you write Python code for it?

page = site.Pages['Brian Baker']
cats = page.categories()
for cat in cats:
    print(cat['title'])


I am working on the file "How many Australian Open players need photos in Wikipedia?.ipynb".
The first task is:
I'll worry about hasImage in a minute. Right now there is a more pressing problem: fixing up the names. I need to get rid of those newlines and make them 'firstname lastname' to match the Wikipedia naming convention.
Can you write Python code for it?

def normaliseName(name):
    last, first = name.strip().split(', ')
    return ' '.join([first, last])

hasimage = []
needsimage = []

with open(PLAYERSFILE) as players:
    for player in players:
        forwardname = normaliseName(player)
        page = getPage(forwardname)
        if hasImage(page):
            hasimage.append(forwardname)
        else:
            needsimage.append(forwardname)

print("Has image:", hasimage)
print("Needs image:", needsimage)


I am working on the file "How many Australian Open players need photos in Wikipedia?.ipynb".
The first task is:
Now we can put it all together:
Can you write Python code for it?

needspage = []
disambigs = []
hasimage = []
needsimage = []

with open(PLAYERSFILE) as players:
    for player in players:
        forwardname = normaliseName(player)
        page = getPage(forwardname)
        if not page:
            needspage.append(forwardname)
            continue

        if isDisambiguation(page):
            disambigs.append(page.name)
        elif hasImage(page):
            hasimage.append(page.name)
        else:
            needsimage.append(page.name)

print("Has image:", hasimage)
print("Disambig:", disambigs)
print("Needs image:", needsimage)
print("No page:", needspage)


I am working on the file "How many Australian Open players need photos in Wikipedia?.ipynb".
The first task is:
Page doesn't exist
Gregoire Barrere (or rather Grégoire Barrère) doesn't have a page yet. The API also copes with this pretty well:
Can you write Python code for it?


I am working on the file "How many Australian Open players need photos in Wikipedia?.ipynb".
The first task is:
So I update my getPage function:
Can you write Python code for it?

def getPage(name):
    page = site.Pages[name].resolve_redirect()
    if not page.exists:
        return
    return page

needspage = []
hasimage = []
needsimage = []

with open(PLAYERSFILE) as players:
    for player in players:
        forwardname = normaliseName(player)
        page = getPage(forwardname)
        if not page:
            needspage.append(forwardname)
            continue

        if hasImage(page):
            hasimage.append(page.name)
        else:
            needsimage.append(page.name)

print("Needs page:", needspage)
print("Has image:", hasimage)
print("Needs image:", needsimage)


I am working on the file "How many Australian Open players need photos in Wikipedia?.ipynb".
The first task is:
That's... a lot of flags. It's because editors like to do this kind of thing:
<img src="blog/flags.png" />
So to filter them out, what do they have in common?
What jumps out at me is that they are SVGs. SVGs are not normally used for photographs (which is what we are trying to detect), so that will be a good start.
Can you write Python code for it?

def isBoring(imagename):
    # Flags, Increase2.svg, Decrease2.svg
    return imagename.endswith('.svg')

def hasImage(page):
    images = page.images()
    imgnames = [image['title'] for image in images]
    interestingImages = [imgname for imgname in imgnames
                         if not isBoring(imgname)]
    return bool(interestingImages)

for player in['Marcos Baghdatis', 'Bai Yan', 'Mirza Bašić']:
    page = getPage(player)
    print(player, hasImage(page))


I am working on the file "How many Australian Open players need photos in Wikipedia?.ipynb".
The first task is:
This reveals a few issues I need to deal with before I start looking at images. The Marcos Baghdatis article seems legit. Yan Bai and Mirza Basic are redirects. Brian Baker is a disambiguation page, and Gregoire Barrere maybe doesn't have a page yet. 😢 Can anyone fix that?
Redirects
If I type in "Yan Bai" at Wikipedia, I get whisked off to https://en.wikipedia.org/wiki/Bai_Yan . There is a visual hint that something happened:
<img src="blog/redirect.png" />
Happily, the API knows about redirects and can automatically resolve them for me.
Can you write Python code for it?

def getPage(name):
    return site.Pages[name].resolve_redirect()

for player in ['Yan Bai', 'Mirza Basic']:
    page = getPage(player)
    print(player.upper())
    print(page.text()[:200])


I am working on the file "How to create 3D plots.ipynb".
The first task is:
1. In browser interactive plot with webgl
This option is the most convenient for interactive use, and it is the default. A 3D plot will appear inside the web browser, including controls to change the quanitity plotted, and with mouse controls to zoom, pan etc.
In theory this should work in any modern web browser, but it has been most extensively tested under google chrome. If this does not work, then try upgrading your web browser and then your graphics card drivers.
This option is the preferred approach, even though it is somewhat experimental. Note that it is based on a javascript webgl animation loop, so CPU usage may be high even just to view the plot.
Before plotting this way, you should have passed notebook=True when creating the Simulation object.
Can you write Python code for it?

sim.plot_3d(solution=I["J", :])#, output_format='webgl')#, compress_scalars=5)


I am working on the file "How to create 3D plots.ipynb".
The first task is:
2. Generating a VTK file for external tools
Another option is to plot to a .vtk file. This is a common format for 3D visualisation of scientific plots. This file format can be read by the prgrams Paraview or VisIt.
This method is not convenient for interactive use, since an external program must be used to view the plots. However, these software packages are very flexible and are well-suited to producing publication quality figures.
Note that the libraries for generating .vtk files are incompatible with Python 3.x, so Python 2.7 should be used
To generate such a file, use the sim.plot_3d function with the option output_format='vtk'.
Can you write Python code for it?

# make sure the output directory exists
if not osp.exists('output'):
    os.makedirs('output')

sim.plot_3d(solution=I, output_format='vtk', filename=osp.join('output', 'bcsrr-planewave.vtp'))


I am working on the file "How to create 3D plots.ipynb".
The first task is:
A simulation object named sim is created to manage the simulation. This gives us convenient access to most of the plotting functions.
Can you write Python code for it?

sim = openmodes.Simulation(notebook=True)

filename = osp.join(openmodes.geometry_dir, "SRR.geo")
mesh_tol = 1e-3
outer_radius = 4e-3
srr = sim.load_mesh(filename, mesh_tol, parameters={'inner_radius': 2.5e-3, 'outer_radius': outer_radius})

ring1 = sim.place_part(srr)
ring2 = sim.place_part(srr)
ring2.rotate(axis = [0, 0, 1], angle = 180)
ring2.translate([0, 0, 2e-3])

# construct the source plane wave with given polarisation and propagation direction
e_inc = np.array([0, 1, 0])
k_dir = np.array([1, 0, 0])
plane_wave = PlaneWaveSource(e_inc, k_dir)

# solve the currents for one frequency
s = 2j*np.pi*4e9
Z = sim.impedance(s)
V = sim.source_vector(plane_wave, s)
I = Z.solve(V)


I am working on the file "How to think about Latent Dirichlet Allocation.ipynb".
The first task is:
Sci-kit learn does not implement
LDA with Yelp: An Example
This is based on the paper by McAuley & Leskovec (2013).

References

https://www.cs.princeton.edu/~blei/papers/BleiNgJordan2003.pdf

LDA Notes

Only for text corpora?
No but it was the main thing that it was decided for.

Seen as an alternative to tf-idf
disadvantages of tf-idf is that it provides a relatively small amount of reduction in description legnth and reveals little in the way of inter- or intra-document statistical structure.
What they mean by this is that it tells us nothing about the structure to know that the shows up more often in one document than the other.

pLSI takes a step forward compared to LSI in that it models each word in a document from a mixture model.
problem is pLSI provides no probabilistic model at the level of documents.
Leads to overfitting with

p/LSI are based on the bag-of-words approach.
Other uses for LDA include callaborative filtering and content-based image retrieval

Dirchlet Distribution
The Dirchlet distribution is paramterized with the number of categories, $K$, and a vector of concentration parameters, $\boldsymbol{\alpha}$
It is a distribution over multinomials.
The craziness of LDA distributions

The Dirchlet distribution is a generalization of the beta distribution.
The beta distribution is itself a pretty crazy distribution:

To make the Beta distribution extra confusing, the two parameters, $\alpha$ and $\beta$ are both abstract.

a 3d distribution will model the distribution for three topics

Fun facts about the beta distribution

Model the behavior of random variables limited to intervals of finite length in a wide variety of disciplines
Used in Bayesian Inference as a conjugate prior probability distribution for various other distributions.
defined between [0,1]
$\alpha$ and $\beta$ are concentration parameters. The smaller they are, the more sparse the distribution is.
Sme people claim that it will be better
The beta distribution is built for proportions which is good....
Can you write Python code for it?

%pylab inline

import numpy as np
s = np.random.dirichlet((10, 5, 3), 50).transpose()
plt.barh(range(50), s[0])
plt.barh(range(50), s[1], left=s[0], color='g')
plt.barh(range(50), s[2], left=s[0]+s[1], color='r')
_ = plt.title("Lengths of Strings")

[i.mean() for i in s] #Mean of each of the lengths of the string

[i/18 for i in [10, 5, 3]] # Predicted values of each of the lengths of the string


I am working on the file "How-to-Use-Tensorflow-for-Time-Series-Live--master/demo_full_notes.ipynb".
The first task is:
As you can see in the picture below that is done by unpacking the columns (axis = 1) of the batch into a Python list. The RNN will simultaneously be training on different parts in the time-series; steps 4 to 6, 16 to 18 and 28 to 30 in the current batch-example. The reason for using the variable names “plural”_”series” is to emphasize that the variable is a list that represent a time-series with multiple entries at each step.
Can you write Python code for it?

Image(url= "https://cdn-images-1.medium.com/max/1600/1*f2iL4zOkBUBGOpVE7kyajg.png")
#Schematic of the current batch split into columns, the order index is shown on each data-point
#and arrows show adjacent time-steps.


I am working on the file "How-to-Use-Tensorflow-for-Time-Series-Live--master/demo_full_notes.ipynb".
The first task is:
The fact that the training is done on three places simultaneously in our time-series, requires us to save three instances of states when propagating forward. That has already been accounted for, as you see that the init_state placeholder has batch_size rows.
Can you write Python code for it?

#Forward pass
#state placeholder
current_state = init_state
#series of states through time
states_series = []

#for each set of inputs
#forward pass through the network to get new state value
#store all states in memory
for current_input in inputs_series:
    #format input
    current_input = tf.reshape(current_input, [batch_size, 1])
    #mix both state and input data
    input_and_state_concatenated = tf.concat(1, [current_input, current_state])  # Increasing number of columns
    #perform matrix multiplication between weights and input, add bias
    #squash with a nonlinearity, for probabiolity value
    next_state = tf.tanh(tf.matmul(input_and_state_concatenated, W) + b)  # Broadcasted addition
    #store the state in memory
    states_series.append(next_state)
    #set current state to next one
    current_state = next_state


I am working on the file "How-to-Use-Tensorflow-for-Time-Series-Live--master/demo_full_notes.ipynb".
The first task is:
The figure below shows the input data-matrix, and the current batch batchX_placeholder
is in the dashed rectangle. As we will see later, this “batch window” is slided truncated_backprop_length
steps to the right at each run, hence the arrow. In our example below batch_size = 3, truncated_backprop_length = 3,
and total_series_length = 36. Note that these numbers are just for visualization purposes, the values are different in the code.
The series order index is shown as numbers in a few of the data-points.
Can you write Python code for it?

Image(url= "https://cdn-images-1.medium.com/max/1600/1*n45uYnAfTDrBvG87J-poCA.jpeg")

#Now it’s time to build the part of the graph that resembles the actual RNN computation,
#first we want to split the batch data into adjacent time-steps.

# Unpack columns
#Unpacks the given dimension of a rank-R tensor into rank-(R-1) tensors.
#so a bunch of arrays, 1 batch per time step

# Change to unstack for new version of TF
inputs_series = tf.unstack(batchX_placeholder, axis=1)
labels_series = tf.unstack(batchY_placeholder, axis=1)


I am working on the file "How-to-Use-Tensorflow-for-Time-Series-Live--master/demo_full_notes.ipynb".
The first task is:
The last line is adding the training functionality, TensorFlow will perform back-propagation for us automatically — the computation graph is executed once for each mini-batch and the network-weights are updated incrementally.
Notice the API call to sparse_softmax_cross_entropy_with_logits, it automatically calculates the softmax internally and then computes the cross-entropy. In our example the classes are mutually exclusive (they are either zero or one), which is the reason for using the “Sparse-softmax”, you can read more about it in the API. The usage is to havelogits is of shape [batch_size, num_classes] and labels of shape [batch_size].
Can you write Python code for it?

#visualizer
def plot(loss_list, predictions_series, batchX, batchY):
    plt.subplot(2, 3, 1)
    plt.cla()
    plt.plot(loss_list)

    for batch_series_idx in range(5):
        one_hot_output_series = np.array(predictions_series)[:, batch_series_idx, :]
        single_output_series = np.array([(1 if out[0] < 0.5 else 0) for out in one_hot_output_series])

        plt.subplot(2, 3, batch_series_idx + 2)
        plt.cla()
        plt.axis([0, truncated_backprop_length, 0, 2])
        left_offset = range(truncated_backprop_length)
        plt.bar(left_offset, batchX[batch_series_idx, :], width=1, color="blue")
        plt.bar(left_offset, batchY[batch_series_idx, :] * 0.5, width=1, color="red")
        plt.bar(left_offset, single_output_series * 0.3, width=1, color="green")

    plt.draw()
    plt.pause(0.0001)


I am working on the file "How-to-Use-Tensorflow-for-Time-Series-Live--master/demo_full_notes.ipynb".
The first task is:
There is a visualization function so we can se what’s going on in the network as we train. It will plot the loss over the time, show training input, training output and the current predictions by the network on different sample series in a training batch.
Can you write Python code for it?

#Step 3 Training the network
with tf.Session() as sess:
    #we stupidly have to do this everytime, it should just know
    #that we initialized these vars. v2 guys, v2..
    sess.run(tf.initialize_all_variables())
    #interactive mode
    plt.ion()
    #initialize the figure
    plt.figure()
    #show the graph
    plt.show()
    #to show the loss decrease
    loss_list = []

    for epoch_idx in range(num_epochs):
        #generate data at eveery epoch, batches run in epochs
        x,y = generateData()
        #initialize an empty hidden state
        _current_state = np.zeros((batch_size, state_size))

        print("New data, epoch", epoch_idx)
        #each batch
        for batch_idx in range(num_batches):
            #starting and ending point per batch
            #since weights reoccuer at every layer through time
            #These layers will not be unrolled to the beginning of time,
            #that would be too computationally expensive, and are therefore truncated
            #at a limited number of time-steps
            start_idx = batch_idx * truncated_backprop_length
            end_idx = start_idx + truncated_backprop_length

            batchX = x[:,start_idx:end_idx]
            batchY = y[:,start_idx:end_idx]

            #run the computation graph, give it the values
            #we calculated earlier
            _total_loss, _train_step, _current_state, _predictions_series = sess.run(
                [total_loss, train_step, current_state, predictions_series],
                feed_dict={
                    batchX_placeholder:batchX,
                    batchY_placeholder:batchY,
                    init_state:_current_state
                })

            loss_list.append(_total_loss)

            if batch_idx%100 == 0:
                print("Step",batch_idx, "Loss", _total_loss)
                plot(loss_list, _predictions_series, batchX, batchY)

plt.ioff()
plt.show()


I am working on the file "How-to-Use-Tensorflow-for-Time-Series-Live--master/demo_full_notes.ipynb".
The first task is:
Time series of squares, the elevated black square symbolizes an echo-output, which is activated three steps from the echo input (black square). The sliding batch window is also striding three steps at each run, which in our sample case means that no batch will encapsulate the dependency, so it can not train.
The network will be able to exactly learn the echo behavior so there is no need for testing data.
The program will update the plot as training progresses, Blue bars denote a training input signal (binary one), red bars show echos in the training output and green bars are the echos the net is generating. The different bar plots show different sample series in the current batch. Fully trained at 100 epochs look like this
Can you write Python code for it?


I am working on the file "How-to-Use-Tensorflow-for-Time-Series-Live--master/demo_full_notes.ipynb".
The first task is:
You can see that we are moving truncated_backprop_length steps forward on each iteration (line 15–19), but it is possible have different strides. This subject is further elaborated in this article. The downside with doing this is that truncated_backprop_length need to be significantly larger than the time dependencies (three steps in our case) in order to encapsulate the relevant training data. Otherwise there might a lot of “misses”, as you can see on the figure below.
Can you write Python code for it?


I am working on the file "How_to_Python/01_How_to_Python_Variables.ipynb".
The first task is:
A diferencia de otros lenguajes, no hubo que especificar que tipo de variable seria cada una. El interprete lo hace automaticamente.
Nombres de variables:

El nombre de una variable debe empezar con una letra o con un guion bajo (_), pero NUNCA con un numero
El nombre de una variable solo puede contener caracteres alfa-numericos o guiones bajos
Los nombres de variables reconocen mayusculas (hola != HoLa)

Operadores
Para realizar calculos matematicos complejos primero se debe poder realizar calculos simples
- suma ( + ), resta ( - ), multiplicacion ( * ), division ( / )
Ejemplos
Can you write Python code for it?

print(2 + 2)
print(2 - 2)
print(2 * 2)
print(2 / 2)


I am working on the file "How_to_Python/01_How_to_Python_Variables.ipynb".
The first task is:
Colecciones:
Las colecciones son objetos que representan un conjunto de objetos. Hay 4 objetos de este tipo nativos de python:
- Listas : Colecciones cuyos elementos estan ordenados y son intercambiables. Se permiten elementos duplicados
- Tuplas : Colecciones cuyos elementos estan ordenados y no son intercambiables. Se permiten elementos duplicados
- Set : Colecciones cuyos elementos no estan ordenados y no estan indexados. No se permiten elementos duplicados
- Diccionarios : Colecciones cuyos elementos no estan ordenados, son intercambiables y estan indexados. No se permiten elementos duplicados
Ejemplos:
Can you write Python code for it?

l = list([1, 2, 3])
t = tuple((4, 5))
s = set({6, 7, 8})
d = dict({'a': 1})
print(l)
print(t)
print(s)
print(d)


I am working on the file "How_to_Python/01_How_to_Python_Variables.ipynb".
The first task is:
En el caso de los diccionarios, el operador in solo busca si un elemento existe dentro de las llaves del diccionario, y no dentro de los valores!!
Podemos encontrar el largo de una coleccion facilmente
Can you write Python code for it?


I am working on the file "How_to_Python/01_How_to_Python_Variables.ipynb".
The first task is:
Hasta ahora las listas, tuplas y diccionarios se han creado usando list(), tuple(), y dict() respectivamente, sin embargo se pueden crear sin usaar estas instrucciones:
Can you write Python code for it?


I am working on the file "How_to_Python/01_How_to_Python_Variables.ipynb".
The first task is:
Los mas usados son las listas, tuplas y diccionarios asi que veremos operaciones basicas con ellos
Acceder a elementos:
Para acceder a elementos de una coleccion se utilizan los parentesis cuadrados con el indice en su interior (por ejemplo, l[3])
- En el caso de las listas y tuplas, los elementos estan indexados numericamentes tal que el primer elemento es el numero 0
- Tambien se puede acceder a los elementos usando numeros negativos. En este caso, el ultimo elemento es el numero -1
- Los diccionarios tienen una indexizacion personalizada. Al crear uno se hace dict({llave: valor})
Can you write Python code for it?

l = list([1, 2, 3])
print(l[0])
print(l[-1])
t = tuple((4, 5, 6))
print(t[1])
print(t[-2])
d = dict({'llave1': 7, 'llave2': 8})
print(d['llave1'])
print(d['llave2'])


I am working on the file "How_to_Python/01_How_to_Python_Variables.ipynb".
The first task is:
Modificacion de elementos:
(recuerden que las tuplas no son modificables!)
Can you write Python code for it?


I am working on the file "How_to_Python/01_How_to_Python_Variables.ipynb".
The first task is:
Operadores booleanos:

and : retorna True si se comparan dos booleanos True
or : retorna True si al menos uno de los booleanos comparados es True
not : invierte el booleano
== : retorna True si dos objetos son iguales
!= : retorna True si dos objetos no son iguales
a < b : retorna True si a es menor que b
a <= b : retorna True si a es menor o igual que b
a > b : retorna True si a es mayor que b
a >= b : retorna True si a es mayor o igual que b

Ejemplos:
Can you write Python code for it?

print(True and False)
print(True and True)
print(True or False)
print(False or False)
print(not True)
print(not False)


I am working on the file "How_to_Python/01_How_to_Python_Variables.ipynb".
The first task is:
Operadores de identidad:

is : retorna True si ambas variables son el mismo objeto
is not : retorna False si las variables no son el mismo objeto

Operadores de pertenencia:

in : Retorna True si una secuencia esta presente en un objeto
not in : Retorna True si una secuencia no esta presente en un objeto

Operaciones a nivel de bits:

& bitwise AND
| bitwise OR
^ bitwise XOR
~ NOT
<< left shift
>> right shift

Ejemplos:
Can you write Python code for it?

print('10 en binario: {0:b}'.format(10))
print(' 5 en binario: 0{0:b}'.format(5))
print('10 & 5 en binario: {0:b}'.format(10 & 5), end='\n\n')

print('10 en binario: {0:b}'.format(10))
print(' 5 en binario: 0{0:b}'.format(5))
print('10 | 5 en binario: {0:b}'.format(10 | 5), end='\n\n')

print('10 en binario: {0:b}'.format(10))
print(' 7 en binario: 0{0:b}'.format(7))
print('10 ^ 7 en binario: {0:b}'.format(10 ^ 7), end='\n\n')

print('13 en binario: {0:b}'.format(13))
print('~13 en binario: {0:b}'.format(~13), end='\n\n')

print('10 en binario: {0:b}'.format(10))
print('10 >> 1 en binario: {0:b}'.format(10 >> 1), end='\n\n')

print('10 en binario: {0:b}'.format(10))
print('10 << 1 en binario: {0:b}'.format(10 << 1), end='\n\n')


I am working on the file "How_to_Python/01_How_to_Python_Variables.ipynb".
The first task is:
Podemos revisar si un elemento existe dentro de una coleccion:
Can you write Python code for it?


I am working on the file "Human Body Temperature Using EDA/sliderule_dsi_inferential_statistics_exercise_1.ipynb".
The first task is:
1) Is the distribution of body temperatures normal?

Although this is not a requirement for CLT to hold (read CLT carefully), it gives us some peace of mind that the population may also be normally distributed if we assume that this sample is representative of the population.

We first start by viewing the histogram of the human body temperatures.
Can you write Python code for it?

# Plots the histogram of temperatures
import matplotlib.pyplot as plt
import seaborn as sns

temperature = df['temperature']

sns.set()
plt.hist(temperature, bins='auto', normed=True)
plt.xlabel('Temperature(F)')
plt.ylabel('Count')
plt.title('Human Body Temperature')
plt.show()


I am working on the file "Human Body Temperature Using EDA/sliderule_dsi_inferential_statistics_exercise_1.ipynb".
The first task is:
2) Is the sample size large? Are the observations independent?

Remember that this is a condition for the CLT, and hence the statistical tests we are using, to apply.

The rule of thumb for the Central Limit Theorem is that a sample size of 30 or more is considered a large sample size. The sample size is large since we have a sample size of 130. The observations are also independent.
3) Is the true population mean really 98.6 degrees F?

Would you use a one-sample or two-sample test? Why?
In this situation, is it appropriate to use the $t$ or $z$ statistic?
Now try using the other test. How is the result be different? Why?

A one-sample test should be used here because we only have one set of data available that we will compare to single mean. It is appropriate to use the $z$ statistic in this case because the sample size is 30 or greater. The $t$ statistic should be used if the sample size is less than 30.
Can you write Python code for it?


I am working on the file "Human Body Temperature Using EDA/sliderule_dsi_inferential_statistics_exercise_1.ipynb".
The first task is:
If we define an "abnormal" temperature to be outside the 95% confidence interval, this would include temperatures greater than 99.478 and less than 96.723.
6) Is there a significant difference between males and females in normal temperature?

What test did you use and why?
Write a story with your conclusion in the context of the original problem.

A two-sample permutation test with the differences in means will be appropriate for this problem. A permuatation test is appropriate for this because we will be testing whether males and females have the same distribution and similar mean temperatures. First we should visualize the data with exploratory data analysis.
Can you write Python code for it?

# Plots the ECDF for the temperatures of males and females

male_temperature = df[df['gender'] == 'M']['temperature']
female_temperature = df[df['gender'] == 'F']['temperature']

x_male, y_male = ecdf(male_temperature)
x_female, y_female = ecdf(female_temperature)

plt.plot(x_male, y_male, marker='.', linestyle='none', color='red')
plt.plot(x_male, y_male, marker='.', linestyle='none', color='blue')
plt.xlabel('Temperature(F)')
plt.ylabel('ECDF')
plt.legend(('Male', 'Female'), loc='lower right')
plt.title('Male vs Female: Human Body Temperature')
plt.show()

male_and_female_diff = np.abs(np.mean(male_temperature) - np.mean(female_temperature))
print('The difference between the male and female mean temperatures is', male_and_female_diff)


I am working on the file "Human Body Temperature Using EDA/sliderule_dsi_inferential_statistics_exercise_1.ipynb".
The first task is:
The average temperatures of all humans is estimated with 95% confidence to be 98.25 +/- 0.126 or between 98.124 and 98.376 in degrees Fahrenheit. If we define an "abnormal" temperature to be outside of the range of the mean, this would include all temperatures greater than 98.376 and less than 98.124.
Can you write Python code for it?

# Calculates 95% confidence interval
confidence_interval = np.percentile(temperature, [2.5, 97.5])

print('We expect 95% of the temperature data to be between', confidence_interval[0], 'and', confidence_interval[1])


I am working on the file "Human Body Temperature Using EDA/sliderule_dsi_inferential_statistics_exercise_1.ipynb".
The first task is:
The p value from the $t$ test is different but it still implies that the null hypothesis is false.
4) Draw a small sample of size 10 from the data and repeat both tests.

Which one is the correct one to use?
What do you notice? What does this tell you about the difference in application of the $t$ and $z$ statistic?

Since we will be drawing a random sample of size 10, the $t$ statistic will not be more appropriate to use.
Can you write Python code for it?

# Draws random sample of 10
sample = np.random.choice(temperature, size=10)

sample

# Performs t test
t2 = (np.mean(sample) - 98.6) / (np.std(sample) / np.sqrt(len(sample)))

print('t =', t2)

p_t2 = stats.t.sf(np.abs(t), len(sample)-1)*2
print('p = ', p_t2)

# Performs z test
z2 = (np.mean(sample) - 98.6) / (np.std(sample) / np.sqrt(len(sample)))

print('z =', z2)

p_z2 = stats.norm.sf(abs(z))*2
print('p =', p_z2)


I am working on the file "Human Body Temperature Using EDA/sliderule_dsi_inferential_statistics_exercise_1.ipynb".
The first task is:
The p value is extremely small after 100,000 replicates. This implies that the true mean is different from 98.6 degrees F
We can repeate the hypothesis test by also calculating the z-score to verify our results above.
$z_score$ = $(sample_mean - population_mean)$ / $population_standard_deviation$
Since we do not know the population's standard deviation we can approximate it to be:
$population_standard_deviation$ $\approx$ $sample_standard_deviation$ / $sample_size^{0.5}$
Can you write Python code for it?

# Calculates z and p values and performs z test
z = (np.mean(temperature) - 98.6) / (np.std(temperature) / np.sqrt(len(temperature)))
print('z =', z)

p_z = stats.norm.sf(abs(z))*2
print('p = p(z >= 5.476) + p(z <= -5.476) =', p_z)


I am working on the file "Human Body Temperature Using EDA/sliderule_dsi_inferential_statistics_exercise_1.ipynb".
The first task is:
The p value is extremely small which confirms that the true mean is likely different from 98.6. We will compare the results with the t statistic. The $t$ and $z$ values should be approximately the same.
Can you write Python code for it?

# Performs t test
t = z
print('t =', t)

p_t = stats.t.sf(np.abs(t), len(temperature)-1)*2
print('p = p(t >= 5.476) + p(t <= -5.476) =', p_t)


I am working on the file "Human Body Temperature Using EDA/sliderule_dsi_inferential_statistics_exercise_1.ipynb".
The first task is:
The p values for the t and z tests are significantly different. This shows that if you apply the wrong test to a problem you can end up with an incorrect result. It is important to know when it is appropriate to apply the $z$ statistic and the $t$ statistic. When the sample size is less than 30, the $t$ statistic should be used.
5) At what temperature should we consider someone's temperature to be "abnormal"?

Start by computing the margin of error and confidence interval.
Can you write Python code for it?

# Calculates margin of error for sample mean with 95% confidence

print('The mean temperature of the data is', np.mean(temperature))

z = 1.96 # this is the value of z for 95% confidence

error = z * np.std(temperature) / np.sqrt(len(temperature))

print('margin of error for a sample mean =', error)


I am working on the file "Human Body Temperature Using EDA/sliderule_dsi_inferential_statistics_exercise_1.ipynb".
The first task is:
We can see that the male and female ECDF graphs overlap which tells us that there is a small difference between the two data sets to begin with (0.289). We can now continue with hypothesis testing to see if this difference is due to the differenes in gender or by chance.
$H_0$: There is no difference in the distribution and means of males and females.
$H_A$: There is a difference in the distribution and means of males and females.
Can you write Python code for it?

permutation_replicates = np.empty(100000)

size = len(permutation_replicates)

for i in range(size):
    combined_perm_temperatures = np.random.permutation(np.concatenate((male_temperature, female_temperature)))

    male_permutation = combined_perm_temperatures[:len(male_temperature)]
    female_permutation = combined_perm_temperatures[len(male_temperature):]

    permutation_replicates[i] = np.abs(np.mean(male_permutation) - np.mean(female_permutation))

p_val = np.sum(permutation_replicates >= male_and_female_diff) / len(permutation_replicates)

print('p =', p_val)


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
1.3 Lecture des données métier
Les données peuvent être préalablement téléchargées ou directement lues. Ce sont celles originales du dépôt de l'UCI.
Can you write Python code for it?

# Lecture des données d'apprentissage
# Attention, il peut y avoir plusieurs espaces comme séparateur dans le fichier
Xtrain = pd.read_table("X_train.txt", sep = '\s+', header = None)
Xtrain.head()

# Variable cible
ytrain = pd.read_table("y_train.txt", sep = '\s+', header = None, names = list('y'))
# Le type dataFrame est inutile et même gênant pour les la suite
ytrain = ytrain["y"]

# Lecture des données de test
Xtest = pd.read_table("X_test.txt", sep = '\s+', header = None)
Xtest.shape

ytest = pd.read_table("y_test.txt", sep = '\s+', header = None, names = list('y'))
ytest = ytest["y"]
# Significaiton des codes de y
label_dic = {1 : "Marcher", 2 : "Monter escalier", 3 : "Descendre escalier",
   4 : "Assis", 5 : "Debout", 6 : "Couche"}
labels = label_dic.values()


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
2 Exploration par Analyse en composantes principales
2.1 Principe
Il est important de se faire une idée précise de la structure des données.  Une analyse en composantes principales est adaptée à cet objectif.

Elle recherche les axes de plus grande dispersion du nuages des individus dans $R^p$ avec $p=561$. Ces axes sont définis par les vecteurs propres de la matrice des covariances ou des corrélations si les variables sont réduites (divisées par l'écart-type).
Les représentations graphiques des individus sont obtenues par projection sur les sous-espaces engendrés par les premiers vecteurs propres. Elles préservent au mieux les distances entre ceux-ci.
Les coordonnées sont stockées dans la matrice des composantes principales qui sont aussi les combinaisons linéaires de plus grande variance des variables. Ce sont des variables décorrélées, orthogonales deux à deux.
Les representations graphiques des variables initiales conservent au mieux les angles entre les vecteurs variables dans l'espace $R^n$ de façon à interpréter leurs corrélations qui, géométriquement, sont les cosinus de ces angles.
Can you write Python code for it?

from sklearn.decomposition import PCA
from sklearn.preprocessing import scale


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
2.2 Valeurs propres ou variances des composantes principales
Représentation de la décroissance des valeurs propres, les variances des variables ou composantes principales.
Can you write Python code for it?


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
3 Exploration par Analyse Factorielle Discriminante (AFD)
3.1 Principe
L'ACP ne prend pas en compte la présence de la variable qualitative à modéliser, contrairement à l'analyse factorielle discriminante (AFD). L'AFD est adaptée à ce contexte "supervisé" puisque l'activité est connue sur un échantillon d'apprentissage. L'AFD est une ACP des barycentres des classes munissant l'espace des indivudus d'une métrique spécifique dite de Mahalanobis. Métrique définie par l'inverse de la matrice de covariance intraclasse. L'objectif est alors de visualiser les capacités des variables à discriminer les classes.
La librairie scikit-learn ne propose pas de fonction spécifique d'analyse factorielle discriminante mais les coordonnées des individus dans la base des vecteurs discriminants sont obtenues comme résultats de l'analyse discriminante linéaire décisionnnelle. Cette dernière sera utilisé avec une finalité prédictive dans un deuxième temps (voir section 4 de ce calepin).
Les résultats de la fonction LinearDiscriminantAnalysis de scikit-learn sont identiques à ceux de la fonction lda de R. Elle est donc utilisée strictement de la même façon.
Can you write Python code for it?

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
method = LinearDiscriminantAnalysis()
lda = method.fit(Xtrain, ytrain)
X_r2 = lda.transform(Xtrain)


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
5.2. Prévision de l'activité pour l'échantillon test
Une fois le modèle estimé, l'erreur de prévision est évaluée, sans biais optimiste, sur un autre échantillon, dit échantillon test, qui n'a pas participé à l'apprentissage du modèle.
Can you write Python code for it?

from sklearn.metrics import confusion_matrix
print("Score : %f, time running : %d secondes" %(scoreLR, te-ts))
pd.DataFrame(confusion_matrix(ytest, ypredLR), index = labels, columns=labels)


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
Calcul de la matrice des composantes principales. C'est aussi un changement (transformation) de base; de la base canonique dans la base des vecteurs propres.
Can you write Python code for it?

pca = PCA()
X_r = pca.fit_transform(Xtrain)


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
Graphe illisible en mettant les libellés en clair. Seule une * est représentée.
Can you write Python code for it?

# coordonnées des variables
coord1 = pca.components_[0] * np.sqrt(pca.explained_variance_[0])
coord2 = pca.components_[1] * np.sqrt(pca.explained_variance_[1])
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1, 1, 1)
for i, j in zip(coord1, coord2, ):
    plt.text(i, j, "*")
    plt.arrow(0, 0, i, j, color = 'r')
plt.axis((-1.2, 1.2, -1.2, 1.2))
# cercle
c = plt.Circle((0,0), radius = 1, color = 'b', fill = False)
ax.add_patch(c)
plt.show()


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
La fonction définie ci-après affiche un nuage de points dans un plan factoriel.
Can you write Python code for it?

def plot_pca(X_R, fig, ax, nbc, nbc2):
    for i in range(6):
        xs = X_R[ytrain == i + 1, nbc - 1]
        ys = X_R[ytrain == i + 1, nbc2 - 1]
        label = label_dic[i + 1]
        color = cmaps(i)
        ax.scatter(xs, ys, color = color, alpha = .8, s = 1, label = label)
        ax.set_xlabel("PC%d : %.2f %%" %(nbc, pca.explained_variance_ratio_[nbc - 1] * 100), fontsize = 10)
        ax.set_ylabel("PC%d : %.2f %%" %(nbc2, pca.explained_variance_ratio_[nbc2 - 1] * 100), fontsize = 10)


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
Q Que dire de la séparation des classes. Sont-elles toutes séparables deux à deux ?
Q Que dire de la forme des nuages notamment dans le premier plan ? Critiquer l'hypothèse utilisée dans l'AFD.
Comme pour l'ACP, la représentation des variables n'apporterait rien.
4 Apprentissage et prévision de l'activité par Analyse Factorielle Discriminante (AFD)
D'autres méthodes sont successivement testées dans les calepins complétant l'étude: SVM, $k$ plus proches voisins, forêts aléatoires, réseaux de neurones... Seule l'analyse discriminante décisionnelle et la régression logistique sont utilisées dans ce calepin pour illustrer la phase d'apprentissage / modélisation pour la prévision du comportement.
On rappelle que l'analyse factorielle discriminante a deux facettes : exploratoire et prédictive. L'analyse exploratoire réalise une ACP des centres des classes avec la métrique de Mahalanobis (qui a pour effet de rendre les données 'sphériques'), cf section 3. La prédiction est obtenue en utilisant le centre le plus proche au sens de cette métrique, en tenant compte des fréquences de chaque classe. De façon équivalente, c'est la prévision bayésienne obtenue lorsque le prior est un mélange gaussien homoscédastique.
4.1. Estimation du modèle
Can you write Python code for it?

method = LinearDiscriminantAnalysis()
ts = time.time()
method.fit(Xtrain, ytrain)
scoreLDA = method.score(Xtest, ytest)
ypredLDA = method.predict(Xtest)
te = time.time()


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
Q Quelles sont les classes qui restent difficiles à discriminer ?
Q Commenter la qualité des résultats obtenus. Sont-ils cohérents avec l'approche exploratoire ?
5 Apprentissage et prévision de l'activité par Régression logistique
Une méthode ancienne mais finalement efficace sur ces données. La régression logistique est adaptée à la prévision d'une variable binaire. Dans le cas multiclasse, la fonction logistique de la librairie Scikit-learn estime par défaut un modèle par classe: une classe contre les autres.
La probabilité d'appartenance d'un individu à une classe est modélisée à l'aide d'une combinaison linéaire des variables explicatives. Pour transformer une combinaison linéaire à valeur dans $R$ en une probabilité à valeurs dans l'intervalle $[0, 1]$, une fonction de forme sigmoïdale, inverse de la fonction logit, est appliquée.  Ceci donne: $P(y_i=1)=\frac{e^{Xb}}{1+e^{Xb}}$ ou encore, $\log\frac{P(y_i=1)}{1-P(y_i=1)}=Xb$
5.1  Estimation du modèle sans optimisation
Le modèle est estimé sans chercher à raffiner les valeurs de certains paramètres (pénalisation). Ce sera fait dans un deuxième temps.
Can you write Python code for it?

from sklearn.linear_model import LogisticRegression
ts = time.time()
method = LogisticRegression()
method.fit(Xtrain, ytrain)
scoreLR = method.score(Xtest, ytest)
ypredLR = method.predict(Xtest)
te = time.time()


I am working on the file "HumanActivityRecognition/Explo-Python-HAR-metier.ipynb".
The first task is:
Un graphique plus explicite décrit les distribution de ces composantes par des diagrames boîtes; seules les premières sont affichées.
Can you write Python code for it?


I am working on the file "Human_Temp.ipynb".
The first task is:
We see the sample size is n= 130 and as a general rule of thumb inorder for CLT to be validated
it is necessary for n>30. Hence the sample size is compartively large.
Question 3
HO: The true population mean is 98.6 degrees F (Null hypothesis)
H1: The true population mean is not 98.6 degrees F (Alternative hypothesis)
Alternatively we can state that,
HO: μ1 = μ2
H1: μ1 ≠ μ2
Can you write Python code for it?

from statsmodels.stats.weightstats import ztest
from scipy.stats import ttest_ind
from scipy.stats import ttest_1samp
t_score=ttest_1samp(t,98.6)
t_score_abs=abs(t_score[0])
t_score_p_abs=abs(t_score[1])
z_score=ztest(t,value=98.6)
z_score_abs=abs(z_score[0])
p_value_abs=abs(z_score[1])
print("The z score is given by: %F and the p-value is given by %6.9F"%(z_score_abs,p_value_abs))
print("The t score is given by: %F and the p-value is given by %6.9F"%(t_score_abs,t_score_p_abs))


I am working on the file "Huntsman Telephoto Array specifications.ipynb".
The first task is:
A given exposure time corresponds to an angle of rotation about the telescope mount hour angle axis.
Can you write Python code for it?

ha_angles = (exposure_times.to(u.hour) * (u.hourangle / u.hour)).to(u.degree)
ha_angles


I am working on the file "Huntsman Telephoto Array specifications.ipynb".
The first task is:
All imaging units shall have the camera y axis aligned with the North-South axis to within a tolerance of $\pm$2.5 degrees (TBC)

Image quality
Abraham & van Dokkum (2014) report that imaging units of the design proposed for the Huntsman Telephoto Array are capable of producing a point spread function (PSF) with full width at half maximum (FWHM) of $\sim1.5''$, as measured by (undersampled) 3rd order polynomial fitting by SExtractor.  When image sensor tilts (PSF degradation $<0.4''$) and imperfect telescope tracking are taken into account average FWHM of $< 2''$ were still achieved across the entire field of view.  The Huntsman Telephoto Array should at least match this.
Can you write Python code for it?

central_fwhm = 1.5 * u.arcsecond
tilt_fwhm_degradation = 0.4 * u.arcsecond
max_fwhm = 2 * u.arcsecond
max_fwhm


I am working on the file "Huntsman Telephoto Array specifications.ipynb".
The first task is:
All imaging units should point in the same direction to within a tolerance of 5 arcminutes radius on sky (TBC)

All data will be resampled prior to combination so some relative rotation between imaging units is acceptable.
Can you write Python code for it?

north_alignment_tolerance = 2.5 * u.degree
north_alignment_tolerance


I am working on the file "Huntsman Telephoto Array specifications.ipynb".
The first task is:
Each imaging unit shall deliver an instantaneous field of view of $2.6 \pm 0.1 \times 1.9 \pm 0.1$ degrees

Exposure time
Individual exposure times of 5-30 minutes are anticipated (5-10 minutes for broadband observations, 30 minutes for narrowband).
Can you write Python code for it?

exposure_times = ((5, 10, 30) * u.minute)
exposure_times


I am working on the file "Huntsman Telephoto Array specifications.ipynb".
The first task is:
Each imaging unit shall deliver an on-sky spatial sampling of $2.8\pm 0.1'' /$ pixel

Field of view
Derived from the specifications of the chosen hardware.
Can you write Python code for it?

fov = resolution * sampling
fov.to(u.degree)


I am working on the file "Huntsman Telephoto Array specifications.ipynb".
The first task is:
Mass
The telescope mount is rated for a maximum payload (not including counterweights) of 109 kg, therefore the total mass of imaging units plus support structure(s) should not exceed this value.  The mass of the lens is 4.1 kg (source here), the mass of the CCD camera is 0.8 kg (source here) and the mass of the adaptor is estimated to be no more than 0.2 kg.
Can you write Python code for it?

lens_mass = 4.1 * u.kilogram
camera_mass = 0.8 * u.kilogram
adaptor_mass = 0.2 * u.kilogram
imaging_unit_mass = lens_mass + camera_mass + adaptor_mass

max_payload_mass = 109 * u.kilogram

max_struture_mass = max_payload_mass - max(n_units) * imaging_unit_mass
max_struture_mass


I am working on the file "Huntsman Telephoto Array specifications.ipynb".
The first task is:
The system shall meet all requirements with exposure times of up to 30 minutes

Number of imaging units
The maximum number of imaging units per telescope mount is really determined by the mount payload mass limit and the aperture size of the enclosure.  The Dragonfly Telephoto Array are currently operating with 10 imaging units on a single mount, the Huntsman Telephoto Array should be capable of at least matching this.
Can you write Python code for it?


I am working on the file "Huntsman Telephoto Array specifications.ipynb".
The first task is:
The system shall satisfy all functional requirements (e.g. image quality, alignment) while observing any sky position with a zenith distance less than 60 degrees. The system is not required to meet functional requirements if observing a sky position with a zenith distance of greater than 60 degrees

Mechanical requirements
Support structure(s)

The mechanical support structure(s) shall allow the number of imaging units specified in the science requirements to be attached to the telescope mount
Can you write Python code for it?


I am working on the file "Huynh2007/Fig2.1a.ipynb".
The first task is:
The Lagrange basis using the Gauss-Lobatto-Legendre quadrature points.
Can you write Python code for it?


I am working on the file "Huynh2007/Fig2.1a.ipynb".
The first task is:
This is used to plotting the basis functions.
Can you write Python code for it?

x = numpy.linspace(-1., 1., 100)
phi = Lk(x)

pyplot.figure(figsize=(6, 3))
pyplot.plot(x, phi[:, 0], 'r-', lw=3, label="1st basis")
pyplot.plot(x, phi[:, 1], 'm-', lw=3, label="2nd basis")
pyplot.plot(x, phi[:, 2], 'c-', lw=3, label="3rd basis")
pyplot.plot(x, phi[:, 3], 'g-', lw=3, label="4th basis")
pyplot.plot(xi, numpy.zeros(4), 'k.--', lw=2,
            markersize=15, markerfacecolor='r', markeredgecolor='r')
pyplot.plot(xi, numpy.ones(4), 'k.--', lw=2,
            markersize=15, markerfacecolor='r', markeredgecolor='r')
pyplot.ylim(-0.2, 1.05)
pyplot.title(r'$\eta(\xi)$', fontsize=25)
pyplot.xlabel(r'$\xi$', fontsize=15)
pyplot.grid()
pyplot.legend(loc=0, bbox_to_anchor=(1.02, 1.04));


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
A-3. Exact solution
$$
u_{exact} = e^{\left[-40(x-0.5-t)^2\right]}
$$
Can you write Python code for it?

def u_exact(x, t):
    """initial condition of u"""
    temp = x - 0.5 - t
    return numpy.exp(-40 * temp * temp)


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
A-4. Flux function
The PDE is already in conservative form.
And thus the flux of the conservation law in this problem is
$$
f(x) = f(u(x)) = u(x)
$$
Can you write Python code for it?

def flux(u):
    """flux in PDE"""
    return u


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
A-5. Wave propagation speed, a
$$
a(x) = a(u) = \frac{df}{du} = 1
$$
B. Distribution of solution points on single element
B-1. Equally distribution
Solution points are equally distributed on a single element but exclude the two end nodes.
Can you write Python code for it?

def equalDistrib(N):
    """"""
    return numpy.linspace(-1., 1., N, endpoint=False) + 1. / N


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
Again, let's define a dictionary to collect these correction function.
Can you write Python code for it?

dCorrectionFunc = {
    "DG": dgDG,
    "SG": dgSG,
    "LumpLo": dgLumpLo,
    "LumpChLo": dgLumpChLo,
    "Ga": dgGa
}


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
B-2. Legendre-Lobatto
Solution points are the quadrature nodes of Gauss-Lobatto-Legendre quadrature.
Can you write Python code for it?

def LegendreLobatto(N):
    """"""
    return quad.GaussLobattoJacobi(N).nodes


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
B-3. Gauss (or Legendre-Gauss)
Solution points are the quadrature nodes of Gauss-Legendre quadrature.
Can you write Python code for it?

def LegendreGauss(N):
    """"""
    return quad.GaussJacobi(N).nodes


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
B-4. Chebyshev-Lobatto
Solution points are the quadrature nodes of Gauss-Lobatto-Chebyshev quadrature.
Can you write Python code for it?

def ChebyshevLobatto(N):
    """"""
    return - numpy.cos(numpy.arange(N, dtype=numpy.float64) * numpy.pi / (N-1))


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
C-4. Lumping for Chebyshev-Lobatto points, $g_{Lump,Ch-Lo}$
$g_{Lump,Ch-Lo}$ is defined by the same definitions of $g_{Lump,Lo}$, except now the points defining the function (i.e. $\xi_i$) are Chebyshev-Lobatto points. Therefore, we can get $g_{Lump,Ch-Lo}$ through the same workflow of $g_{Lump,Lo}$.
Can you write Python code for it?

def dgLumpChLo(xi, side):
    """flux correction scheme: lumpped Chebyshev-Lobatto, aka. g2

    This function returns the derivatives of g_{Lump,Ch-Lo} at solution points

    Args:
        xi: numpy.ndarray, local coordinates of solution points
        side: string, either 'left' or 'right', indicating which bouyndary of
              element will be corrected
    """

    # number of solution points, also the order of correction polynomial
    N = xi.size
    nodes = ChebyshevLobatto(N)

    # dg/dx
    if side == 'left':
        dg = poly.Polynomial(roots=nodes[1:])

        g = dg.integral()  # note: this is indefinite integral!
        # correct the scaling, so that final g(-1) = 1
        leading = 1. / (g(-1) - g(1))
    elif side == 'right':
        dg = poly.Polynomial(roots=nodes[:-1])

        g = dg.integral()  # note: this is indefinite integral!
        # correct the scaling, so that final g(1) = 1
        leading = 1. / (g(1) - g(-1))
    else:
        raise ValueError("illegal value for argument 'side'")

    return dg(xi) * leading


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
C-5. Staggered-grid scheme with Gauss points, $g_{Ga}$
The definition of $g_{Ga}$ follows the same concept of $g_{SG}$, except that now the points defining Lagrange basis polynomial are $N-1$ Gauss points plus $\xi=-1$ and $\xi=1$ at the ends. That is,
$$
\left{
\begin{align}
&\xi_0 = -1 \
&\xi_N = 1 \
&\xi_i = the\ i_{th}\ root\ of\ L_{N-1}(\xi),\ for\ i=1,\dots,N-1
\end{align}
\right.
$$
The remaining part of the definition follows that of $g_{SG}$.
Can you write Python code for it?

def dgGa(xi, side):
    """flux correction scheme: stagger grid spectral difference, but with Gauss
    quadrature points as interior zeros

    This function returns the derivatives of g_{Ga} at solution points

    Args:
        xi: numpy.ndarray, local coordinates of solution points
        side: string, either 'left' or 'right', indicating which bouyndary of
              element will be corrected
    """

    # number of solution points, also the order of correction polynomial
    N = xi.size
    nodes = numpy.pad(quad.GaussJacobi(N-1).nodes,
                      (1, 1), 'constant', constant_values=(-1., 1.))

    # dg/dx
    if side == 'left':
        g = poly.Polynomial(roots=nodes[1:])
        g /= g(nodes[0])
    elif side == 'right':
        g = poly.Polynomial(roots=nodes[:-1])
        g /= g(nodes[-1])
    else:
        raise ValueError("illegal value for argument 'side'")

    return g.derive()(xi)


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
C. Flux correction function ($g_{LB}$ and $g_{RB}$)
Though what we'll define mathematically are flux correction functions (i.e. $g_{LB}$ and $g_{RB}$),
we only need the derivatives of these functions at solution points in numerical solvers, that is, $\frac{dg_{LB}}{d\xi}(\xi_k)$ and $\frac{dg_{RB}}{d\xi}(\xi_k)$, where $\xi_k,\ k=1,\dots,N$ are solution points on each element.
So the Python function defined below only return a numpy.ndarray of these derivatives.
$N$ in this section always represents the number of solution points on each element.
C-1. Discontinuous Galerkin, $g_{DG}$ (a.k.a. $g_1$)
$g_{DG}$ is defined as Radau polynomial (p.s. the roots of Radau polynomials are the quadrature nodes of Gauss-Radau-Legendre quadrature).
$$
\left{
\begin{align}
g_{DG,LB}(\xi) & = R_{R,N}(\xi) = (-1)^N\frac{1}{2}(L_N(\xi) - L_{N-1}(\xi)) \
g_{DG,RB}(\xi) & = R_{L,N}(\xi) = \frac{1}{2}(L_N(\xi) + L_{N-1}(\xi)) \
\end{align}
\right.
$$
where the subscripts $LB$ and $RB$ denote whether the functions apply to left or right interface of each element; $R_{R,N}$ and $R_{L,N}$ are right- and left-Radau polynomial of order $N$; $L_N$ represents Legendre polynomial of order $N$.
Can you write Python code for it?

def dgDG(xi, side):
    """flux correction scheme: discontinuous Galerkin, aka. g1

    This function returns the derivatives of g_{DG} at solution points

    Args:
        xi: numpy.ndarray, local coordinates of solution points
        side: string, either 'left' or 'right', indicating which bouyndary of
              element will be corrected
    """

    # number of solution points, also the order of correction polynomial
    N = xi.size

    # dg/dx
    if side == 'left':
        dg = poly.Radau(N, end=1).derive()
    elif side == 'right':
        dg = poly.Radau(N, end=-1).derive()
    else:
        raise ValueError("illegal value for argument 'side'")
    return dg(xi)


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
D-2. Flux reconstruction
Can you write Python code for it?

def FR1D(u, basis, dMatrix, intID, dgL, dgR, invJ):
    """flux reconstruction

    Args:
        u: Ne by K numpy ndarray (Ne: number of elements; K: number of solution
            points per element); primary variable in PDE
        basis: Lagrange basis polynomial based on solution points on each
            element; so far, given that the domain is discretized by the same
            type of standard element, so only one set of Lagrange basis is
            required
        dMatrix: derivative matrix associated to the given Lagrnage basis
        intID: Ne+1 by 2 numpy integer ndarray; array indicating the id of left
            and right elements at each interface
        dgL, dgR: numpy ndarray with length K; correction values of locally
            derived flux due to left and right interfaces; given that the
            distributions and orders of solution points on all elements are the
            same, all elements share the same dgL and dgR (because they only
            depend on local coordinate)
        invJ: float; inversed Jacobian; due to the size of elements in this
            problem are the same, all elements share single invJ
    """

    # obtain number of elements and number of solution points per element
    Ne, K = u.shape

    # upwind flux at each interface
    # note: in this problem, a = df/du = 1, so upwind values always come from
    # the left elements of interfaces
    fupw = numpy.array(
        [numpy.dot(basis(1), u[intID[i, 0]]) for i in range(Ne+1)])

    # dF/dxi, local derived and corrected flux
    RHS = numpy.zeros_like(u)
    for i in range(Ne):
        f = u[i]  # for this problem, flux is u itself
        df = numpy.dot(dMatrix, f)
        fL = numpy.dot(basis(-1), f)
        fR = numpy.dot(basis(1), f)
        RHS[i] = df + (fupw[i] - fL) * dgL + (fupw[i+1] - fR) * dgR

    # dF/dx = invJ * (dF/dxi)
    RHS *= invJ

    # in explicit time-marching schemes, RHS = - (dF/dx)
    return -RHS

def solve(DistribType, CorrectionType, dt, Nt):
...


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
D-3. Fromm's scheme
Can you write Python code for it?

def FrommScheme(u, dx):
    """"""

    Np = u.size
    flux = numpy.zeros_like(u)

    # the most left point
    flux[0] = - 0.25 * (u[-2] - 5 * u[-1] + 3 * u[0] + u[1]) / dx

    # interior points
    for i in range(1, Np-1):
        flux[i] = - 0.25 * (u[i-2] - 5 * u[i-1] + 3 * u[i] + u[i+1]) / dx

    # the most right point
    flux[-1] = - 0.25 * (u[-3] - 5 * u[-2] + 3 * u[-1] + u[0]) / dx

    return flux

def solveFromm(dt, Nt):
    """a wrapper for solving advection equation with Fromm's scheme"""

    xBC_L = 0.
    xBC_R = 1.
    L = 1.
    Np = 40

    dx = L / Np

    x = numpy.linspace(xBC_L, xBC_R, Np, endpoint=False) + 0.5 * dx
    u = u_init(x)

    # define RHS function using local variables
    rhs = functools.partial(FrommScheme, dx=dx)

    # time marching
    t = 0
    for i in range(Nt):
        u = RK4(u, dt, rhs)
        t += dt

    return x, u, t


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
E. Fig 9.1
Can you write Python code for it?

fig, ax = pyplot.subplots(2, 3, True, True)
fig.suptitle("Equally spaced solution points, t=10", fontsize=25)
fig.set_figheight(8)
fig.set_figwidth(18)
pyplot.close()

# (a) piecewise-linear upwind (Van Lee's MUSCL)
Nt = 533; dt = 10. / Nt
x, u, t = solveFromm(dt, Nt)
plotResults(x, u, r"$Fromm's scheme$", ax[0, 0])

# (b) DG
Nt = 764; dt = 10. / Nt
x, u, t = solve("equal", "DG", dt, Nt)
plotResults(x, u, r"$DG$", ax[0, 1])

# (c) staggered-grid
Nt = 432; dt = 10. / Nt
x, u, t = solve("equal", "SG", dt, Nt)
plotResults(x, u, r"$Staggered-grid$", ax[0, 2])

# (d) Lump, Lo
Nt = 385; dt = 10. / Nt
x, u, t = solve("equal", "LumpLo", dt, Nt)
plotResults(x, u, r"$g_{Lump,Lo}$", ax[1, 0])

# (e) Ga
Nt = 490; dt = 10. / Nt
x, u, t = solve("equal", "Ga", dt, Nt)
plotResults(x, u, r"$g_{Ga}$", ax[1, 1])

# (f) Lump, Ch-Lo
Nt = 562; dt = 10. / Nt
x, u, t = solve("equal", "LumpChLo", dt, Nt)
plotResults(x, u, r"$g_{Lump,Ch-Lo}$", ax[1, 2])

fig


I am working on the file "Huynh2007/Fig9.1-9.2.ipynb".
The first task is:
F. Fig 9.2
Can you write Python code for it?

fig, ax = pyplot.subplots(2, 3, True, True)
fig.suptitle("Gauss solution points, t=50", fontsize=25)
fig.set_figheight(8)
fig.set_figwidth(18)
pyplot.close()

# (a) piecewise-linear upwind (Van Lee's MUSCL)
Nt = 2667; dt = 50. / Nt
x, u, t = solveFromm(dt, Nt)
plotResults(x, u, r"$Fromm's scheme$", ax[0, 0])

# (b) DG
Nt = 764 * 5; dt = 50. / Nt
x, u, t = solve("LegendreGauss", "DG", dt, Nt)
plotResults(x, u, r"$DG$", ax[0, 1])

# (c) staggered-grid
Nt = 432 * 5; dt = 50. / Nt
x, u, t = solve("LegendreGauss", "SG", dt, Nt)
plotResults(x, u, r"$Staggered-grid$", ax[0, 2])

# (d) Lump, Lo
Nt = 385 * 5; dt = 50. / Nt
x, u, t = solve("LegendreGauss", "LumpLo", dt, Nt)
plotResults(x, u, r"$g_{Lump,Lo}$", ax[1, 0])

# (e) Ga
Nt = 490 * 5; dt = 50. / Nt
x, u, t = solve("LegendreGauss", "Ga", dt, Nt)
plotResults(x, u, r"$g_{Ga}$", ax[1, 1])

# (f) Lump, Ch-Lo
Nt = 562 * 5; dt = 50. / Nt
x, u, t = solve("LegendreGauss", "LumpChLo", dt, Nt)
plotResults(x, u, r"$g_{Lump,Ch-Lo}$", ax[1, 2])

fig


I am working on the file "Huynh2007/check_fL_not_eq_f(uL).ipynb".
The first task is:
Define a function to calculate flux at a location given a known $u$.
Can you write Python code for it?

def f(ui):
    '''flux at spificic location using known velocity at the same location'''
    return 0.5 * ui * ui


I am working on the file "Huynh2007/check_fL_not_eq_f(uL).ipynb".
The first task is:
The coordinates of solution points using Gauss-Legendre quadrature points.
Can you write Python code for it?


I am working on the file "Huynh2007/check_fL_not_eq_f(uL).ipynb".
The first task is:
The exact solution of $u(x)$.
Can you write Python code for it?

def u_exact(x):
    '''exact solution of u'''
    return numpy.sin(x)


I am working on the file "Huynh2007/check_fL_not_eq_f(uL).ipynb".
The first task is:
Use $u$ at solution points and Lagrange interpolation to calculate $u$ at left boundary.
Can you write Python code for it?


I am working on the file "Huynh2007/check_fL_not_eq_f(uL).ipynb".
The first task is:
Use $u$ at the left boundary to calculate flux at that boundary (that is, the $f(u_L)$ in the paper).
Can you write Python code for it?


I am working on the file "Hy -level PLCParser.ipynb".
The first task is:
Adding more complex custom operators
Can you write Python code for it?

%%plc

; use operator macro to add mean operator with custom symbol
(defoperator mean x̄ [&rest args]
  (/ (sum args) (len args)))

; try prefix notation with nested structure
(print (x̄ 1 2 3 4))
(print (x̄ 1 2 (x̄ 3 4)))

; note that infix notation in cell magics needs to be prefixed with
; #$ reader macro marker while in line magics it is not required
(print #$(1 x̄ 2 x̄ 3 x̄ 4))


I am working on the file "Hy -level PLCParser.ipynb".
The first task is:
Line magics
Prefix and infix support
Can you write Python code for it?

%plc (1 and? 1)

%plc (True ⊕ False ⊕ True ⊕ False ⊕ True)

%plc ( ( ∧ 1 1 1 ) ∨ ( ∧ 1 1 0 ) )


I am working on the file "Hy -level PLCParser.ipynb".
The first task is:
Order of precedence
By default order of precedence is from left to right. Here we will use defoperators to define additional operators beyond logical ones. Then for variety we use defmixfix macro to evaluate clause. First evaluation will give 9 as an answer because evaluation is started from 1 + 2 and then that is multiplied
Can you write Python code for it?

%%plc

(defoperators * +)
(print "First"
  (defmixfix 1 + 2 * 3))

(defprecedence * +)
(print "Second"
  (defmixfix 1 + 2 * 3))


I am working on the file "Hy -level PLCParser.ipynb".
The first task is:
Registering additional operators
Can you write Python code for it?

%%plc
; register + sign for infix notation
#>+
; evaluate code
#$(1 + (2 + (3)))


I am working on the file "Hyperbolic Rig.ipynb".
The first task is:
$\lambda_1$, can be simplified to be
\begin{align}
    \lambda_1=\frac{c_1}{L_{v_1} \sqrt{k_1\cdot(k_1 - 2)}
    + k_1 \left(c_1 - z_w \right)}
\end{align}
where $L_{v_1} = ||{\vec{f_1} - \vec{p_w}}|| =|\sqrt{x_w^2+y_w^2+(c_1 - z_w)^2} |$, which is the distance between $P_w$
and mirror 1's focus point $F_1$.
Can you write Python code for it?

L1w = sqrt(xw**2 + yw**2 + (c1-zw)**2)
lambda1_simple = c1/(L1w*sqrt(k1*(k1-2)) + k1*(c1-zw))
display(lambda1_simple)
lambda1_simple_den = (abs(sqrt(k1*(k1-2)*xw**2 + yw**2 + (c1-zw)**2)) + k1*(c1-zw))
den1_simple = lambda1_simple_den*lambda1_simple_den


I am working on the file "Hyperbolic Rig.ipynb".
The first task is:
$\lambda_2$, can be simplified to be
\begin{align}
    \lambda_2=\frac{c_2}{L_{v_2} \sqrt{k_2\cdot(k_2 - 2)}
    - k_2 \left(d-c_2-z_w \right)}
\end{align}
where $L_{v_2} = ||{\vec{f_2} - \vec{p_w}}|| =|\sqrt{x_w^2+y_w^2+(d-c_2-z_w)^2} |$, which is the distance between $P_w$
and mirror 2's focus point $F_2$.
Can you write Python code for it?

display(expand(d - lambda2*zw - (d - c2)*(1 - lambda2)))
display(expand(c2 + lambda2*(d - c2 - zw)))


I am working on the file "Hyperbolic Rig.ipynb".
The first task is:
Let $\lambda_1 = 1 - s_1$, so that:
Can you write Python code for it?

s1_eqn = 1 - lambda1

xr1_on_line = simplify(x1_on_line.subs({s1: s1_eqn}))
display(xr1_on_line)

yr1_on_line = simplify(y1_on_line.subs({s1: s1_eqn}))
display(yr1_on_line)

zr1_on_line = expand(z1_on_line.subs({s1: s1_eqn}))
display(zr1_on_line)

mirror_1_intersection = mirror_1_eqn_top.subs({x1: xr1_on_line, y1: yr1_on_line, z1:zr1_on_line})
display(mirror_1_intersection)


I am working on the file "Hyperbolic Rig.ipynb".
The first task is:
Line passing through points $P_w$ and $F_1$
Line equations:
Can you write Python code for it?

lambda1, s1 = symbols("lambda_1, s_1", real=True)
pos_F1 = (0, 0, c1)

x1_on_line = xw + s1 * (pos_F1[0]-xw)
display(x1_on_line)

y1_on_line = yw + s1 * (pos_F1[1]-yw)
display(y1_on_line)

z1_on_line = zw + s1 * (pos_F1[2]-zw)
display(z1_on_line)


I am working on the file "Hyperbolic Rig.ipynb".
The first task is:
Simplifying the +  solution of $t_1$, we get:
\begin{align}
    t_1=\frac{c_1}{k_1-||{q_1}||\sqrt{k_1\cdot(k_1 - 2)}}
\end{align}
BP Mirror 2:
Can you write Python code for it?

q2, xq2, yq2, zq2, t2, theta2, phi2 = symbols("q_2, x_q2, y_q2, z_q2, t_2, theta_2, phi_2", real=True)
x2_bp_line_eqn = t2*xq2
y2_bp_line_eqn = t2*yq2
z2_bp_line_eqn = d-t2
mirror2_bp_intersection = mirror_2_eqn_bottom.subs({x2: x2_bp_line_eqn, y2: y2_bp_line_eqn, z2:z2_bp_line_eqn})
display(mirror2_bp_intersection)
t2_sln = solve([mirror2_bp_intersection], t2)
t2_sln_minus = simplify(t2_sln[0])
display(t2_sln_minus)
t2_sln_plus = together(t2_sln[1])
display(t2_sln_plus)


I am working on the file "Hyperbolic Rig.ipynb".
The first task is:
Solving for intersection point $P_2$
Can you write Python code for it?

lambda2_sln = solve([mirror_2_intersection], lambda2)
lambda2_sln_minus = simplify(lambda2_sln[0])
display(lambda2_sln_minus)
lambda2_sln_plus = simplify(lambda2_sln[1])
display(lambda2_sln_plus)
display(lambda1_sln_plus)

lambda2_func_minus = lambdify([c2, k2, xw, yw, zw, d], lambda2_sln_minus, modules=['numpy'])
lambda2_func_plus = lambdify([c2, k2, xw, yw, zw, d], lambda2_sln_plus, modules=['numpy'])


I am working on the file "Hyperparameter Optimization.ipynb".
The first task is:
Play time!
Can you write Python code for it?

extra_trees_search = partial(
    discriminative_smbo,
    discriminative_model_fn=extra_trees_fn,
    model_score_fn=maximum_uncertainty,
    generator_fn=random_generator,
)
gaussian_process_search = partial(
    discriminative_smbo,
    discriminative_model_fn=gaussian_process_fn,
    model_score_fn=expected_improvement_3,
    generator_fn=random_generator,
)

names_and_search_fns = [
    ("grid", grid_search),
    ("grid_shuffle", partial(grid_search, shuffle=True)),
    ("rand", random_search),
    ("hyperopt", hyperopt_search),
    ("xtrees", extra_trees_search),
    # ("gp", gaussian_process_search),
]

num_dims = 2
eval_fn = branin_hoo

results = []
for name, search_fn in names_and_search_fns:
    with timer(name):
        res = search_fn(evaluation_fn=eval_fn, num_dims=num_dims)
    results.append((name, res))

plot_bests(results)

num_dims = 100
eval_fn = branin_hoo_with_useless_dimensions(num_dims)

results = []
for name, search_fn in names_and_search_fns:
    with timer(name):
        res = search_fn(evaluation_fn=eval_fn, num_dims=num_dims)
    results.append((name, res))

plot_bests(results)


I am working on the file "Hyperparameter Optimization.ipynb".
The first task is:
Setup
Assumptions

search space for each parameter is random uniform between 0 and 1
200 iterations for each algorithm

utils
Can you write Python code for it?

@contextlib.contextmanager
def timer(title):
    start_time = time.time()
    try:
        yield
    finally:
        duration = time.time() - start_time
        print("%s took %fs" % (title, duration))


I am working on the file "Hyperparameter Optimization.ipynb".
The first task is:
evaluation functions
Can you write Python code for it?

def branin_hoo(params, noisy=False):
    """
    http://www.sfu.ca/~ssurjano/branin.html
    """
    unscaled_x1, unscaled_x2 = params

    x1 = unscaled_x1 * 15 - 5
    x2 = unscaled_x2 * 15

    a = 1
    b = 5.1 / (4 * np.pi ** 2)
    c = 5 / np.pi
    r = 6
    s = 10
    t = 1 / (8 * np.pi)

    term1 = a * (x2 - b * x1 ** 2 + c * x1 - r) ** 2
    term2 = s * (1 - t) * np.cos(x1)
    return term1 + term2 + s

branin_hoo([(np.pi + 5) / 15, 2.275 / 15])

def branin_hoo_with_useless_dimensions(total_dims):
    assert total_dims > 2

    p1 = np.random.randint(total_dims)
    p2 = np.random.randint(total_dims)

    def inner(params, noisy=False):
        return branin_hoo([params[p1], params[p2]], noisy)

    return inner


I am working on the file "Hyperparameter Optimization.ipynb".
The first task is:
generator functions
Can you write Python code for it?

def random_generator(X, y, num_candidates, num_dims):
    return np.random.rand(num_candidates, num_dims)


I am working on the file "Hyperparameter Optimization.ipynb".
The first task is:
model score functions
NOTE: higher score = better
Can you write Python code for it?

def minimum_mean(f_min, means, stds):
    return -means

def maximum_uncertainty(f_min, means, stds):
    return stds

def expected_improvement_1(f_min, means, stds):
    """
    assumes log scale cost

    made for optimizing run time of optimization algorithms

    http://www.cs.ubc.ca/~hutter/papers/11-LION5-SMAC.pdf
    """
    # v is a scaled version of the best score
    v = (f_min - means) / stds
    term1 = f_min * scipy.stats.norm.cdf(v)
    # NOTE (v - stds) seems super wrong to me!
    term2 = np.exp(0.5 * stds ** 2 + means) * scipy.stats.norm.cdf(v - stds)
    return term1 - term2

def expected_improvement_2(f_min, means, stds):
    """
    older version of equation 1

    probably also assumes log scale cost

    http://www.cs.ubc.ca/~hutter/papers/11-LION5-SMAC.pdf
    """
    # ignore f_min and replace with means + stds
    return expected_improvement_1(means + stds, means, stds)

def expected_improvement_3(f_min, means, stds):
    """
    http://arxiv.org/abs/1208.3719
    """
    u = (f_min - means) / stds
    return stds * (u * scipy.stats.norm.cdf(u) + scipy.stats.norm.pdf(u))


I am working on the file "Hyperparameter Optimization.ipynb".
The first task is:
plotting
Can you write Python code for it?

def plot_bests(results):
    lines = []
    for name, res in results:
        y = res["y"]
        label = "%s=%f" % (name, min(y))
        bests = [np.min(y[:l]) for l in range(1, len(y) + 1)]
        line, = pylab.plot(bests, label=label)
        lines.append(line)
    pylab.legend(handles=lines)