markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
Finally, putting everything together, we have heap_pop: | def heap_pop(heap):
# swap root with last value
heap[0], heap[-1] = heap[-1], heap[0]
# remove last value
result = heap.pop()
# restore heap properties
for i in range(len(heap)):
percolate_down(heap, 0, len(heap))
return result | Heaps.ipynb | abeschneider/algorithm_notes | mit |
To see heap_pop in action: | heap = []
heap_insert(heap, 1)
heap_insert(heap, 100)
heap_insert(heap, 20)
heap_insert(heap, 5)
heap_insert(heap, 3)
print(heap)
print(heap_pop(heap))
print(heap_pop(heap))
print(heap_pop(heap))
print(heap_pop(heap))
print(heap_pop(heap)) | Heaps.ipynb | abeschneider/algorithm_notes | mit |
Examples
Example 1 | testing = (__name__ == "__main__")
if testing:
! jupyter nbconvert --to python hadamard.ipynb
import numpy as np
import sys,os
import matplotlib.image as mpimg
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
sys.path.append(ia898path)
import ia898.src as ia
if ... | src/hadamard.ipynb | robertoalotufo/ia898 | mit |
Measuring time: | if testing:
f = mpimg.imread('../data/cameraman.tif')
print('Computational time is:')
%timeit ia.hadamard(f) | src/hadamard.ipynb | robertoalotufo/ia898 | mit |
Enhancement factor of mobility depending on charge carrier concentration $g_1$ | c = 10**np.linspace(-10, 0., 101)
for nsigma in [3, 4, 5, 6][::-1]:
g1 = egdm.g1(nsigma, np.where(c < egdm.g1_max_c, c, egdm.g1_max_c))
plt.plot(c, g1, label='$\hat{\sigma}$ = %s'%nsigma)
testing.store(g1, rtol=1e-7)
# solve for g1(x)=2
c2 = brentq(lambda x: egdm.g1(nsigma, x) - 2., 1e-10, 1e1)
... | examples/egdm/egdm-g1-g2-g3.ipynb | mzszym/oedes | agpl-3.0 |
Enhancement factor of mobility depending on electric field $g_2$ | En = np.linspace(0., 2.5, 101)
for nsigma in [3, 4, 5, 6][::-1]:
g2 = egdm.g2(nsigma, np.where(En < egdm.g2_max_En, En, egdm.g2_max_En))
testing.store(g2, rtol=1e-7)
plt.plot(En, g2, label='$\hat{\sigma}$ = %s'%nsigma)
plt.yscale('log')
plt.ylim([1., 1e3])
plt.xlabel('normalized electric field, $E_n=eaF/\s... | examples/egdm/egdm-g1-g2-g3.ipynb | mzszym/oedes | agpl-3.0 |
Enhancement factor of diffusion $g_3$ | c = 10**np.linspace(-4, 0., 1001)
for nsigma in [3, 4, 5, 6][::-1]:
g3 = egdm.g3(nsigma, np.where(c < egdm.g3_max_c, c, egdm.g3_max_c))
plt.plot(c, g3, label='$\hat{\sigma}$ = %s'%nsigma)
testing.store(g3, rtol=1e-7)
# solve for g3(x)=2
c2 = brentq(lambda x: egdm.g3(nsigma, x) - 2., 1e-4, 0.5)
p... | examples/egdm/egdm-g1-g2-g3.ipynb | mzszym/oedes | agpl-3.0 |
The object returned by load_breast_cancer() is a scikit-learn Bunch object, which is similar to a dictionary. | cancer.keys() | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Question 0 (Example)
How many features does the breast cancer dataset have?
This function should return an integer. | # You should write your whole answer within the function provided. The autograder will call
# this function and compare the return value against the correct solution value
def answer_zero():
# This function returns the number of features of the breast cancer dataset, which is an integer.
# The assignment quest... | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Question 1
Scikit-learn works with lists, numpy arrays, scipy-sparse matrices, and pandas DataFrames, so converting the dataset to a DataFrame is not necessary for training this model. Using a DataFrame does however help make many things easier such as munging data, so let's practice creating a classifier with a pandas... | def answer_one():
df = pd.DataFrame(data=cancer['data'], columns=cancer['feature_names'])
df['target'] = cancer['target']
return df
answer_one() | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Question 2
What is the class distribution? (i.e. how many instances of malignant (encoded 0) and how many benign (encoded 1)?)
This function should return a Series named target of length 2 with integer values and index = ['malignant', 'benign'] | def answer_two():
cancerdf = answer_one()
malignant = (cancerdf['target']==0).sum()
benign = (cancerdf['target']==1).sum()
ans = [malignant, benign]
return ans
answer_two() | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Question 3
Split the DataFrame into X (the data) and y (the labels).
This function should return a tuple of length 2: (X, y), where
* X has shape (569, 30)
* y has shape (569,). | cancerdf = answer_one()
cancerdf.iloc[:, :-1]
def answer_three():
cancerdf = answer_one()
X= cancerdf.iloc[:, :-1]
y= cancerdf['target']
return X, y | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Question 4
Using train_test_split, split X and y into training and test sets (X_train, X_test, y_train, and y_test).
Set the random number generator state to 0 using random_state=0 to make sure your results match the autograder!
This function should return a tuple of length 4: (X_train, X_test, y_train, y_test), where ... | from sklearn.model_selection import train_test_split
def answer_four():
X, y = answer_three()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25 , random_state=0)
return X_train, X_test, y_train, y_test
answer_four() | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Question 5
Using KNeighborsClassifier, fit a k-nearest neighbors (knn) classifier with X_train, y_train and using one nearest neighbor (n_neighbors = 1).
*This function should return a * sklearn.neighbors.classification.KNeighborsClassifier. | from sklearn.neighbors import KNeighborsClassifier
def answer_five():
X_train, X_test, y_train, y_test = answer_four()
# Your code here
return # Return your answer | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Question 6
Using your knn classifier, predict the class label using the mean value for each feature.
Hint: You can use cancerdf.mean()[:-1].values.reshape(1, -1) which gets the mean value for each feature, ignores the target column, and reshapes the data from 1 dimension to 2 (necessary for the precict method of KNeigh... | def answer_six():
cancerdf = answer_one()
means = cancerdf.mean()[:-1].values.reshape(1, -1)
# Your code here
return # Return your answer | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Question 7
Using your knn classifier, predict the class labels for the test set X_test.
This function should return a numpy array with shape (143,) and values either 0.0 or 1.0. | def answer_seven():
X_train, X_test, y_train, y_test = answer_four()
knn = answer_five()
# Your code here
return # Return your answer | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Question 8
Find the score (mean accuracy) of your knn classifier using X_test and y_test.
This function should return a float between 0 and 1 | def answer_eight():
X_train, X_test, y_train, y_test = answer_four()
knn = answer_five()
# Your code here
return # Return your answer | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Optional plot
Try using the plotting function below to visualize the differet predicition scores between training and test sets, as well as malignant and benign cells. | def accuracy_plot():
import matplotlib.pyplot as plt
%matplotlib notebook
X_train, X_test, y_train, y_test = answer_four()
# Find the training and testing accuracies by target value (i.e. malignant, benign)
mal_train_X = X_train[y_train==0]
mal_train_y = y_train[y_train==0]
ben_train_X = ... | python-machine-learning/Assignment 1.ipynb | atulsingh0/MachineLearning | gpl-3.0 |
Github
https://github.com/jbwhit/OSCON-2015/commit/6750b962606db27f69162b802b5de4f84ac916d5
A few Python Basics | # Create a [list]
days = ['Monday', # multiple lines
'Tuesday', # acceptable
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday', # trailing comma is fine!
]
days
# Simple for-loop
for day in days:
print(day)
# Double for-loop
for day in days:
fo... | notebooks/07-Some_basics.ipynb | jbwhit/jupyter-best-practices | mit |
Specify the model:
The model is specified in the same way as any Pyro model, except that a keyword argument, observations, must be used to input a dictionary with each observation as a key. Since inference compilation involves learning to perform inference for any observed values, it is not important what the values in... | def model(prior_mean, observations={"x1": 0, "x2": 0}):
x = pyro.sample("z", dist.Normal(prior_mean, torch.tensor(5**0.5)))
y1 = pyro.sample("x1", dist.Normal(x, torch.tensor(2**0.5)), obs=observations["x1"])
y2 = pyro.sample("x2", dist.Normal(x, torch.tensor(2**0.5)), obs=observations["x2"])
return x | tutorial/source/csis.ipynb | uber/pyro | apache-2.0 |
And the guide:
The guide will be trained (a.k.a. compiled) to use the observed values to make proposal distributions for each unconditioned sample statement. In the paper [1], a neural network architecture is automatically generated for any model. However, for the implementation in Pyro the user must specify a task-spe... | class Guide(nn.Module):
def __init__(self):
super().__init__()
self.neural_net = nn.Sequential(
nn.Linear(2, 10),
nn.ReLU(),
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 10),
nn.ReLU(),
nn.Linear(10, 5),
n... | tutorial/source/csis.ipynb | uber/pyro | apache-2.0 |
Now create a CSIS instance:
The object is initialised with the model; the guide; a PyTorch optimiser for training the guide; and the number of importance-weighted samples to draw when performing inference. The guide will be optimised for a particular value of the model/guide argument, prior_mean, so we use the value se... | optimiser = pyro.optim.Adam({'lr': 1e-3})
csis = pyro.infer.CSIS(model, guide, optimiser, num_inference_samples=50)
prior_mean = torch.tensor(1.) | tutorial/source/csis.ipynb | uber/pyro | apache-2.0 |
Now we 'compile' the instance to perform inference on this model:
The arguments given to csis.step are passed to the model and guide when they are run to evaluate the loss. | for step in range(n_steps):
csis.step(prior_mean) | tutorial/source/csis.ipynb | uber/pyro | apache-2.0 |
And now perform inference by importance sampling:
The compiled guide program should now be able to propose a distribution for z that approximates the posterior, $p(z | x_1, x_2)$, for any $x_1, x_2$. The same prior_mean is entered again, as well as the observed values inside observations. | posterior = csis.run(prior_mean,
observations={"x1": torch.tensor(8.),
"x2": torch.tensor(9.)})
marginal = pyro.infer.EmpiricalMarginal(posterior, "z") | tutorial/source/csis.ipynb | uber/pyro | apache-2.0 |
We now plot the results and compare with importance sampling:
We observe $x_1 = 8$ and $x_2 = 9$. Inference is performed by taking 50 samples using CSIS, and 50 using importance sampling from the prior. We then plot the resulting approximations to the posterior distributions, along with the analytic posterior. | import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
with torch.no_grad():
# Draw samples from empirical marginal for plotting
csis_samples = torch.stack([marginal() for _ in range(1000)])
# Calculate empirical marginal with importance sampling
is_posterior = pyro.infer.Importance(mod... | tutorial/source/csis.ipynb | uber/pyro | apache-2.0 |
We'll use the Delaney dataset from the MoleculeNet suite to run our experiments in this tutorial. Let's load up our dataset for our experiments, and then make some uncertainty predictions. | import deepchem as dc
import numpy as np
import matplotlib.pyplot as plot
tasks, datasets, transformers = dc.molnet.load_delaney()
train_dataset, valid_dataset, test_dataset = datasets
model = dc.models.MultitaskRegressor(len(tasks), 1024, uncertainty=True)
model.fit(train_dataset, nb_epoch=20)
y_pred, y_std = model.... | examples/tutorials/25_Uncertainty_In_Deep_Learning.ipynb | lilleswing/deepchem | mit |
All of this looks exactly like any other example, with just two differences. First, we add the option uncertainty=True when creating the model. This instructs it to add features to the model that are needed for estimating uncertainty. Second, we call predict_uncertainty() instead of predict() to produce the output. ... | # Generate some fake data and plot a regression line.
x = np.linspace(0, 5, 10)
y = 0.15*x + np.random.random(10)
plot.scatter(x, y)
fit = np.polyfit(x, y, 1)
line_x = np.linspace(-1, 6, 2)
plot.plot(line_x, np.poly1d(fit)(line_x))
plot.show() | examples/tutorials/25_Uncertainty_In_Deep_Learning.ipynb | lilleswing/deepchem | mit |
The line clearly does not do a great job of fitting the data. There are many possible reasons for this. Perhaps the measuring device used to capture the data was not very accurate. Perhaps y depends on some other factor in addition to x, and if we knew the value of that factor for each data point we could predict y ... | plot.figure(figsize=(12, 3))
line_x = np.linspace(0, 5, 50)
for i in range(3):
plot.subplot(1, 3, i+1)
plot.scatter(x, y)
fit = np.polyfit(np.concatenate([x, [3]]), np.concatenate([y, [i]]), 10)
plot.plot(line_x, np.poly1d(fit)(line_x))
plot.show() | examples/tutorials/25_Uncertainty_In_Deep_Learning.ipynb | lilleswing/deepchem | mit |
Each of them perfectly interpolates the data points, yet they clearly are different models. (In fact, there are infinitely many 10th degree polynomials that exactly interpolate any ten data points.) They make identical predictions for the data we fit them to, but for any other value of x they produce different predic... | abs_error = np.abs(y_pred.flatten()-test_dataset.y.flatten())
plot.scatter(y_std.flatten(), abs_error)
plot.xlabel('Standard Deviation')
plot.ylabel('Absolute Error')
plot.show() | examples/tutorials/25_Uncertainty_In_Deep_Learning.ipynb | lilleswing/deepchem | mit |
The first thing we notice is that the axes have similar ranges. The model clearly has learned the overall magnitude of errors in the predictions. There also is clearly a correlation between the axes. Values with larger uncertainties tend on average to have larger errors. (Strictly speaking, we expect the absolute e... | plot.hist(abs_error/y_std.flatten(), 20)
plot.show() | examples/tutorials/25_Uncertainty_In_Deep_Learning.ipynb | lilleswing/deepchem | mit |
Dataset Parameters
Let's create the ParameterSet which would be added to the Bundle when calling add_dataset. Later we'll call add_dataset, which will create and attach this ParameterSet for us. | ps, constraints = phoebe.dataset.orb()
print ps | 2.0/tutorials/ORB.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
times | print ps['times'] | 2.0/tutorials/ORB.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
Compute Options
Let's look at the compute options (for the default PHOEBE 2 backend) that relate to dynamics and the ORB dataset | ps_compute = phoebe.compute.phoebe()
print ps_compute | 2.0/tutorials/ORB.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
dynamics_method | print ps_compute['dynamics_method'] | 2.0/tutorials/ORB.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
The 'dynamics_method' parameter controls how stars and components are placed in the coordinate system as a function of time and has several choices:
* keplerian (default): Use Kepler's laws to determine positions. If the system has more than two components, then each orbit is treated independently and nested (ie ther... | print ps_compute['ltte'] | 2.0/tutorials/ORB.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
The 'ltte' parameter sets whether light travel time effects (Roemer delay) are included. If set to False, the positions and velocities are returned as they actually are for that given object at that given time. If set to True, they are instead returned as they were or will be when their light reaches the origin of th... | b.add_dataset('orb', times=np.linspace(0,3,201))
b.run_compute()
b['orb@model'].twigs
print b['times@primary@orb01@orb@model']
print b['xs@primary@orb01@orb@model']
print b['vxs@primary@orb01@orb@model'] | 2.0/tutorials/ORB.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
Plotting
By default, orb datasets plot as 'ys' vx 'xs' (plane of sky). Notice the y-scale here with inclination set to 90. | axs, artists = b['orb@model'].plot() | 2.0/tutorials/ORB.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
As always, you have access to any of the arrays for either axes, so if you want to plot 'vxs' vs 'times' | axs, artists = b['orb@model'].plot(x='times', y='vxs') | 2.0/tutorials/ORB.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
3d axes are not yet supported for orbits, but hopefully will be soon.
Once they are supported, they will default to x, y, and z positions plotted on their respective axes. | fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
axs, artists = b['orb@model'].plot(xlim=(-4,4), ylim=(-4,4), zlim=(-4,4)) | 2.0/tutorials/ORB.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
Data
https://www.drivendata.org/competitions/54/machine-learning-with-a-heart/page/109/
- Numeric
- slope_of_peak_exercise_st_segment (int, semi-categorical, 1-3)
- resting_blood_pressure (int)
- chest_pain_type (int, semi-categorical, 1-4)
- num_major_vessels (int, semi-categorical, 0-3)
- resting_... | features = pd.read_csv('train_values.csv')
labels = pd.read_csv('train_labels.csv')
features.head()
labels.head()
FEATURES = ['slope_of_peak_exercise_st_segment',
'thal',
'resting_blood_pressure',
'chest_pain_type',
'num_major_vessels',
'fasting_b... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Decision Trees | tree_mean_acc = 0
tree_score_df = pd.DataFrame(columns = ['Fold', 'Accuracy', 'Precision', 'Recall'])
for fold_ind, (train_indices, test_indices) in enumerate(stratified_kflod_validator.split(x, y), 1):
x_train, x_test = x[train_indices], x[test_indices]
y_train, y_test = y[train_indices], y[test_indices]... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
KNN | # TODO Normalize
knn_mean_score_df = pd.DataFrame(columns = ['k', 'Avg. Accuracy', 'Avg. Precision', 'Avg. Recall'])
normalized_x = sklearn.preprocessing.normalize(x) # No improvement over un-normalized data.
mean_accs = []
for k in list(range(1, 10)) + [math.ceil(len(features) * step) for step in [0.1, 0.2, 0.3, 0.... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Naive Bayes | nb_classifier_types = [sklearn.naive_bayes.GaussianNB,
sklearn.naive_bayes.MultinomialNB,
sklearn.naive_bayes.ComplementNB,
sklearn.naive_bayes.BernoulliNB]
nb_mean_score_df = pd.DataFrame(columns = ['Type', 'Avg. Accuracy', 'Avg. Precision', 'Avg. R... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
SVM | svm_classifier_type = sklearn.svm.SVC
# Avg.
# Args -> acc / prec / rec
#
# kernel: linear -> 78.89 % 78.31 % 73.75 %
# kernel: linear, C: 0.1 -> 84.44 % 88.54 % 75.00 %
#
# * No... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Shallow Neural Nets
Import deps | import pandas as pd
from sklearn.model_selection import train_test_split
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, Dropout, Flatten, BatchNor... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Import data | features = pd.read_csv('train_values.csv')
labels = pd.read_csv('train_labels.csv')
print(labels.head())
features.head()
FEATURES = ['slope_of_peak_exercise_st_segment',
'thal',
'resting_blood_pressure',
'chest_pain_type',
'num_major_vessels',
'fas... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Define model | input_shape = (1,15)
num_classes = 2
print(x.shape)
print(y.shape)
print(x[:1])
print(y[:1]) | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Architecture 0 - Inflating Dense 120-225, 0.5 Dropout, Batch Norm, Sigmoid Classification | arch_cnt = 'arch-0-3'
model = Sequential()
model.add(
Dense(120, input_dim=15, kernel_initializer='normal',
# kernel_regularizer=keras.regularizers.l2(0.001), # pierd 0.2 acc
activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(225, input_dim=15, kernel_initializer='normal', activation='... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Architecture 1 - Deflating Dense 225-112, 0.5 Dropout, Batch Norm, Sigmoid Classification | arch_cnt = 'arch-1'
model = Sequential()
model.add(
Dense(225, input_dim=15, kernel_initializer='normal',
# kernel_regularizer=keras.regularizers.l2(0.001), # pierd 0.2 acc
activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(112, input_dim=15, kernel_initializer='normal', activation='re... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Architecture 2 - Deflating Dense 225-112, 0.5 Dropout, Batch Norm, Sigmoid Classification, HE Initialization | arch_cnt = 'arch-2'
model = Sequential()
model.add(
Dense(225, input_dim=15, kernel_initializer='he_uniform',
kernel_regularizer=keras.regularizers.l2(0.001), # pierd 0.2 acc
activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(112, input_dim=15, kernel_initializer='he_uniform', activati... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Architecture 3 - Deflating Dense 225-112, 0.5 Dropout, Batch Norm, Sigmoid Classification, L2 = 1e^-4 | arch_cnt = 'arch-3-4'
model = Sequential()
model.add(
Dense(225, input_dim=15, kernel_initializer='normal',
kernel_regularizer=keras.regularizers.l2(0.0001), # pierd 0.2 acc
activation='relu'))
model.add(Dropout(0.5))
model.add(
Dense(112, input_dim=15, kernel_initializer='normal',
... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Architecture 3 - Deflating Dense 225-112, 0.5 Dropout, Batch Norm, Sigmoid Classification, L2 = 1e^-3 | arch_cnt = 'arch-3-3'
model = Sequential()
model.add(
Dense(225, input_dim=15, kernel_initializer='normal',
kernel_regularizer=keras.regularizers.l2(0.001), # pierd 0.2 acc
activation='relu'))
model.add(Dropout(0.5))
model.add(
Dense(112, input_dim=15, kernel_initializer='normal',
... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Architecture 3 - Deflating Dense 225-112, 0.5 Dropout, Batch Norm, Sigmoid Classification, L2 = 1e^-2 | arch_cnt = 'arch-3-2'
model = Sequential()
model.add(
Dense(225, input_dim=15, kernel_initializer='normal',
kernel_regularizer=keras.regularizers.l2(0.01), # pierd 0.2 acc
activation='relu'))
model.add(Dropout(0.5))
model.add(
Dense(112, input_dim=15, kernel_initializer='normal',
... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Architecture 3 - Deflating Dense 225-112, 0.5 Dropout, Batch Norm, Sigmoid Classification, L2 = 1e^-1 | arch_cnt = 'arch-3-1'
model = Sequential()
model.add(
Dense(225, input_dim=15, kernel_initializer='normal',
kernel_regularizer=keras.regularizers.l2(0.1), # pierd 0.2 acc
activation='relu'))
model.add(Dropout(0.5))
model.add(
Dense(112, input_dim=15, kernel_initializer='normal',
k... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Ensemble Methods | import matplotlib.pyplot as plt
%matplotlib inline | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Bagging Strategies
Random Forests | from sklearn.ensemble import RandomForestClassifier
# x_train, x_test, y_train, y_test
clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0)
clf.fit(x_train, y_train)
print(clf.feature_importances_)
print(clf.predict(x_test))
# make predictions for test data
y_pred = clf.predict(x_test)
pre... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
ExtraTrees | from sklearn.ensemble import ExtraTreesClassifier
# x_train, x_test, y_train, y_test
clf = ExtraTreesClassifier(n_estimators=100, max_depth=2, random_state=0)
clf.fit(x_train, y_train)
print(clf.feature_importances_)
print(clf.predict(x_test))
# make predictions for test data
y_pred = clf.predict(x_test)
predict... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
Stacking Strategies
SuperLearner
Boosting Strategies
xgboost | # import xgboost as xgb
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
# x_train, x_test, y_train, y_test
model = XGBClassifier()
model.fit(x_train, y_train)
print(model)
# make predictions for test data
y_pred = model.predict(x_test)
predictions = [round(value) for value in y_pred]
#... | kaggle/machine-learning-with-a-heart/Lab4.ipynb | xR86/ml-stuff | mit |
When to use python? -- 50xp, Status : Earned
Python is a pretty versatile language. For what applications can you use Python?
Ans: All of the above
Any comments? -- 100xp, Satatus : Earned
We can add comments to python scripts.
Comments are short snippets of plain english, to help you and others understand what... | # Just testing division
print(5 / 8)
# Additon works too ( added comment here )
print(7 + 10) | .ipynb_checkpoints/DAT208x - Week 1 - Python Basics-checkpoint.ipynb | dataDogma/Computer-Science | gpl-3.0 |
Python as a calculator -- 100xp, Status : Earned
Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division, there is also support for more advanced operations such as:
Exponentiation:. This operator raises the number to its left to the power of the number to i... | """Suppose you have $100, which you can invest with a 10% return each year. After one year, it's
100 x 1.1 = 110 dollars, and after two years it's 100 x 1.1 x 1.1 = 121.
Add code to calculate how much money you end up with after 7 years"""
print(5 + 5)
print(5 - 5)
# Multiplication and division
print(3 * 5)
print(1... | .ipynb_checkpoints/DAT208x - Week 1 - Python Basics-checkpoint.ipynb | dataDogma/Computer-Science | gpl-3.0 |
Parsevals theorem when applied to discrete Fourier Transform looks like this.
$\sum {n=0}^{N-1}|x[n]|^{2}={\frac {1}{N}}\sum {k=0}^{N-1}|X[k]|^{2}$
Source: https://en.wikipedia.org/wiki/Parseval%27s_theorem | # check Parseval's theorem holds numerically
nsamps=1000
# window
w = signal.tukey(nsamps,0.1)
a = np.random.normal(0,1,nsamps) * w
A = np.fft.fft(a)
b = (1/np.sqrt(2*np.pi))*(signal.gaussian(nsamps,10))
B = np.fft.fft(b)
c = np.convolve(a,b,'same')
C = np.fft.fft(c)
# signal c is convolution of Gaussian noise (a) ... | devel/Parseval.ipynb | JackWalpole/splitwavepy | mit |
Furthermore by the convolution theorem: C = A * B.
And therefore sum(C^2) = sum(A^2 * B^2) | AB = A * B
ab = np.fft.ifft(AB)
plt.plot(np.roll(ab,500))
plt.plot(c)
sumAB = np.sum(np.abs(A**2*B**2))/nsamps
print('sum A*B',sumAB)
print('difference',np.abs(sumt-sumAB))
print('percent',(np.abs(sumt-sumAB)/sumt)*100) | devel/Parseval.ipynb | JackWalpole/splitwavepy | mit |
Parsevals theorem as applied in Silver and Chan (and Walsh).
$\sum {n=0}^{N-1}|x[n]|^{2}={\frac {1}{N}}\sum {k=1}^{N-2}|X[k]|^{2}+\frac{1}_{2}\sum|X[0,N-1]|$
Source: https://en.wikipedia.org/wiki/Parseval%27s_theorem | def ndf(y,taper=True,detrend=True):
"""
Uses the improvement found by Walsh et al (2013).
By default will detrend data to ensure zero mean
and will taper edges using a Tukey filter affecting amplitudes of 5% of data at edges
"""
if taper is True:
y = y * signal.tukey(y.size,0.05)
... | devel/Parseval.ipynb | JackWalpole/splitwavepy | mit |
NumPy
tested with version 1.9 (1.13.1) | import numpy as np
np.__version__ | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
Requests
tested with version 2.7 (2.18.1) Required for using the Shim interface to SciDB. | import requests
requests.__version__ | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
Pandas (optional)
tested with version 0.15. (0.20.3) Required only for importing/exporting SciDB arrays as Pandas Dataframe objects. | import pandas as pd
pd.__version__ | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
SciPy (optional)
tested with versions 0.10-0.12. (0.19.0) Required only for importing/exporting SciDB arrays as SciPy sparse matrices. | import scipy
scipy.__version__ | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
2) Importar scidbpy
pip install git+http://github.com/paradigm4/scidb-py.git@devel | import scidbpy
scidbpy.__version__
from scidbpy import connect | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
conectarse al servidor de Base de datos | sdb = connect('http://localhost:8080') | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
3) Leer archivo con cada una de las ondas | import urllib.request # urllib2 in python2 the lib that handles the url stuff
target_url = "https://www.physionet.org/physiobank/database/mimic2wdb/matched/RECORDS-waveforms"
data = urllib.request.urlopen(target_url) # it's a file like object and works just like a file
lines = data.readlines();
line = str(lines[100]) | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
Quitarle caracteres especiales | carpeta,onda = line.replace('b\'','').replace('\'','').replace('\\n','').split("/")
onda | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
4) Importar WFDB para conectarse a physionet | import wfdb
sig, fields = wfdb.srdsamp(onda,pbdir='mimic2wdb/matched/'+carpeta) #, sampfrom=11000
print(sig)
print("signame: " + str(fields['signame']))
print("units: " + str(fields['units']))
print("fs: " + str(fields['fs']))
print("comments: " + str(fields['comments']))
print("fields: " + str(fields)) | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
Busca la ubicacion de la señal tipo II | signalII = None
try:
signalII = fields['signame'].index("II")
except ValueError:
print("List does not contain value")
if(signalII!=None):
print("List contain value") | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
Normaliza la señal y le quita los valores en null | array = wfdb.processing.normalize(x=sig[:, signalII], lb=-2, ub=2)
arrayNun = array[~np.isnan(array)]
arrayNun = np.trim_zeros(arrayNun)
arrayNun | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
Cambiar los guiones "-" por raya al piso "_" porque por algun motivo SciDB tiene problemas con estos caracteres
Si el arreglo sin valores nulos no queda vacio lo sube al SciDB | ondaName = onda.replace("-", "_")
if arrayNun.size>0 :
sdb.input(upload_data=array).store(ondaName,gc=False)
# sdb.iquery("store(input(<x:int64>[i], '{fn}', 0, '{fmt}'), "+ondaName+")", upload_data=array) | Jupyter/LoadDataMimic-II.ipynb | davidgutierrez/HeartRatePatterns | gpl-3.0 |
First reload the data we generated in notmist.ipynb. | pickle_file = 'notMNIST.pickle'
with open(pickle_file, 'rb') as f:
save = pickle.load(f)
train_dataset = save['train_dataset']
train_labels = save['train_labels']
valid_dataset = save['valid_dataset']
valid_labels = save['valid_labels']
test_dataset = save['test_dataset']
test_labels = save['test_labels'... | udacity_notebook/3_regularization.ipynb | ds-hwang/deeplearning_udacity | mit |
Reformat into a shape that's more adapted to the models we're going to train:
- data as a flat matrix,
- labels as float 1-hot encodings. | image_size = 28
num_labels = 10
def reformat(dataset, labels):
dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)
# Map 2 to [0.0, 1.0, 0.0 ...], 3 to [0.0, 0.0, 1.0 ...]
labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
return dataset, labels
train_dataset, train_l... | udacity_notebook/3_regularization.ipynb | ds-hwang/deeplearning_udacity | mit |
1.1 Loading HDF5 files
HDF5 files can be read using a few different classes operating on different levels. The hierarchy meaningful to the end user is in the following (from low to high),
* mpes.fprocessing.File() -- local import of h5py.File(), a low-level Python HDF5 parser (wrapped over even lower C code).
* mpes.fp... | hdff = fp.File(fpath)
hdff
hdfr = fp.hdf5Reader(fpath)
hdfr | examples/Tutorial_01_HDF5 File Management.ipynb | RealPolitiX/mpes | mit |
New attributes and methods in the hdf5Reader() class | print( list(set(dir(hdfr)) - set(dir(hdff))) )
hdfp = fp.hdf5Processor(fpath)
hdfp | examples/Tutorial_01_HDF5 File Management.ipynb | RealPolitiX/mpes | mit |
New attributes and methods in the hdf5Processer() class | print( list(set(dir(hdfp)) - set(dir(hdfr))) ) | examples/Tutorial_01_HDF5 File Management.ipynb | RealPolitiX/mpes | mit |
1.2 Retrieving components from HDF5 files
Reading components can also be done at different levels, the level of hdf5Reader() or above is recommended. | hdfp.summarize()
print(list(hdfr.readGroup(hdfr, 'EventFormat'))) | examples/Tutorial_01_HDF5 File Management.ipynb | RealPolitiX/mpes | mit |
1.3 Converting HDF5 files
Conversion of hdf5 to Matlab (mat) format (no data processing). | hdfr.convert('mat', save_addr='../data/data_131') | examples/Tutorial_01_HDF5 File Management.ipynb | RealPolitiX/mpes | mit |
Conversion to parquet format | hdfr.convert('parquet', save_addr='../data/data_131_parquet', pq_append=False, chunksz=1e7, \
compression='gzip') | examples/Tutorial_01_HDF5 File Management.ipynb | RealPolitiX/mpes | mit |
1.4 Splitting HDF5 files | hdfs = fp.hdf5Splitter(fpath)
hdfs.split(nsplit=50, save_addr=r'../data/data_114_parts/data_114_', pbar=True) | examples/Tutorial_01_HDF5 File Management.ipynb | RealPolitiX/mpes | mit |
1.5 Retrieve binned data from stored HDF5 file
Read binned data over 3 axes | fpath_binned = r'../data/binres_114.h5'
bindict = fp.readBinnedhdf5(fpath_binned, combined=True)
bindict.keys() | examples/Tutorial_01_HDF5 File Management.ipynb | RealPolitiX/mpes | mit |
Read binned data over 4 axes | fpath_binned = r'../data/data_114_4axis_binned.h5'
bindict = fp.readBinnedhdf5(fpath_binned, combined=True)
bindict.keys()
bindict = fp.readBinnedhdf5(fpath_binned, combined=False)
bindict.keys() | examples/Tutorial_01_HDF5 File Management.ipynb | RealPolitiX/mpes | mit |
Time series analysis
NOTE: Some of the example in this chapter have been updated to work with more recent versions of the libraries.
Load the data from "Price of Weed". | download("https://github.com/AllenDowney/ThinkStats2/raw/master/code/mj-clean.csv")
transactions = pd.read_csv("mj-clean.csv", parse_dates=[5])
transactions.head() | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
The following function takes a DataFrame of transactions and compute daily averages. | def GroupByDay(transactions, func=np.mean):
"""Groups transactions by day and compute the daily mean ppg.
transactions: DataFrame of transactions
returns: DataFrame of daily prices
"""
grouped = transactions[["date", "ppg"]].groupby("date")
daily = grouped.aggregate(func)
daily["date"] = ... | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
The following function returns a map from quality name to a DataFrame of daily averages. | def GroupByQualityAndDay(transactions):
"""Divides transactions by quality and computes mean daily price.
transaction: DataFrame of transactions
returns: map from quality to time series of ppg
"""
groups = transactions.groupby("quality")
dailies = {}
for name, group in groups:
dail... | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
dailies is the map from quality name to DataFrame. | dailies = GroupByQualityAndDay(transactions) | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
The following plots the daily average price for each quality. | import matplotlib.pyplot as plt
thinkplot.PrePlot(rows=3)
for i, (name, daily) in enumerate(dailies.items()):
thinkplot.SubPlot(i + 1)
title = "Price per gram ($)" if i == 0 else ""
thinkplot.Config(ylim=[0, 20], title=title)
thinkplot.Scatter(daily.ppg, s=10, label=name)
if i == 2:
plt.xti... | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
We can use statsmodels to run a linear model of price as a function of time. | import statsmodels.formula.api as smf
def RunLinearModel(daily):
model = smf.ols("ppg ~ years", data=daily)
results = model.fit()
return model, results | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
Here's what the results look like. | from IPython.display import display
for name, daily in dailies.items():
model, results = RunLinearModel(daily)
print(name)
display(results.summary()) | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
Now let's plot the fitted model with the data. | def PlotFittedValues(model, results, label=""):
"""Plots original data and fitted values.
model: StatsModel model object
results: StatsModel results object
"""
years = model.exog[:, 1]
values = model.endog
thinkplot.Scatter(years, values, s=15, label=label)
thinkplot.Plot(years, results... | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
The following function plots the original data and the fitted curve. | def PlotLinearModel(daily, name):
"""Plots a linear fit to a sequence of prices, and the residuals.
daily: DataFrame of daily prices
name: string
"""
model, results = RunLinearModel(daily)
PlotFittedValues(model, results, label=name)
thinkplot.Config(
title="Fitted values",
... | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
Here are results for the high quality category: | name = "high"
daily = dailies[name]
PlotLinearModel(daily, name) | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
Moving averages
As a simple example, I'll show the rolling average of the numbers from 1 to 10. | array = np.arange(10) | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
With a "window" of size 3, we get the average of the previous 3 elements, or nan when there are fewer than 3. | series = pd.Series(array)
series.rolling(3).mean() | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
The following function plots the rolling mean. | def PlotRollingMean(daily, name):
"""Plots rolling mean.
daily: DataFrame of daily prices
"""
dates = pd.date_range(daily.index.min(), daily.index.max())
reindexed = daily.reindex(dates)
thinkplot.Scatter(reindexed.ppg, s=15, alpha=0.2, label=name)
roll_mean = pd.Series(reindexed.ppg).roll... | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
Here's what it looks like for the high quality category. | PlotRollingMean(daily, name) | code/chap12ex.ipynb | AllenDowney/ThinkStats2 | gpl-3.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.