markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
Moreover, if we now print the sum of the word frequencies for each of our nine texts, we see that the relative values sum to 1: | print(BOW.sum(axis=1)) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
That looks great. Let us now build a model with a more serious vocabulary size (=300) for the actual cluster analysis: | vec = CountVectorizer(max_features=300, tokenizer=nltk.word_tokenize)
BOW = vec.fit_transform(texts).toarray()
BOW = BOW / BOW.sum(axis=1, keepdims=True) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
Clustering algorithms are based on essentially based on the distances between texts: clustering algorithms typically start by calculating the distance between each pair of texts in a corpus, so that they know for each text how (dis)similar it is from any other text. Only after these pairwise-distances have been calcula... | from scipy.spatial.distance import pdist, squareform | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
The function pdist() ('pairwise distances') is a function which we can use to calculate the distance between each pair of texts in our corpus. Using the squareform() function, we will eventually obtain a 9x9 matrix, the structure of which is conceptually easy to understand: this square distance matrix (named dm) will h... | dm = squareform(pdist(BOW))
print(dm.shape) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
As is clear from the shape info, we have obtained a 9 by 9 matrix, which holds the distance between each pair of texts. Note that the distance from a text to itself is of course zero (cf. diagonal cells): | print(dm[3][3])
print(dm[8][8]) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
Additionally, we can observe that the distance from text A to text B, is equal to the distance from B to A: | print(dm[2][3])
print(dm[3][2]) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
We can visualize this distance matrix as a square heatmap, where darker cells indicate a larger distance between texts. Again, we use the matplotlib package to achieve this: | import matplotlib.pyplot as plt
fig, ax = plt.subplots()
heatmap = ax.pcolor(dm, cmap=plt.cm.Blues)
ax.set_xticks(np.arange(dm.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(dm.shape[1])+0.5, minor=False)
ax.set_xticklabels(titles, minor=False, rotation=90)
ax.set_yticklabels(authors, minor=False)
plt.show() | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
As you can see, the little squares representing texts by the same author already show a tendency to invite lower distance scores. But how are these distances exactly calculated?
Each text in our distance matrix is represented as a row, consisting of 100 numbers. Such a list of numbers is also called a document vector, ... | a = [2, 5, 1, 6, 7]
b = [4, 5, 1, 7, 3] | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
Can you calculate the manhattan distance between a and b by hand? Compare the result you obtain to this line of code: | from scipy.spatial.distance import cityblock as manhattan
print(manhattan(a, b)) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
This is an example of one popular distance metric which is currently used a lot in digital text analysis. Alternatives (which might ring a bell from math classes in high school) include the Euclidean distance or cosine distance. Our dm distance matrix from above can be created with any of these option, by specifying th... | dm = squareform(pdist(BOW), 'cosine') # or 'euclidean', or 'cosine' etc.
fig, ax = plt.subplots()
heatmap = ax.pcolor(dm, cmap=plt.cm.Reds)
ax.set_xticks(np.arange(dm.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(dm.shape[1])+0.5, minor=False)
ax.set_xticklabels(titles, minor=False, rotation=90)
ax.set_yticklabel... | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
Cluster trees
Now that we have learned how to calculate the pairwise distances between texts, we are very close to the dendrogram that I promised you a while back. To be able to visualize a dendrogram, we must first figure out the (branch) linkages in the tree, because we have to determine which texts are most similar ... | from scipy.cluster.hierarchy import linkage
linkage_object = linkage(dm) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
We are now ready to draw the actual dendrogram, which we do in the following code block. Note that we annotate the outer leaf nodes in our tree (i.e. the actual texts) using the labels argument. With the orientation argument, we make sure that our dendrogram can be easily read from left to right: | from scipy.cluster.hierarchy import dendrogram
d = dendrogram(Z=linkage_object, labels=titles, orientation='right') | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
Using the authors as labels is of course also a good idea: | d = dendrogram(Z=linkage_object, labels=authors, orientation='right') | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
As we can see, Jane Austen's novels form a tight and distinctive cloud; an author like Thackeray is apparantly more difficult to tell apart. The actual distance between nodes is hinted at on the horizontal length of the branches (i.e. the values on the x-axis in this plot). Note that in this code block too we can easil... | dm = squareform(pdist(BOW, 'euclidean'))
linkage_object = linkage(dm, method='ward')
d = dendrogram(Z=linkage_object, labels=authors, orientation='right') | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
Exercise
The code repository also contains a larger folder of novels, called victorian_large. Use the code block below to copy and paste code snippets from above, which you can slightly adapt to do the following things:
1. Read in the texts, producing 3 lists of texts, authors and titles. How many texts did you load (u... | # exercise code goes here | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
Topic modeling
Up until now, we have been working with fairly small, dummy-size corpora to introduce you to some standard methods for text analysis in Python. When working with real-world data, however, we are often confronted with much larger and noisier datasets, sometimes even datasets that are too large to read or ... | import os
documents, names = [], []
for filename in sorted(os.listdir('data/newsgroups')):
try:
with open('data/newsgroups/'+filename, 'r') as f:
text = f.read()
documents.append(text)
names.append(filename)
except:
continue
print(len(documents)) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
As you can see, we are dealing with 3,551 documents. Have a look at some of the documents and try to find out what they are about. Vary the index used to select a random document and print out its first 1000 characters or so: | print(documents[3041][:1000]) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
You might already get a sense of the kind of topics that are being discussed. Also, you will notice that these are rather noisy data, which is challenging for humans to process manually. In the last part of this tutorial we will use a technique called topic modelling. This technique will automatically determine a numbe... | vec = CountVectorizer(max_df=0.95, min_df=5, max_features=2000, stop_words='english')
BOW = vec.fit_transform(documents)
print(BOW.shape) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
Note that we make use of a couple of additional bells and whistles that ship with sklearn's CountVectorizer. Can you figure out what they mean (hint: df here stands for document frequency)? In topic modelling we are not interested in the type of high-frequency grammatical words that we have used up until now. Such word... | print(vec.get_feature_names()) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
We are now ready to start modelling the topics in this text collection. For this we make use of a popular technique called Latent Dirichlet Allocation or LDA, which is also included in the sklearn library. In the code block below, you can safely ignore most of the settings which we use when we initialize the model, but... | from sklearn.decomposition import LatentDirichletAllocation
lda = LatentDirichletAllocation(n_topics=50,
max_iter=10,
learning_method='online',
learning_offset=50.,
random_state=0)
lda.fit(BOW... | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
After the model has (finally!) been fitted, we can now inspect our topics. We do this by finding out which items in our vocabulary have the highest score for each topic. The topics are available as lda.components_ after the model has been fitted. | feature_names = vec.get_feature_names()
for topic_idx, topic in enumerate(lda.components_):
print('Topic', topic_idx, '> ', end='')
print(' '.join([feature_names[i] for i in topic.argsort()[:-12 - 1:-1]])) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
Can you make sense of these topics? Which are the main thematic categories that you can discern?
DIY
Try to run the algorithm with more topics and allow more iterations (but don't exaggerate!): do the results get more interpretable?
Now that we have build a topic model, we can use it to represent our corpus. Instead ... | topic_repr = lda.transform(BOW)
print(topic_repr.shape) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
As you can see, we obtain another sort of document matrix, where the number of columns corresponds to the number of topics we extracted. Let us now find out whether this representation yields anything useful. It is difficult to visualize 3,000+ documents all at once, so in the code block below, I select a smaller subse... | comb = list(zip(names, topic_repr))
import random
random.seed(10000)
random.shuffle(comb)
comb = comb[:30]
subset_names, subset_topic_repr = zip(*comb) | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
We can now use our clustering algorithm from above in an exactly parallel way. Go on and try it (because of the random aspect of the previous code block, it possible that you obtain a different random selection). | dm = squareform(pdist(subset_topic_repr), 'cosine') # or 'euclidean', or 'cosine' etc.
linkage_object = linkage(dm, method='ward')
fig_size = plt.rcParams["figure.figsize"]
plt.rcParams["figure.figsize"] = [15, 9]
d = dendrogram(Z=linkage_object, labels=subset_names, orientation='right') | Digital Text Analysis.ipynb | mikekestemont/leyden-workshop | mit |
Tymoshenko theory
$u_1 \left( \alpha_1, \alpha_2, \alpha_3 \right)=u\left( \alpha_1 \right)+\alpha_3\gamma \left( \alpha_1 \right) $
$u_2 \left( \alpha_1, \alpha_2, \alpha_3 \right)=0 $
$u_3 \left( \alpha_1, \alpha_2, \alpha_3 \right)=w\left( \alpha_1 \right) $
$ \left(
\begin{array}{c}
u_1 \
\frac { \partial u_1 } {... | T=zeros(12,6)
T[0,0]=1
T[0,2]=alpha3
T[1,1]=1
T[1,3]=alpha3
T[3,2]=1
T[8,4]=1
T[9,5]=1
T
B=Matrix([[0, 1/(A*(K*alpha3 + 1)), 0, 0, 0, 0, 0, 0, K/(K*alpha3 + 1), 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1/(A*(K*alpha3 + 1)), 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0... | py/notebooks/.ipynb_checkpoints/LinearSolShellsFEM-checkpoint.ipynb | tarashor/vibrations | mit |
Cartesian coordinates | import fem.geometry as g
import fem.model as m
import fem.material as mat
import fem.shell.shellsolver as s
import fem.shell.mesh1D as me
import plot
stiffness_matrix_func = lambdify([A, K, mu, la, h], S_in, "numpy")
mass_matrix_func = lambdify([A, K, rho, h], M_in, "numpy")
def stiffness_matrix(material, geometry, ... | py/notebooks/.ipynb_checkpoints/LinearSolShellsFEM-checkpoint.ipynb | tarashor/vibrations | mit |
Ejercicio
Simplifica los cocientes entre factoriales:
- $\frac{7!}{6!}$
- $\frac{{8!}}{{9!}}$
- $\frac{{9!}}{{5!.4!}}$
- $\frac{{m!}}{{(m - 1)!}}$
- $\frac{{\left( {m + 1} \right)!}}{{\left( {m - 1} \right)!}}$ | enunciado = list([r'\frac{7!}{6!}',r'\frac{{8!}}{{9!}}',r'\frac{{9!}}{{5!\cdot 4!}}',r'\frac{{m!}}{{(m - 1)!}}', r'\frac{{( {m + 1} )!}}{{( {m - 1} )!}}'])
enunciado
enunciado = list([r'\frac{7!}{6!}',r'\frac{{8!}}{{9!}}',r'\frac{{9!}}{{5!\cdot 4!}}',r'\frac{{m!}}{{(m - 1)!}}', r'\frac{{( {m + 1} )!}}{{( {m - 1} )!}}'... | tmp/Ejercicios 1_3.ipynb | crdguez/mat4ac | gpl-3.0 |
Ejercicio
Calcula las siguientes operaciones:
- $\binom{252}{250}$
- $\binom{25}{3} + \binom{25}{4} = \binom{26}{4}$
- $\binom{9}{6} + \binom{9}{7} + \binom{10}{2}=\binom{10}{7}+\binom{10}{8}=\binom{11}{8}$
- $\binom{4}{2} + \binom{4}{3} + \binom{5}{4}+\binom{6}{5} + \binom{7}{6} + \binom{8}{7}=\binom{9}{7}$
- $\bino... | from sympy.functions.combinatorial.numbers import nC, nP, nT
nC(5,3)
from sympy import *
expr = sympify("nC(5,3)")
display(expr.expand())
enunciado = [[252,250], [25,3], [25,4]]
for i in range(len(enunciado)):
display(nC(enunciado[i][0],enunciado[i][1]))
nC(enunciado[0][0],enunciado[0][1])
factorial(252)/(fac... | tmp/Ejercicios 1_3.ipynb | crdguez/mat4ac | gpl-3.0 |
Set up our toy problem (1D optimisation of the forrester function) and collect 3 initial points. | target_function, space = forrester_function()
x_plot = np.linspace(space.parameters[0].min, space.parameters[0].max, 200)[:, None]
y_plot = target_function(x_plot)
X_init = np.array([[0.2],[0.6], [0.9]])
Y_init = target_function(X_init)
plt.figure(figsize=(12, 8))
plt.plot(x_plot, y_plot, "k", label="Objective Function... | notebooks/Emukit-tutorial-Max-Value-Entropy-Search-Example.ipynb | EmuKit/emukit | apache-2.0 |
Fit our GP model to the observed data. | gpy_model = GPy.models.GPRegression(X_init, Y_init, GPy.kern.RBF(1, lengthscale=0.08, variance=20), noise_var=1e-10)
emukit_model = GPyModelWrapper(gpy_model)
| notebooks/Emukit-tutorial-Max-Value-Entropy-Search-Example.ipynb | EmuKit/emukit | apache-2.0 |
Lets plot the resulting acqusition functions for the chosen model on the collected data. Note that MES takes a fraction of the time of ES to compute (plotted on a log scale). This difference becomes even more apparent as you increase the dimensions of the sample space. | ei_acquisition = ExpectedImprovement(emukit_model)
es_acquisition = EntropySearch(emukit_model,space)
mes_acquisition = MaxValueEntropySearch(emukit_model,space)
t_0=time.time()
ei_plot = ei_acquisition.evaluate(x_plot)
t_ei=time.time()-t_0
es_plot = es_acquisition.evaluate(x_plot)
t_es=time.time()-t_ei
mes_plot = mes_... | notebooks/Emukit-tutorial-Max-Value-Entropy-Search-Example.ipynb | EmuKit/emukit | apache-2.0 |
<table align="left">
<td>
<a href="https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/automl/automl-tabular-classification.ipynb"">
<img src="https://cloud.google.com/ml-engine/images/colab-logo-32px.png" alt="Colab logo"> Run in Colab
</a>
<... | import os
# The Google Cloud Notebook product has specific requirements
IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists("/opt/deeplearning/metadata/env_version")
# Google Cloud Notebook requires dependencies to be installed with '--user'
USER_FLAG = ""
if IS_GOOGLE_CLOUD_NOTEBOOK:
USER_FLAG = "--user" | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Install the latest version of the Vertex AI client library.
Run the following command in your virtual environment to install the Vertex SDK for Python: | ! pip install {USER_FLAG} --upgrade google-cloud-aiplatform | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Install the Cloud Storage library: | ! pip install {USER_FLAG} --upgrade google-cloud-storage | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Before you begin
Set up your Google Cloud project
The following steps are required, regardless of your notebook environment.
Select or create a Google Cloud project. When you first create an account, you get a $300 free credit towards your compute/storage costs.
Make sure that billing is enabled for your project.
... | PROJECT_ID = ""
# Get your Google Cloud project ID from gcloud
if not os.getenv("IS_TESTING"):
shell_output = !gcloud config list --format 'value(core.project)' 2>/dev/null
PROJECT_ID = shell_output[0]
print("Project ID: ", PROJECT_ID) | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Authenticate your Google Cloud account
If you are using Notebooks, your environment is already
authenticated. Skip this step.
If you are using Colab, run the cell below and follow the instructions
when prompted to authenticate your account via oAuth.
Otherwise, follow these steps:
In the Cloud Console, go to the Crea... | import os
import sys
# If you are running this notebook in Colab, run this cell and follow the
# instructions to authenticate your GCP account. This provides access to your
# Cloud Storage bucket and lets you submit training jobs and prediction
# requests.
# The Google Cloud Notebook product has specific requirements... | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Create a Cloud Storage bucket
The following steps are required, regardless of your notebook environment.
This notebook demonstrates how to use Vertex AI SDK for Python to create an AutoML model based on a tabular dataset. You will need to provide a Cloud Storage bucket where the dataset will be stored.
Set the name of ... | BUCKET_NAME = "gs://[your-bucket-name]" # @param {type:"string"}
REGION = "[your-region]" # @param {type:"string"}
if BUCKET_NAME == "" or BUCKET_NAME is None or BUCKET_NAME == "gs://[your-bucket-name]":
BUCKET_NAME = "gs://" + PROJECT_ID + "aip-" + TIMESTAMP | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Copy dataset into your Cloud Storage bucket | IMPORT_FILE = "petfinder-tabular-classification.csv"
! gsutil cp gs://cloud-samples-data/ai-platform-unified/datasets/tabular/{IMPORT_FILE} {BUCKET_NAME}/data/
gcs_source = f"{BUCKET_NAME}/data/{IMPORT_FILE}" | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Import Vertex SDK for Python
Import the Vertex SDK into your Python environment and initialize it. | import os
from google.cloud import aiplatform
aiplatform.init(project=PROJECT_ID, location=REGION) | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Launch a Training Job to Create a Model
Once we have defined your training script, we will create a model. The run function creates a training pipeline that trains and creates a Model object. After the training pipeline completes, the run function returns the Model object. | job = aiplatform.AutoMLTabularTrainingJob(
display_name="train-petfinder-automl-1",
optimization_prediction_type="classification",
column_transformations=[
{"categorical": {"column_name": "Type"}},
{"numeric": {"column_name": "Age"}},
{"categorical": {"column_name": "Breed1"}},
... | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Deploy your model
Before you use your model to make predictions, you need to deploy it to an Endpoint. You can do this by calling the deploy function on the Model resource. This function does two things:
Creates an Endpoint resource to which the Model resource will be deployed.
Deploys the Model resource to the Endpoi... | endpoint = model.deploy(
machine_type="n1-standard-4",
) | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Predict on the endpoint
This sample instance is taken from an observation in which Adopted = Yes
Note that the values are all strings. Since the original data was in CSV format, everything is treated as a string. The transformations you defined when creating your AutoMLTabularTrainingJob inform Vertex AI to transform ... | prediction = endpoint.predict(
[
{
"Type": "Cat",
"Age": "3",
"Breed1": "Tabby",
"Gender": "Male",
"Color1": "Black",
"Color2": "White",
"MaturitySize": "Small",
"FurLength": "Short",
"Vaccinated": "N... | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Undeploy the model
To undeploy your Model resource from the serving Endpoint resource, use the endpoint's undeploy method with the following parameter:
deployed_model_id: The model deployment identifier returned by the prediction service when the Model resource is deployed. You can retrieve the deployed_model_id using... | endpoint.undeploy(deployed_model_id=prediction.deployed_model_id) | notebooks/official/automl/automl-tabular-classification.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Overview: Deploying the Dress Recommender
Let's say you want to make an app which can recommend dresses to you based on a photo you took.
You need a way to deploy the model previously built. Turi Predictive Services helps do this in an easy and scalable way. In this notebook, we demonstrate how do that for the dress r... | if os.path.exists('dress_sf_processed.sf'):
reference_sf = graphlab.SFrame('dress_sf_processed.sf')
else:
reference_sf = graphlab.SFrame('https://static.turi.com/datasets/dress_sf_processed.sf')
reference_sf.save('dress_sf_processed.sf')
if os.path.exists('dress_nn_model'):
nn_model = graphlab.load_mod... | strata-sj-2016/ml-in-production/deploy-dress-recommender.ipynb | turi-code/tutorials | apache-2.0 |
Load an already created service | import graphlab as gl
ps = gl.deploy.predictive_service.load(TBD)
ps
#ps.add('dress_similar', dress_similar)
#ps.update('dress_similar', dress_similar)
ps.apply_changes() | strata-sj-2016/ml-in-production/deploy-dress-recommender.ipynb | turi-code/tutorials | apache-2.0 |
Query via REST
Query from anywhere. Here, we issue a request via the requests library, and convert the returning JSON back into an SFrame. This could easily be done from outside of Python, though. | import json
import requests
from requests.auth import HTTPBasicAuth
def restful_query(x):
headers = {'content-type': 'application/json'}
payload = {'data': {'url': url} }
end_point = 'http://TBD/query/dress_similar'
return requests.post(
end_point,
json.dumps(payload),
headers=h... | strata-sj-2016/ml-in-production/deploy-dress-recommender.ipynb | turi-code/tutorials | apache-2.0 |
We'll use 100 inducing points | M = 100
Z = kmeans2(X, M, minit='points')[0] | demos/demo_mnist.ipynb | ICL-SML/Doubly-Stochastic-DGP | apache-2.0 |
We'll compare three models: an ordinary sparse GP and DGPs with 2 and 3 layers.
We'll use a batch size of 1000 for all models | m_sgp = SVGP(X, Y, RBF(784, lengthscales=2., variance=2.), MultiClass(10),
Z=Z, num_latent=10, minibatch_size=1000, whiten=True)
def make_dgp(L):
kernels = [RBF(784, lengthscales=2., variance=2.)]
for l in range(L-1):
kernels.append(RBF(30, lengthscales=2., variance=2.))
model = DGP(X... | demos/demo_mnist.ipynb | ICL-SML/Doubly-Stochastic-DGP | apache-2.0 |
For the SGP model we'll calcuate accuracy by simply taking the max mean prediction: | def assess_model_sgp(model, X_batch, Y_batch):
m, v = model.predict_y(X_batch)
l = model.predict_density(X_batch, Y_batch)
a = (np.argmax(m, 1).reshape(Y_batch.shape).astype(int)==Y_batch.astype(int))
return l, a | demos/demo_mnist.ipynb | ICL-SML/Doubly-Stochastic-DGP | apache-2.0 |
For the DGP models we have stochastic predictions. We need a single prediction for each datum, so to do this we take $S$ samples for the one-hot predictions ($(S, N, 10)$ matrices for mean and var), then we take the max over the class means (to give a $(S, N)$ matrix), and finally we take the modal class over the sampl... | S = 100
def assess_model_dgp(model, X_batch, Y_batch):
m, v = model.predict_y(X_batch, S)
l = model.predict_density(X_batch, Y_batch, S)
a = (mode(np.argmax(m, 2), 0)[0].reshape(Y_batch.shape).astype(int)==Y_batch.astype(int))
return l, a | demos/demo_mnist.ipynb | ICL-SML/Doubly-Stochastic-DGP | apache-2.0 |
We need batch predictions (we might run out of memory otherwise) | def batch_assess(model, assess_model, X, Y):
n_batches = max(int(len(X)/1000), 1)
lik, acc = [], []
for X_batch, Y_batch in zip(np.split(X, n_batches), np.split(Y, n_batches)):
l, a = assess_model(model, X_batch, Y_batch)
lik.append(l)
acc.append(a)
lik = np.concatenate(lik, 0)
... | demos/demo_mnist.ipynb | ICL-SML/Doubly-Stochastic-DGP | apache-2.0 |
Now we're ready to go
The sparse GP: | iterations = 20000
AdamOptimizer(0.01).minimize(m_sgp, maxiter=iterations)
l, a = batch_assess(m_sgp, assess_model_sgp, Xs, Ys)
print('sgp test lik: {:.4f}, test acc {:.4f}'.format(l, a)) | demos/demo_mnist.ipynb | ICL-SML/Doubly-Stochastic-DGP | apache-2.0 |
Using more inducing points improves things, but at the expense of very slow computation (500 inducing points takes about a day)
The two layer DGP: | AdamOptimizer(0.01).minimize(m_dgp2, maxiter=iterations)
l, a = batch_assess(m_dgp2, assess_model_dgp, Xs, Ys)
print('dgp2 test lik: {:.4f}, test acc {:.4f}'.format(l, a)) | demos/demo_mnist.ipynb | ICL-SML/Doubly-Stochastic-DGP | apache-2.0 |
And the three layer: | AdamOptimizer(0.01).minimize(m_dgp3, maxiter=iterations)
l, a = batch_assess(m_dgp3, assess_model_dgp, Xs, Ys)
print('dgp3 test lik: {:.4f}, test acc {:.4f}'.format(l, a)) | demos/demo_mnist.ipynb | ICL-SML/Doubly-Stochastic-DGP | apache-2.0 |
<p style="color:darkred;"> <b>Figyelem, a második sort beljebb húztuk!</b> A behúzás a Python jelölése az utasítások csoportosítására! </p>
A behúzás mértékére általánosan bevett szokás, hogy a szintaktikailag alacsonyabb szintű programrészek négy szóközzel beljebb vannak tolva. Ha tehát két if utasítást ágyazunk egymá... | today='Monday';
time='12:00';
if today=='Monday':
if time=='12:00':
print('Nyomassuk a pythont!') | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Bonyolultabb kritériumszerkezetet az else és elif parancsok segítségével konstruálhatunk: | x = 1
if x < 0:
x = 0
print('Negatív, lecseréltem nullára')
elif x == 0:
print('Nulla')
elif x == 1:
print('Egy')
else:
print('Egynél több.') | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Hiányozhat, de lehet egy vagy akár egynél több elif rész, az else rész szintén elmaradhat. Az elif kulcsszó – amely az ‘else if’ rövidítése – hasznos a felesleges behúzások elkerülésre. Egy if ...elif ... elif ... sor helyettesíti a más nyelvekben található switch és case utasításokat.
A for utasítás
A számítógépek leg... | days_of_the_week = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
for day in days_of_the_week:
print(day) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Ez a kódrészlet a days_of_the_week listán megy végig, és a meglátogatott elemet hozzárendeli a day változóhoz, amit ciklusváltozónak is neveznek. Ezek után mindent végrehajt, amit a beljebb tabulált (angolul: indented) parancsblokkban írtunk (most csak egy print utasítás), amihez felhasználhatja a ciklusváltozót is. Mi... | for macska in days_of_the_week:
print(macska) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
A ciklus utasításblokkja állhat több utasításból is: | for day in days_of_the_week:
statement = "Today is " + day
print(statement) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
A range() parancs remekül használható, ha a for ciklusban adott számú műveletet szeretnénk elvégezni: | for i in range(20):
print(i," szer ",i ,"az pontosan ",i*i) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Akkor válik mindez még érdekesebbé, ha az eddig tanult iterációt és feltételvizsgálatot kombináljuk: | for day in days_of_the_week:
statement = "Today is " + day
print(statement)
if day == "Sunday":
print (" Sleep in")
elif day == "Saturday":
print (" Do chores")
else:
print (" Go to work") | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Figyeljük meg, a fenti példában hogy ágyazódik egymás alá a for és az if!
Egy példaprogram: Fibbonacci-sorozat
A Fibonacci-sorozat első két eleme 0 és 1, majd a következő elemet mindig az előző kettő összegéből számoljuk ki: 0,1,1,2,3,5,8,13,21,34,55,89,...
Ha nagyobb $n$ értékekre is ki akarjuk számolni a sorozatot, ... | n = 10 # ennyi elemet szeretnénk meghatározni
sequence = [0,1] # az első két elem
for i in range(2,n): # számok 2-től n-ig, Figyelni kell, hogy n ne legyen kisebb mint 2!!
sequence.append(sequence[i-1]+sequence[i-2])
print (sequence) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Nézzük végig lépésről lépésre! Először $n$ értékét, azaz a kiszámolandó sorozat hosszát állítjuk be 10-re. A sorozatot majdan tároló listát sequence-nek neveztük el, és inicializáltuk az első két értékkel. A "kézi munka" után következhet a gép automatikus munkája, az iteráció.
Az iterációt 2-vel kezdjük (ez ugye a 0-s ... | def fibonacci(sequence_length):
"A Fibonacci sorozat elso *sequence_length* darab eleme" # ez csak a 'help'-hez kell
sequence = [0,1]
if 0 < sequence_length < 3:
return sequence[:sequence_length]
for i in range(2,sequence_length):
sequence.append(sequence[i-1]+sequence[i-2])
return ... | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Most már meghívhatjuk a fibonacci() függvényt különböző hosszakra: | fibonacci(5) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Elemezzük a fenti kódot! A már megszokott módon a kettőspont és behúzás határozza meg a függvénydefinícióhoz tartozó kódblokkot.
A 2. sorban idézőjelek közt szerepel a "docstring", ami a függvény működését magyarázza el röviden, és később a help paranccsal hívható elő: | help(fibonacci) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
A notebookos környezetben a docstring a ? segítségével is elérhető: | ?fibonacci | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
A docstring-et jupyter környezetben úgy is megtekinthetjük, ha egyes függvények hasában, azaz a zárójelek között SHIFT+TAB-ot nyomunk. Próbáld ki ezt az alábbi cellán (annélkül hogy lefuttatnád azt)! | fibonacci() | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
A függvény kimenetelét a return kulcsszó határozza meg. Ha a fügvény definiálása során nem használtunk return utasítást akkor a függvény None (semmi) értéket ad vissza. Ha egy függvény lefutott, és nem hajtott végre return utasítást, akkor is None értékkel tér vissza. A fibonacci fügvény például egy listát ad vissza: | x=fibonacci(10)
x | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Ez a függvény viszont nem tér vissza sehol: | def ures_fuggveny(x):
print('Én egy ures fuggvény vagyok,\nannak ellenére hogy beszélek,\nnem térek vissza változóval!!')
y=x-2; | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Így a z válltozóban nem tárolódik semmilyen érték! | z=ures_fuggveny(3)
z
print(z) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Egy függvénynek lehet több bemeneti változója is: | def osszead(a,b):
return a+b | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Az is előfordulhat, hogy több értéket szeretnénk visszakapni egy függvényből. Ezt például az alábbiak alapján tehetjük meg: | def plusminus(a,b):
return a+b,a-b
p,m=plusminus(2,3)
print (p)
print (m) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Paraméter lista és a "kicsomagolás"
Előfordulhat, hogy egy függvénynek sok bemenő paramétere van, vagy hogy egy függvény bemenő paramétereit egy másik függvény eleve egy listába rendezi. Egy tipikus ilyen példa, amint azt későb látni fogjuk, a függvényillesztés esete. Ilyenkor a paramétereket tartalmazó lista "kicsomag... | # ez lesz az illesztendő függvény
def poly5(x,a0,a1,a2,a3,a4,a5):
return a0+a1*x+a2*x**2+a3*x**3+a4*x**4+a5*x**5 | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Az illesztés során hat darab illesztési paramétert határozunk meg: $a_0,a_1,a_2,a_3,a_4,a_5$ ám ezeket az illesztő program egy listába rendezve adja a kezünkbe: | # ezek az illesztés során meghatározott paraméterek
# az alábbi sorrendnek megfelelően
# params=[a0,a1,a2,a3,a4,a5]
params=[ 2.27171539, -1.1368942 , 0.65380304, -0.25005187, -0.1751268 , -0.48828309]; | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Ha ki szeretnénk értékelni az illesztett polinomot az $x=0.3$ helyen, akkor azt megtehetjük az alábbi módon: | poly5(0.3,params[0],params[1],params[2],params[3],params[4],params[5]) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
vagy az ennél sokkal kompaktabb módszerrel: | poly5(0.3,*params) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Ez a konstrukció lehetővé teszi, hogy a függvény definiálása során felkészísük a függvényt arra, hogy a bemenő
paraméterek száma ne legyen rögzített. Ha a függvény deklaráció során egy paraméter elé *-t teszünk akkor az a paraméter tetszőleges hosszúságú lehet! Vizsgáljuk meg az alábbi példát: | def adok_mit_kapok(*argv): #Így, a *-al, készítünk fel egy függvényt
#válltozó számú paraméter fogadására
print("Nekem ",len(argv),"db bemenő paraméterem jött")
for arg in argv:
print ("Ez e... | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
A fenti blokkban definiált függvény tetszőleges számú bemenő paramétert elfogad! A futás során közli, hogy hány paraméter érkezett, kiírja azokat, illetve a paraméterlista utolsó tagját mint a függvény vissza térési értékét állítja be. | adok_mit_kapok('Gáspár','Menyhért','Boldizsár') | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Természetesen a tetszőleges paraméter helyett egy "kicsomagolt" tetszőleges hosszúságú listát is használhatunk! | adok_mit_kapok(*params) #természetesen itt is működik a kicsomagolás.. | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Függvények kulcsszavakkal
Azon kívül, hogy a szótárak már önmagukban is igen hasznos adatstruktúrák, később látni fogjuk, hogy sokszor függvények bizonyos paramétereit is szokás szótárakba szedni. Az ilyen paramétereket szokás kulcsszavas változóknak vagy kulcsszavas argumentumnak (angol nyelven keyword argument) hívni... | #Így adunk meg alapértelmezett értékeket
def students(ido, allapot='lelkesen figyelik a tanárt', tevekenyseg='kísérletezés', ora='fizika'):
print("Ezek a diákok "+ora+"órán "+tevekenyseg+" közben mindig "+allapot+"!");
print("Még akkor is, ha épp ",ido,"-t mutat az óra!"); | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
A függvény első paraméterét meg kell adnunk, ha nem adunk többet, akkor az alapértelmezett értékek ugranak be: | students('17:00') | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Ha egy kulcsszavas argumentumot kap a függvény, akkor azt értelemszerűen használja: | students('8:00',allapot='unott képet vágnak') | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
A kulcsszavas argumentumok sorrendjére nem kell figyelni: | students('8:00',ora='ógörög',allapot='pánikolva izzadnak',tevekenyseg='TÉMAZÁRÓ') | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Ha a deklaráció során nem használt kulcsszót adunk meg, akkor hibát kapunk: | students('17:00',tanar='Mici néni') | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Hasonlóan problémába ütközünk, ha egy kulcsszót kétszer is alkalmazunk: | students('8:00',ora='ógörög',ora='kémia') | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Kulcsszavas argumentumok szótárának kicsomagolása **-jel segítségével történik: | diak_hozzaallas={'ora':'ének','allapot':'nyüszítenek'};
students(12,**diak_hozzaallas) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Amint a sima paramétereknél is, itt is előfordulhat az, hogy tetszőleges hosszúságú dict-et szeretnénk feldolgozni. Erre példa, ha egy olyan függvényt írunk, amely esetleg több más fügvényt hív, melyeknek tovább akarjuk adni a bejövő paraméterek egy részét.
Az alábbi függvény egy tetszőleges szótárat vár a bemenetre, ... | def kulcsot_adok_amit_kapok(**szotar):
print('A szotár hossza:',len(szotar))
for kulcs in list(szotar.keys()):
if kulcs=='hamburger':
print('Van hamburger!')
return szotar[kulcs]
kulcsot_adok_amit_kapok(makaróni=1,torta='finom') #itt már nem jelent hibát ha előre meg nemhatározo... | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Általános függvénydeklarációs szokások
Amint azt a fentiekben láttuk, függvényeknek többféleképpen is adhatunk paramétereket,
változók (ezek lehetnek sima változók, meghatározott hosszúságú listák, vagy akár kulcsszavas változók is)
előre meg nem határozott hosszúságú változó lista
kulcsszavas változók tetszőleges ho... | def bonyolult_fuggveny(valtozo1,valtozo2,valtozo3='ELZETT',*args,**kwargs):
if ((len(args)==0 and len(kwargs)==0)):
return valtozo3+str(valtozo2)+str(valtozo1)
elif (len(args)!=0 and len(kwargs)==0):
return 'Van valami az args-ban!'
elif (len(args)==0 and len(kwargs)!=0):
return 'Van... | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
A fenti függvény első két változója "sima" változó, a harmadik egy kulcsszavas változó az alapértelmezett 'ELZETT' értékkel, és ezen kívül megengedünk még egyéb tetszőleges hosszú "sima" változók listáját (args), illetve tetszőleges hosszú kulcsszavas változók listáját (kwargs). Nézzük meg hogy a notebook során korábba... | bonyolult_fuggveny(1,2)
bonyolult_fuggveny(1,2,valtozo3='MULTLOCK')
bonyolult_fuggveny(1,2,*days_of_the_week)
bonyolult_fuggveny(1,2,**kaja)
bonyolult_fuggveny(1,2,*days_of_the_week,**kaja) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
A Lambda-formák ☠
Egy függvénynek nemcsak változókat, hanem más függvényeket is megadhatunk bemenetként. Például gondolhatunk egy olyan függvényre, ami egy matematikai függvényt ábrázol!
Ilyen esetekben, amikor egy függvény a bemenetére másik függvényt vár, sokszor előfordul, hogy hosszadalmas külön definiálni a bemen... | def funfun(g,x):
print('Ez volt az x változó: ',x)
return g(x)
def fx(x):
return x**2-1/x;
funfun(fx,0.1) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Ekkor egy úgynevezett lambda-forma segítségével alkalmazhatjuk az ennél kicsivel kompaktabb kifejezést (megúszva a függvénydeklarációt): | funfun(lambda x:x**2-1/x,0.1) | notebooks/Package02/mintapelda02.ipynb | oroszl/szamprob | gpl-3.0 |
Data retrieval
Files
Remote files can be downloaded directly from python. Depending on file format they can also be opened and read directly from python.
For python files are either text or binary, opened with 'b' binary mode. Python looks for line endings when reading text files: \n on Unix, \r\n on Windows.
open(file... | # An arbitrary collection of objects
data1 = {
'a': [1, 2.0, 3, 4+6j],
'b': ("character string", b"byte string"),
'c': {None, True, False}
}
print(data1)
# write pickled data
with open('data.pickle', 'wb') as f:
pickle.dump(data1, f)
# reads the resulting pickled data
with open('data.pickle', 'rb') as... | Wk04-Data-retrieval-and-preprocessing.ipynb | streety/biof509 | mit |
Retrieving remote data | ICGC_API = 'https://dcc.icgc.org/api/v1/download?fn=/release_18/Projects/BRCA-US/'
expression_fname = 'protein_expression.BRCA-US.tsv.gz'
if not Path(expression_fname).is_file():
print("Downloading file", ICGC_API + expression_fname, "saving it as", expression_fname)
urllib.request.urlretrieve(ICGC_API + expre... | Wk04-Data-retrieval-and-preprocessing.ipynb | streety/biof509 | mit |
Connecting to a remote database server
SQL databases are convenient for storing and accessing data that requires concurrent access and control of integrity.
Example: UCSC Genomes database http://genome.ucsc.edu/cgi-bin/hgTables
We use SQLAlchemy package and pymysql MySQL driver, which has the following major objects:
... | engine = sa.create_engine('mysql+pymysql://genome@genome-mysql.cse.ucsc.edu/hg38', poolclass=sa.pool.NullPool) | Wk04-Data-retrieval-and-preprocessing.ipynb | streety/biof509 | mit |
The connection is an instance of Connection, which is a proxy object for an actual DBAPI connection. The DBAPI connection is retrieved from the connection pool at the point at which Connection is created. | connection = engine.connect()
result = connection.execute("SHOW TABLES")
for row in result:
print("Table:", row[0])
connection.close()
# Connection supports context manager
with engine.connect() as connection:
result = connection.execute("DESCRIBE refGene")
for row in result:
print("Columns:", row)... | Wk04-Data-retrieval-and-preprocessing.ipynb | streety/biof509 | mit |
Pandas can read data directly from the database | snp_table = sa.Table('snp147Common',
meta,
sa.PrimaryKeyConstraint('name'),
extend_existing=True)
# Getting data into pandas:
import pandas as pd
expr = sa.select([snp_table]).where(snp_table.c.chrom == 'chrY').limit(5)
pd.read_sql(expr, engine) | Wk04-Data-retrieval-and-preprocessing.ipynb | streety/biof509 | mit |
Download the dataset
This next chunk of code will download the face images dataset we're going to use for this tutorial.
Then, it will convert these images to numpy array, so dlib can understand it, and append each one of them to a list.
Finally, in the last line, this list is converted into a larger numpy array contai... | url_list = [
'http://fei.edu.br/~cet/frontalimages_spatiallynormalized_part1.zip',
'http://fei.edu.br/~cet/frontalimages_spatiallynormalized_part2.zip',
]
archive = [ZipFile(urlretrieve(url)[0], 'r') for url in url_list]
images = [image for zipfile in archive for image in zipfile.namelist()]
face_db = []
for ... | my_notebooks/facial_landmarks.ipynb | ddfabbro/ipython_tutorial | mit |
Download and extract landmarks predictor
Next, we need to download the trained model which is able to predict the location of each of the 68 landmarks in a face image.
Since I don't want this tutorial to have any additional step other than the code available here, the following script will automatically download the fi... | url = 'http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2'
filepath = urlretrieve(url)[0]
data = bz2.BZ2File(filepath).read()
with open(filepath, 'wb') as f:
f.write(data)
print(filepath) | my_notebooks/facial_landmarks.ipynb | ddfabbro/ipython_tutorial | mit |
Create the landmarks dataset
With the trained model in hands ~~(hope it didn't take long to download)~~, we can build our landmarks dataset for each face image accordingly.
First, we need to define the face detector using dlib.get_frontal_face_detector(), and then, we specify the landmarks predictor using dlib.shape_pr... | detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(filepath)
landmarks_db = []
for face in face_db:
rect = detector(face)[0]
shape = predictor(face, rect)
landmarks = np.array([[p.x, p.y] for p in shape.parts()])
landmarks_db.append(landmarks)
landmarks_db = np.array(landmark... | my_notebooks/facial_landmarks.ipynb | ddfabbro/ipython_tutorial | mit |
Results
So lets recap. We have a dataset containing face images and another dataset contained 68 landmarks coordinates for each face.
This last chunk of code shows how to plot a sample of 15 faces with landmarks annotated. | def plot_landmarks(image,vtk):
plt.imshow(image,cmap='gray',origin="lower")
plt.scatter(vtk[:,0],vtk[:,1],marker='+',color='w')
plt.xlim([0,image.shape[1]])
plt.ylim([0,image.shape[0]])
plt.gca().invert_yaxis()
plt.axis('off')
np.random.seed(1)
fig = plt.figure(figsize=(20.,14.7))
fig.subpl... | my_notebooks/facial_landmarks.ipynb | ddfabbro/ipython_tutorial | mit |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.