欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Tensorflow實(shí)現(xiàn)將標(biāo)簽變?yōu)閛ne-hot形式

 更新時(shí)間:2020年05月22日 10:52:45   作者:星夜孤帆  
這篇文章主要介紹了Tensorflow實(shí)現(xiàn)將標(biāo)簽變?yōu)閛ne-hot形式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

將數(shù)據(jù)標(biāo)簽變?yōu)轭愃芃NIST的one-hot編碼形式

def one_hot(indices, 
 depth, 
 on_value=None, 
 off_value=None, 
 axis=None, 
 dtype=None, 
 name=None):
 """Returns a one-hot tensor.
 
 The locations represented by indices in `indices` take value 
 `on_value`,
 while all other locations take value `off_value`.
 
 `on_value` and `off_value` must have matching data types. If 
 `dtype` is also
 provided, they must be the same data type as specified by 
 `dtype`.
 
 If `on_value` is not provided, it will default to the value `1` with 
 type
 `dtype`
 
 If `off_value` is not provided, it will default to the value `0` with 
 type
 `dtype`
 
 If the input `indices` is rank `N`, the output will have rank 
 `N+1`. The
 new axis is created at dimension `axis` (default: the new axis is 
 appended
 at the end).
 
 If `indices` is a scalar the output shape will be a vector of 
 length `depth`
 
 If `indices` is a vector of length `features`, the output shape will 
 be:
 
 ```
 features x depth if axis == -1
 depth x features if axis == 0
 ```
 
 If `indices` is a matrix (batch) with shape `[batch, features]`, the 
 output
 shape will be:
 
 ```
 batch x features x depth if axis == -1
 batch x depth x features if axis == 1
 depth x batch x features if axis == 0
 ```
 
 If `dtype` is not provided, it will attempt to assume the data 
 type of
 `on_value` or `off_value`, if one or both are passed in. If none 
 of
 `on_value`, `off_value`, or `dtype` are provided, `dtype` will 
 default to the
 value `tf.float32`.
 
 Note: If a non-numeric data type output is desired (`tf.string`, 
 `tf.bool`,
 etc.), both `on_value` and `off_value` _must_ be provided to 
 `one_hot`.
 
 For example:
 
 ```python
 indices = [0, 1, 2]
 depth = 3
 tf.one_hot(indices, depth) # output: [3 x 3]
 # [[1., 0., 0.],
 # [0., 1., 0.],
 # [0., 0., 1.]]
 
 indices = [0, 2, -1, 1]
 depth = 3
 tf.one_hot(indices, depth,
 on_value=5.0, off_value=0.0,
 axis=-1) # output: [4 x 3]
 # [[5.0, 0.0, 0.0], # one_hot(0)
 # [0.0, 0.0, 5.0], # one_hot(2)
 # [0.0, 0.0, 0.0], # one_hot(-1)
 # [0.0, 5.0, 0.0]] # one_hot(1)
 
 indices = [[0, 2], [1, -1]]
 depth = 3
 tf.one_hot(indices, depth,
 on_value=1.0, off_value=0.0,
 axis=-1) # output: [2 x 2 x 3]
 # [[[1.0, 0.0, 0.0], # one_hot(0)
 # [0.0, 0.0, 1.0]], # one_hot(2)
 # [[0.0, 1.0, 0.0], # one_hot(1)
 # [0.0, 0.0, 0.0]]] # one_hot(-1)
 ```
 
 Args:
 indices: A `Tensor` of indices.
 depth: A scalar defining the depth of the one hot dimension.
 on_value: A scalar defining the value to fill in output when 
 `indices[j]
 = i`. (default: 1)
 off_value: A scalar defining the value to fill in output when 
 `indices[j]
 != i`. (default: 0)
 axis: The axis to fill (default: -1, a new inner-most axis).
 dtype: The data type of the output tensor.
 
 Returns:
 output: The one-hot tensor.
 
 Raises:
 TypeError: If dtype of either `on_value` or `off_value` don't 
 match `dtype`
 TypeError: If dtype of `on_value` and `off_value` don't match 
 one another
 """
 with ops.name_scope(name, "one_hot", 
 [indices, depth, on_value, off_value, axis, 
  dtype]) as name:
 on_exists = on_value is not None
 off_exists = off_value is not None
 on_dtype = ops.convert_to_tensor(on_value).dtype.base_dtype 
  if on_exists else None
 off_dtype = ops.convert_to_tensor(off_value).dtype.
  base_dtype if off_exists else None
 if on_exists or off_exists:
  if dtype is not None:
  # Ensure provided on_value and/or off_value match dtype
  if (on_exists and on_dtype != dtype):
   raise TypeError("dtype {0} of on_value does not match "
   "dtype parameter {1}".format(on_dtype, dtype))
  if (off_exists and off_dtype != dtype):
   raise TypeError("dtype {0} of off_value does not match "
   "dtype parameter {1}".format(off_dtype, dtype))
  else:
  # dtype not provided: automatically assign it
  dtype = on_dtype if on_exists else off_dtype
 elif dtype is None:
  # None of on_value, off_value, or dtype provided. Default 
  dtype to float32
  dtype = dtypes.float32
 if not on_exists:
  # on_value not provided: assign to value 1 of type dtype
  on_value = ops.convert_to_tensor(1, dtype, name="
  on_value")
  on_dtype = dtype
 if not off_exists:
  # off_value not provided: assign to value 0 of type dtype
  off_value = ops.convert_to_tensor(0, dtype, name="
  off_value")
  off_dtype = dtype
 if on_dtype != off_dtype:
  raise TypeError("dtype {0} of on_value does not match "
  "dtype {1} of off_value".format(on_dtype, off_dtype))
 return gen_array_ops._one_hot(indices, depth, on_value, 
  off_value, axis, 
  name)
 
 
Enter: apply completion.
 + Ctrl: remove arguments and replace current word (no Pop-
 up focus).
 + Shift: remove arguments (requires Pop-up focus).
import tensorflow as tf
import numpy as np
data = np.linspace(0,9,10)
label = tf.one_hot(data,10)
with tf.Session() as sess:
 print(data)
 print(sess.run(label))

補(bǔ)充知識(shí):數(shù)據(jù)清洗—制作one-hot

使用pandas進(jìn)行one-hot編碼

pandas.get_dummies(data, prefix=None, prefix_sep='_', dummy_na=False, columns=None, sparse=False, drop_first=False, dtype=None)

pandas中g(shù)et_dummies()函數(shù)可以將字段進(jìn)行編碼,轉(zhuǎn)換為01形式,其中prefix可以為每個(gè)新展開(kāi)的列名添加前綴。

但是,筆者發(fā)現(xiàn)它較易使用在數(shù)據(jù)為每一列為單獨(dú)的字符:

df = pd.DataFrame({'A': ['a', 'b', 'a'], 'B': ['b', 'a', 'c'], 'C': [1, 2, 3]})

## one-hot
df_dumm = pd.get_dummies(df)

my_one_hot

但是對(duì)于數(shù)據(jù)為下面形式的可就不能直接轉(zhuǎn)換了,需要先預(yù)處理一下,之后轉(zhuǎn)換為one-hot形式:

我的做法是:

## tqdm_notebook可以導(dǎo)入tqdm包來(lái)使用
def one_hot_my(dataframe, attri):
 sample_attri_list = []
 sample_attri_loc_dic = {}
 loc = 0
 dataframe[attri] = dataframe[attri].astype(str)
 for attri_id in tqdm_notebook(dataframe[attri]):
  attri_id_pro = attri_id.strip().split(',')
  for key in attri_id_pro:
   if key not in sample_attri_loc_dic.keys():
    sample_attri_loc_dic[key] = loc
    loc+=1
  sample_attri_list.append(attri_id_pro)
 print("開(kāi)始完成one-hot.......")  
 one_hot_attri = []
 for attri_id in tqdm_notebook(sample_attri_list):
  array = [0 for _ in range(len(sample_attri_loc_dic.keys()))]
  for key in attri_id:
   array[sample_attri_loc_dic[key]] = 1
  one_hot_attri.append(array)
 print("封裝成dataframe.......") 
 ## 封裝成dataframe
 columns = [attri+x for x in sample_attri_loc_dic.keys()]
 one_hot_rig_id_df = pd.DataFrame(one_hot_attri,columns=columns)
 return one_hot_rig_id_df

對(duì)屬性二值化可以采用:

## 對(duì)屬性進(jìn)行二值化
def binary_apply(key, attri, dataframe):
 key_modify = 'is_' + ''.join(lazy_pinyin(key)) + '_' + attri
 print(key_modify)
 dataframe[key_modify] = dataframe.apply(lambda x:1 if x[attri]== key else 0, axis=1)
 return dataframe

對(duì)字符進(jìn)行編碼,將字符轉(zhuǎn)換為0,1,2…:

## 對(duì)字符進(jìn)行編碼
# columns = ['job', 'marital', 'education','default','housing' ,'loan','contact', 'poutcome']
def encode_info(dataframe, columns):
 for col in columns:
  print(col)
  dataframe[col] = pd.factorize(dataframe[col])[0]
 return dataframe

以上這篇Tensorflow實(shí)現(xiàn)將標(biāo)簽變?yōu)閛ne-hot形式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • pandas groupby 分組取每組的前幾行記錄方法

    pandas groupby 分組取每組的前幾行記錄方法

    下面小編就為大家分享一篇pandas groupby 分組取每組的前幾行記錄方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • pandas如何讀取mysql數(shù)據(jù)

    pandas如何讀取mysql數(shù)據(jù)

    這篇文章主要介紹了pandas如何讀取mysql數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • python實(shí)現(xiàn)掃描局域網(wǎng)指定網(wǎng)段ip的方法

    python實(shí)現(xiàn)掃描局域網(wǎng)指定網(wǎng)段ip的方法

    這篇文章主要介紹了python實(shí)現(xiàn)掃描局域網(wǎng)指定網(wǎng)段ip的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • Python實(shí)現(xiàn)生成隨機(jī)日期字符串的方法示例

    Python實(shí)現(xiàn)生成隨機(jī)日期字符串的方法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)生成隨機(jī)日期字符串的方法,涉及Python日期時(shí)間及隨機(jī)數(shù)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • 簡(jiǎn)單理解Python中基于生成器的狀態(tài)機(jī)

    簡(jiǎn)單理解Python中基于生成器的狀態(tài)機(jī)

    這篇文章主要介紹了簡(jiǎn)單理解Python中基于生成器的狀態(tài)機(jī),來(lái)自于IBM官方技術(shù)文檔,需要的朋友可以參考下
    2015-04-04
  • Python環(huán)境變量設(shè)置方法

    Python環(huán)境變量設(shè)置方法

    默認(rèn)情況下,在windows下安裝python之后,系統(tǒng)不會(huì)自動(dòng)添加相應(yīng)的環(huán)境變量。此時(shí)在命令行輸入python命令是不能執(zhí)行的,配置方法如下
    2016-08-08
  • 好用的Python編輯器WingIDE的使用經(jīng)驗(yàn)總結(jié)

    好用的Python編輯器WingIDE的使用經(jīng)驗(yàn)總結(jié)

    WingIDE是個(gè)專為python程序語(yǔ)言設(shè)計(jì)的集成開(kāi)發(fā)環(huán)境。從1999年起,Wingware公司便開(kāi)始專注于python開(kāi)發(fā),目前WingIDE已經(jīng)是著名的python開(kāi)發(fā)框架,面向項(xiàng)目風(fēng)格的 IDE 對(duì)于大型產(chǎn)品非常有用, 是個(gè)很有前途的開(kāi)發(fā)環(huán)境。
    2016-08-08
  • Python float函數(shù)實(shí)例用法

    Python float函數(shù)實(shí)例用法

    在本篇文章里小編給大家整理的了一篇關(guān)于Python float函數(shù)實(shí)例用法,有興趣的朋友們可以學(xué)習(xí)下。
    2021-03-03
  • python從入門到精通(DAY 1)

    python從入門到精通(DAY 1)

    本文是此次python從入門到精通系列文章的第一篇,給大家匯總一下常用的Python的基礎(chǔ)知識(shí),非常的簡(jiǎn)單,但是很全面,有需要的小伙伴可以參考下
    2015-12-12
  • Python中返回字典鍵的值的values()方法使用

    Python中返回字典鍵的值的values()方法使用

    這篇文章主要介紹了Python中返回字典鍵的值的values()方法使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05

最新評(píng)論