markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
Two random points
After we probe two points at random, we can fit a Gaussian Process and start the bayesian optimization procedure. Two points should give us a uneventful posterior with the uncertainty growing as we go further from the observations. | plot_gp(bo, x, y) | examples/visualization.ipynb | ysasaki6023/NeuralNetworkStudy | mit |
After one step of GP (and two random points) | bo.maximize(init_points=0, n_iter=1, kappa=5)
plot_gp(bo, x, y) | examples/visualization.ipynb | ysasaki6023/NeuralNetworkStudy | mit |
After two steps of GP (and two random points) | bo.maximize(init_points=0, n_iter=1, kappa=5)
plot_gp(bo, x, y) | examples/visualization.ipynb | ysasaki6023/NeuralNetworkStudy | mit |
After three steps of GP (and two random points) | bo.maximize(init_points=0, n_iter=1, kappa=5)
plot_gp(bo, x, y) | examples/visualization.ipynb | ysasaki6023/NeuralNetworkStudy | mit |
After four steps of GP (and two random points) | bo.maximize(init_points=0, n_iter=1, kappa=5)
plot_gp(bo, x, y) | examples/visualization.ipynb | ysasaki6023/NeuralNetworkStudy | mit |
After five steps of GP (and two random points) | bo.maximize(init_points=0, n_iter=1, kappa=5)
plot_gp(bo, x, y) | examples/visualization.ipynb | ysasaki6023/NeuralNetworkStudy | mit |
After six steps of GP (and two random points) | bo.maximize(init_points=0, n_iter=1, kappa=5)
plot_gp(bo, x, y) | examples/visualization.ipynb | ysasaki6023/NeuralNetworkStudy | mit |
After seven steps of GP (and two random points) | bo.maximize(init_points=0, n_iter=1, kappa=5)
plot_gp(bo, x, y) | examples/visualization.ipynb | ysasaki6023/NeuralNetworkStudy | mit |
Load and process review dataset | products = pd.read_csv('../../data/amazon_baby_subset.csv')
products['sentiment']
products['sentiment'].size
products.head(10).name
print ('# of positive reviews =', len(products[products['sentiment']==1]))
print ('# of negative reviews =', len(products[products['sentiment']==-1]))
# The same feature processing (s... | notebooks/classification/module-4-linear-classifier-regularization-pandas.ipynb | rthadani/coursera-ml | epl-1.0 |
Train-Validation split
We split the data into a train-validation split with 80% of the data in the training set and 20% of the data in the validation set. We use seed=2 so that everyone gets the same result.
Note: In previous assignments, we have called this a train-test split. However, the portion of data that we don'... | with open('../../data/module-4-assignment-train-idx.json', 'r') as f:
train_idx = json.load(f)
train_data = products.ix[train_idx]
with open ('../../data/module-4-assignment-validation-idx.json', 'r') as f:
v_idx = json.load(f)
validation_data = products.ix[v_idx] | notebooks/classification/module-4-linear-classifier-regularization-pandas.ipynb | rthadani/coursera-ml | epl-1.0 |
Convert Frame to NumPy array
Just like in the second assignment of the previous module, we provide you with a function that extracts columns from an SFrame and converts them into a NumPy array. Two arrays are returned: one representing features and another representing class labels.
Note: The feature matrix includes a... | import numpy as np
def get_numpy_data(data_frame, features, label):
data_frame['intercept'] = 1
features = ['intercept'] + features
features_frame = data_frame[features]
feature_matrix = features_frame.as_matrix()
label_array = data_frame[label]
return(feature_matrix, label_array)
feature_matr... | notebooks/classification/module-4-linear-classifier-regularization-pandas.ipynb | rthadani/coursera-ml | epl-1.0 |
Building on logistic regression with no L2 penalty assignment
Let us now build on Module 3 assignment. Recall from lecture that the link function for logistic regression can be defined as:
$$
P(y_i = +1 | \mathbf{x}_i,\mathbf{w}) = \frac{1}{1 + \exp(-\mathbf{w}^T h(\mathbf{x}_i))},
$$
where the feature vector $h(\mathb... | def prediction(score):
return (1 / (1 + np.exp(-score)))
'''
produces probablistic estimate for P(y_i = +1 | x_i, w).
estimate ranges between 0 and 1.
'''
def predict_probability(feature_matrix, coefficients):
# Take dot product of feature_matrix and coefficients
scores = np.dot(feature_matrix, coefficie... | notebooks/classification/module-4-linear-classifier-regularization-pandas.ipynb | rthadani/coursera-ml | epl-1.0 |
Adding L2 penalty
Let us now work on extending logistic regression with L2 regularization. As discussed in the lectures, the L2 regularization is particularly useful in preventing overfitting. In this assignment, we will explore L2 regularization in detail.
Recall from lecture and the previous assignment that for logi... | def feature_derivative_with_L2(errors, feature, coefficient, l2_penalty, feature_is_constant):
# Compute the dot product of errors and feature
derivative = np.dot(feature, errors)
# add L2 penalty term for any feature that isn't the intercept.
if not feature_is_constant:
derivative = der... | notebooks/classification/module-4-linear-classifier-regularization-pandas.ipynb | rthadani/coursera-ml | epl-1.0 |
Quiz Question: Does the term with L2 regularization increase or decrease $\ell\ell(\mathbf{w})$?
The logistic regression function looks almost like the one in the last assignment, with a minor modification to account for the L2 penalty. Fill in the code below to complete this modification. | from math import sqrt
def logistic_regression_with_L2(feature_matrix, sentiment, initial_coefficients, step_size, l2_penalty, max_iter):
coefficients = np.array(initial_coefficients) # make sure it's a numpy array
for itr in range(max_iter):
# Predict P(y_i = +1|x_i,w) using your predict_probability()... | notebooks/classification/module-4-linear-classifier-regularization-pandas.ipynb | rthadani/coursera-ml | epl-1.0 |
Compare coefficients
We now compare the coefficients for each of the models that were trained above. We will create a table of features and learned coefficients associated with each of the different L2 penalty values.
Below is a simple helper function that will help us create this table. | important_words.insert(0, 'intercept')
data = np.array(important_words)
table = pd.DataFrame(columns = ['words'], data = data)
def add_coefficients_to_table(coefficients, column_name):
table[column_name] = coefficients
return table
important_words.remove('intercept')
add_coefficients_to_table(coefficients_0_pe... | notebooks/classification/module-4-linear-classifier-regularization-pandas.ipynb | rthadani/coursera-ml | epl-1.0 |
Using the coefficients trained with L2 penalty 0, find the 5 most positive words (with largest positive coefficients). Save them to positive_words. Similarly, find the 5 most negative words (with largest negative coefficients) and save them to negative_words.
Quiz Question. Which of the following is not listed in eithe... | def make_tuple(column_name):
word_coefficient_tuples = [(word, coefficient) for word, coefficient in zip( table['words'], table[column_name])]
return word_coefficient_tuples
positive_words = list(map(lambda x: x[0], sorted(make_tuple('coefficients [L2=0]'), key=lambda x:x[1], reverse=True)[:5]))
negative_word... | notebooks/classification/module-4-linear-classifier-regularization-pandas.ipynb | rthadani/coursera-ml | epl-1.0 |
Let us observe the effect of increasing L2 penalty on the 10 words just selected. We provide you with a utility function to plot the coefficient path. | import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = 10, 6
def make_coefficient_plot(table, positive_words, negative_words, l2_penalty_list):
cmap_positive = plt.get_cmap('Reds')
cmap_negative = plt.get_cmap('Blues')
xx = l2_penalty_list
plt.plot(xx, [0.]*len(xx), '-... | notebooks/classification/module-4-linear-classifier-regularization-pandas.ipynb | rthadani/coursera-ml | epl-1.0 |
Below, we compare the accuracy on the training data and validation data for all the models that were trained in this assignment. We first calculate the accuracy values and then build a simple report summarizing the performance for the various models. | train_accuracy = {}
train_accuracy[0] = get_classification_accuracy(feature_matrix_train, sentiment_train, coefficients_0_penalty)
train_accuracy[4] = get_classification_accuracy(feature_matrix_train, sentiment_train, coefficients_4_penalty)
train_accuracy[10] = get_classification_accuracy(feature_matrix_train, se... | notebooks/classification/module-4-linear-classifier-regularization-pandas.ipynb | rthadani/coursera-ml | epl-1.0 |
Viewing Sentences Line by Line
Unlike the displaCy dependency parse, the NER viewer has to take in a Doc object with an ents attribute. For this reason, we can't just pass a list of spans to .render(), we have to create a new Doc from each span.text: | for sent in doc.sents:
displacy.render(nlp(sent.text), style='ent', jupyter=True) | nlp/UPDATED_NLP_COURSE/02-Parts-of-Speech-Tagging/03-Visualizing-NER.ipynb | rishuatgithub/MLPy | apache-2.0 |
<div class="alert alert-info"><font color=black>**NOTE**: If a span does not contain any entities, displaCy will issue a harmless warning:</font></div> | doc2 = nlp(u'Over the last quarter Apple sold nearly 20 thousand iPods for a profit of $6 million. '
u'By contrast, my kids sold a lot of lemonade.')
for sent in doc2.sents:
displacy.render(nlp(sent.text), style='ent', jupyter=True) | nlp/UPDATED_NLP_COURSE/02-Parts-of-Speech-Tagging/03-Visualizing-NER.ipynb | rishuatgithub/MLPy | apache-2.0 |
<div class="alert alert-info"><font color=black>**WORKAROUND:** We can avert this with an additional bit of code:</font></div> | for sent in doc2.sents:
docx = nlp(sent.text)
if docx.ents:
displacy.render(docx, style='ent', jupyter=True)
else:
print(docx.text) | nlp/UPDATED_NLP_COURSE/02-Parts-of-Speech-Tagging/03-Visualizing-NER.ipynb | rishuatgithub/MLPy | apache-2.0 |
Viewing Specific Entities
You can pass a list of entity types to restrict the visualization: | options = {'ents': ['ORG', 'PRODUCT']}
displacy.render(doc, style='ent', jupyter=True, options=options) | nlp/UPDATED_NLP_COURSE/02-Parts-of-Speech-Tagging/03-Visualizing-NER.ipynb | rishuatgithub/MLPy | apache-2.0 |
Customizing Colors and Effects
You can also pass background color and gradient options: | colors = {'ORG': 'linear-gradient(90deg, #aa9cfc, #fc9ce7)', 'PRODUCT': 'radial-gradient(yellow, green)'}
options = {'ents': ['ORG', 'PRODUCT'], 'colors':colors}
displacy.render(doc, style='ent', jupyter=True, options=options) | nlp/UPDATED_NLP_COURSE/02-Parts-of-Speech-Tagging/03-Visualizing-NER.ipynb | rishuatgithub/MLPy | apache-2.0 |
For more on applying CSS background colors and gradients, visit https://www.w3schools.com/css/css3_gradients.asp
Creating Visualizations Outside of Jupyter
If you're using another Python IDE or writing a script, you can choose to have spaCy serve up HTML separately.
Instead of displacy.render(), use displacy.serve(): | displacy.serve(doc, style='ent', options=options) | nlp/UPDATED_NLP_COURSE/02-Parts-of-Speech-Tagging/03-Visualizing-NER.ipynb | rishuatgithub/MLPy | apache-2.0 |
Numeri Reali (?) | y=1.0
print(y)
type(x), type(y)
type(x+y) | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
Type conversion o type cast
Sono usati per convertire un tipo in un altro, in maniera dinamica. Per convertire un oggetto in un dato tipo, basta usare il nome del tipo come se fosse una funzione. Esempi: | a = "3.0"
type(a)
b = int(a)
b = float(a)
type(b)
x = float(1)/3
y = 1/3
z = 1.0//3.0
print(x+y+z)
2.4//2.5 | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
ATTENZIONE: La divisione tra numeri interi non considera il resto | x = 1.0/3
y = 1.0/3
z = 1/3
print(x+y+z) | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
NOTA: In Python 2.7 questa espressione viene valutata a 0.666666 in quanto z vienve valutata con una divisione tra interi | 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 == 1.0
1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 + 1/10 | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
ATTENZIONE: Ricordarsi di come vengono rappresentati i numeri al calcolatori con la rappresentazione in virgola mobile (floating points).
Numeri Complessi | x = 1+3j
y = 4+2j
z = 2+1j
print(type(x))
print(x+y*z)
print(z.real, z.imag, z.conjugate)
z.conjugate()
z.real = 3 | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
Comandi utili nel workspace
Per controllare le variabili in memoria (nel workspace): | who | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
Per rimuovere una variabile dal workspace: | del x
who
print(x+y+z)
help(complex) | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
Funzioni
Supponiamo di voler scrivere una funzione che calcola il doppio di un numero dato ovvero la funzione:
$f : \mathbb{C} \rightarrow \mathbb{C}$
con:
$f(x) = 2 x$
In python dobbiamo scrivere (ATTENZIONE AGLI SPAZI DEL TAB): | # Notare l'identazione nella funzione seguente
def f(x):
"""Funzione che il doppio di x"""
return x*2
print(type(f))
f(27)
f(1.2)
f(1+3j)
f("Ciao")
2*"domanda"
help(f) | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
Vogliamo ora scrivere la funzione:
$$f : \mathbb{C} \times \mathbb{C} \rightarrow \mathbb{C}$$
con
$$f(x,y) = x^y$$
In python scriviamo: | def Potenza(base, esponente):
"""
Calcola la potenza (base)^(esponente).
"""
return base**esponente
Potenza(2, 3)
def Potenza2(base, esponente=2):
"""
Calcola la potenza (base)^(esponente).
Se il secondo parametro non viene passato,
calcola la potenza quadrata, ovvero (esponente=2... | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
Python supporta la possibilitร di usare i nomi di variabili usati nella definizione di una funzione per passare gli argomenti (chiamati keyword arguments). Questo si differenza dalla tipica notazione posizionale di molti linguaggi di programmazione, tipo il C, e permette di rendere il codice piรน leggibile e di evitare... | As = [5,3,2,8,7,13]
print(As)
print(type(As))
# help(list) | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
IMPORTANTE: Abituarsi ad usare il tasto tab per avere il completamento automatico | # Aggiungere un elemento ad una lista
As.append(27)
print(As)
As.remove(7)
print(As)
As.insert(4, 12)
print(As)
# Invertire l'ordine di una lista
As.reverse()
print(As)
# Ordinamento come funzione
Bs = sorted(As)
print(As, Bs)
# Ordinamento "IN_PLACE"
As.sort()
print(As) | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
COMMENTO: Ricordarsi la differenza che alcune funzioni operano IN PLACE | print(As)
for x in As:
print(x)
# Equivalente in ANSI-C
# for (int i=0; i<n; i++)
# printf("%f\n", As[i]);
# Iterare gli elementi di una lista (esempio di commento in python)
for n in As:
print("x=", str(n)," \t -> f(x^3) =",str(Potenza(n,3)))
list(map(lambda x: Potenza(x,3), As))
# Iterare una lista ... | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
Operazioni di Slicing
Per ottenere una sotto lista, si puรฒ usare un espressione di Slicing. Per esempio, l'espressione Lista[start:end],
restituisce la sottolista che inizia in posizione "start" e finisce in posizione "end". Esempio: | As
As[3:5]
As[-1]
head, tail = As[0], As[1:]
print(head, tail) | Introduzione a Python - Prima parte.ipynb | mathcoding/Programmazione2 | mit |
Series
Sรฉries sรฃo estruturas unidimensionais, como um array do Numpy de dimensรฃo 1 | a = pd.Series([20, 50, 190, 11, 76])
a | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
Os dados em uma sรฉrie podem conter um รญndice, permitindo uma otimizaรงรฃo no acesso dos dados | dados = [20, 50, 190, 11, 76]
rotulos = ['a', 'b', 'c', 'd', 'e']
b = pd.Series(dados, index=rotulos)
b | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
Alรฉm disso, o รญndice pode ser utilizado para dar uma semรขntia ao dado de uma sรฉrie, permitindo tambรฉm que o mesmo seja acessado pelo รญndice atribuรญdo | print(a[2])
print(b[2])
print(b['c']) | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
As Series tambรฉm possuem um mรฉtodo de transformaรงรฃo, conforme apresentado na aula anterior. Esse mรฉtodo chama-se apply: ele recebe uma funรงรฃo que serรก aplicada sobre todos os elementos da Serie, retornando entรฃo uma Serie com os resultados | a.apply(lambda x: 2*x) | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
DataFrame
Um DataFrame รฉ uma tabela onde cada coluna รฉ uma Serie. Assim como as Series, o DataFrame possui um รญndice, porรฉm, o รญndice refere-se a uma linha inteira, ou seja, ao elemento naquela posiรงรฃo em todas suas colunas | matriz = np.array([[1, 2, 3], [4, 5, 6]])
nomes_linhas = ['L1', 'L2']
nomes_cols = ['C1', 'C2', 'C3']
df = pd.DataFrame(matriz, index=nomes_linhas, columns=nomes_cols)
df | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
Para fins de exportaรงรฃo, um DataFrame pode ser representado em diversos formatos | print(df.to_latex()) # latex
print(df.to_csv(index=False)) #csv
print(df.to_json()) # JSON
print(df.to_html()) #HTML | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
Enquanto em uma Serie utilizamos os colchetes ([]) para acessar um elemento em um certo รญndice, no DataFrame o operador refere-se ร uma Serie, permitindo acessรก-la, sobrescrevรช-la ou adicionar uma nova | df['C3']
df['C4'] = [1, 0]
df
df['C4'] = [4, 7]
df | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
Um DataFrame tambรฉm pode ser transposto, ou seja, as labels das suas colunas viram รญndices e os รญndices viram as novas colunas | df.transpose() | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
Podemos tambรฉm ordenar as linhas do DataFrame a partir de uma de suas colunas | df.sort_values(by='C4', ascending=False) | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
Importando um dataset real
Plataforma Kaggle - Competiรงรตes de Ciรชncias de Dados
Titanic: Machine Learning from Disaster
Nela, sรฃo providenciadas diversas informaรงรตes sobre os passageiros, como idade, sexo, cabine, valor do tรญquete pago, entre outros.
O pandas possui funรงรตes prรฉ-definidas para a leitura de alguns forma... | df = pd.read_csv('titanic.csv')
df.head() # 5 primeiras linhas
df.tail() # รบltimas 5 linhas
df.columns # colunas do dataframe
df.describe() # calcula estatรญsticas para cada coluna numรฉrica do DataFrame | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
O operador colchetes ([]) do pandas tambรฉm pode ser usado como uma filtragem, ou seja, dada uma condiรงรฃo (ou predicado), ele retorna apenas as linhas do DataFrame que satisfaรงam o predicado.
OBS: escrever um predicado nesse operador nem sempre รฉ tรฃo simples quanto um predicado Python comum | df[df.Sex == "female"] | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
O DataFrame permite tambรฉm que sejam relizadas contagens sobre os valores presentes nas sรฉries, permitindo assim analizarmos a ocorrรชncia de certos dados categรณricos | df['Sex'].value_counts()
df['Survived'].apply(lambda s: "Yes" if s == 1 else "No").value_counts() | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
Por รบltimo mas nunca menos importante, podemos agrupar as linhas do DataFrame a partir de uma coluna e operar sobre os grupos criados | df.groupby('Sex')['Survived'].value_counts() | 2020/02-python-bibliotecas-manipulacao-dados/Pandas.ipynb | InsightLab/data-science-cookbook | mit |
map_element
In some situations, you may not want to decorate a function. In these cases you can use map_element
map_element(func, in_stream, out_stream, state=None, name=None, kwargs)
where func is the function applied to each element of the input stream.
Next, we implement the previous example without using decorato... | def example():
def f(v): return v+10
x, y = Stream(), Stream()
map_element(func=f, in_stream=x, out_stream=y)
x.extend([1, 2, 3, 4])
# Execute a step
run()
print ('recent values of stream y are')
print (recent_values(y))
example() | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Mapping element to element : function with keyword arguments
The function
f(v, addend) = return v * multiplier + addend
maps v to the return value. The first parameter v is an element of an input stream, and the arguments addend and multiplier are keyword arguments of the function.
Decorating the function with @fmap_... | @fmap_e
def f(v, addend, multiplier): return v * multiplier + addend
# f is a function that maps a stream to a stream
def example():
x = Stream()
# Specify the keyword argument: addend
y = f(x, addend=20, multiplier=2)
x.extend([1, 2, 3, 4])
# Execute a step
run()
print ('recent ... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Mapping element to element: Function with state
Strictly speaking a function cannot have state; however, we bend the definition here to allow functions with states that operate on streams.
Look at this example: The input and output streams of a function are x and y, respectively; and we want:
y[n] = (x[0] + x[1] + ... ... | @fmap_e
def f(v, state, multiplier):
output = (v + state)*multiplier
next_state = v + state
return output, next_state
# f is a function that maps a stream to a stream
def example():
x = Stream()
# Specify the keyword argument: multiplier
y = f(x, state=0, multiplier=2)
x.extend([1, 2,... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Same example using map_element instead of a decorator | def f(v, state, multiplier):
output = (v + state)*multiplier
next_state = v + state
return output, next_state
def example():
x, y = Stream(), Stream()
map_element(func=f, in_stream=x, out_stream=y, state=0, multiplier=2)
x.extend([1, 2, 3, 4])
# Execute a step
run()
prin... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Saving state in an object
You can save the state of a stream in an object such as a dict as shown in the following example of the Fibonacci sequence. | def example():
x = Stream('x')
# Object in which state is saved
s = {'a':0, 'b':1}
@fmap_e
def fib(v, fib):
# Update state
fib['a'], fib['b'] = fib['a'] + fib['b'], fib['a']
return fib['a'] + v
# Declare stream y
y = fib(x, fib=s)
x.extend([0,... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Filtering elements in a stream
We are given a function f that returns a Boolean. We apply the decorator @filter_e to get a function that takes an input stream and returns an output stream consisting of those elements in the input stream for which f returns True.
In the following example, positive(v) returns True exactl... | from IoTPy.agent_types.basics import filter_e
@filter_e
def positive(v, threshold): return v > threshold
# f is a function that maps a stream to a stream
def example():
x = Stream()
y = positive(x, threshold=0)
x.extend([-1, 2, -3, 4])
# Execute a step
run()
print ('recent values of... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Using filter_element instead of a decorator
Just as you may prefer to use map_element instead of the decorator fmap_e in some situations, you may also prefer to use filter_element instead of the decorator filter_e. The previous example, implemented without decorators, is given next. | from IoTPy.agent_types.op import filter_element
def example():
def positive(v, threshold): return v > threshold
x, y = Stream(), Stream()
filter_element(func=positive, in_stream=x, out_stream=y, threshold=0)
x.extend([-1, 2, -3, 4])
# Execute a step
run()
print ('recent value... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Function that maps list to list
In some cases, using functions that map lists to lists is more convenient than functions that map element to element.
When such a function is decorated with @fmap_l, the function becomes one that maps streams to streams.
Example: Decorate the function increment_odd_numbers | from IoTPy.agent_types.basics import fmap_l
@fmap_l
def increment_odd_numbers(the_list):
return [v+1 if v%2 else v for v in the_list]
def example():
x = Stream()
y = increment_odd_numbers(x)
x.extend([0, 1, 2, 3, 4, 5, 6])
# Execute a step
run()
print ('recent values of stream y ... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Example: incremental computations from list to list
Given a list x we can generate list y where y[j] = x[0] + .. + x[j] by:
y = list(accumulate(x))*
For example, if x = [1, 2, 3] then y = [1, 3, 6]
Now suppose we extend x with the list [4, 5, 6, 7], then we can get the desired y = [1, 3, 6, 10, 15, 21, 28] by calling... | from itertools import accumulate
def incremental_accumulate(the_list, state):
print ("the_list ", the_list)
output_list = [v + state for v in list(accumulate(the_list))]
next_state = output_list[-1]
return output_list, next_state
def example():
x = [1, 2, 3]
y, state = incremental_accumulate(x... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Incremental computations from stream to stream
We can decorate the incremental computation from list to list to obtain a computation from stream to stream. This is illustrated in the next example. | from itertools import accumulate
@fmap_l
def incremental_accumulate(the_list, state):
output_list = [v + state for v in list(accumulate(the_list))]
next_state = output_list[-1]
return output_list, next_state
def example():
x = Stream()
y = incremental_accumulate(x, state=0)
x.extend([10, ... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Example with state and keyword argument
We want to output the elements of the accumulated stream that exceed a threshold. For example, if a stream x is [10, 20, -30, 50, -40] then the accumulation stream is [10, 30, 0, 50, 10] and the elements of the accumulated stream that exceed a threshold of 25 are [30, 50]. | from itertools import accumulate
@fmap_l
def total_exceeds_threshold(the_list, state, threshold):
output_list = [v + state for v in list(accumulate(the_list)) if v + state > threshold]
state += sum(the_list)
return output_list, state
def example():
x = Stream()
y = total_exceeds_threshold(x, state... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Example: function composition
We can also solve the previous problem by concatenating the functions positive and incremental_accumulate. | def example():
x = Stream()
y = positive(incremental_accumulate(x, state=0), threshold=25)
x.extend([10, 20, -30, 50, -40])
# Execute a step
run()
print ('recent values of stream y are')
print (recent_values(y))
x.extend([10, 20, -30])
# Execute a step
run()
pr... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Using list_element instead of the decorator fmap_l
The next example illustrates how map_element can be used with state and keyword arguments. It is the same as the previous example, except that it doesn't use decorators. | from IoTPy.agent_types.basics import map_list
def total_exceeds_threshold(the_list, state, threshold):
output_list = [v + state for v in list(accumulate(the_list)) if v + state > threshold]
state += sum(the_list)
return output_list, state
def example():
x, y = Stream(), Stream()
map_list(func=tota... | examples/FunctionsStreamToStream.ipynb | AssembleSoftware/IoTPy | bsd-3-clause |
Time stepping | # Init Seismograms
Seismogramm=np.zeros((3,nt)); # Three seismograms
# Calculation of some coefficients
i_dx=1.0/(dx)
i_dx3=1.0/(dx**3)
c9=dt**3/24.0
kx=np.arange(5,nx-4)
print("Starting time stepping...")
## Time stepping
for n in range(2,nt):
# Inject source wavelet
p[xscr]=p[xscr]+q[n]
# ... | JupyterNotebook/1D/FD_1D_DX4_DT4_LW_fast.ipynb | florianwittkamp/FD_ACOUSTIC | gpl-3.0 |
Save seismograms | ## Save seismograms
np.save("Seismograms/FD_1D_DX4_DT4_LW_fast",Seismogramm)
## Plot seismograms
fig, (ax1, ax2, ax3) = plt.subplots(3, 1)
fig.subplots_adjust(hspace=0.4,right=1.6, top = 2 )
ax1.plot(t,Seismogramm[0,:])
ax1.set_title('Seismogram 1')
ax1.set_ylabel('Amplitude')
ax1.set_xlabel('Time in s')
ax1.set_xlim... | JupyterNotebook/1D/FD_1D_DX4_DT4_LW_fast.ipynb | florianwittkamp/FD_ACOUSTIC | gpl-3.0 |
Now we'll create a function that will save an animation and embed it in
an html string. Note that this will require ffmpeg or mencoder to be
installed on your system. For reasons entirely beyond my limited understanding
of video encoding details, this also requires using the libx264 encoding
for the resulting mp4 to ... | from tempfile import NamedTemporaryFile
VIDEO_TAG = """<video controls>
<source src="data:video/x-m4v;base64,{0}" type="video/mp4">
Your browser does not support the video tag.
</video>"""
def anim_to_html(anim):
if not hasattr(anim, '_encoded_video'):
with NamedTemporaryFile(suffix='.mp4') as f:
... | AnimationEmbedding.ipynb | cbcoutinho/gravBody2D | gpl-3.0 |
With this HTML function in place, we can use IPython's HTML display tools
to create a function which will show the video inline: | from IPython.display import HTML
def display_animation(anim):
plt.close(anim._fig)
return HTML(anim_to_html(anim)) | AnimationEmbedding.ipynb | cbcoutinho/gravBody2D | gpl-3.0 |
Example of Embedding an Animation
The result looks something like this -- we'll use a basic animation example
taken from my earlier
Matplotlib Animation Tutorial post: | from matplotlib import animation
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
#... | AnimationEmbedding.ipynb | cbcoutinho/gravBody2D | gpl-3.0 |
Making the Embedding Automatic
We can go a step further and use IPython's display hooks to automatically
represent animation objects with the correct HTML. We'll simply set the
_repr_html_ member of the animation base class to our HTML converter
function: | animation.Animation._repr_html_ = anim_to_html | AnimationEmbedding.ipynb | cbcoutinho/gravBody2D | gpl-3.0 |
Now simply creating an animation will lead to it being automatically embedded
in the notebook, without any further function calls: | animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True) | AnimationEmbedding.ipynb | cbcoutinho/gravBody2D | gpl-3.0 |
โฃ๏ธNotes:
* It seems that in ADF, when test statistic is lower than critical value, it's stationary; but in KPSS when test statistic is higher than critical value, it's stationary.
* Also covariance and mean are always overlap here.
* Let's analysis above results:
* The visualization is showing that, standard deviatio... | # Change 1 - Differencing
## I still want to try differencing.
ts_log_diff = ts_log - ts_log.shift(3) # I tried 1, 7 steps too
plt.figure(figsize=(9,7))
plt.plot(ts_log_diff)
plt.show()
ts_log_diff.dropna(inplace=True)
test_stationarity(ts_log_diff) | sequencial_analysis/time_series_stationary_measures.ipynb | hanhanwu/Hanhan_Data_Science_Practice | mit |
โฃ๏ธNotes:
* I tried step=1,7 and 3 here. 1,7 all failed in ADF.
* Let's analysis above results with step=3:
* The visualization is showing that, mean and standard devitation are showing less correlation to the time.
* ADF test statstic is showing 95% confidence of differencing stationary, since the absolute value of... | # Change 2 - Remove trend with moving average
## As we found above, log series seems to be a trend stationary
moving_avg = ts_log.rolling(window=12,center=False).mean() # taking average of LAST 2 years (36-12) values
plt.figure(figsize=(9,7))
plt.plot(ts_log)
plt.plot(moving_avg, color='orange')
plt.show()
ts_log_mov... | sequencial_analysis/time_series_stationary_measures.ipynb | hanhanwu/Hanhan_Data_Science_Practice | mit |
โฃ๏ธNotes:
As we noted above, ts_log looks like trend stationary, so if we remove the trend, the series should be strict stationary.
In change 2, with moving average we can remove the trend. And now both ADF and KPSS are showing 90% confidence of stationary, so ts_log_moving_avg_diff should be strict stationary. | # Change 3 - Remove trend with weighted moving average
expwighted_avg = ts_log.ewm(alpha=0.9,ignore_na=False,min_periods=0,adjust=True).mean()
plt.figure(figsize=(9,7))
plt.plot(ts_log)
plt.plot(expwighted_avg, color='red')
plt.show()
ts_log_ewma_diff = ts_log - expwighted_avg
test_stationarity(ts_log_ewma_diff) | sequencial_analysis/time_series_stationary_measures.ipynb | hanhanwu/Hanhan_Data_Science_Practice | mit |
Network Architecture
The encoder part of the network will be a typical convolutional pyramid. Each convolutional layer will be followed by a max-pooling layer to reduce the dimensions of the layers. The decoder though might be something new to you. The decoder needs to convert from a narrow representation to a wide rec... | learning_rate = 0.001
inputs_ = tf.placeholder(tf.float32, (None, 28, 28,1), nam)
targets_ =
### Encoder
conv1 =
# Now 28x28x16
maxpool1 =
# Now 14x14x16
conv2 =
# Now 14x14x8
maxpool2 =
# Now 7x7x8
conv3 =
# Now 7x7x8
encoded =
# Now 4x4x8
### Decoder
upsample1 =
# Now 7x7x8
conv4 =
# Now 7x7x8
upsample2 = ... | autoencoder/Convolutional_Autoencoder.ipynb | tkurfurst/deep-learning | mit |
What we want is $C^1$ continuity, i.e., first-derivatives are continuous, without needing values of f_left and f_right outside where they are defined.
To accomplish this, we'll join the second derivatives of both functions by a line, around a region $[x_0, x_1]$ that contains the threshold: | d_dx_f_left_ = sympy.diff(f_left_, x_, 1)
d_dx_f_right_ = sympy.diff(f_right_, x_, 1)
d_dx_f_ = Piecewise(
(d_dx_f_left_, x_ < x_threshold),
(d_dx_f_right_, True)
)
d_dx_f = sympy.lambdify(x_, d_dx_f_)
d_dx_f_left = sympy.lambdify(x_, d_dx_f_left_)
d_dx_f_right = sympy.lambdify(x_, d_dx_f_right_)
plt.plot(... | smooth_transition_between_analytic_functions.ipynb | ESSS/notebooks | mit |
The derivative is indeed very discontinuous! Let's fix this!
They do not need to be symmetric around the threshold | x_0 = x_threshold * 0.8
x_1 = x_threshold * 1.2
d_dx_at_x_0 = d_dx_f_left_.evalf(subs={x_: x_0})
d_dx_at_x_1 = d_dx_f_right_.evalf(subs={x_: x_1})
d_dx_f_center_ = d_dx_at_x_0 + ((x_ - x_0) / (x_1 - x_0)) * (d_dx_at_x_1 - d_dx_at_x_0)
d_dx_f_smooth_ = Piecewise(
(d_dx_f_left_, x_ < x_0),
(d_dx_f_center_, x_ ... | smooth_transition_between_analytic_functions.ipynb | ESSS/notebooks | mit |
So now we just have to integrate d_dx_f_center and adjust it's integral constant to create a better piecewise f_smooth | f_center_ = sympy.integrate(d_dx_f_center_, x_)
f_left = sympy.lambdify(x_, f_left_)
f_center = sympy.lambdify(x_, f_center_)
# f_center(x0) == f_left(x0)
f_center_ = f_center_ + (f_left(x_0) - f_center(x_0))
f_smooth_ = Piecewise(
(f_left_, x_ < x_0),
(f_center_, x_ < x_1),
(f_right_, True)
)
f_smooth ... | smooth_transition_between_analytic_functions.ipynb | ESSS/notebooks | mit |
Much better!
Now let's generalize a way to create f_center: | sympy.init_printing()
x_0_, x_1_, f_0_ = sympy.symbols('x_0,x_1,f_{left}(x_0)', real=True)
# df_0 is f_left'(x_0)
# df_1 is f_right'(x_1)
df_0_, df_1_ = sympy.symbols('df_0,df_1', real=True)
a = (x_ - x_0_) / (x_1_ - x_0_)
# d_dx_f_center_ = d_dx_at_x_0 + a * (d_dx_at_x_1 - d_dx_at_x_0)
d_dx_f_center_ = (1 - a) * ... | smooth_transition_between_analytic_functions.ipynb | ESSS/notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืคืขืืื ื ืจืฆื ืืฉื ืืช ืืช ืกืืจ ืืืจืืืื ืืื ืฉืื ืื ื ืฉืืืืื ืืคืื ืงืฆืื.<br>
ื ืขืฉื ืืืช ืืงืจืืื ืืคืื ืงืฆืื, ืขืึพืืื ืืขืืจืช ืฉื ืืืจืืืื ื ืืืืืจ ืืื ืืขืืจืช ืืขืจื ืฉืื ืื ื ืจืืฆืื ืืืขืืืจ ืืืื:
</p> | my_range(start=0, end=5) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืฉืืจื ืืื ืืคืื ื ืืช ืกืืจ ืืืจืืืื ืืื.<br>
ืืืืื ืฉืืงืจืืื ืืชืื ื ืืช ืฉืืืช ืืคืจืืืจืื ืืชืืืืื ืืืืชืจืช ืืคืื ืงืฆืื, ืืขืจืืื ื ืฉืืื ืืืงืื ืื ืืื.<br>
ืืฉืืื ืืื ื ืงืจืืช <dfn>keyword arguments</dfn> (<dfn>"ืืจืืืื ืืื ืืคื ืฉื"</dfn>), ืืื ืื ืื ื ืืขืืืจืื ืืช ืืืจื... | import random
random.randrange(100, 200) # ืืืื ืคืืืช
random.randrange(start=100, stop=200) # ืืืื ืืืชืจ | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<div class="align-center" style="display: flex; text-align: right; direction: rtl;">
<div style="display: flex; width: 10%; float: right; ">
<img src="images/warning.png" style="height: 50px !important;" alt="ืืืืจื!">
</div>
<div style="width: 90%">
<p style="text-align: right; direction: r... | ghibli_release_dates = {
'Castle in the Sky': '1986-08-02',
'My Neighbor Totoro': '1988-04-16',
'Spirited Away': '2001-07-20',
'Ponyo': '2008-07-19',
}
ponyo_release_date = ghibli_release_dates.get('Ponyo')
men_in_black_release_date = ghibli_release_dates.get('Men in Black')
print(f"Ponyo release date:... | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ื ืืืฉ ืืช ืืคืื ืงืฆืื <code>get</code> ืืขืฆืื ื. ืืฉื ืื ืืืืช, ืืืจืื ืืฉืืืืฉ ืฉืื ื ืืืงืฆืช:<br>
</p> | def get(dictionary, key):
if key in dictionary:
return dictionary[key]
return None
ponyo_release_date = get(ghibli_release_dates, 'Ponyo')
men_in_black_release_date = get(ghibli_release_dates, 'Men in Black')
print(f"Ponyo release date: {ponyo_release_date}")
print(f"Men in Black release date: ... | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืืืืืฉ ืฉืื ื ืื ืืืฉืื. ืืคืขืืื ืืืงืืจืืช, <code>get</code> ืขื ืืืืื, ืคืืขืืช ืืฆืืจื ืืชืืืืืช ืืืชืจ.<br>
ืืคืฉืจ ืืืขืืืจ ืื ืคืจืืืจ ื ืืกืฃ, ืฉืงืืืข ืื ืืืืืจ ืื ืืืคืชื ืฉืืขืืจื ื ืืคืจืืืจ ืืจืืฉืื ืื ื ืืฆื ืืืืืื:
</p> | ponyo_release_date = ghibli_release_dates.get('Ponyo', '???')
men_in_black_release_date = ghibli_release_dates.get('Men in Black', '???')
print(f"Ponyo release date: {ponyo_release_date}")
print(f"Men in Black release date: {men_in_black_release_date}") | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืฉืืื ืื ืืืชื ืืืืช ืืืืืืืช ืฉื ืืคืขืืื <code>get</code>!<br>
ืื ืืืคืชื ืฉืืขืืจื ื ืืืจืืืื ื ืืจืืฉืื ืื ืงืืื ืืืืืื, ืืื ืืืืืจื ืืช ืืขืจื ืฉืืชืื ืืืจืืืื ื ืืฉื ื.<br>
ืืคืฉืจ ืืืขืืืจ ืื ืืจืืืื ื ืืื, ืืืคืฉืจ ืืืขืืืจ ืื ืฉื ื ืืจืืืื ืืื. ืืื ืืชืคืงืืช ืืจืืื ืืฉื ื ืืืฆ... | def get_new_prices(l):
l2 = []
for item in l:
l2.append(item + 1)
return l2
prices = [42, 73, 300]
print(get_new_prices(prices)) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืชืื ืืื ืงืฆืจ ืืคืื ืงืฆืื ืฉืื ืื ื ืืืคืืช ืืืืื ืืืกืืจื ืืื ืืืืช ืืืืจืืื.<br>
ืื ืื ืงืจืื ืืืืจืืื ืืืืึพืืืืื ื'ืืืคื ืืจืื ืืืฆืจ ืืืชื ื ืงืฉืจ, ืืืืงืฉ ืืฉืืื ืืชืืื ื ืื ืฉืืืื ืืืขืืืช ืืช ืืืืจื ืืืืฆืจืื ืืจืฆืื ื.<br>
ืืื ืืขืืื ืืืจืืฉื, ื ืื ื ืคืื ืงืฆืื ืฉืืงืืืช ืจืฉืืื,... | def get_new_prices(l, increment_by):
l2 = []
for item in l:
l2.append(item + increment_by)
return l2
prices = [42, 73, 300]
print(get_new_prices(prices, 1))
print(get_new_prices(prices, 2)) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืจืื ืคืืฆื ืืฉืืจื ืืจืื ืืืฉืจ, ืืืืงืฉ ืฉืืืื ืืืจืื ืืคืื ืงืฆืื, ืื ืืคืฉืจ.<br>
ืื ืืงืืจื ืืคืื ืงืฆืื ืืขืืืจ ืื ืจืง ืืช ืจืฉืืืช ืืืืืจืื, ืืขืื ืืช ืื ืืืืืจืื ืืฉืงื, ืืืจืืจืช ืืืื.<br>
ืื ืื ืืืขืืจ ืืืจืืืื ื ืืฉื ื, ืืขืื ืืช ืืืืืจ ืืคื ืืขืจื ืฉืฆืืื ืืืืชื ืืจืืืื ื.<br>
<... | def get_new_prices(l, increment_by=1):
l2 = []
for item in l:
l2.append(item + increment_by)
return l2
prices = [42, 73, 300]
print(prices)
print(get_new_prices(prices))
print(get_new_prices(prices, 5)) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืฉืื ืื ื ืจืืฆืื ืืืืืืจ ืคืจืืืจ ืขื ืขืจื ืืจืืจืช ืืืื, ื ืืื ืืงืืืข ืืช ืขืจื ืืจืืจืช ืืืืื ืฉืื ืืืืชืจืช ืืคืื ืงืฆืื.<br>
ืื ืืืขืืจ ืืจืืืื ื ืฉืืื ืืคืื ืงืฆืื โ ืคืืืชืื ืชืฉืชืืฉ ืืขืจื ืฉืืืขืืจ.<br>
ืื ืื โ ืืืืงื ืขืจื ืืจืืจืช ืืืืื ืฉืืืืืจ ืืืืชืจืช ืืคืื ืงืฆืื.
</p>
<p style=... | print(get_new_prices(prices, 5))
print(get_new_prices(prices)) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<div class="align-center" style="display: flex; text-align: right; direction: rtl; clear: both;">
<div style="display: flex; width: 10%; float: right; clear: both;">
<img src="images/exercise.svg" style="height: 50px !important;" alt="ืชืจืืื">
</div>
<div style="width: 70%">
<p style="text-a... | def get_new_prices(l, increment_by=1, discount=0):
l2 = []
for item in l:
new_price = item + increment_by - discount
l2.append(new_price)
return l2
prices = [42, 73, 300]
print(prices)
print(get_new_prices(prices, 10, 1)) # ืืขืืื ืฉื 10, ืื ืื ืฉื 1
print(get_new_prices(prices, 5)) # ืืขืืื ... | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืื ืื ืืงืจื ืืฉื ืจืฆื ืืชืช ืจืง ืื ืื?<br>
ืืืงืจื ืืื, ืืฉื ืจืฆื "ืืืื" ืืขื ืืื ืืขืจืื ืืจืืจืช ืืืืื, ื ืฆืืจื ืืืขืืืจ ืืช ืฉืืืช ืืคืจืืืจืื ืืงืจืืื ืืคืื ืงืฆืื.<br>
ืืืืืื ืืืื ืื ืื ื ืืขืืื ืืช ืืืืืจ ืึพ1 (ืื ืื ืืจืืจืช ืืืืื), ืืืืจืืืื ืืืชื ืึพ5:
</p> | prices = [42, 73, 300]
print(prices)
print(get_new_prices(prices, discount=5)) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืื ืืื ื ืขื ืืื ืฉื ืกืื ืื, ืืื ืืฉ ืืืคื ืืกืืจ ืืฆืืื ืฉืืืช ืืคืจืืืจืื ืื ืืฉืื ืืืืืื:
</p> | print(get_new_prices(prices, increment_by=10, discount=1)) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<span style="text-align: right; direction: rtl; float: right; clear: both;">ืืกืคืจ ืืฉืชื ื ืฉื ืืจืืืื ืืื<span>
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืคืื ืงืฆืื ืืคืืืชืื ืืช <code>max</code>, ืืืฉื, ืืชื ืืืช ืืืืคื ืืฉืื ื.<br>
ืืื ืืืืขืช ืืงืื ืื ืืกืคืจ ืฉืืื ืฉื ืืจืืืื ืืื, ืืืืืืื ืื ืืื ืืื ืืืื... | max(13, 256, 278, 887, 989, 457, 6510, 18, 865, 901, 401, 704, 640) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ื ืืื ืื ืื ืื ื ืืืืฉ ืคืื ืงืฆืื ืฉืืงืืืช ืืกืคืจ ืืฉืชื ื ืฉื ืคืจืืืจืื ืื ืืงืืืช.<br>
ื ืชืืื ืืืืืฉ ืคืื ืงืฆืื ืืืคืฉืืช ืืืื, ืฉืืงืืืช ืืกืคืจ ืืฉืชื ื ืฉื ืคืจืืืจืื ืืืืคืืกื ืืืชื:
</p> | def silly_function(*parameters):
print(parameters)
print(type(parameters))
print('-' * 20)
silly_function('Shmulik', 'Shlomo')
silly_function('Shmulik', 1, 1, 2, 3, 5, 8, 13)
silly_function() | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืื ืืชืจืืฉ ืืืืืื ืืืืจืื ื, ืืขืฆื?<br>
ืืฉืคืจืืืจ ืืืืืจ ืืืืชืจืช ืืคืื ืงืฆืื ืขื ืืกืืื ืืืืืืช, ืืคืฉืจ ืืฉืืื ืื ืืืชื ืคืจืืืจ ืืกืคืจ ืืืชื ืืืืื ืฉื ืืจืืืื ืืื.<br>
ืืขืจื ืฉืืืื ืก ืืคืจืืืจ ืืืื ืืกืื <code>tuple</code>, ืฉืืืืจืื ืื ืื ืืืืืจืื ืฉืืืขืืจื ืืืจืืืื ืืื.
</... | def silly_function2(*parameters):
print(f"Printing all the items in {parameters}:")
for parameter in parameters:
print(parameter)
print("-" * 20)
silly_function2('Shmulik', 'Shlomo')
silly_function2('Shmulik', 1, 1, 2, 3, 5, 8, 13)
silly_function2() | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<div class="align-center" style="display: flex; text-align: right; direction: rtl; clear: both;">
<div style="display: flex; width: 10%; float: right; clear: both;">
<img src="images/exercise.svg" style="height: 50px !important;" alt="ืชืจืืื">
</div>
<div style="width: 70%">
<p style="text-a... | def my_max(*numbers):
if not numbers: # ืื ืื ืกืืคืงื ืืจืืืื ืืื, ืืื ืืงืกืืืื
return None
maximum = numbers[0]
for number in numbers:
if number > maximum:
maximum = number
return maximum
my_max(13, 256, 278, 887, 989, 457, 6510, 18, 865, 901, 401, 704, 640) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืืชืจืช ืืคืื ืงืฆืื ืืืืื ืืืืื ืืฉืชื ืื ื ืืกืคืื ืืคื ื ืืืืืืืช.<br>
ื ืจืื ืืืืืื ืคืื ืงืฆืื ืฉืืงืืืช ืืืื ืื ืื ืืืช ืืืืจื ืื ืืืืฆืจืื ืฉืงื ืื ื, ืืืืืืจื ืืช ืืกืืื ืืกืืคื ืฉืขืืื ื ืืฉืื:
</p> | def get_final_price(discount, *prices):
return sum(prices) - discount
get_final_price(10000, 3.141, 90053) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืฃ ืฉืืืื ืจืืฉืื ืืคืื ืงืฆืื <code>get_final_price</code> ืขืฉืืื ืืืืจืืืช ืืื ืืื, ืืืื ืืืืืืจ ืืฉืืืืฉ ืืืืื ืืชืืื ื ืืื ืฉื ืคืืืชืื.<br>
ืืืืืื ืืื ืืื ื ืืืืืื ืืืืฉืืช ืืืฆืืช ืืืคื ืฉื ืคืืืชืื, ืืื ืืืื ืืื ืืืืื ืืจืืขื ืืืื ืืฉืืืืฉ ืืืืืืืช.
</p>
<p style... | def get_final_price(prices, discount):
return sum(prices) - discount
get_final_price(prices=(3.141, 90053), discount=10000) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<span style="text-align: right; direction: rtl; float: right; clear: both;">ืชืจืืื ืืื ืืื: ืกืืื ืืจื<span>
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืชืื ืคืื ืงืฆืื ืืฉื <code>create_path</code> ืฉืืืืื ืืงืื ืืกืคืจ ืืืชื ืืืืื ืฉื ืืจืืืื ืืื.<br>
ืืคืจืืืจ ืืจืืฉืื ืืืื ืืืช ืืืื ื ืฉืื ืืงืืฆืื ืืืืืก... | def print_introduction(name, age):
return f"My name is {name} and I am {age} years old."
print_introduction(age=2019, name="Gandalf") | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืื ืื ืื ื ืจืฆื ืืืขืืืจ ืืคืื ืงืฆืื ืฉืื ื ืืกืคืจ ืืืชื ืืืืื ืฉื ืืจืืืื ืืื ืืคื ืฉื?<br>
ื ืืื ืืืืืื ืืช ืืคืขืืื <code>format</code> ืขื ืืืจืืืืช.<br>
<code>format</code> ืืื ืคืื ืงืฆืื ืืืืฉื ืืื ืื ืืืข ืืืืืช ืืืฉืืืช ืฉื ืืืจืืืื ืืื ืฉืืืขืืจืื ืื ืืคื ืฉื.<br>
... | message = "My name is {name} and I am {age} years old"
formatted_message = message.format(name="Gandalf", age=2019)
print(formatted_message)
song = "I'll {action} a story of a {animal}.\nA {animal} who's {key} is {value}."
formatted_song = song.format(action="sing", animal="duck", key="name", value="Alfred Kwak")
prin... | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ื ืืชืื ืื ืื ืื ื ืคืื ืงืฆืื ืฉืืกืืืืช ืืงืื ืืกืคืจ ืืืชื ืืืืื ืฉื ืืจืืืื ืืื ืืคื ืฉื.<br>
ื ืืขืืจ ืชืืืื ืืืืืืชื ื ืืืืชืืงื, <code>silly_function</code>, ืืื ืืจืืืช ืืื ืืงืกื ืงืืจื:
</p> | def silly_function(**kwargs):
print(kwargs)
print(type(kwargs))
silly_function(a=5, b=6, address="221B Baker St, London, England.") | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right; clear: both;">
ืืืชื ืืืืช ืืื ืืชืจืืฉืช ืืืืื ืฉืืฉืชืืฉื ื ืืฉืชื ืืืืืืืช ืืคื ื ืฉื ืืืฉืชื ื.<br>
ืืฉืืืืฉ ืืฉืชื ืืืืืืืช ืืืคืฉืจ ืื ื ืืืขืืืจ ืืกืคืจ ืืืชื ืืืืื ืฉื ืืจืืืื ืืื ืขื ืฉื, ืืืืคื ืฉืืืืืจ ืงืฆืช ืืช ืืฉืืืืฉ ืืืืืืืช ืฉืจืืื ื ืงืืื.<br>
ืืืฉืชื ื ืฉืื ื ืฉืืจืื ืื ืชืื ืื ืืื ืืกืื ืืืืื,... | def print_sushi_recipe(**ingredients_and_amounts):
for ingredient, amount in ingredients_and_amounts.items():
print(f"{amount} grams of {ingredient}")
print_sushi_recipe(rice=300, water=300, vinegar=15, sugar=10, salt=3, fish=600) | week05/2_Functions_Part_2.ipynb | PythonFreeCourse/Notebooks | mit |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.