python not用法_Python numpy.not_equal()用法及代码示例

关于:

numpy.not_equal(x1,x2 [,out]):检查两个元素是否相等。

参数:

x1, x2 :[array_like]Input Array whose elements we want to check

out :[ndarray, optional]Output array that returns True/False.

A placeholder the same shape as x1 to store the result.

返回:

Boolean array

代码1:

# Python Program illustrating

# numpy.not_equal() method

import numpy as geek

a  = geek.not_equal([1., 2.], [1., 3.])

print("Not equal : \n", a, "\n")

b = geek.not_equal([1, 2], [[1, 3],[1, 4]])

print("Not equal : \n", b, "\n")

输出:

Not equal :

[False True]

Not equal :

[[False True]

[False True]]

代码2:

# Python Program illustrating

# numpy.not_equal() method

import numpy as geek

# Here we will compare Complex values with int

a = geek.array([0 + 1j, 2])

b = geek.array([1,2])

d  = geek.not_equal(a, b)

print("Comparing complex with int using .not_equal() : ", d)

输出:

Comparing complex with int using .not_equal() : [ True False]

代码3:

# Python Program illustrating

# numpy.not_equal() method

import numpy as geek

# Here we will compare Float with int values

a = geek.array([1.1, 1])

b = geek.array([1, 2])

d  = geek.not_equal(a, b)

print("\nComparing float with int using .not_equal() : ", d)

输出:

Comparing float with int using .not_equal() : [ True True]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果将数据集从Emnist转换为Fashion-MNIST,代码需要相应修改。修改之后的代码如下: example_dataset = fashion_mnist_train.create_tf_dataset_for_client( fashion_mnist_train.client_ids[0]) ### 回答2: 当数据改变为本地的Fashion MNIST时,代码需要做如下修改: 1. 导入适用于Fashion MNIST的数据集:`from tensorflow.keras.datasets import fashion_mnist` 2. 加载Fashion MNIST数据集:`(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()` 3. 调整数据格式:Fashion MNIST数据集的形状需要改变为类似于EMNIST的形状,即将每个样本从二维矩阵转换为一维向量。代码如下: ```python x_train = x_train.reshape(x_train.shape[0], -1) x_test = x_test.reshape(x_test.shape[0], -1) ``` 4. 标准化数据:为了提高训练的效果,需要将数据标准化处理。代码如下: ```python x_train = x_train / 255.0 x_test = x_test / 255.0 ``` 5. 创建客户端数据集:根据EMNIST数据集中的代码,我们可以创建Fashion MNIST的客户端数据集。代码如下: ```python import tensorflow as tf from collections import defaultdict def create_tf_dataset_for_client(client_id): dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)) client_dataset = dataset.filter(lambda x, y: tf.equal(tf.argmax(y, axis=-1), client_id)) client_ids = [ tf.argmax(y, axis=-1) for y in y_train] result = defaultdict(list) for i, client_id in enumerate(client_ids): result[client_id.numpy()].append((x_train[i], y_train[i])) return result[client_id] ``` 以上即为将代码适应为本地的Fashion MNIST数据集的修改。请注意,此处只展示了主要步骤和关键代码,实际使用时可能还需要进行一些调整和完善。 ### 回答3: 如果将数据改为本地的fashion_mnist数据集,代码需要进行以下修改: 1. 导入所需的模块: ```python import tensorflow as tf from tensorflow.keras.datasets import fashion_mnist ``` 2. 加载fashion_mnist数据集: ```python (x_train, y_train), (_, _) = fashion_mnist.load_data() ``` 3. 将数据集转换为tf.data.Dataset对象: ```python dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)) ``` 4. 定义数据的预处理函数(可根据需求进行修改): ```python def preprocess_fn(image, label): image = tf.cast(image, tf.float32) / 255.0 # 将像素值归一化到0-1之间 label = tf.cast(label, tf.int32) # 转换标签的数据类型 return image, label ``` 5. 对数据集进行预处理: ```python dataset = dataset.map(preprocess_fn) ``` 6. 若需要选择特定客户端的数据,可以使用下述方法(此处为例子,需根据实际需求进行调整): ```python client_id = 0 # 选择第一个客户端 dataset = dataset.batch(32).filter(lambda x, y: tf.equal(tf.strings.reduce_sum(x), client_id)) ``` 7. 最后,将dataset用于训练(将emnist_train替换为上述定义的fashion_mnist数据集): ```python example_dataset = dataset ``` 这样,代码就根据新的fashion_mnist数据集进行了相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值