markdown
stringlengths
0
37k
code
stringlengths
1
33.3k
path
stringlengths
8
215
repo_name
stringlengths
6
77
license
stringclasses
15 values
feature_names is the name of the feature columns.
print(iris_data['feature_names'])
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
target_names, despite the name, is not the names of the target columns. There is only one column of targets. Instead, target_names is the human-readable names of the classes in the target list within the bunch. In this case,target_names is the names of the three species of iris in this dataset.
print(iris_data['target_names'])
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
We can now examine target and see that it contains zeros, ones, and twos. These correspond to the target names 'setosa', 'versicolor', and 'virginica'.
print(iris_data['target'])
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Last, we'll look at the data within the bunch. The data is an array of arrays. Each sub-array contains four values. These values match up with the feature_names. The first item in each sub-array is 'sepal length (cm)', the next is 'sepal width (cm)', and so on.
iris_data['data']
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
The number of target values should always equal the number of rows in the data.
print(len(iris_data['data'])) print(len(iris_data['target']))
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Bunch objects are an adequate container for data. They can be used directly to feed models. However, Bunch objects are not very good for analyzing and manipulating your data. In this course, we will typically convert Bunch objects into Pandas DataFrame objects to make analysis, data cleaning, visualization, and train/t...
import pandas as pd import numpy as np iris_df = pd.DataFrame( data=np.append( iris_data['data'], np.array(iris_data['target']).reshape(len(iris_data['target']), 1), axis=1), columns=np.append(iris_data['feature_names'], ['species']) ) iris_df.sample(n=10)
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
You might notice that the integer representation of species got converted to a floating point number along the way. We can change that back.
iris_df['species'] = iris_df['species'].astype('int64') iris_df.sample(n=10)
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Exercise 1 Load the Boston house price dataset into a Pandas DataFrame. Append the target values to the last column of the DataFrame called boston_df. Name the target column 'PRICE'. Student Solution
# Your answer goes here
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Fetching Fetching is similar to loading. Scikit-learn will first see if it can find the dataset locally, and, if so, it will simply load the data. Otherwise, it will attempt to pull the data from the internet. We can see fetching in action with the fetch_california_housing function below.
from sklearn.datasets import fetch_california_housing housing_data = fetch_california_housing() type(housing_data)
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
The dataset is once again a Bunch. If you follow the link to the fetch_california_housing documentation, you notice that the dataset is a regression dataset as opposed to the iris dataset, which was a classification dataset. We can see the difference in the dataset by checking out the attributes of the Bunch.
dir(housing_data)
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
We see that four of the attributes that we expect are present, but 'target_names' is missing. This is because our target is now a continuous variable (home price) and not a discrete value (iris species).
print(housing_data['target'][:10])
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Converting a Bunch of regression data to a DataFrame is no different than for a Bunch of classification data.
import pandas as pd import numpy as np housing_df = pd.DataFrame( data=np.append( housing_data['data'], np.array(housing_data['target']).reshape(len(housing_data['target']), 1), axis=1), columns=np.append(housing_data['feature_names'], ['price']) ) housing_df.sample(n=10)
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Generating In the example datasets we've seen so far in this Colab, the data is static and loaded from a file. Sometimes it makes more sense to generate a dataset. For this, we can use one of the many generator functions. make_regression is a generator that creates a dataset with an underlying regression that you can t...
from sklearn.datasets import make_regression features, targets = make_regression(n_samples=10, n_features=1, random_state=42) features, targets
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
We can use a visualization library to plot the regression data.
import matplotlib.pyplot as plt plt.plot(features, targets, 'b.') plt.show()
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
That data appears to have a very linear pattern! If we want to make it more realistic (non-linear), we can add some noise during data generation. Remember that random_state is for reproducibility only. Don't use this in your code unless you have a good reason to.
from sklearn.datasets import make_regression features, targets = make_regression(n_samples=10, n_features=1, random_state=42, noise=5.0) plt.plot(features, targets, 'b.') plt.show()
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
There are dozens of dataset loaders and generators in the scikit-learn datasets package. When you want to play with a new machine learning algorithm, they are a great source of data for getting started. Exercise 2 Search the scikit-learn datasets documentation and find a function to make a "Moons" dataset. Create a dat...
# Your answer goes here
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Exercise 3 In Exercise Two, you created a "moons" dataset. In that dataset, the features are $(x,y)$-coordinates that can be graphed in a scatterplot. The targets are zeros and ones that represent a binary classification. Use matplotlib's scatter method to visualize the data as a scatterplot. Use the c argument to make...
# Your answer goes here
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Models Machine learning involves training a model to gain insight and predictive power from a dataset. Scikit-learn has support for many different types of models, ranging from classic algebraic models to more modern deep learning models. Throughout the remainder of this course, you will learn about many of these model...
from sklearn.datasets import make_regression from sklearn.linear_model import LinearRegression regression = LinearRegression() regression.fit(features, targets) predictions = regression.predict(features) plt.plot(features, targets, 'b.') plt.plot(features, predictions, 'r-') plt.show()
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
At this point, don't worry too much about the details of what LinearRegression is doing. There is a deep-dive into regression problems coming up soon. For now, just note the fit/predict pattern for training estimators, and know that you'll see it throughout our adventures with scikit-learn. Transformers In practice, it...
features
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
We will now create a MinMaxScaler and fit it to our feature data. Each transformer has different information that it needs in order to perform a transformation. In the case of the MinMaxScaler, the smallest and largest values in the data are needed.
from sklearn.preprocessing import MinMaxScaler transformer = MinMaxScaler() transformer.fit(features) transformer.data_min_, transformer.data_max_
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
You might notice that the values are stored in arrays. This is because transformers can operate on more than one feature. In this case, however, we have only one. Next, we need to apply the transformation to our features. After the transformation, we can now see that all of the features fall between the range of zero t...
features = transformer.transform(features) features
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Pipelines A pipeline is simply a series of transformers, often with an estimator at the end. In the example below, we use a Pipeline class to perform min-max scaling or our feature data and then train a linear regression model using the scaled features.
from sklearn.pipeline import Pipeline features, targets = make_regression( n_samples=10, n_features=1, random_state=42, noise=5.0) pipeline = Pipeline([ ('scale', MinMaxScaler()), ('regression', LinearRegression()) ]) pipeline.fit(features, targets) predictions = pipeline.predict(features) plt.plot(feature...
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
Metrics So far we have seen ways that scikit-learn can help you get data, modify that data, train a model, and finally, make predictions. But how do we know how good these predictions are? Scikit-learn also comes with many functions for measuring model performance in the metrics package. Later in this course, you will ...
from sklearn.metrics import mean_squared_error mean_squared_error(targets, predictions)
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
In this case, the MSE value alone doesn't have much meaning. Since the data that we fit the regression to isn't related to any real-world metrics, the MSE is hard to interpret alone. As we learn more about machine learning and begin training models on real data, you'll learn how to interpret MSE and other metrics in th...
print(regression.score(features, targets)) print(pipeline.score(features, targets))
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
The return value of the score method depends on the estimator being used. In the case of LinearRegression, the score is the $R^2$ score, where scores closer to 1.0 are better. You can find the metric that score returns in the documentation for the given estimator you're using. Exercise 4 Use the Pipeline class to combi...
# Your answer goes here
content/03_regression/01_introduction_to_sklearn/colab.ipynb
google/applied-machine-learning-intensive
apache-2.0
使用 TensorFlow 进行分布式训练 <table class="tfo-notebook-buttons" align="left"> <td><a target="_blank" href="https://tensorflow.google.cn/guide/distributed_training" class=""><img src="https://tensorflow.google.cn/images/tf_logo_32px.png" class="">在 TensorFlow.org 上查看</a></td> <td><a target="_blank" href="https://colab.res...
# Import TensorFlow import tensorflow as tf
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
策略类型 tf.distribute.Strategy 打算涵盖不同轴上的许多用例。目前已支持其中的部分组合,将来还会添加其他组合。其中一些轴包括: 同步和异步训练:这是通过数据并行进行分布式训练的两种常用方法。在同步训练中,所有工作进程都同步地对输入数据的不同片段进行训练,并且会在每一步中聚合梯度。在异步训练中,所有工作进程都独立训练输入数据并异步更新变量。通常情况下,同步训练通过全归约实现,而异步训练通过参数服务器架构实现。 硬件平台:您可能需要将训练扩展到一台机器上的多个 GPU 或一个网络中的多台机器(每台机器拥有 0 个或多个 GPU),或扩展到 Cloud TPU 上。 要支持这些用例,有六种策略可选。在下一部分,我...
mirrored_strategy = tf.distribute.MirroredStrategy()
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
这会创建一个 MirroredStrategy 实例,该实例使用所有对 TensorFlow 可见的 GPU,并使用 NCCL 进行跨设备通信。 如果您只想使用机器上的部分 GPU,您可以这样做:
mirrored_strategy = tf.distribute.MirroredStrategy(devices=["/gpu:0", "/gpu:1"])
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
如果您想重写跨设备通信,可以通过提供 tf.distribute.CrossDeviceOps 的实例,使用 cross_device_ops 参数来实现。目前,除了默认选项 tf.distribute.NcclAllReduce 外,还有 tf.distribute.HierarchicalCopyAllReduce 和 tf.distribute.ReductionToOneDevice 两个选项。
mirrored_strategy = tf.distribute.MirroredStrategy( cross_device_ops=tf.distribute.HierarchicalCopyAllReduce())
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
TPUStrategy 您可以使用 tf.distribute.experimental.TPUStrategy 在张量处理单元 (TPU) 上运行 TensorFlow 训练。TPU 是 Google 的专用 ASIC,旨在显著加速机器学习工作负载。您可通过 Google Colab、TensorFlow Research Cloud 和 Cloud TPU 平台进行使用。 就分布式训练架构而言,TPUStrategy 和 MirroredStrategy 是一样的,即实现同步分布式训练。TPU 会在多个 TPU 核心之间实现高效的全归约和其他集合运算,并将其用于 TPUStrategy。 下面演示了如何将 TPUStrategy...
multiworker_strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy()
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
MultiWorkerMirroredStrategy 目前为您提供两种不同的集合运算实现方法。CollectiveCommunication.RING 通过将 gRPC 用作通信层来实现基于环的集合。CollectiveCommunication.NCCL 使用 NVIDIA 的 NCCL 来实现集合。CollectiveCommunication.AUTO 会将选择推迟到运行时。集合实现的最佳选择取决于 GPU 的数量和种类,以及集群中的网络互连。您可以通过以下方式来指定:
multiworker_strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy( tf.distribute.experimental.CollectiveCommunication.NCCL)
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
与多 GPU 训练相比,多工作进程训练的一个主要差异是多工作进程的设置。TF_CONFIG 环境变量是在 TensorFlow 中为作为集群一部分的每个工作进程指定集群配置的标准方法。详细了解如何设置 TF_CONFIG。 注:此策略处于 experimental 阶段,我们目前正在进行改进,使其能够用于更多场景。敬请期待 API 的未来变化。 CentralStorageStrategy tf.distribute.experimental.CentralStorageStrategy 也执行同步训练。变量不会被镜像,而是放在 CPU 上,且运算会复制到所有本地 GPU 。如果只有一个 GPU,则所有变量和运算都将被放在该 GPU...
central_storage_strategy = tf.distribute.experimental.CentralStorageStrategy()
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
这会创建一个 CentralStorageStrategy 实例,该实例将使用所有可见的 GPU 和 CPU。在副本上对变量的更新将先进行聚合,然后再应用于变量。 注:此策略处于 experimental 阶段,我们目前正在进行改进,使其能够用于更多场景。敬请期待 API 的未来变化。 ParameterServerStrategy tf.distribute.experimental.ParameterServerStrategy 支持在多台机器上进行参数服务器训练。在此设置中,有些机器会被指定为工作进程,有些会被指定为参数服务器。模型的每个变量都会被放在参数服务器上。计算会被复制到所有工作进程的所有 GPU 中。 就代码而言,该...
default_strategy = tf.distribute.get_strategy()
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
该策略有两个主要用途: 它允许无条件编写可感知分布的库代码。例如,在优化器中,我们可以执行 tf.distribute.get_strategy() 并使用该策略来减少梯度,而它将始终返回一个我们可以在其上调用归约 API 的策略对象。
# In optimizer or other library code # Get currently active strategy strategy = tf.distribute.get_strategy() strategy.reduce("SUM", 1.) # reduce some values
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
与库代码类似,它可以用来在使用或不使用分布策略的情况下编写最终用户的程序,而无需条件逻辑。以下示例代码段展示了这一点:
if tf.config.list_physical_devices('gpu'): strategy = tf.distribute.MirroredStrategy() else: # use default strategy strategy = tf.distribute.get_strategy() with strategy.scope(): # do something interesting print(tf.Variable(1.))
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
OneDeviceStrategy tf.distribute.OneDeviceStrategy 是一种会将所有变量和计算放在单个指定设备上的策略。 strategy = tf.distribute.OneDeviceStrategy(device="/gpu:0") 此策略与默认策略在诸多方面存在差异。在默认策略中,与没有任何分布策略的 TensorFlow 运行相比,变量放置逻辑保持不变。但是当使用 OneDeviceStrategy 时,在其作用域内创建的所有变量都会被显式地放在指定设备上。此外,通过 OneDeviceStrategy.run 调用的任何函数也会被放在指定设备上。 通过该策略分布的输入将被预提取到指定设备。...
mirrored_strategy = tf.distribute.MirroredStrategy() with mirrored_strategy.scope(): model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(1,))]) model.compile(loss='mse', optimizer='sgd')
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
在此示例中我们使用了 MirroredStrategy,因此我们可以在有多个 GPU 的机器上运行。strategy.scope() 会指示 Keras 使用哪个策略来进行分布式训练。我们可以通过在此作用域内创建模型/优化器/指标来创建分布式变量而非常规变量。设置完成后,您就可以像平常一样拟合模型。MirroredStrategy 负责将模型的训练复制到可用的 GPU 上,以及聚合梯度等。
dataset = tf.data.Dataset.from_tensors(([1.], [1.])).repeat(100).batch(10) model.fit(dataset, epochs=2) model.evaluate(dataset)
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
我们在这里使用了 tf.data.Dataset 来提供训练和评估输入。您还可以使用 Numpy 数组:
import numpy as np inputs, targets = np.ones((100, 1)), np.ones((100, 1)) model.fit(inputs, targets, epochs=2, batch_size=10)
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
在上述两种情况(数据集或 Numpy)中,给定输入的每个批次都被平均分到了多个副本中。例如,如果对 2 个 GPU 使用 MirroredStrategy,大小为 10 的每个批次将被均分到 2 个 GPU 中,每个 GPU 每步会接收 5 个输入样本。如果添加更多 GPU,每个周期的训练速度就会更快。在添加更多加速器时通常需要增加批次大小,以便有效利用额外的计算能力。您还需要根据模型重新调整学习率。您可以使用 strategy.num_replicas_in_sync 获得副本数量。
# Compute global batch size using number of replicas. BATCH_SIZE_PER_REPLICA = 5 global_batch_size = (BATCH_SIZE_PER_REPLICA * mirrored_strategy.num_replicas_in_sync) dataset = tf.data.Dataset.from_tensors(([1.], [1.])).repeat(100) dataset = dataset.batch(global_batch_size) LEARNING_RATES_BY_BATCH...
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
目前支持的策略 训练 API | MirroredStrategy | TPUStrategy | MultiWorkerMirroredStrategy | CentralStorageStrategy | ParameterServerStrategy --- | --- | --- | --- | --- | --- Keras API | 支持 | 支持 | 实验性支持 | 实验性支持 | 计划于 2.3 后支持 示例和教程 下列教程和示例完整演示了上述集成到 Keras 的过程: 使用 MirroredStrategy 训练 MNIST 的教程。 使用 MultiWorkerMirroredStrategy 训练 MNI...
with mirrored_strategy.scope(): model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(1,))]) optimizer = tf.keras.optimizers.SGD()
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
接下来,我们创建输入数据集并调用 tf.distribute.Strategy.experimental_distribute_dataset,以根据策略分布数据集。
dataset = tf.data.Dataset.from_tensors(([1.], [1.])).repeat(100).batch( global_batch_size) dist_dataset = mirrored_strategy.experimental_distribute_dataset(dataset)
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
然后,我们定义一个训练步骤。我们将使用 tf.GradientTape 来计算梯度,并使用优化器来应用这些梯度以更新模型变量。要分布此训练步骤,我们加入一个 train_step 函数,并将此函数和从之前创建的 dist_dataset 获得的数据集输入一起传递给 tf.distrbute.Strategy.run:
loss_object = tf.keras.losses.BinaryCrossentropy( from_logits=True, reduction=tf.keras.losses.Reduction.NONE) def compute_loss(labels, predictions): per_example_loss = loss_object(labels, predictions) return tf.nn.compute_average_loss(per_example_loss, global_batch_size=global_batch_size) def train_step(input...
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
以上代码还需注意以下几点: 我们使用了 tf.nn.compute_average_loss 来计算损失。tf.nn.compute_average_loss 将每个样本的损失相加,然后将总和除以 global_batch_size。这很重要,因为稍后在每个副本上计算出梯度后,会通过对它们求和使其在副本中聚合。 我们使用了 tf.distribute.Strategy.reduce API 来聚合 tf.distribute.Strategy.run 返回的结果。tf.distribute.Strategy.run 会从策略中的每个本地副本返回结果,且有多种方法使用该结果。可以 reduce 它们以获得聚合值。还可以通过执行 tf...
for dist_inputs in dist_dataset: print(distributed_train_step(dist_inputs))
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
在上面的示例中,我们通过迭代 dist_dataset 为训练提供输入。我们还提供 tf.distribute.Strategy.make_experimental_numpy_dataset 以支持 Numpy 输入。您可以在调用 tf.distribute.Strategy.experimental_distribute_dataset 之前使用此 API 来创建数据集。 迭代数据的另一种方法是显式地使用迭代器。当您希望运行给定数量的步骤而非迭代整个数据集时,可能会用到此方法。现在可以将上面的迭代修改为:先创建迭代器,然后在迭代器上显式地调用 next 以获得输入数据。
iterator = iter(dist_dataset) for _ in range(10): print(distributed_train_step(next(iterator)))
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
上面是使用 tf.distribute.Strategy API 来分布自定义训练循环最简单的情况。我们正在改进这些 API。由于此用例还需进一步调整才能适应您的代码,我们未来会发布单独的详细指南。 目前支持的策略 训练 API | MirroredStrategy | TPUStrategy | MultiWorkerMirroredStrategy | CentralStorageStrategy | ParameterServerStrategy :-- | :-- | :-- | :-- | :-- | :-- 自定义训练循环 | 支持 | 支持 | 实验性支持 | 实验性支持 | 计划于 2.3 后支持 示例和教程 下面是...
mirrored_strategy = tf.distribute.MirroredStrategy() config = tf.estimator.RunConfig( train_distribute=mirrored_strategy, eval_distribute=mirrored_strategy) regressor = tf.estimator.LinearRegressor( feature_columns=[tf.feature_column.numeric_column('feats')], optimizer='SGD', config=config)
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
我们在这里使用了预制 Estimator,但同样的代码也适用于自定义 Estimator。train_distribute 决定训练如何分布,eval_distribute 决定评估如何分布。这是与 Keras 的另一个区别,在 Keras 中,我们会对训练和评估使用相同的策略。 现在,我们可以使用输入函数来训练和评估这个 Estimator:
def input_fn(): dataset = tf.data.Dataset.from_tensors(({"feats":[1.]}, [1.])) return dataset.repeat(1000).batch(10) regressor.train(input_fn=input_fn, steps=10) regressor.evaluate(input_fn=input_fn, steps=10)
site/zh-cn/guide/distributed_training.ipynb
tensorflow/docs-l10n
apache-2.0
Exercício 1 - Utilizando o método update_with_media, realize a atualização do status utilizando a imagem fia.jpg disponível na pasta da aula. Imprima com o status "Programação com Python e Twitter na FIA!"
retorno = api.update_with_media(filename='fia.jpg',status='Test. Upload media via python') print(retorno.text)
Python/2016-07-29/aula4-parte5-exercicios-Copy1.ipynb
rubensfernando/mba-analytics-big-data
mit
Exercício 2 - Salve o retorno do tweet do exercício anterior e imprima as seguintes informações: * tweet * id * created_at * lang * text * user * screen_name, * friends_count * time_zone Por fim, remova o tweet, utilizando o método destroy_status.
print(retorno.id) print(retorno.created_at) print(retorno.lang) print(retorno.text) print(retorno.user.screen_name) print(retorno.user.friends_count) print(retorno.user.time_zone) retornoDestroy = api.destroy_status(retorno.id)
Python/2016-07-29/aula4-parte5-exercicios-Copy1.ipynb
rubensfernando/mba-analytics-big-data
mit
Exercício 3 - Utilizando o método home_timeline(), recupere os 10 tweets atuais. Para cada um desses tweets, imprima: * o screen_name * o texto do tweet * o id do usuário
home = api.home_timeline(count=10) for i, tweet in enumerate(home): print(tweet.user.screen_name) print(tweet.text) print(tweet.user.id) print('\n')
Python/2016-07-29/aula4-parte5-exercicios-Copy1.ipynb
rubensfernando/mba-analytics-big-data
mit
Exercício 4 - Para cada tweet do exercício anterior, utilize o id do usuário e imprima o texto dos 5 primeiros tweets de cada um dos 10 usuários (user_timeline).
for i, user in enumerate(home): help(api.user_timeline)
Python/2016-07-29/aula4-parte5-exercicios-Copy1.ipynb
rubensfernando/mba-analytics-big-data
mit
Create Some Mock Data First, let's create a default galaxy halo model:
model = halomod.TracerHaloModel( z=0.2, transfer_model='EH', rnum=30, rmin=0.1, rmax=30, hod_model='Zehavi05', hod_params={ "M_min": 12.0, "M_1": 12.8, 'alpha': 1.05, 'central': True }, dr_table=0.1, dlnk=0.1, dlog10m=0.05 )
docs/examples/fitting.ipynb
steven-murray/halomod
mit
Now, let's create some mock data with some Gaussian noise:
np.random.seed(1234) mock_data = model.corr_auto_tracer + np.random.normal(scale = 0.1 * np.abs(model.corr_auto_tracer)) mock_ngal = model.mean_tracer_den plt.plot(model.r, model.corr_auto_tracer) plt.scatter(model.r, mock_data) plt.xscale('log') plt.yscale('log')
docs/examples/fitting.ipynb
steven-murray/halomod
mit
Define a likelihood Now let's define a likelihood function, based on some input model for $\xi(r)$. The model is simply a $\chi^2$ likelihood.
def chi_square(model, data, sigma): return np.sum(norm.logpdf(data, loc=model, scale=sigma))
docs/examples/fitting.ipynb
steven-murray/halomod
mit
Define an emcee-compatible likelihood function Now we define a likelihood function for emcee. There is a bit more flexibility here, as this function needs to calculate priors on all input parameters, and handle exceptions as well. This means this function is rather specific to the problem at hand. We will define a very...
fiducial_model = model.clone()
docs/examples/fitting.ipynb
steven-murray/halomod
mit
First, we define a small utility function that will take a dictionary in which keys may be dot-paths, and converts it to a nested dictionary:
def flat_to_nested_dict(dct: dict) -> dict: """Convert a dct of key: value pairs into a nested dict. Keys that have dots in them indicate nested structure. """ def key_to_dct(key, val, dct): if '.' in key: key, parts = key.split('.', maxsplit=1) if k...
docs/examples/fitting.ipynb
steven-murray/halomod
mit
So, this will do the following:
flat_to_nested_dict( { 'nested.key': 1, 'nested.key2': 2, 'non_nested': 3 } )
docs/examples/fitting.ipynb
steven-murray/halomod
mit
This will enable us to pass a list of parameter names that we want updated, which could be parameters of nested models. This means our posterior function is fairly general, and can accept any model parameters to be updated:
def log_prob(param_values, param_names, data, model, bounds=None, derived=()): # Pack parameters into a dict params = dict(zip(param_names, param_values)) # Allow for simple bounded flat priors. bounds = bounds or {} for key, val in params.items(): bound = bounds.get(key, (-np.inf, np.i...
docs/examples/fitting.ipynb
steven-murray/halomod
mit
We can test that the log_prob function works:
log_prob( [12.0, 12.8, 1.05], ['hod_params.M_min', 'hod_params.M_1', 'hod_params.alpha'], (mock_data, mock_ngal), model, derived=['satellite_fraction', 'mean_tracer_den'] )
docs/examples/fitting.ipynb
steven-murray/halomod
mit
Notice the derived parameters: we can pass any quantity of the TracerHaloModel, and it will be stored on every iteration. Nice! Run emcee Let's run a simple model in which we just want to fit $\sigma_8$ and the spectral index $n_s$. We use the popular emcee package, and pass in our log_prob model:
backend = emcee.backends.HDFBackend("backend.h5") backend.reset(100, 3) blobs_dtype = [("sat_frac", float), ("tracer_den", float), ("bias_effective_tracer", float), ("corr_auto_tracer", (float, len(mock_data)))] sampler = emcee.EnsembleSampler( nwalkers = 100, ndim = 3, log_prob_fn = log_prob, kwargs = ...
docs/examples/fitting.ipynb
steven-murray/halomod
mit
On the advice of the emcee documentation, we set up some initial positions of the walkers around the solution.
initialpos = np.array([ fiducial_model.hod.params['M_min'], fiducial_model.hod.params['M_1'], fiducial_model.hod.params['alpha'] ]) + 1e-4 * np.random.normal(size=(sampler.nwalkers, sampler.ndim)) sampler.run_mcmc(initialpos, nsteps=10000, progress=True);
docs/examples/fitting.ipynb
steven-murray/halomod
mit
Now we can plot the posterior in a corner plot, along with the derived parameters, and the true input values:
flatchain = sampler.get_chain(discard=500, thin=5, flat=True) blobs = sampler.get_blobs(discard=500, thin=5, flat=True) flatchain = np.hstack(( flatchain, np.atleast_2d(blobs['sat_frac']).T, np.atleast_2d(np.log10(blobs['tracer_den'])).T, np.atleast_2d(blobs['bias_effective_tracer']).T )) np.save('...
docs/examples/fitting.ipynb
steven-murray/halomod
mit
And we're done! The posterior contains the truth to within 1-sigma. Let's also plot the residuals:
xi_out = sampler.get_blobs(discard=500, thin=5, flat=True)['corr_auto_tracer'] quantiles = np.quantile(xi_out, [0.16, 0.50, 0.84], axis=0) plt.scatter(model.r, mock_data / quantiles[1]) plt.errorbar(model.r, mock_data / quantiles[1], yerr = 0.2*np.abs(fiducial_model.corr_auto_tracer) / quantiles[1], fmt='none') plt....
docs/examples/fitting.ipynb
steven-murray/halomod
mit
Is it big enough? Now we have all our data loaded into variable big_data, but can we really say it's Big Data?
print "We have {} posts".format(len(big_data))
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
Wow! So data! Very big! Seriously though... it's not big. In fact it's rather small. How small is small? Here's a clue...
import os print "The source file is {} bytes. Pathetic.".format(os.stat(INPUT_FILE).st_size)
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
At the time this was written, the file was just about 3MB, and there were fewer than 2k posts... note that excludes comments made on posts, but still, this stuff is small. It is small enough that at no point do we need to do anything clever from a data indexing/caching/storage perspective, so to start we will take the ...
import itertools all_the_fields = set(itertools.chain.from_iterable(big_data)) print "We have {} different field names:".format(len(all_the_fields)) print all_the_fields
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
Are we missing anything? A good way to sanity check things is to actually inspect the data, so let's look at a random item:
import random import pprint # re-run this as much as you like to inspect different items pprint.pprint(random.choice(big_data))
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
From that you should be able to sense that we are missing some things - it isn't simply that there are some number of fields that describe each item, because some of those fields have data hierarchies beneath them, for example:
pprint.pprint(big_data[234])
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
From that we can see some fields have hierarchies within them, e.g. likes have a list of id dictionaries, which happen to be relatively trivial (names and ids... I wonder why Facebook didn't just post the id and make you look up the name?) but the comment field is a bit more complex, wherein it contains a list of dicti...
pprint.pprint(big_data[234]['comments'][0]['data'][1]['message_tags'])
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
Data quality annoyances Actually I'm not even sure why the comments field is a single entry list. Is that always the case?
set([len(data['comments']) for data in big_data if 'comments' in data])
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
Apparently that's not always the case, sometimes there are 2 items in the list, let's see what that looks like...
multi_item_comment_lists = [data['comments'] for data in big_data if ('comments' in data) and (len(data['comments']) > 1)] print len(multi_item_comment_lists) pprint.pprint(multi_item_comment_lists[0])
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
Skimming the above it looks as though very long comment threads are split into multiple "pages" in the comments list. This may be an artifact of the paging code in pull_feed.py, which is not ideal. At some point we may fix it there, but for the time being we'll just consider it a data quality inconvenience that we will...
def flatten_comments_pages(post): flattened_comments = [] for page in post: flattened_comments += page['data'] return flattened_comments post_comments_paged = multi_item_comment_lists[0] print "Post has {} comments".format(len(flatten_comments_pages(post_comments_paged)))
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
Start plotting things already dammit Now that we're counting comments, it's natural to ask: what does the number-of-comments-per-post distribution look like? IMPORTANT NOTE: Beyond this point, we start to "follow the data" as we analyse things, and we do so in a time-relative way (e.g. comparing the last N days of pos...
comments_threads = [data['comments'] for data in big_data if 'comments' in data] count_of_posts_with_no_comments = len(big_data) - len(comments_threads) comments_counts = [0] * count_of_posts_with_no_comments comments_counts += [len(flatten_comments_pages(thread)) for thread in comments_threads] import matplotlib.pypl...
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
This sort of adds up intuitively; posts with long comment threads will be rare, though from experience with this forum it does not seem right to conclude that there is a lot of posting going on with no interaction... the community is a bit more engaged than that. But since this is Facebook, comments aren't the only wa...
likes_threads = [data['likes']['data'] for data in big_data if 'likes' in data] count_of_posts_with_no_likes = len(big_data) - len(likes_threads) likes_counts = [0] * count_of_posts_with_no_likes likes_counts += [len(thread) for thread in likes_threads] plt.hist(likes_counts, bins=max(likes_counts)) plt.title("Likes-p...
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
Note that the above does not include Likes on Comments made on posts; only Likes made on posts themselves are counted. While this paints the picture of a more engaged community, it still doesn't feel quite right. It seems unusual these days to find a post go by without a Like or two. I have a hunch that the zero-like p...
import datetime import dateutil import pytz def epoch_utc_s(date_string): dt_local = dateutil.parser.parse(str(date_string)) dt_utc = dt_local.astimezone(pytz.utc) nineteenseventy = datetime.datetime(1970,1,1) epoch_utc = dt_utc.replace(tzinfo=None) - nineteenseventy return int(epoch_utc.total_seco...
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
In general it seems my hunch may have been right, but it will be clearer if we plot it.
plt.hist(timestamps_of_posts_without_likes, alpha=0.5, label='non-Liked posts') plt.hist(timestamps_of_posts_with_likes, alpha=0.5, label='Liked posts') plt.title("Liked vs non-Liked posts") plt.xlabel("Time (epoch UTC s)") plt.ylabel("Count of posts") plt.legend(loc='upper left') plt.show()
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
This is looking pretty legit now. We can see that lately there's been a significant uptick in the number of posts, and an uptick in the ratio of posts that receive at least one Like. As another sanity check, we can revisit the Likes-per-post Histogram, but only include recent posts. While we're at it we might as well d...
def less_than_n_days_ago(date_string, n): query_date = epoch_utc_s(date_string) today_a_year_ago = epoch_utc_s(datetime.datetime.now(pytz.utc) - datetime.timedelta(days=n)) return query_date > today_a_year_ago # try changing this variable then re-running this cell... days_ago = 30 # create a slice of our ...
legacy/Getting Meta with Big Data Malaysia.ipynb
BigDataMalaysia/bigdatamy_fb_group_analysis
mit
build database
from __future__ import print_function from itertools import (islice, izip) import arrow import github3 import requests from sqlalchemy import or_ from github_settings import (ry_username, ry_password, username, password, # token, ...
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
reading in data using pandas
# http://www.datacarpentry.org/python-ecology-lesson/08-working-with-sql import sqlite3 from itertools import islice # Create a SQL connection to our SQLite database con = sqlite3.connect("gitenberg.db") cur = con.cursor() # the result of a "cursor.execute" can be iterated over by row for row in islice(cur.execute(...
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
Build a specific book Mr.-Spaceship_32522
class MetadataWriterRunner2(MetadataWriterRunner): def __init__(self, dbfname, gh_username, gh_password, access_token=None, max_exceptions=None, repos_list=None): super(MetadataWriterRunner2, self).__init__(dbfname, gh_username, gh_password, access_token, max_excep...
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
metadatawrite
mwr = MetadataWriterRunner("gitenberg.db", username, password) mwr.run(1) mwr.exceptions() job = BuildRepo2(username=username, password=password, repo_name='', repo_owner='GITenberg', update_travis_commit_msg='build using gitenberg.travis', ...
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
Building books
session = create_session("gitenberg.db") (session.query(Repo) .filter(or_(Repo.buildable == None, Repo.buildable == True)) .filter(Repo.datebuilt == None) .filter(Repo.metadata_written != None) ).count() rjr = RepoJobRunner("gitenberg.db", username, password, GITENBERG_TRAVIS_ACCESS_TOKEN, max_exceptions=20) rjr....
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
StatusUpdater
class StatusUpdateRunner2(StatusUpdateRunner): def __init__(self, dbfname, gh_username, gh_password, access_token=None, max_exceptions=None, repos_list=None): super(StatusUpdateRunner2, self).__init__(dbfname, gh_username, gh_password, access_token, max_exceptions) ...
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
overall stats
(session.query(Repo) .filter(Repo.ebooks_in_release_count == 3) ).count() session.query(Repo.ebooks_in_release_count).distinct().all()
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
sql SELECT ebooks_in_release_count, count (ebooks_in_release_count) FROM Repos GROUP BY ebooks_in_release_count
# how many built (session.query(Repo) .filter(Repo.datebuilt != None).count()) # how many for which we know lastbuilt status (session.query(Repo) .filter(Repo.last_build_state != None).count()) # http://stackoverflow.com/a/4086229/7782 from sqlalchemy import func (session.query(Repo.ebooks_in_release_count, func....
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
debugging errors / failures
failed_builds = (session.query(Repo) .filter(Repo.last_build_state == 'failed')) failed_builds.count() for (i, repo) in enumerate(islice(failed_builds,None)): url = url = "https://travis-ci.org/GITenberg/{repo_name}/builds/{last_build_id}".format(repo_name=repo.repo_name, last_build_id=r...
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
let's look at https://travis-ci.org/GITenberg/American-Hand-Book-of-the-Daguerreotype_167/builds/150209405 cannot read from /home/travis/build/GITenberg/American-Hand-Book-of-the-Daguerreotype_167/book.epub The case of the image file names don't match -- case sensitivity. For https://travis-ci.org/GITenberg/Literary-...
# repo_name = "American-Hand-Book-of-the-Daguerreotype_167" gtj = GitenbergTravisJob(username, password, repo_name, 'GITenberg', update_travis_commit_msg='build using gitenberg.travis', tag_commit_message='build using gitenberg.travis', access_token=GITENBERG_TRAVIS_ACCESS_TOKEN) gtj.travis_...
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
How to read log files from travis? revisit menegazzo/travispy: Travis CI API for Python
# How to read log files from travis b = gtj.travis.build(gtj.travis_repo.last_build_id) j = b.jobs[-1] j.id j.log.body[:100]
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
update repos with started status
(session.query(Repo) .filter(Repo.last_build_state == 'started') ).count() class StatusUpdateRunnerForStartedJobs(StatusUpdateRunner): def repos(self, n): return islice((self.session().query(Repo) .filter(Repo.last_build_state == 'started') ),n) sur2 = StatusUpdateRunnerForStart...
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
rerunning jobs that have error status
class ErroredRepoJobRunner(RepoJobRunner): def repos(self, n): return islice((self.session().query(Repo) .filter(Repo.last_build_state == 'errored') ),n) erjr = ErroredRepoJobRunner("gitenberg.db", username, password, GITENBERG_TRAVIS_ACCESS_TOKEN, max_exceptions=20) ...
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
Misc
for repo in session.query(Repo).filter_by(ebooks_in_release_count = 3): repo.has_metadata = True repo.has_source = True repo.buildable = True repo.updated = arrow.now().isoformat() session.commit() import gitenberg b = gitenberg.Book(1) b.parse_book_metadata() b.meta.metadata import yaml md = rep...
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
wondering if not add_all -- any add or update function? python - SQLAlchemy insert or update example - Stack Overflow
repo1.version = '0.0.5' session.dirty session.new our_repo = session.query(Repo).filter_by(repo_name='Repo1').first() # doctest:+NORMALIZE_WHITESPACE our_repo
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
personal access tokens
gh = github3.login(ry_username, password=ry_password) from itertools import islice auths = [{'name': auth.name, 'created_at':auth.created_at, 'updated_at':auth.updated_at} for auth in islice(gh.iter_authorizations(),None)] sorted(auths, key=lambda r: r['created_at'])
build_all_gitenberg.ipynb
rdhyee/nypl50
apache-2.0
TF简介
# 例1: a+b a = tf.placeholder(dtype=tf.float32, shape=[2]) # 定义占位符,可以feed满足相应条件的数据 b = tf.placeholder(dtype=tf.float32, shape=[2]) c = a + b with tf.Session() as sess: # 创建一个会话 print sess.run(c, feed_dict={a:[1.,2.], b:[3.,3.]}) # 例2: 最小值 f(x) = x(1-x)sin(x) import matplotlib.pylab as plt %matplotlib inline x = ...
tensorflow_tinanic.ipynb
gengyj/ml-basic-course
gpl-3.0
LR算法
# 说明: 我们还是使用tinanic数据,见sklearn_titanic.ipynb import cPickle with open("../kaggle_titanic/data/train_data","rb") as f: X_train, y_train = cPickle.load(f) X_train = X_train.astype(np.float32) y_train = y_train.reshape((-1,1)).astype(np.float32) X_tra, X_val, y_tra, y_val = train_test_split(X_train,y_train, test_si...
tensorflow_tinanic.ipynb
gengyj/ml-basic-course
gpl-3.0
LR算法简介: 对样本${x^{i}, y^{i}}(i=1,2,...,N),其中y^i \in {0,1}$,LR模型为: $$h(x) = sigmod(\vec{w} \cdot \vec{x}+b)$$ 其中,$sigmod(t) = \frac{1}{1+e^{-t}}$, $\vec{w},b$为参数。其损失函数为 $$L(\vec{w}) = -\sum_{i=0}^{N}(y^i \log h(x^i) + (1-y^i) \log (1-h(x^i)))$$
N_INPUT = 14 MAX_STEP = 1000 def inference(x): # 一般情况下,把正向传播部分到到一起,称之为infercence,如果要修改模型,很多时候修改这部分就可以了 w = tf.Variable(np.random.randn(N_INPUT,1),dtype=tf.float32) b = tf.Variable([0.], dtype=tf.float32) h = tf.matmul(x,w) + b # h = x * w + b return h x = tf.placeholder(tf.float3...
tensorflow_tinanic.ipynb
gengyj/ml-basic-course
gpl-3.0
添加一个隐藏层 LR算法是单层的NN,前面分析过,我们使用LR模型是欠拟合的,我们可以通过增加隐藏层复杂化,看看效果如何?
N_INPUT = 14 MAX_STEP = 1000 N_HID = 7 def inference(x): w1 = tf.Variable(np.random.randn(N_INPUT,N_HID),dtype=tf.float32) b1 = tf.Variable([0.], dtype=tf.float32) h1 = tf.nn.tanh(tf.matmul(x,w1) + b1) w2 = tf.Variable(np.random.randn(N_HID,1),dtype=tf.float32) b2 = tf.Variable([0.], dtype=tf.float...
tensorflow_tinanic.ipynb
gengyj/ml-basic-course
gpl-3.0
比较
import numpy as np import matplotlib.pylab as plt %matplotlib inline acc1 = np.array(acc1) acc2 = np.array(acc2) plt.figure(figsize=(12,6)) plt.plot(acc1[:,0],acc1[:,1],'b--') plt.plot(acc1[:,0],acc1[:,2],'b-') plt.plot(acc2[:,0],acc2[:,1],'g--') plt.plot(acc2[:,0],acc2[:,2],'g-') plt.title("step vs. accuracy")
tensorflow_tinanic.ipynb
gengyj/ml-basic-course
gpl-3.0
II. RNN <img src="rnnAML.png" width=900>
import keras from keras.layers import Concatenate,Dense,Embedding rnn_num_units = 64 embedding_size = 16 #Let's create layers for our recurrent network #Note: we create layers but we don't "apply" them yet embed_x = Embedding(n_tokens,embedding_size) # an embedding layer that converts character ids into embeddings #...
_files/bacterial_names/RNNs_KERAS.ipynb
vaxherra/vaxherra.github.io
mit
III. Sampling
x_t = tf.placeholder('int32',(None,)) h_t = tf.Variable(np.zeros([1,rnn_num_units],'float32')) next_probs,next_h = rnn_one_step(x_t,h_t) def generate_sample(seed_phrase=None,max_length=MAX_LENGTH): ''' The function generates text given a phrase of length at least SEQ_LENGTH. parameters: T...
_files/bacterial_names/RNNs_KERAS.ipynb
vaxherra/vaxherra.github.io
mit
Product and expectation value QobjEvo.mul_vec(t,state) = spmv(QobjEvo(t), state) QobjEvo.expect(t, state, real) = cy_expect_psi/cy_expect_rho_vec (QobjEvo(t), state, real)
from qutip.cy.spmatfuncs import spmv, cy_expect_rho_vec, cy_expect_psi spmv(td_func(2).data, vec) == td_func.mul_vec(2,vec) print(td_func(2).data * mat_c == td_func.mul_mat(2,mat_c)) mat_c.flags print(td_func(2).data * mat_f == td_func.mul_mat(2,mat_f)) mat_f.flags cy_expect_psi(td_str(2).data, vec, 0) == td_str.ex...
development/development-qobjevo-adv.ipynb
qutip/qutip-notebooks
lgpl-3.0