markdown stringlengths 0 1.02M | code stringlengths 0 832k | output stringlengths 0 1.02M | license stringlengths 3 36 | path stringlengths 6 265 | repo_name stringlengths 6 127 |
|---|---|---|---|---|---|
Finding the most important node i.e character in these networks.Let's use our network analysis knowledge to decrypt these Graphs that we have just created.Is it Jon Snow, Tyrion, Daenerys, or someone else? Let's see! Network Science offers us many different metrics to measure the importance of a node in a network as w... | # We use the in-built degree_centrality method
deg_cen_book1 = nx.degree_centrality(graphs[0])
deg_cen_book5 = nx.degree_centrality(graphs[4]) | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
`degree_centrality` returns a dictionary and to access the results we can directly use the name of the character. | deg_cen_book1['Daenerys-Targaryen'] | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
Top 5 important characters in the first book according to degree centrality. | # The following expression sorts the dictionary by
# degree centrality and returns the top 5 from a graph
sorted(deg_cen_book1.items(),
key=lambda x:x[1],
reverse=True)[0:5] | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
Top 5 important characters in the fifth book according to degree centrality. | sorted(deg_cen_book5.items(),
key=lambda x:x[1],
reverse=True)[0:5] | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
To visualize the distribution of degree centrality let's plot a histogram of degree centrality. | plt.hist(deg_cen_book1.values(), bins=30)
plt.show() | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
The above plot shows something that is expected, a high portion of characters aren't connected to lot of other characters while some characters are highly connected all through the network. A close real world example of this is a social network like Twitter where a few people have millions of connections(followers) but... | # A log-log plot to show the "signature" of power law in graphs.
from collections import Counter
hist = Counter(deg_cen_book1.values())
plt.scatter(np.log2(list(hist.keys())),
np.log2(list(hist.values())),
alpha=0.9)
plt.show() | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
ExerciseCreate a new centrality measure, weighted_degree(Graph, weight) which takes in Graph and the weight attribute and returns a weighted degree dictionary. Weighted degree is calculated by summing the weight of the all edges of a node and find the top five characters according to this measure. | from nams.solutions.got import weighted_degree
plt.hist(list(weighted_degree(graphs[0], 'weight').values()), bins=30)
plt.show()
sorted(weighted_degree(graphs[0], 'weight').items(), key=lambda x:x[1], reverse=True)[0:5] | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
Betweeness centralityLet's do this for Betweeness centrality and check if this makes any difference. As different centrality method use different measures underneath, they find nodes which are important in the network. A centrality method like Betweeness centrality finds nodes which are structurally important to the n... | # First check unweighted (just the structure)
sorted(nx.betweenness_centrality(graphs[0]).items(),
key=lambda x:x[1], reverse=True)[0:10]
# Let's care about interactions now
sorted(nx.betweenness_centrality(graphs[0],
weight='weight_inv').items(),
key=lambda x:x[1], reverse=True)[0:10] | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
We can see there are some differences between the unweighted and weighted centrality measures. Another thing to note is that we are using the weight_inv attribute instead of weight(the number of interactions between characters). This decision is based on the way we want to assign the notion of "importance" of a charact... | # by default weight attribute in PageRank is weight
# so we use weight=None to find the unweighted results
sorted(nx.pagerank_numpy(graphs[0],
weight=None).items(),
key=lambda x:x[1], reverse=True)[0:10]
sorted(nx.pagerank_numpy(
graphs[0], weight='weight').items(),
key=lambda x:x[1], reverse=Tr... | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
Exercise Is there a correlation between these techniques?Find the correlation between these four techniques.- pagerank (weight = 'weight')- betweenness_centrality (weight = 'weight_inv')- weighted_degree- degree centralityHINT: Use pandas correlation | from nams.solutions.got import correlation_centrality
correlation_centrality(graphs[0]) | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
Evolution of importance of characters over the booksAccording to degree centrality the most important character in the first book is Eddard Stark but he is not even in the top 10 of the fifth book. The importance changes over the course of five books, because you know stuff happens ;)Let's look at the evolution of deg... | evol = [nx.degree_centrality(graph)
for graph in graphs]
evol_df = pd.DataFrame.from_records(evol).fillna(0)
evol_df[['Eddard-Stark',
'Tyrion-Lannister',
'Jon-Snow']].plot()
plt.show()
set_of_char = set()
for i in range(5):
set_of_char |= set(list(
evol_df.T[i].sort_values(
... | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
ExercisePlot the evolution of betweenness centrality of the above mentioned characters over the 5 books. | from nams.solutions.got import evol_betweenness
evol_betweenness(graphs) | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
So what's up with Stannis Baratheon? | sorted(nx.degree_centrality(graphs[4]).items(),
key=lambda x:x[1], reverse=True)[:5]
sorted(nx.betweenness_centrality(graphs[4]).items(),
key=lambda x:x[1], reverse=True)[:5]
nx.draw(nx.barbell_graph(5, 1), with_labels=True) | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
As we know the a higher betweenness centrality means that the node is crucial for the structure of the network, and in the case of Stannis Baratheon in the fifth book it seems like Stannis Baratheon has characterstics similar to that of node 5 in the above example as it seems to be the holding the network together.As e... | nx.betweenness_centrality(nx.barbell_graph(5, 1)) | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
Community detection in NetworksA network is said to have community structure if the nodes of the network can be easily grouped into (potentially overlapping) sets of nodes such that each set of nodes is densely connected internally. There are multiple algorithms and definitions to calculate these communites in a netwo... | import nxviz as nv
from nxviz import annotate
plt.figure(figsize=(8, 8))
partition = community.best_partition(graphs[0], randomize=False)
# Annotate nodes' partitions
for n in graphs[0].nodes():
graphs[0].nodes[n]["partition"] = partition[n]
graphs[0].nodes[n]["degree"] = graphs[0].degree(n)
nv.matrix(gr... | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
A common defining quality of a community is thatthe within-community edges are denser than the between-community edges. | # louvain community detection find us 8 different set of communities
partition_dict = {}
for character, par in partition.items():
if par in partition_dict:
partition_dict[par].append(character)
else:
partition_dict[par] = [character]
len(partition_dict)
partition_dict[2] | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
If we plot these communities of the network we see a denser network as compared to the original network which contains all the characters. | nx.draw(nx.subgraph(graphs[0], partition_dict[3]))
nx.draw(nx.subgraph(graphs[0],partition_dict[1])) | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
We can test this by calculating the density of the network and the community.Like in the following example the network between characters in a community is 5 times more dense than the original network. | nx.density(nx.subgraph(
graphs[0], partition_dict[4])
)/nx.density(graphs[0]) | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
Exercise Find the most important node in the partitions according to degree centrality of the nodes using the partition_dict we have already created. | from nams.solutions.got import most_important_node_in_partition
most_important_node_in_partition(graphs[0], partition_dict) | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
SolutionsHere are the solutions to the exercises above. | from nams.solutions import got
import inspect
print(inspect.getsource(got)) | _____no_output_____ | MIT | notebooks/05-casestudies/01-gameofthrones.ipynb | khanin-th/Network-Analysis-Made-Simple |
SICP 习题 (2.8) 解题总结:区间的减法 SICP 习题 2.8 需要我们完成区间运算的减法,区间运算的加法书中已经有了,代码如下: | (define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y)))) | _____no_output_____ | MIT | cn/.ipynb_checkpoints/sicp-2-08-checkpoint.ipynb | DamonDeng/sicp_exercise |
以上代码很简单,就是计算区间的加法时将两个区间的起点相加,称为新区间的起点,然后将两个区间的终点相加,成为新区间的终点。减法时加法的逆运算,我们看着加法的代码照猫画虎一番就可以了,代码如下: | (define (sub-interval x y)
(make-interval (- (lower-bound x) (lower-bound y))
(- (upper-bound x) (upper-bound y)))) | _____no_output_____ | MIT | cn/.ipynb_checkpoints/sicp-2-08-checkpoint.ipynb | DamonDeng/sicp_exercise |
Learning Tree Structure from Data using the Chow-Liu Algorithm In this notebook, we show an example for learning the structure of a Bayesian Network using the Chow-Liu algorithm. We will first build a model to generate some data and then attempt to learn the model's graph structure back from the generated data. Fir... | import networkx as nx
import matplotlib.pyplot as plt
from pgmpy.models import BayesianNetwork
# construct the tree graph structure
model = BayesianNetwork([('A', 'B'), ('A', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'F')])
nx.draw_circular(model, with_labels=True, arrowsize=30, node_size=800, alpha=0.3, font_weight='bold'... | _____no_output_____ | MIT | examples/Structure Learning with Chow-Liu.ipynb | vbob/pgmpy |
Then, add CPDs to our tree to create a Bayesian network | from pgmpy.factors.discrete import TabularCPD
# add CPD to each edge
cpd_a = TabularCPD('A', 2, [[0.4], [0.6]])
cpd_b = TabularCPD('B', 3, [[0.6,0.2],[0.3,0.5],[0.1,0.3]], evidence=['A'], evidence_card=[2])
cpd_c = TabularCPD('C', 2, [[0.3,0.4],[0.7,0.6]], evidence=['A'], evidence_card=[2])
cpd_d = TabularCPD('D', 3, ... | _____no_output_____ | MIT | examples/Structure Learning with Chow-Liu.ipynb | vbob/pgmpy |
Next, generate sample data from our tree Bayesian network | from pgmpy.sampling import BayesianModelSampling
# sample data from BN
inference = BayesianModelSampling(model)
df_data = inference.forward_sample(size=10000)
print(df_data)
| Generating for node: D: 100%|██████████| 6/6 [00:00<00:00, 275.41it/s] | MIT | examples/Structure Learning with Chow-Liu.ipynb | vbob/pgmpy |
Finally, apply the Chow-Liu algorithm to learn the tree graph from sample data | from pgmpy.estimators import TreeSearch
# learn graph structure
est = TreeSearch(df_data, root_node="A")
dag = est.estimate(estimator_type="chow-liu")
nx.draw_circular(dag, with_labels=True, arrowsize=30, node_size=800, alpha=0.3, font_weight='bold')
plt.show()
| Building tree: 100%|██████████| 15/15.0 [00:00<00:00, 4518.10it/s]
| MIT | examples/Structure Learning with Chow-Liu.ipynb | vbob/pgmpy |
To parameterize the learned graph from data, check out the other tutorials for more info | from pgmpy.estimators import BayesianEstimator
# there are many choices of parametrization, here is one example
model = BayesianNetwork(dag.edges())
model.fit(df_data, estimator=BayesianEstimator, prior_type='dirichlet', pseudo_counts=0.1)
model.get_cpds() | _____no_output_____ | MIT | examples/Structure Learning with Chow-Liu.ipynb | vbob/pgmpy |
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. Inference PyTorch GPT2 Model with ONNX Runtime on CPUIn this tutorial, you'll be introduced to how to load a GPT2 model from PyTorch, convert it to ONNX, and inference it using ONNX Runtime.**Note: this work is still in progress... | # Enable pass state in input.
enable_past_input = False
import os
cache_dir = "./gpt2"
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
output_dir = './gpt2_onnx'
if not os.path.exists(output_dir):
os.makedirs(output_dir) | _____no_output_____ | MIT | onnxruntime/python/tools/bert/notebooks/Inference_GPT2_with_OnnxRuntime_on_CPU.ipynb | lienching/onnxruntime |
Benchmark You will need git clone the onnxruntime repository like```consolegit clone https://github.com/microsoft/onnxruntime.git```Then update the bert_tools_dir according to the path in your machine. | # Assume you have git clone the repository of onnxruntime from github.
bert_tools_dir = r'D:\Git\onnxruntime\onnxruntime\python\tools\bert'
benchmark_script = os.path.join(bert_tools_dir, 'benchmark_gpt2.py')
if enable_past_input:
%run $benchmark_script --model_type gpt2 --cache_dir $cache_dir --output_dir $output... | _____no_output_____ | MIT | onnxruntime/python/tools/bert/notebooks/Inference_GPT2_with_OnnxRuntime_on_CPU.ipynb | lienching/onnxruntime |
If you only need the benchmark results. You can skip the remaining parts.In the following, we will introduce the benchmark script. Load pretrained model | from transformers import GPT2Model, GPT2Tokenizer
model_class, tokenizer_class, model_name_or_path = (GPT2Model, GPT2Tokenizer, 'gpt2')
tokenizer = tokenizer_class.from_pretrained(model_name_or_path, cache_dir=cache_dir)
model = model_class.from_pretrained(model_name_or_path, cache_dir=cache_dir)
model.eval().cpu()
... | _____no_output_____ | MIT | onnxruntime/python/tools/bert/notebooks/Inference_GPT2_with_OnnxRuntime_on_CPU.ipynb | lienching/onnxruntime |
Inference with ONNX Runtime OpenMP Environment VariableOpenMP environment variables are very important for CPU inference of GPT2 model. It has large performance impact on GPT2 model so you might need set it carefully according to benchmark script.Setting environment variables shall be done before importing onnxruntime... | import psutil
# You may change the settings in this cell according to Performance Test Tool result.
use_openmp = True
# ATTENTION: these environment variables must be set before importing onnxruntime.
if use_openmp:
os.environ["OMP_NUM_THREADS"] = str(psutil.cpu_count(logical=True))
else:
os.environ["OMP_NUM_... | _____no_output_____ | MIT | onnxruntime/python/tools/bert/notebooks/Inference_GPT2_with_OnnxRuntime_on_CPU.ipynb | lienching/onnxruntime |
Additional InfoNote that running Jupyter Notebook has slight impact on performance result since Jupyter Notebook is using system resources like CPU and memory etc. It is recommended to close Jupyter Notebook and other applications, then run the benchmark script in a console to get more accurate performance numbers.[On... | machine_info_script = os.path.join(bert_tools_dir, 'MachineInfo.py')
%run $machine_info_script --silent | _____no_output_____ | MIT | onnxruntime/python/tools/bert/notebooks/Inference_GPT2_with_OnnxRuntime_on_CPU.ipynb | lienching/onnxruntime |
Tuberculosis WHO General setup.___ | import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = [12, 8] | _____no_output_____ | MIT | Data cleaning/Tuberculosis.ipynb | olgarozhdestvina/Data-Science-and-Machine-Learning |
Load the data set.___ | # Load the data set
tb = pd.read_csv('../Data/tuberculosis.csv')
tb.head()
tb.tail() | _____no_output_____ | MIT | Data cleaning/Tuberculosis.ipynb | olgarozhdestvina/Data-Science-and-Machine-Learning |
There are several issues with the data set: * Missing values* Confusing names (for example, m04 means male 0-4 years old) | tb.columns
# Plot the columns from m04 to fu for the last row in the data set.
plt.plot(tb.loc[5768, 'm04':'fu'])
plt.show()
# And now the same as above for all rows
for _, row in tb.iterrows():
plt.plot(row['m04':'fu'], color='C0', alpha=0.1) | _____no_output_____ | MIT | Data cleaning/Tuberculosis.ipynb | olgarozhdestvina/Data-Science-and-Machine-Learning |
Data cleaning.___ | # Melt columns from m04 to fu into a sex and cases columns
tb_melt = tb.melt(tb.columns[:2], tb.columns[2:], 'sex_age', 'cases')
tb_melt
# Created a new column 'age' from 'sex'
tb_melt['age'] = tb_melt.sex_age.apply(lambda x: x[1:])
tb_melt['age']
def age_format(x):
""" Reformatting age column """
if len(x) == ... | _____no_output_____ | MIT | Data cleaning/Tuberculosis.ipynb | olgarozhdestvina/Data-Science-and-Machine-Learning |
Simple Linear Regression. Minimal example Using the same code as before, please solve the following exercises 1. Change the number of observations to 100,000 and see what happens. 2. Change the number of observations to 1,000,000 and see what happens. 3. Play around with the learning rate. Values like 0.0001... | # We must always import the relevant libraries for our problem at hand. NumPy is a must for this example.
import numpy as np
# matplotlib and mpl_toolkits are not necessary. We employ them for the sole purpose of visualizing the results.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D | _____no_output_____ | MIT | course_2/course_material/Part_7_Deep_Learning/S43_L300/Minimal_example_All_Exercises.ipynb | Alexander-Meldrum/learning-data-science |
Generate random input data to train on | # First, we should declare a variable containing the size of the training set we want to generate.
observations = 1000
# We will work with two variables as inputs. You can think about them as x1 and x2 in our previous examples.
# We have picked x and z, since it is easier to differentiate them.
# We generate them rand... | (1000, 2)
| MIT | course_2/course_material/Part_7_Deep_Learning/S43_L300/Minimal_example_All_Exercises.ipynb | Alexander-Meldrum/learning-data-science |
Generate the targets we will aim at | # We want to "make up" a function, use the ML methodology, and see if the algorithm has learned it.
# We add a small random noise to the function i.e. f(x,z) = 2x - 3z + 5 + <small noise>
noise = np.random.uniform(-1, 1, (observations,1))
# Produce the targets according to the f(x,z) = 2x - 3z + 5 + noise definition.
... | (1000, 1)
| MIT | course_2/course_material/Part_7_Deep_Learning/S43_L300/Minimal_example_All_Exercises.ipynb | Alexander-Meldrum/learning-data-science |
Plot the training dataThe point is to see that there is a strong trend that our model should learn to reproduce. Initialize variables | # We will initialize the weights and biases randomly in some small initial range.
# init_range is the variable that will measure that.
# You can play around with the initial range, but we don't really encourage you to do so.
# High initial ranges may prevent the machine learning algorithm from learning.
init_range = 0.... | [[0.02158668]
[0.04520037]]
[0.07680059]
| MIT | course_2/course_material/Part_7_Deep_Learning/S43_L300/Minimal_example_All_Exercises.ipynb | Alexander-Meldrum/learning-data-science |
Set a learning rate | # Set some small learning rate (denoted eta in the lecture).
# 0.02 is going to work quite well for our example. Once again, you can play around with it.
# It is HIGHLY recommended that you play around with it.
learning_rate = 0.02 | _____no_output_____ | MIT | course_2/course_material/Part_7_Deep_Learning/S43_L300/Minimal_example_All_Exercises.ipynb | Alexander-Meldrum/learning-data-science |
Train the model | # We iterate over our training dataset 100 times. That works well with a learning rate of 0.02.
# The proper number of iterations is something we will talk about later on, but generally
# a lower learning rate would need more iterations, while a higher learning rate would need less iterations
# keep in mind that a high... | 237249.78007243446
5739587.921816474
1992902565.643724
719554963540.3586
259830306790502.12
9.382439477181739e+16
3.387987017559843e+19
1.2233978230391737e+22
4.417674051463696e+24
1.5952165074557885e+27
5.76030661387594e+29
2.0800394260452784e+32
7.510996035316175e+34
2.7122111598526613e+37
9.793760163154793e+39
3.536... | MIT | course_2/course_material/Part_7_Deep_Learning/S43_L300/Minimal_example_All_Exercises.ipynb | Alexander-Meldrum/learning-data-science |
Print weights and biases and see if we have worked correctly. | # We print the weights and the biases, so we can see if they have converged to what we wanted.
# When declared the targets, following the f(x,z), we knew the weights should be 2 and -3, while the bias: 5.
print (weights, biases)
# Note that they may be convergING. So more iterations are needed. | [[-1.53536772e+125 -1.53536772e+125 -1.53536772e+125 ... -1.53536772e+125
-1.53536772e+125 -1.53536772e+125]
[-1.52680119e+124 -1.52680119e+124 -1.52680119e+124 ... -1.52680119e+124
-1.52680119e+124 -1.52680119e+124]] [-4.2058021e+128]
| MIT | course_2/course_material/Part_7_Deep_Learning/S43_L300/Minimal_example_All_Exercises.ipynb | Alexander-Meldrum/learning-data-science |
Plot last outputs vs targetsSince they are the last ones at the end of the training, they represent the final model accuracy. The closer this plot is to a 45 degree line, the closer target and output values are. | # We print the outputs and the targets in order to see if they have a linear relationship.
# Again, that's not needed. Moreover, in later lectures, that would not even be possible.
plt.plot(outputs,targets)
plt.xlabel('outputs')
plt.ylabel('targets')
plt.show() | _____no_output_____ | MIT | course_2/course_material/Part_7_Deep_Learning/S43_L300/Minimal_example_All_Exercises.ipynb | Alexander-Meldrum/learning-data-science |
Sea $f(x)=e^x$ | def f(x):
z = np.cos(x) + np.sin(3*x) + np.cos(np.sqrt(x)) + np.cos(18*x)
return z
f = lambda x: np.exp(x) | _____no_output_____ | MIT | codes/clase_14/interp_error_lineal.ipynb | mlares/computacion2020 |
y una partición regular en el intervalo $[0, 1]$ donde se construye el polinomio interpolante de orden $n$, $P_n(x)$. Interpolación de Newton con N puntos: | N = 30
xd = np.linspace(2, 10, N)
yd = f(xd)
xi = np.linspace(min(xd), max(xd), 200)
ym = f(xi) | _____no_output_____ | MIT | codes/clase_14/interp_error_lineal.ipynb | mlares/computacion2020 |
_______ | yl = it.interp_newton(xi, xd, yd)
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot()
ax.plot(xi, yl, linewidth=1.4, linestyle='-', color='orchid',
label='lagrange')
ax.plot(xd, yd, marker='o', linestyle='None', color='navy', markersize=5)
ax.grid()
ax.legend()
fig = plt.figure(figsize=(12, 6))
ax = fig.a... | _____no_output_____ | MIT | codes/clase_14/interp_error_lineal.ipynb | mlares/computacion2020 |
Veamos juntos los errores para diferentes N | fig, axs = plt.subplots(5, 4, figsize=[15, 18])
for N, ax in zip(range(6, 66, 3), axs.flat):
xd = np.linspace(2, 10, N)
yd = f(xd)
xi = np.linspace(min(xd), max(xd), 200)
ym = f(xi)
ylgg = it.interp_lagrange(xi, xd, yd)
mx = max(ylgg-ym)
ylin = np.interp(xi, xd, yd)
... | _____no_output_____ | MIT | codes/clase_14/interp_error_lineal.ipynb | mlares/computacion2020 |
Exercice 1: Write a Python class named square constructed by a length and two methods which will compute the area and the perimeter of the square. | class square():
#define your methods
def __init__(self,longueur):
self.longueur = longueur
def aire_carree(self):
return self.longueur**2
def perimetre(self):
return self.longueur*4
square1 = square(5)
print('Aire est : \n',square1.aire_carree())
print('Perimetre est :\n',square... | Aire est :
25
Perimetre est :
20
| MIT | exercices/part4.ipynb | AbdelwahabHassan/python-bootcamp |
Exercice 2: Write a python class rectangle that inherits from the square class. | class rectangle(square):
def __init__(self,longueur,largeur):
self.largeur = largeur
super().__init__(longueur)
square = rectangle(5,2)
print('Aire_R est : \n',square.aire_carree())
print('Perimetre_R est :\n',square.perimetre()) | Aire_R est :
25
Perimetre_R est :
20
| MIT | exercices/part4.ipynb | AbdelwahabHassan/python-bootcamp |
Exercice 3: | class SampleClass:
def __init__(self, a):
## private varibale in Python
self.a = a
@SampleClass
def work(x):
x = SampleClass(3)
return x
print('p1 --->',work.a)
work.a = 23
print('p2--->',work.a)
| p1 ---> <function work at 0x7f93280d45e0>
p2---> 23
| MIT | exercices/part4.ipynb | AbdelwahabHassan/python-bootcamp |
FloPy Using FloPy to simplify the use of the MT3DMS ```SSM``` packageA multi-component transport demonstration | import os
import sys
import numpy as np
# run installed version of flopy or add local path
try:
import flopy
except:
fpth = os.path.abspath(os.path.join('..', '..'))
sys.path.append(fpth)
import flopy
print(sys.version)
print('numpy version: {}'.format(np.__version__))
print('flopy version: {}'.format... | flopy is installed in /Users/jdhughes/Documents/Development/flopy_git/flopy_fork/flopy
3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 14:38:56)
[Clang 4.0.1 (tags/RELEASE_401/final)]
numpy version: 1.17.3
flopy version: 3.3.1
| CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
First, we will create a simple model structure | nlay, nrow, ncol = 10, 10, 10
perlen = np.zeros((10), dtype=np.float) + 10
nper = len(perlen)
ibound = np.ones((nlay,nrow,ncol), dtype=np.int)
botm = np.arange(-1,-11,-1)
top = 0. | _____no_output_____ | CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
Create the ```MODFLOW``` packages | model_ws = 'data'
modelname = 'ssmex'
mf = flopy.modflow.Modflow(modelname, model_ws=model_ws)
dis = flopy.modflow.ModflowDis(mf, nlay=nlay, nrow=nrow, ncol=ncol,
perlen=perlen, nper=nper, botm=botm, top=top,
steady=False)
bas = flopy.modflow.ModflowBas(mf... | _____no_output_____ | CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
We'll track the cell locations for the ```SSM``` data using the ```MODFLOW``` boundary conditions.Get a dictionary (```dict```) that has the ```SSM``` ```itype``` for each of the boundary types. | itype = flopy.mt3d.Mt3dSsm.itype_dict()
print(itype)
print(flopy.mt3d.Mt3dSsm.get_default_dtype())
ssm_data = {} | {'CHD': 1, 'BAS6': 1, 'PBC': 1, 'WEL': 2, 'DRN': 3, 'RIV': 4, 'GHB': 5, 'MAS': 15, 'CC': -1}
[('k', '<i8'), ('i', '<i8'), ('j', '<i8'), ('css', '<f4'), ('itype', '<i8')]
| CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
Add a general head boundary (```ghb```). The general head boundary head (```bhead```) is 0.1 for the first 5 stress periods with a component 1 (comp_1) concentration of 1.0 and a component 2 (comp_2) concentration of 100.0. Then ```bhead``` is increased to 0.25 and comp_1 concentration is reduced to 0.5 and comp_2 co... | ghb_data = {}
print(flopy.modflow.ModflowGhb.get_default_dtype())
ghb_data[0] = [(4, 4, 4, 0.1, 1.5)]
ssm_data[0] = [(4, 4, 4, 1.0, itype['GHB'], 1.0, 100.0)]
ghb_data[5] = [(4, 4, 4, 0.25, 1.5)]
ssm_data[5] = [(4, 4, 4, 0.5, itype['GHB'], 0.5, 200.0)]
for k in range(nlay):
for i in range(nrow):
ghb_data[0... | [('k', '<i8'), ('i', '<i8'), ('j', '<i8'), ('bhead', '<f4'), ('cond', '<f4')]
| CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
Add an injection ```well```. The injection rate (```flux```) is 10.0 with a comp_1 concentration of 10.0 and a comp_2 concentration of 0.0 for all stress periods. WARNING: since we changed the ```SSM``` data in stress period 6, we need to add the well to the ssm_data for stress period 6. | wel_data = {}
print(flopy.modflow.ModflowWel.get_default_dtype())
wel_data[0] = [(0, 4, 8, 10.0)]
ssm_data[0].append((0, 4, 8, 10.0, itype['WEL'], 10.0, 0.0))
ssm_data[5].append((0, 4, 8, 10.0, itype['WEL'], 10.0, 0.0)) | [('k', '<i8'), ('i', '<i8'), ('j', '<i8'), ('flux', '<f4')]
| CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
Add the ```GHB``` and ```WEL``` packages to the ```mf``` ```MODFLOW``` object instance. | ghb = flopy.modflow.ModflowGhb(mf, stress_period_data=ghb_data)
wel = flopy.modflow.ModflowWel(mf, stress_period_data=wel_data) | _____no_output_____ | CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
Create the ```MT3DMS``` packages | mt = flopy.mt3d.Mt3dms(modflowmodel=mf, modelname=modelname, model_ws=model_ws)
btn = flopy.mt3d.Mt3dBtn(mt, sconc=0, ncomp=2, sconc2=50.0)
adv = flopy.mt3d.Mt3dAdv(mt)
ssm = flopy.mt3d.Mt3dSsm(mt, stress_period_data=ssm_data)
gcg = flopy.mt3d.Mt3dGcg(mt) | found 'rch' in modflow model, resetting crch to 0.0
SSM: setting crch for component 2 to zero. kwarg name crch2
| CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
Let's verify that ```stress_period_data``` has the right ```dtype``` | print(ssm.stress_period_data.dtype) | [('k', '<i8'), ('i', '<i8'), ('j', '<i8'), ('css', '<f4'), ('itype', '<i8'), ('cssm(01)', '<f4'), ('cssm(02)', '<f4')]
| CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
Create the ```SEAWAT``` packages | swt = flopy.seawat.Seawat(modflowmodel=mf, mt3dmodel=mt,
modelname=modelname, namefile_ext='nam_swt', model_ws=model_ws)
vdf = flopy.seawat.SeawatVdf(swt, mtdnconc=0, iwtable=0, indense=-1)
mf.write_input()
mt.write_input()
swt.write_input() | _____no_output_____ | CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
And finally, modify the ```vdf``` package to fix ```indense```. | fname = modelname + '.vdf'
f = open(os.path.join(model_ws, fname),'r')
lines = f.readlines()
f.close()
f = open(os.path.join(model_ws, fname),'w')
for line in lines:
f.write(line)
for kper in range(nper):
f.write("-1\n")
f.close()
| _____no_output_____ | CC0-1.0 | examples/Notebooks/flopy3_multi-component_SSM.ipynb | aleaf/flopy |
The ``Tabulator`` widget allows displaying and editing a pandas DataFrame. The `Tabulator` is a largely backward compatible replacement for the [`DataFrame`](./DataFrame.ipynb) widget and will eventually replace it. It is built on the [Tabulator](http://tabulator.info/) library, which provides for a wide range of featu... | df = pd.DataFrame({
'int': [1, 2, 3],
'float': [3.14, 6.28, 9.42],
'str': ['A', 'B', 'C'],
'bool': [True, False, True],
'date': [dt.date(2019, 1, 1), dt.date(2020, 1, 1), dt.date(2020, 1, 10)]
}, index=[1, 2, 3])
df_widget = pn.widgets.Tabulator(df)
df_widget | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
FormattersBy default the widget will pick bokeh ``CellFormatter`` and ``CellEditor`` types appropriate to the dtype of the column. These may be overriden by explicit dictionaries mapping from the column name to the editor or formatter instance. For example below we create a ``SelectEditor`` instance to pick from four ... | from bokeh.models.widgets.tables import NumberFormatter, BooleanFormatter
bokeh_formatters = {
'float': NumberFormatter(format='0.00000'),
'bool': BooleanFormatter(),
}
pn.widgets.Tabulator(df, formatters=bokeh_formatters) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
The list of valid Bokeh formatters includes: * [BooleanFormatter](https://docs.bokeh.org/en/latest/docs/reference/models/widgets.tables.htmlbokeh.models.widgets.tables.BooleanFormatter)* [DateFormatter](https://docs.bokeh.org/en/latest/docs/reference/models/widgets.tables.htmlbokeh.models.widgets.tables.DateFormatte... | tabulator_formatters = {
'float': {'type': 'progress', 'max': 10},
'bool': {'type': 'tickCross'}
}
pn.widgets.Tabulator(df, formatters=tabulator_formatters) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
The list of valid Tabulator formatters can be found in the [Tabulator documentation](http://tabulator.info/docs/4.9/formatformat-builtin). EditorsJust like the formatters, the `Tabulator` will natively understand the Bokeh `Editor` types. However, in the background it will replace most of them with equivalent editors n... | from bokeh.models.widgets.tables import CheckboxEditor, NumberEditor, SelectEditor, DateEditor, TimeEditor
bokeh_editors = {
'float': NumberEditor(),
'bool': CheckboxEditor(),
'str': SelectEditor(options=['A', 'B', 'C', 'D']),
}
pn.widgets.Tabulator(df[['float', 'bool', 'str']], editors=bokeh_editors) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Therefore it is often preferable to use one of the [Tabulator editors](http://tabulator.info/docs/4.9/editedit) directly: | from bokeh.models.widgets.tables import CheckboxEditor, NumberEditor, SelectEditor
bokeh_editors = {
'float': {'type': 'number', 'max': 10, 'step': 0.1},
'bool': {'type': 'tickCross', 'tristate': True, 'indeterminateValue': None},
'str': {'type': 'autocomplete', 'values': True}
}
pn.widgets.Tabulator(df[[... | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Column layoutsBy default the DataFrame widget will adjust the sizes of both the columns and the table based on the contents, reflecting the default value of the parameter: `layout="fit_data_table"`. Alternative modes allow manually specifying the widths of the columns, giving each column equal widths, or adjusting jus... | custom_df = pd._testing.makeMixedDataFrame()
pn.widgets.Tabulator(custom_df, widths={'index': 70, 'A': 50, 'B': 50, 'C': 70, 'D': 130}) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
You can also declare a single width for all columns this way: | pn.widgets.Tabulator(custom_df, widths=130) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Autosize columnsTo automatically adjust the columns dependending on their content set `layout='fit_data'`: | pn.widgets.Tabulator(custom_df, layout='fit_data', width=400) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
To ensure that the table fits all the data but also stretches to fill all the available space, set `layout='fit_data_stretch'`: | pn.widgets.Tabulator(custom_df, layout='fit_data_stretch', width=400) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
The `'fit_data_fill'` option on the other hand won't stretch the last column but still fill the space: | pn.widgets.Tabulator(custom_df, layout='fit_data_fill', width=400) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Perhaps the most useful of these options is `layout='fit_data_table'` (and therefore the default) since this will automatically size both the columns and the table: | pn.widgets.Tabulator(custom_df, layout='fit_data_table') | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Equal sizeThe simplest option is simply to allocate each column equal amount of size: | pn.widgets.Tabulator(custom_df, layout='fit_columns', width=650) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
StylingThe ability to style the contents of a table based on its content and other considerations is very important. Thankfully pandas provides a powerful [styling API](https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html), which can be used in conjunction with the `Tabulator` widget. Specifically the `T... | style_df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
styled = pn.widgets.Tabulator(style_df) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Next we define two functions which apply styling cell-wise (`color_negative_red`) and column-wise (`highlight_max`), which we then apply to the `Tabulator` using the `.style` API and then display the `styled` table: | def color_negative_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
color = 'red' if val < 0 else 'black'
return 'color: %s' % color
def highlight_max(s):
'''
highlight the maximum in a Series yellow.
... | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
ThemingThe Tabulator library ships with a number of themes, which are defined as CSS stylesheets. For that reason changing the theme on one table will affect all Tables on the page and it will usually be preferable to see the theme once at the class level like this:```pythonpn.widgets.Tabulator.theme = 'default'```For... | sel_df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
select_table = pn.widgets.Tabulator(sel_df, selection=[0, 3, 7])
select_table | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Once initialized, the ``selection`` parameter will return the integer indexes of the selected rows, while the ``selected_dataframe`` property will return a new DataFrame containing just the selected rows: | select_table.selection = [1, 4, 9]
select_table.selected_dataframe | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
The `selectable` parameter declares how the selections work. - `True`: Selects rows on click. To select multiple use Ctrl-select, to select a range use Shift-select- `False`: Disables selection- `'checkbox'`: Adds a column of checkboxes to toggle selections- `'checkbox-single'`: Same as `'checkbox'` but disables (de)se... | pn.widgets.Tabulator(sel_df, selection=[0, 3, 7], selectable='checkbox') | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Additionally we can also disable selection for specific rows by providing a `selectable_rows` function. The function must accept a DataFrame and return a list of integer indexes indicating which rows are selectable, e.g. here we disable selection for every second row: | pn.widgets.Tabulator(sel_df, selectable_rows=lambda df: list(range(0, len(df), 2))) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Freezing rows and columnsSometimes your table will be larger than can be displayed in a single viewport, in which case scroll bars will be enabled. In such cases, you might want to make sure that certain information is always visible. This is where the `frozen_columns` and `frozen_rows` options come in. Frozen columns... | wide_df = pd._testing.makeCustomDataframe(10, 10, r_idx_names=['index'])
pn.widgets.Tabulator(wide_df, frozen_columns=['index'], width=400) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Frozen rowsAnother common scenario is when you have certain rows with special meaning, e.g. aggregates that summarize the information in the rest of the table. In this case you may want to freeze those rows so they do not scroll out of view. You can achieve this by setting a list of `frozen_rows` by integer index (whi... | date_df = pd._testing.makeTimeDataFrame().iloc[:10]
agg_df = pd.concat([date_df, date_df.median().to_frame('Median').T, date_df.mean().to_frame('Mean').T])
agg_df.index= agg_df.index.map(str)
pn.widgets.Tabulator(agg_df, frozen_rows=[-2, -1], width=400) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Row contentsA table can only display so much information without becoming difficult to scan. We may want to render additional information to a table row to provide additional context. To make this possible you can provide a `row_content` function which is given the table row as an argument and should return a panel ob... | from bokeh.sampledata.periodic_table import elements
periodic_df = elements[['atomic number', 'name', 'atomic mass', 'metal', 'year discovered']].set_index('atomic number')
content_fn = lambda row: pn.pane.HTML(
f'<iframe src="http://en.wikipedia.org/wiki/{row["name"]}?printable=yes" width="100%" height="300px"><... | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
The currently expanded rows can be accessed (and set) on the `expanded` parameter: | periodic_table.expanded | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
GroupingAnother useful option is the ability to group specific rows together, which can be achieved using `groups` parameter. The `groups` parameter should be composed of a dictionary mapping from the group titles to the column names: | pn.widgets.Tabulator(date_df, groups={'Group 1': ['A', 'B'], 'Group 2': ['C', 'D']}) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
GroupbyIn addition to grouping columns we can also group rows by the values along one or more columns: | from bokeh.sampledata.autompg import autompg
pn.widgets.Tabulator(autompg, groupby=['yr', 'origin'], height=240) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Hierarchical Multi-indexThe `Tabulator` widget can also render a hierarchical multi-index and aggregate over specific categories. If a DataFrame with a hierarchical multi-index is supplied and the `hierarchical` is enabled the widget will group data by the categories in the order they are defined in. Additionally for ... | from bokeh.sampledata.population import data as population_data
pop_df = population_data[population_data.Year == 2020].set_index(['Location', 'AgeGrp', 'Sex'])[['Value']]
pn.widgets.Tabulator(value=pop_df, hierarchical=True, aggregators={'Sex': 'sum', 'AgeGrp': 'sum'}, height=400) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
PaginationWhen working with large tables we sometimes can't send all the data to the browser at once. In these scenarios we can enable pagination, which will fetch only the currently viewed data from the server backend. This may be enabled by setting `pagination='remote'` and the size of each page can be set using the... | large_df = pd._testing.makeCustomDataframe(100000, 5)
%%time
paginated_table = pn.widgets.Tabulator(large_df, pagination='remote', page_size=10)
paginated_table | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Contrary to the `'remote'` option, `'local'` pagination entirely loads the data but still allows to display it on multiple pages. | %%time
paginated_table = pn.widgets.Tabulator(large_df, pagination='local', page_size=10)
paginated_table | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
FilteringA very common scenario is that you want to attach a number of filters to a table in order to view just a subset of the data. You can achieve this through callbacks or other reactive approaches but the `.add_filter` method makes it much easier. Constant and Widget filtersThe simplest approach to filtering is ... | mixed_df = pd._testing.makeMixedDataFrame()
filter_table = pn.widgets.Tabulator(mixed_df)
filter_table | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Now we will start adding filters one-by-one, e.g. to start with we add a filter for the `'A'` column, selecting a range from 0 to 3: | filter_table.add_filter((0, 3), 'A') | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Next we add dynamic widget based filter, a `RangeSlider` which allows us to further narrow down the data along the `'A'` column: | slider = pn.widgets.RangeSlider(start=0, end=3, name='A Filter')
filter_table.add_filter(slider, 'A') | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Lastly we will add a `MultiSelect` filter along the `'C'` column: | select = pn.widgets.MultiSelect(options=['foo1', 'foo2', 'foo3', 'foo4', 'foo5'], name='C Filter')
filter_table.add_filter(select, 'C') | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Now let's display the table alongside the widget based filters: | pn.Row(
pn.Column(slider, select),
filter_table
) | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
After filtering you can inspect the current view with the `current_view` property: | filter_table.current_view | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Function based filtering For more complex filtering tasks you can supply a function that should accept the DataFrame to be filtered as the first argument and must return a filtered copy of the data. Let's start by loading some data. | import sqlite3
from bokeh.sampledata.movies_data import movie_path
con = sqlite3.Connection(movie_path)
movies_df = pd.read_sql('SELECT Title, Year, Genre, Director, Writer, imdbRating from omdb', con)
movies_df = movies_df[~movies_df.Director.isna()]
movies_table = pn.widgets.Tabulator(movies_df, pagination='remot... | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
By using the `pn.bind` function, which binds widget and parameter values to a function, complex filtering can be achieved. E.g. here we will add a filter function that uses tests whether the string or regex is contained in the 'Director' column of a listing of thousands of movies: | director_filter = pn.widgets.TextInput(name='Director filter', value='Chaplin')
def contains_filter(df, pattern, column):
if not pattern:
return df
return df[df[column].str.contains(pattern)]
movies_table.add_filter(pn.bind(contains_filter, pattern=director_filter, column='Director'))
pn.Row(... | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Client-side filteringIn addition to the Python API the Tabulator widget also offers a client-side filtering API, which can be exposed through `header_filters` or by manually adding filters to the rendered Bokeh model. The API for declaring header filters is almost identical to the API for defining [Editors](Editors). ... | bokeh_editors = {
'float': {'type': 'number', 'max': 10, 'step': 0.1},
'bool': {'type': 'tickCross', 'tristate': True, 'indeterminateValue': None},
'str': {'type': 'autocomplete', 'values': True}
}
header_filter_table = pn.widgets.Tabulator(
df[['float', 'bool', 'str']], height=140, width=400, layout='... | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
When a filter is applied client-side the `filters` parameter is synced with Python. The definition of `filters` looks something like this:```[{'field': 'Director', 'type': '=', 'value': 'Steven Spielberg'}]```Try applying a filter and then inspect the `filters` parameter: | header_filter_table.filters | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
For all supported filtering types see the [Tabulator Filtering documentation](http://tabulator.info/docs/4.9/filter).If we want to change the filter type for the `header_filters` we can do so in the definition by supplying a dictionary indexed by the column names and then either providing a dictionary which may define ... | movie_filters = {
'Title': {'type': 'input', 'func': 'like', 'placeholder': 'Enter title'},
'Year': {'placeholder': 'Enter year'},
'Genre': {'type': 'input', 'func': 'like', 'placeholder': 'Enter genre'},
'Director': {'type': 'input', 'func': 'like', 'placeholder': 'Enter director'},
'Writer': {'typ... | _____no_output_____ | BSD-3-Clause | examples/reference/widgets/Tabulator.ipynb | datalayer-contrib/holoviz-panel |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.