num_features = all_data.select_dtypes(include=['int64','float64','int32']).copy() num_features.drop(['SalePrice'],axis=1,inplace=True) # 去掉目标值房价列 num_feature_names = list(num_features.columns) num_features_data = pd.melt(all_data, value_vars=num_feature_names) g = sns.FacetGrid(num_features_data, col="variable", col_wrap=5, sharex=False, sharey=False) g = g.map(sns.distplot, "value") plt.show()
时间: 2023-06-12 19:07:46 浏览: 54
这段代码主要是使用 seaborn 库来可视化数据集中所有数值型特征的分布情况,其中包括 int64、float64 和 int32 类型的特征。首先,将所有数值型特征列选出来,并去掉目标值列 SalePrice。然后,将所有数值型特征列进行 melt 操作,将其转化为长格式,即将所有列转化为两列,一列为变量名,一列为变量对应的数值。接着,使用 FacetGrid 函数创建一个多子图网格,每个子图对应一个变量,使用 distplot 函数绘制每个变量对应的数值分布直方图。最后,使用 plt.show() 函数展示图形。
相关问题
class sampler (Sampler): def u (self, train size, batch_ size): num_ data = train_ size self .num_ per batch = int(num_ data 1 batch_ size) self .batch size = batch_ size self .range = torch.arange(0, batch_ size) .view(1, batch_ size).long() self.leftover flag = False if num_ data % batch_ size: self.leftover = torch.arange(self .num_ per batch * batch_ size, num_ data) . long( )self.leftover flag = True def_ iter_a (self): rand_ num = torch.randperm(self .num_ per_ batch) .view(-1, 1) * self .batch size self .rand_ num = rand_ num. expand(self .num_ per_ batch, self .batch_size) + self .range self .rand num_view = self .rand_ num. view(-1) if self.leftover_ flag: self .rand_ num_ view = torch.cat((self.rand_ num_ view, self.leftover), 0 return iter(self .rand_ num_ view) def Len_ (self): return num_ data 分析上述代码中的错误
代码中的错误在于使用了错误的语法和变量命名。以下是需要修正的错误:
1. 函数 `u()` 的定义中,应该将 `train size` 改为 `train_size`。同样的,`batch_ size` 改为 `batch_size`。
2. 在 `u()` 函数中,`int(num_ data 1 batch_ size)` 的语法是错误的,应该改为 `int(num_data / batch_size)`。
3. 在 `u()` 函数中,`self.leftover flag` 的语法也是错误的,应该改为 `self.leftover_flag`。
4. 在 `u()` 函数中,`long( )self.leftover flag = True` 的语法是错误的,应该将其分成两行,并且添加缺少的冒号。
5. 在 `def_ iter_a()` 函数中,`rand_ num = torch.randperm(self .num_ per_ batch) .view(-1, 1) * self .batch size` 的语法是错误的,应该将 `self .batch size` 改为 `self.batch_size`。
6. 在 `def_ iter_a()` 函数中,`self .rand num_view = self .rand_ num. view(-1)` 的语法是错误的,应该将 `self .rand num_view = self .rand_ num. view(-1)` 改为 `self.rand_num_view = self.rand_num.view(-1)`。
7. 在 `def_ iter_a()` 函数中,`if self.leftover_ flag:` 的语法是错误的,应该将其改为 `if self.leftover_flag:`。
8. 在 `def_ iter_a()` 函数中,`self .rand_ num_ view = torch.cat((self.rand_ num_ view, self.leftover), 0` 的语法是错误的,应该将其改为 `self.rand_num_view = torch.cat((self.rand_num_view, self.leftover), 0)`。
9. 在 `def Len_()` 函数中,`return num_ data` 的语法是错误的,应该将其改为 `return num_data`。
修正以上错误后的代码如下:
```python
class Sampler:
def u(self, train_size, batch_size):
num_data = train_size
self.num_per_batch = int(num_data / batch_size)
self.batch_size = batch_size
self.range = torch.arange(0, batch_size).view(1, batch_size).long()
self.leftover_flag = False
if num_data % batch_size:
self.leftover = torch.arange(self.num_per_batch * batch_size, num_data).long()
self.leftover_flag = True
def iter_a(self):
rand_num = torch.randperm(self.num_per_batch).view(-1, 1) * self.batch_size
self.rand_num = rand_num.expand(self.num_per_batch, self.batch_size) + self.range
self.rand_num_view = self.rand_num.view(-1)
if self.leftover_flag:
self.rand_num_view = torch.cat((self.rand_num_view, self.leftover), 0)
return iter(self.rand_num_view)
def Len(self):
return num_data
```
以上是修正后的代码。修正后代码中的变量命名和语法都是正确的。
怎么加快这段代码的运行速度num_samples = x_data.shape[0] features = tf.Variable(initial_value=tf.zeros((1, 0)), dtype=tf.float32) batch_size = 32 for i in range(0, num_samples, batch_size): batch = x_data[i:i + batch_size] batch = tf.expand_dims(batch, axis=0) if i + batch_size > num_samples: batch_num = num_samples - i if batch_num == 1: feature_batch = model.predict(batch.reshape(1, *input_shape), batch_size=batch_num) else: feature_batch = model.predict(batch, batch_size=batch_num) features = tf.concat([features, feature_batch], axis=1) else: feature_batch = model.predict(batch, batch_size=batch_size) features = tf.concat([features, feature_batch], axis=1) print(features.shape)
这段代码的运行速度可以通过以下几种方式进行优化:
1. 使用更快的模型:如果模型的计算速度较慢,可以尝试使用更快的模型,例如MobileNet、EfficientNet等。
2. 使用GPU加速:如果你的电脑有GPU,可以使用GPU加速模型的计算。可以使用`tf.config.experimental.set_memory_growth()`方法来设置GPU内存分配策略,以避免内存溢出问题。
3. 使用TensorFlow Dataset:TensorFlow Dataset是一种高效的数据输入管道,可以帮助提高训练速度。可以使用`tf.data.Dataset.from_tensor_slices()`方法来创建一个Dataset对象,然后使用`batch()`方法和`prefetch()`方法来设置批量大小和预取数据。
4. 使用更大的批量大小:如果你的电脑有足够的内存,可以尝试使用更大的批量大小,以提高训练速度。但是需要注意,批量大小过大可能会导致内存溢出问题。
5. 使用多线程预处理数据:如果你的电脑有多个CPU核心,可以使用多线程预处理数据,以加快数据处理速度。可以使用`tf.data.Dataset.map()`方法来定义一个数据预处理函数,并使用`num_parallel_calls`参数来设置线程数。
根据你的代码,可以使用方法1、2和4来进行优化,具体代码如下:
```python
# 方法1:使用更快的模型
from tensorflow.keras.applications import MobileNetV2
model = MobileNetV2(input_shape=input_shape, include_top=False, weights='imagenet')
# 方法2:使用GPU加速
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
tf.config.experimental.set_memory_growth(gpus[0], True)
except RuntimeError as e:
print(e)
# 方法4:使用更大的批量大小
batch_size = 64
# 修改后的代码
features = tf.Variable(initial_value=tf.zeros((1, 0)), dtype=tf.float32)
for i in range(0, num_samples, batch_size):
batch = x_data[i:i + batch_size]
batch = tf.expand_dims(batch, axis=0)
if i + batch_size > num_samples:
batch_num = num_samples - i
if batch_num == 1:
feature_batch = model.predict(batch.reshape(1, *input_shape), batch_size=batch_num)
else:
feature_batch = model.predict(batch, batch_size=batch_num)
features = tf.concat([features, feature_batch], axis=1)
else:
feature_batch = model.predict(batch, batch_size=batch_size)
features = tf.concat([features, feature_batch], axis=1)
print(features.shape)
```
希望这些方法能够帮助你加快代码的运行速度!