markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
centre
Este método recibe una lista de puntos, y devuelve el punto que está en el centro del polígono que forman dichos puntos. Las coordenadas de los puntos deben ser rational. En caso de no pasar una lista de puntos rational, devuelve None. Ejemplos: | points=[(rational(4,5),rational(1,2)),(rational(4,2),rational(3,1)),(rational(8,3),rational(3,5)),(rational(7,2),rational(4,5)),
(rational(7,9),rational(4,9)),(rational(9,8),rational(10,7))]
point=Simplex.centre(points)
print("("+str(point[0])+","+str(point[1])+")")
# Si recibe algo que no es una lista de punt... | Documentation/Tutorial librería Simplex.py.ipynb | carlosclavero/PySimplex | gpl-3.0 |
isThePoint
Este método recibe una lista de puntos, cuyas coordenadas son rational, un valor, que es el cálculo de la distancia al centro, y el centro de los puntos de la lista. El método devuelve el punto de la lista cuya distancia al centro, es el valor introducido. Si ningún punto, cumple la distancia devuelve None. ... | listPoints=[(rational(4,5),rational(1,2)),(rational(4,2),rational(3,1)),(rational(8,3),rational(3,5)),(rational(7,2)
,rational(4,5)),(rational(7,9),rational(4,9)),(rational(9,8),rational(10,7))]
M = (1.811574074074074,1.1288359788359787)
value = 2.7299657524245156
point=Simplex.isThePoint(listPoints, value, M)... | Documentation/Tutorial librería Simplex.py.ipynb | carlosclavero/PySimplex | gpl-3.0 |
calculateOrder
Este método recibe una lista de puntos, cuyas coordenadas son rational, y devuelve la misma lista de puntos, pero ordenadas en sentido horario. En caso de no introducir una lista de rational, devuelve None. Ejemplos: | listPoints=[(rational(4,5),rational(1,2)),(rational(4,2),rational(3,1)),(rational(8,3),rational(3,5)),(rational(7,2),
rational(4,5)), (rational(7,9),rational(4,9)),(rational(9,8),rational(10,7))]
Simplex.calculateOrder(listPoints)
# Si recibe algo que no es una lista de puntos con coordenadas rational
listPoi... | Documentation/Tutorial librería Simplex.py.ipynb | carlosclavero/PySimplex | gpl-3.0 |
pointIsInALine
Este método recibe un punto en una tupla, una restricción sin signos ni recursos en un array de numpy, y el recurso, como un número. El método devuelve True, si el punto, esta sobre la línea que representa la restricción en el plano, en otro caso devuelve False. En caso de que los parámetros introducidos... | # Si el punto está en la línea, devuelve True
point = (3,4)
line = np.array([3,2])
resource = 17
Simplex.pointIsInALine(point, line, resource)
# El método funciona con rational
point = (rational(3,1),rational(4,2))
line = np.array([rational(3,3),rational(2,1)])
resource = rational(7,1)
Simplex.pointIsInALine(point, li... | Documentation/Tutorial librería Simplex.py.ipynb | carlosclavero/PySimplex | gpl-3.0 |
deleteLinePointsOfList
Este método recibe un conjunto de puntos en una lista, un array de numpy con un conjunto de restricciones sin signos, ni recursos, y un array de numpy con los recursos de las restricciones. El método devuelve la lista de puntos, pero sin aquellos puntos que están en la línea que representa alguna... | # Elimina el último punto que está en una línea
listPoints=[(rational(3,1),rational(5,7)),(rational(5,8),rational(6,2)),(rational(4,6),rational(8,9)),(rational(8,1),
rational(2,1))]
matrix=np.array([[rational(2,1),rat... | Documentation/Tutorial librería Simplex.py.ipynb | carlosclavero/PySimplex | gpl-3.0 |
showProblemSolution
Este método resuelve el problema de programación lineal que se le pasa por parámetro, de manera gráfica. Para ello, recibe una matriz de numpy que contiene las restricciones, sin signos ni recursos, un array de numpy que contiene los recursos, una lista de strings, que contienen los signos de las re... | %matplotlib inline
matrix=np.matrix([[rational(2,1),rational(1,1)],[rational(1,1),rational(-1,1)],[rational(5,1),rational(2,1)]])
resources=np.array([rational(18,1),rational(8,1),rational(0,1)])
signs=["<=","<=",">="]
function="max 2 1"
save= False
Simplex.showProblemSolution(matrix, resources, signs, function, save)
... | Documentation/Tutorial librería Simplex.py.ipynb | carlosclavero/PySimplex | gpl-3.0 |
Fitting a decaying oscillation
For this problem you are given a raw dataset in the file decay_osc.npz. This file contains three arrays:
tdata: an array of time values
ydata: an array of y values
dy: the absolute uncertainties (standard deviations) in y
Your job is to fit the following model to this data:
$$ y(t) = A ... | data=np.load('decay_osc.npz')
tdata=data['tdata']
ydata=data['ydata']
dy=data['dy']
tdata,ydata,dy
plt.plot(tdata,ydata)
plt.errorbar?
plt.errorbar(tdata,ydata,dy,fmt='k.')
assert True # leave this to grade the data import and raw data plot | assignments/assignment12/FittingModelsEx02.ipynb | rvperry/phys202-2015-work | mit |
Now, using curve_fit to fit this model and determine the estimates and uncertainties for the parameters:
Print the parameters estimates and uncertainties.
Plot the raw and best fit model.
You will likely have to pass an initial guess to curve_fit to get a good fit.
Treat the uncertainties in $y$ as absolute errors by ... | def model(t,A,o,l,d):
return A*np.exp(-l*t)*np.cos(o*t)+d
theta_best,theta_cov=opt.curve_fit(model,tdata,ydata,np.array((6,1,1,0)),dy,absolute_sigma=True)
print('A = {0:.3f} +/- {1:.3f}'.format(theta_best[0], np.sqrt(theta_cov[0,0])))
print('omega = {0:.3f} +/- {1:.3f}'.format(theta_best[1], np.sqrt(theta_cov[1,1]... | assignments/assignment12/FittingModelsEx02.ipynb | rvperry/phys202-2015-work | mit |
2. Calcule el área de un circulo de radio 5 | r = 5
a = (r**2) * 3.141596
print a | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
3. Escriba código que imprima todos los colores de que están en color_list_1 y no estan presentes en color_list_2
Resultado esperado :
{'Black', 'White'} | color_list_1 = set(["White", "Black", "Red"])
color_list_2 = set(["Red", "Green"])
print color_list_1
print color_list_1 - color_list_2
# Resultado = []
# for i in color_list_1:
# if not color_list_1[i] in color_list_2:
# Resultado += color_list_1[i]
# else... | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
4 Imprima una línea por cada carpeta que compone el Path donde se esta ejecutando python
e.g. C:/User/sergio/code/programación
Salida Esperada:
+ User
+ sergio
+ code
+ programacion | import os
wkd = os.getcwd()
wkd.split("/")
| Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
Manejo de Listas
5. Imprima la suma de números de my_list | my_list = [5,7,8,9,17]
print my_list
suma = 0
for i in my_list:
suma += i
print suma
| Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
6. Inserte un elemento_a_insertar antes de cada elemento de my_list | elemento_a_insertar = 'E'
my_list = [1, 2, 3, 4] | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
La salida esperada es una lista así: [E, 1, E, 2, E, 3, E, 4] | print my_list
print elemento_a_insertar
my_list.insert(0, elemento_a_insertar)
my_list.insert(2, elemento_a_insertar)
my_list.insert(4, elemento_a_insertar)
my_list.insert(6, elemento_a_insertar)
print my_list | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
7. Separe my_list en una lista de lista cada N elementos | N = 3
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'] | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
Salida Epserada: [['a', 'd', 'g', 'j', 'm'], ['b', 'e', 'h', 'k', 'n'], ['c', 'f', 'i', 'l']] |
#new_list = [i**2 for i in range(5)] # lamda functions () to apply a function to each variable in a list and creat another
#print new_list
# function zip to pare lists of the same length. function enumerate.
x = [4,2,5,6]
y = [5,3,1,6]
z = zip(x,y)
print z
N= 3
new_list = [[] for _ in range(N)]
for i, item in e... | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
8. Encuentra la lista dentro de list_of_lists que la suma de sus elementos sea la mayor | list_of_lists = [ [1,2,3], [4,5,6], [10,11,12], [7,8,9] ] | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
Salida Esperada: [10, 11, 12] | print max(list_of_lists) | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
Manejo de Diccionarios
9. Cree un diccionario que para cada número de 1 a N de llave tenga como valor N al cuadrado | N = 5 | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
Salida Esperada: {1:1, 2:4, 3:9, 4:16, 5:25} | Dict = {}
Dict[1] = 1**2
Dict[2] = 2**2
Dict[3] = 3**2
Dict[4] = 4**2
Dict[5] = 5**2
print Dict
N=5
D = {}
for i in range(N):
D[i] = i**2
print D
| Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
10. Concatene los diccionarios en dictionary_list para crear uno nuevo | dictionary_list=[{1:10, 2:20} , {3:30, 4:40}, {5:50,6:60}] | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
Salida Esperada: {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60} | new_dic = {}
for i in range(len(dictionary_list)):
new_dic.update(dictionary_list[i])
print new_dic
Dicc = {}
for i in dictionary_list:
for k in i:
Dicc[k] = i[k]
print Dicc | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
11. Añada un nuevo valor "cuadrado" con el valor de "numero" de cada diccionario elevado al cuadrado | dictionary_list=[{'numero': 10, 'cantidad': 5} , {'numero': 12, 'cantidad': 3}, {'numero': 5, 'cantidad': 45}] | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
Salida Esperada: [{'numero': 10, 'cantidad': 5, 'cuadrado': 100} , {'numero': 12, 'cantidad': 3, , 'cuadrado': 144}, {'numero': 5, 'cantidad': 45, , 'cuadrado': 25}] |
for i in range(0,len(dictionary_list)):
n = dictionary_list[i]['numero']
sqr = n**2
dictionary_list[i]['cuadrado'] = sqr
print dictionary_list | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
Manejo de Funciones
12. Defina y llame una función que reciba 2 parametros y solucione el problema 3 | def loca(list1,list2):
print list1 - list2
loca(color_list_1, color_list_2) | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
13. Defina y llame una función que reciva de parametro una lista de listas y solucione el problema 8 | def marx(lista):
return max(lista)
print marx(list_of_lists) | Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
14. Defina y llame una función que reciva un parametro N y resuleva el problema 9 |
def dic(N):
Dict ={}
for i in range(1,N):
Dict[i] = i**2
return Dict
print dic(4)
| Camilo/Taller 1.ipynb | spulido99/Programacion | mit |
Processing Test
Consolidating the returned CSVs into one is relatively painless
Main issue is that for some reason the time is still in GMT, and needs 5 hours in milliseconds subtracted from the epoch
Validating against Weather Underground read from O'Hare | s3_client = boto3.client('s3')
resource = boto3.resource('s3')
# Disable signing for anonymous requests to public bucket
resource.meta.client.meta.events.register('choose-signer.s3.*', disable_signing)
def file_list(client, bucket, prefix=''):
paginator = client.get_paginator('list_objects')
for result in clie... | nexrad-etl/Validate NEXRAD with Weather Underground.ipynb | NORCatUofC/rain | mit |
NEXRAD at O'Hare Zip 60666 | # Checking against Weather Underground read for O'Hare on this day
print(aug_day_ohare['60666'].sum())
aug_day_ohare['60666'].plot() | nexrad-etl/Validate NEXRAD with Weather Underground.ipynb | NORCatUofC/rain | mit |
Wunderground | wunderground = pd.read_csv('test-aug3/aug-12.csv')
wunderground['PrecipitationIn'] = wunderground['PrecipitationIn'].fillna(0.0)
wunderground['TimeCDT'] = pd.to_datetime(wunderground['TimeCDT'])
wunderground = wunderground.set_index(pd.DatetimeIndex(wunderground['TimeCDT']))
wund_hour = wunderground['PrecipitationIn'].... | nexrad-etl/Validate NEXRAD with Weather Underground.ipynb | NORCatUofC/rain | mit |
Part 1: Data Wrangle
Load and transform the data for analysis | # load federal document data from pickle file
fed_reg_data = r'data/fed_reg_data.pickle'
fed_data = pd.read_pickle(fed_reg_data)
# load twitter data from csv
twitter_file_path = r'data/twitter_01_20_17_to_3-2-18.pickle'
twitter_data = pd.read_pickle(twitter_file_path)
# Change the index (date), to a column
fed_data['d... | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
Combine data for analysis
<p> Create a dataframe that contains:
<ul>
<li> Each document, from both data sets, as a string </li>
<li> The date the text was published </li>
<li> A label for the type of document (0= twitter doc, 1= federal doc) </li>
</ul>
</p> | # keep text strings and rename columns
fed = fed_data[['str_text', 'date']].rename({'str_text': 'texts'}, axis = 'columns')
tweet = twitter_data[['text', 'date']].rename({'text': 'texts'}, axis = 'columns')
# Add a label for the type of document (Tweet = 0, Fed = 1)
tweet['label'] = 0
fed['label'] = 1
# concatinate t... | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
Transform text data into a word-frequency array
<p> Computers cannot understand a text like humans, so in order to analyze text data, I first need to make every word a feature (column) in an array, where each document (row) is represented by a weighted* frequency of each word (column) they contain. An example text and ... | # nonsense words, and standard words like proclimation and dates
more_stop = set(['presidential', 'documents', 'therfore','i','donald', 'j', 'trump', 'president', 'order',
'authority', 'vested', 'articles','january','february','march','april','may','june','july','august','september','october',
... | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
Excluded Words
<p>
Below is a printed list of all of the excluded words. I include this because I am not a political scientist or a linguist. What I consider to be nonsense maybe important and you may want to modify this list.
</p> | # print excluded words from the matrix features
print(tfidf.get_stop_words()) | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
Part 2: Analysis
Use unsupervised machine learning to analyze both President Trump's tweets, official presidential actions and explore any correlation between the two
Part 2A: Determine the document's topics
<p> Model the documents with non-zero matrix factorization (NMF):
<ul>
<li> Instantiate NMF model with 260 ... | # instantiate model
NMF_model = NMF(n_components=260 , init = 'nndsvd')
# fit the model
NMF_model.fit(text_mat)
# transform the text frequecy matrix using the fitted NMF model
nmf_features = NMF_model.transform(text_mat)
# create a dataframe with words as a columns, NMF components as rows
components_df = pd.DataFram... | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
Part 2B: Find the top 5 topic words (components) for each document
<p> Using the components dataframe create a dictionary with components as keys, and top words as values:
<ul>
<li> Make an empty dictionary and loop through each row of NMF components</li>
<li> Add to the dictionary where the key is the NMF comp... | # create dictionary with the key = component, value = top 5 words
topic_dict = {}
for i in range(0,260):
component = components_df.iloc[i, :]
topic_dict[i] = component.nlargest()
# look at a few of the component topics
print(topic_dict[0].index)
print(topic_dict[7].index) | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
Part 2C: Cosine Similarity
<p> The informal and non-regular grammar used in tweets makes a direct comparison with documents published by the Executive Office, which uses formal vocabulary and grammar, difficult. Therefore, I will use the metric, cosine similarity, which compares the distance between feature vectors, in... | # normalize previouly found nmf features
norm_features = normalize(nmf_features)
#dataframe of document's NMF features, where rows are documents and columns are NMF components
df_norms = pd.DataFrame(norm_features)
# initialize empty dictionary
similarity_dict= {}
# loop through each row of the df_norms dataframe
for... | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
Part 3: Use the cosine similarity results to explore how (or if) President Trump's tweets and official actions correlate
Part 3A: Find Twitter documents that have at least one federal document in its top 5 cosine similarity scores (and vice versa)
<p> Using the results of part 2C, find the types of documents are the ... | # dataframe with document ID and labels
doc_label_df = comb_text[['label', 'ID']].copy().set_index('ID')
# inialize list for the sum of all similar documents label
label_sums =[]
similarity_score_sum = []
# loop through all of the documents
for doc_num in doc_label_df.index:
# sum the similarity scores
similar... | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
Part 3B: Look at the topics of tweets that have similar federal documents (and vice versa)
<p> Isolate documents with mixed types of similar documents and high similarity scores
<ul>
<li> Filter dataframe to include only top_similar_label_sums with a value of 1, 2, 3, or 4</li>
<li> Filter again to only includ... | # Filter dataframe for federal documents with similar tweets, and vice versa
df_filtered = doc_label_df[doc_label_df['sum_of_labels'] != 0][doc_label_df['sum_of_labels'] != 5].copy().reset_index()
# Make sure it worked
print(df_filtered.head())
print(len(df_filtered))
# Look at the ones that have all top 5 documents ... | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
Remove duplicate highly similar groups | # create a list of all the group lists
doc_groups = []
for doc_id in highly_similar.ID:
doc_groups.append(sorted(list(similarity_dict[doc_id][0])))
# make the interior lists tuples, then make a set of them
unique_groups = set([tuple(x) for x in doc_groups])
unique_groups | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
Part 3C: Manually look at the documents. Are they similar?
Components = 100 , Highly similar score = 4.9
<p> Four of the 5 unique groups are basically the same
<ul> {(58, 80, 105, 149, 1139),
(58, 80, 126, 149, 1139),
(58, 80, 126, 185, 1139),
(58, 80, 149, 185, 1139),
(131, 1... | print(comb_text.texts.loc[1892])
print(comb_text.texts.loc[27]) | similarity_analysis.ipynb | mtchem/Twitter-Politics | mit |
As always, let's do imports and initialize a longger and a new Bundle. | import phoebe
from phoebe import u # units
logger = phoebe.logger()
b = phoebe.default_binary() | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
Accessing Settings
Settings are found with their own context in the Bundle and can be accessed through the get_setting method | b.get_setting() | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
or via filtering/twig access | b['setting'] | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
and can be set as any other Parameter in the Bundle
Available Settings
Now let's look at each of the available settings and what they do
phoebe_version
phoebe_version is a read-only parameter in the settings to store the version of PHOEBE used.
dict_set_all
dict_set_all is a BooleanParameter (defaults to False) that co... | b['dict_set_all@setting']
b['teff@component'] | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
In our default binary there are temperatures ('teff') parameters for each of the components ('primary' and 'secondary'). If we were to do:
b['teff@component'] = 6000
this would raise an error. Under-the-hood, this is simply calling:
b.set_value('teff@component', 6000)
which of course would also raise an error.
In ord... | b.set_value_all('teff@component', 4000)
print(b['value@teff@primary@component'], b['value@teff@secondary@component']) | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
If you want dictionary access to use set_value_all instead of set_value, you can enable this parameter | b['dict_set_all@setting'] = True
b['teff@component'] = 8000
print(b['value@teff@primary@component'], b['value@teff@secondary@component']) | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
Now let's disable this so it doesn't confuse us while looking at the other options | b.set_value_all('teff@component', 6000)
b['dict_set_all@setting'] = False | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
dict_filter
dict_filter is a Parameter that accepts a dictionary. This dictionary will then always be sent to the filter call which is done under-the-hood during dictionary access. | b['incl'] | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
In our default binary, there are several inclination parameters - one for each component ('primary', 'secondary', 'binary') and one with the constraint context (to keep the inclinations aligned).
This can be inconvenient... if you want to set the value of the binary's inclination, you must always provide extra informat... | b['dict_filter@setting'] = {'context': 'component'}
b['incl'] | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
Now we no longer see the constraint parameters.
All parameters are always accessible with method access: | b.filter(qualifier='incl') | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
Now let's reset this option... keeping in mind that we no longer have access to the 'setting' context through twig access, we'll have to use methods to clear the dict_filter | b.set_value('dict_filter@setting', {}) | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
run_checks_compute (/figure/solver/solution)
The run_checks_compute option allows setting the default compute option(s) sent to b.run_checks, including warnings in the logger raised by interactive checks (see phoebe.interactive_checks_on).
Similar options also exist for checks at the figure, solver, and solution level. | b['run_checks_compute@setting']
b.add_dataset('lc')
b.add_compute('legacy')
print(b.run_checks())
b['run_checks_compute@setting'] = ['phoebe01']
print(b.run_checks()) | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
auto_add_figure, auto_remove_figure
The auto_add_figure and auto_remove_figure determine whether new figures are automatically added to the Bundle when new datasets, distributions, etc are added. This is False by default within Python, but True by default within the UI clients. | b['auto_add_figure']
b['auto_add_figure'].description
b['auto_remove_figure']
b['auto_remove_figure'].description | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
web_client, web_client_url
The web_client and web_client_url settings determine whether the client is opened in a web-browser or with the installed desktop client whenever calling b.ui or b.ui_figures. For more information, see the UI from Jupyter tutorial. | b['web_client']
b['web_client'].description
b['web_client_url']
b['web_client_url'].description | development/tutorials/settings.ipynb | phoebe-project/phoebe2-docs | gpl-3.0 |
Estimators
<table class="tfo-notebook-buttons" align="left">
<td>
<a target="_blank" href="https://www.tensorflow.org/guide/estimator"><img src="https://www.tensorflow.org/images/tf_logo_32px.png" />View on TensorFlow.org</a>
</td>
<td>
<a target="_blank" href="https://colab.research.google.com/github/ten... | !pip install -U tensorflow_datasets
import tempfile
import os
import tensorflow as tf
import tensorflow_datasets as tfds | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
Advantages
Similar to a tf.keras.Model, an estimator is a model-level abstraction. The tf.estimator provides some capabilities currently still under development for tf.keras. These are:
Parameter server based training
Full TFX integration
Estimators Capabilities
Estimators provide the following benefits:
You can run... | def train_input_fn():
titanic_file = tf.keras.utils.get_file("train.csv", "https://storage.googleapis.com/tf-datasets/titanic/train.csv")
titanic = tf.data.experimental.make_csv_dataset(
titanic_file, batch_size=32,
label_name="survived")
titanic_batches = (
titanic.cache().repeat().shuffle(500)... | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
The input_fn is executed in a tf.Graph and can also directly return a (features_dics, labels) pair containing graph tensors, but this is error prone outside of simple cases like returning constants.
2. Define the feature columns.
Each tf.feature_column identifies a feature name, its type, and any input pre-processing. ... | age = tf.feature_column.numeric_column('age')
cls = tf.feature_column.categorical_column_with_vocabulary_list('class', ['First', 'Second', 'Third'])
embark = tf.feature_column.categorical_column_with_hash_bucket('embark_town', 32) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
3. Instantiate the relevant pre-made Estimator.
For example, here's a sample instantiation of a pre-made Estimator named LinearClassifier: | model_dir = tempfile.mkdtemp()
model = tf.estimator.LinearClassifier(
model_dir=model_dir,
feature_columns=[embark, cls, age],
n_classes=2
) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
For more information, you can go the linear classifier tutorial.
4. Call a training, evaluation, or inference method.
All Estimators provide train, evaluate, and predict methods. | model = model.train(input_fn=train_input_fn, steps=100)
result = model.evaluate(train_input_fn, steps=10)
for key, value in result.items():
print(key, ":", value)
for pred in model.predict(train_input_fn):
for key, value in pred.items():
print(key, ":", value)
break | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
Benefits of pre-made Estimators
Pre-made Estimators encode best practices, providing the following benefits:
Best practices for determining where different parts of the computational graph should run, implementing strategies on a single machine or on a
cluster.
Best practices for event (summary) writing and univer... | keras_mobilenet_v2 = tf.keras.applications.MobileNetV2(
input_shape=(160, 160, 3), include_top=False)
keras_mobilenet_v2.trainable = False
estimator_model = tf.keras.Sequential([
keras_mobilenet_v2,
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(1)
])
# Compile the model
estimator_mod... | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
Create an Estimator from the compiled Keras model. The initial model state of the Keras model is preserved in the created Estimator: | est_mobilenet_v2 = tf.keras.estimator.model_to_estimator(keras_model=estimator_model) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
Treat the derived Estimator as you would with any other Estimator. | IMG_SIZE = 160 # All images will be resized to 160x160
def preprocess(image, label):
image = tf.cast(image, tf.float32)
image = (image/127.5) - 1
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
return image, label
def train_input_fn(batch_size):
data = tfds.load('cats_vs_dogs', as_supervised=True)
t... | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
To train, call Estimator's train function: | est_mobilenet_v2.train(input_fn=lambda: train_input_fn(32), steps=50) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
Similarly, to evaluate, call the Estimator's evaluate function: | est_mobilenet_v2.evaluate(input_fn=lambda: train_input_fn(32), steps=10) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
For more details, please refer to the documentation for tf.keras.estimator.model_to_estimator.
Saving object-based checkpoints with Estimator
Estimators by default save checkpoints with variable names rather than the object graph described in the Checkpoint guide. tf.train.Checkpoint will read name-based checkpoints, b... | import tensorflow.compat.v1 as tf_compat
def toy_dataset():
inputs = tf.range(10.)[:, None]
labels = inputs * 5. + tf.range(5.)[None, :]
return tf.data.Dataset.from_tensor_slices(
dict(x=inputs, y=labels)).repeat().batch(2)
class Net(tf.keras.Model):
"""A simple linear model."""
def __init__(self):
... | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
tf.train.Checkpoint can then load the Estimator's checkpoints from its model_dir. | opt = tf.keras.optimizers.Adam(0.1)
net = Net()
ckpt = tf.train.Checkpoint(
step=tf.Variable(1, dtype=tf.int64), optimizer=opt, net=net)
ckpt.restore(tf.train.latest_checkpoint('./tf_estimator_example/'))
ckpt.step.numpy() # From est.train(..., steps=10) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
SavedModels from Estimators
Estimators export SavedModels through tf.Estimator.export_saved_model. | input_column = tf.feature_column.numeric_column("x")
estimator = tf.estimator.LinearClassifier(feature_columns=[input_column])
def input_fn():
return tf.data.Dataset.from_tensor_slices(
({"x": [1., 2., 3., 4.]}, [1, 1, 0, 0])).repeat(200).shuffle(64).batch(16)
estimator.train(input_fn) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
To save an Estimator you need to create a serving_input_receiver. This function builds a part of a tf.Graph that parses the raw data received by the SavedModel.
The tf.estimator.export module contains functions to help build these receivers.
The following code builds a receiver, based on the feature_columns, that acce... | tmpdir = tempfile.mkdtemp()
serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
tf.feature_column.make_parse_example_spec([input_column]))
estimator_base_path = os.path.join(tmpdir, 'from_estimator')
estimator_path = estimator.export_saved_model(estimator_base_path, serving_input_fn) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
You can also load and run that model, from python: | imported = tf.saved_model.load(estimator_path)
def predict(x):
example = tf.train.Example()
example.features.feature["x"].float_list.value.extend([x])
return imported.signatures["predict"](
examples=tf.constant([example.SerializeToString()]))
print(predict(1.5))
print(predict(3.5)) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
tf.estimator.export.build_raw_serving_input_receiver_fn allows you to create input functions which take raw tensors rather than tf.train.Examples.
Using tf.distribute.Strategy with Estimator (Limited support)
tf.estimator is a distributed training TensorFlow API that originally supported the async parameter server appr... | mirrored_strategy = tf.distribute.MirroredStrategy()
config = tf.estimator.RunConfig(
train_distribute=mirrored_strategy, eval_distribute=mirrored_strategy)
regressor = tf.estimator.LinearRegressor(
feature_columns=[tf.feature_column.numeric_column('feats')],
optimizer='SGD',
config=config) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
Here, you use a premade Estimator, but the same code works with a custom Estimator as well. train_distribute determines how training will be distributed, and eval_distribute determines how evaluation will be distributed. This is another difference from Keras where you use the same strategy for both training and eval.
N... | def input_fn():
dataset = tf.data.Dataset.from_tensors(({"feats":[1.]}, [1.]))
return dataset.repeat(1000).batch(10)
regressor.train(input_fn=input_fn, steps=10)
regressor.evaluate(input_fn=input_fn, steps=10) | site/en-snapshot/guide/estimator.ipynb | tensorflow/docs-l10n | apache-2.0 |
Step 1: Build the MNIST LSTM model. | import numpy as np
import tensorflow as tf
model = tf.keras.models.Sequential([
tf.keras.layers.Input(shape=(28, 28), name='input'),
tf.keras.layers.LSTM(20, time_major=False, return_sequences=True),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(10, activation=tf.nn.softmax, name='output')
])
model.... | tensorflow/lite/examples/experimental_new_converter/Keras_LSTM_fusion_Codelab.ipynb | sarvex/tensorflow | apache-2.0 |
Step 2: Train & Evaluate the model.
We will train the model using MNIST data. | # Load MNIST dataset.
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train.astype(np.float32)
x_test = x_test.astype(np.float32)
# Change this to True if you want to test the flow rapidly.
# Train with a small dataset and only 1 ... | tensorflow/lite/examples/experimental_new_converter/Keras_LSTM_fusion_Codelab.ipynb | sarvex/tensorflow | apache-2.0 |
Step 3: Convert the Keras model to TensorFlow Lite model. | run_model = tf.function(lambda x: model(x))
# This is important, let's fix the input size.
BATCH_SIZE = 1
STEPS = 28
INPUT_SIZE = 28
concrete_func = run_model.get_concrete_function(
tf.TensorSpec([BATCH_SIZE, STEPS, INPUT_SIZE], model.inputs[0].dtype))
# model directory.
MODEL_DIR = "keras_lstm"
model.save(MODEL_D... | tensorflow/lite/examples/experimental_new_converter/Keras_LSTM_fusion_Codelab.ipynb | sarvex/tensorflow | apache-2.0 |
Step 4: Check the converted TensorFlow Lite model.
Now load the TensorFlow Lite model and use the TensorFlow Lite python interpreter to verify the results. | # Run the model with TensorFlow to get expected results.
TEST_CASES = 10
# Run the model with TensorFlow Lite
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
for i in range(TE... | tensorflow/lite/examples/experimental_new_converter/Keras_LSTM_fusion_Codelab.ipynb | sarvex/tensorflow | apache-2.0 |
Cross-validated pipelines including scaling, we need to estimate mean and standard deviation separately for each fold.
To do that, we build a pipeline. | from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
standard_scaler = StandardScaler()
standard_scaler.fit(X_train)
X_train_scaled = standard_scaler.transform(X_train)
svm = SVC().fit(X_train_scaled, y_train)
#pipeline = Pipeline([("scaler"... | Preprocessing and Pipelines.ipynb | amueller/pydata-amsterdam-2016 | cc0-1.0 |
Cross-validation with a pipeline | from sklearn.cross_validation import cross_val_score
cross_val_score(pipeline, X_train, y_train) | Preprocessing and Pipelines.ipynb | amueller/pydata-amsterdam-2016 | cc0-1.0 |
Grid Search with a pipeline | import numpy as np
from sklearn.grid_search import GridSearchCV
param_grid = {'svc__C': 10. ** np.arange(-3, 3),
'svc__gamma' : 10. ** np.arange(-3, 3)
}
grid_pipeline = GridSearchCV(pipeline, param_grid=param_grid)
grid_pipeline.fit(X_train, y_train)
grid_pipeline.score(X_test, y_test) | Preprocessing and Pipelines.ipynb | amueller/pydata-amsterdam-2016 | cc0-1.0 |
Exercise
Make a pipeline out of the StandardScaler and KNeighborsClassifier and search over the number of neighbors. | # %load solutions/pipeline_knn.py | Preprocessing and Pipelines.ipynb | amueller/pydata-amsterdam-2016 | cc0-1.0 |
Note that the default settings on the NCBI BLAST website are not quite
the same as the defaults on QBLAST. If you get different results, you’ll
need to check the parameters (e.g., the expectation value threshold and
the gap values).
For example, if you have a nucleotide sequence you want to search
against the nucleotid... | from Bio.Blast import NCBIWWW
result_handle = NCBIWWW.qblast("blastn", "nt", "8332116") | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
Alternatively, if we have our query sequence already in a FASTA
formatted file, we just need to open the file and read in this record as
a string, and use that as the query argument: | from Bio.Blast import NCBIWWW
fasta_string = open("data/m_cold.fasta").read()
result_handle = NCBIWWW.qblast("blastn", "nt", fasta_string) | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
We could also have read in the FASTA file as a SeqRecord and then
supplied just the sequence itself: | from Bio.Blast import NCBIWWW
from Bio import SeqIO
record = SeqIO.read("data/m_cold.fasta", format="fasta")
result_handle = NCBIWWW.qblast("blastn", "nt", record.seq) | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
Supplying just the sequence means that BLAST will assign an identifier
for your sequence automatically. You might prefer to use the SeqRecord
object’s format method to make a FASTA string (which will include the
existing identifier): | from Bio.Blast import NCBIWWW
from Bio import SeqIO
record = SeqIO.read("data/m_cold.fasta", format="fasta")
result_handle = NCBIWWW.qblast("blastn", "nt", record.format("fasta")) | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
This approach makes more sense if you have your sequence(s) in a
non-FASTA file format which you can extract using Bio.SeqIO (see
Chapter 5 - Sequence Input and Output.)
Whatever arguments you give the qblast() function, you should get back
your results in a handle object (by default in XML format). The next
step would... | with open("data/my_blast.xml", "w") as out_handle:
out_handle.write(result_handle.read())
result_handle.close() | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
After doing this, the results are in the file my_blast.xml and the
original handle has had all its data extracted (so we closed it).
However, the parse function of the BLAST parser (described
in [sec:parsing-blast]) takes a file-handle-like object, so we can
just open the saved file for input: | result_handle = open("data/my_blast.xml") | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
Now that we’ve got the BLAST results back into a handle again, we are
ready to do something with them, so this leads us right into the parsing
section (see Section [sec:parsing-blast] below). You may want to jump
ahead to that now ….
Running BLAST locally
Introduction
Running BLAST locally (as opposed to over the inter... | from Bio.Blast.Applications import NcbiblastxCommandline
help(NcbiblastxCommandline)
blastx_cline = NcbiblastxCommandline(query="opuntia.fasta", db="nr", evalue=0.001,
outfmt=5, out="opuntia.xml")
blastx_cline
print(blastx_cline)
# stdout, stderr = blastx_cline() | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
In this example there shouldn’t be any output from BLASTX to the
terminal, so stdout and stderr should be empty. You may want to check
the output file opuntia.xml has been created.
As you may recall from earlier examples in the tutorial, the
opuntia.fasta contains seven sequences, so the BLAST XML output should
contain... | from Bio.Blast import NCBIWWW
result_handle = NCBIWWW.qblast("blastn", "nt", "8332116") | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
If instead you ran BLAST some other way, and have the BLAST output (in
XML format) in the file my_blast.xml, all you need to do is to open
the file for reading: | result_handle = open("data/my_blast.xml") | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
Now that we’ve got a handle, we are ready to parse the output. The code
to parse it is really quite small. If you expect a single BLAST result
(i.e., you used a single query): | from Bio.Blast import NCBIXML
blast_record = NCBIXML.read(result_handle) | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
or, if you have lots of results (i.e., multiple query sequences): | from Bio.Blast import NCBIXML
blast_records = NCBIXML.parse(result_handle) | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
Just like Bio.SeqIO and Bio.AlignIO (see
Chapters [chapter:Bio.SeqIO] and [chapter:Bio.AlignIO]), we have a
pair of input functions, read and parse, where read is for when
you have exactly one object, and parse is an iterator for when you can
have lots of objects – but instead of getting SeqRecord or
MultipleSeqAlignme... | from Bio.Blast import NCBIXML
blast_records = NCBIXML.parse(result_handle)
blast_record = next(blast_records)
print(blast_record.database_sequences)
# # ... do something with blast_record | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
Or, you can use a for-loop. Note though that you can step through the BLAST records only once. Usually, from each BLAST record you would save the information that you are interested in. If you want to save all returned BLAST records, you can convert the iterator into a list: | for blast_record in blast_records:
#Do something with blast_records
blast_records = list(blast_records)
blast_records = list(blast_records) | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
Now you can access each BLAST record in the list with an index as usual. If your BLAST file is huge though, you may run into memory problems trying to save them all in a list.
Usually, you’ll be running one BLAST search at a time. Then, all you need to do is to pick up the first (and only) BLAST record in blast_records... | from Bio.Blast import NCBIXML
blast_records = NCBIXML.parse(result_handle) | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
I guess by now you’re wondering what is in a BLAST record.
The BLAST record class
A BLAST Record contains everything you might ever want to extract from
the BLAST output. Right now we’ll just show an example of how to get
some info out of the BLAST report, but if you want something in
particular that is not described h... | E_VALUE_THRESH = 0.04
from Bio.Blast import NCBIXML
result_handle = open("data/my_blast.xml", "r")
blast_records = NCBIXML.parse(result_handle)
for alignment in blast_record.alignments:
for hsp in alignment.hsps:
if hsp.expect < E_VALUE_THRESH:
print("****Alignment****")
print("seq... | notebooks/07 - Blast.ipynb | tiagoantao/biopython-notebook | mit |
最后的那个例子揭示了一个小缺陷,替换字符串并不会自动跟被匹配字符串的大小写保持一致。 为了修复这个,你可能需要一个辅助函数,就像下面的这样: | def matchcase(word):
def replace(m):
text = m.group()
if text.isupper():
return word.upper()
elif text.islower():
return word.lower()
elif text[0].isupper():
return word.capitalize()
else:
return word
return replace | 02 strings and text/02.06 search replace case insensitive.ipynb | wuafeing/Python3-Tutorial | gpl-3.0 |
下面是使用上述函数的方法: | re.sub("python", matchcase("snake"), text, flags=re.IGNORECASE) | 02 strings and text/02.06 search replace case insensitive.ipynb | wuafeing/Python3-Tutorial | gpl-3.0 |
First reload the data we generated in 1_notmnist.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_labe... | google_dl_udacity/lesson3/3_regularization.ipynb | jinzishuai/learn2deeplearn | gpl-3.0 |
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 1 to [0.0, 1.0, 0.0 ...], 2 to [0.0, 0.0, 1.0 ...]
labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
return dataset, labels
train_dataset, train_l... | google_dl_udacity/lesson3/3_regularization.ipynb | jinzishuai/learn2deeplearn | gpl-3.0 |
Problem 1
Introduce and tune L2 regularization for both logistic and neural network models. Remember that L2 amounts to adding a penalty on the norm of the weights to the loss. In TensorFlow, you can compute the L2 loss for a tensor t using nn.l2_loss(t). The right amount of regularization should improve your validatio... | graph = tf.Graph()
with graph.as_default():
...
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))+ \
tf.scalar_mul(beta, tf.nn.l2_loss(weights1)+tf.nn.l2_loss(weights2))
| google_dl_udacity/lesson3/3_regularization.ipynb | jinzishuai/learn2deeplearn | gpl-3.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.