12

How to replace the character with a dictionary value in the numpy array?

 2 years ago
source link: https://www.codesd.com/item/how-to-replace-the-character-with-a-dictionary-value-in-the-numpy-array.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to replace the character with a dictionary value in the numpy array?

advertisements

I want to replace characters in a numpy array to specific values in dictionary. For example I want to replace 6837 to fhcg in the same array format.

This is what I tried

import numpy
val=numpy.array([["6837"],["7628"],["3804"],["3031"],["9848"],["8481"],["1220"],["7701"],["7934"]])
d={'1':'a','2':'b','3':'c','4':'d','5':'e','6':'f','7':'g','8':'h','9':'i','0':'x'}

rep = [d[v] for v in val]
new_val= ' '.join(rep)

But its giving this error

TypeError: unhashable type: 'numpy.ndarray'


Your val array is 2d, so you have to be careful how you iterate on it.

In [255]: val.shape
Out[255]: (9, 1)

Let's define a little function that converts one string; the conversion is character by character - it's a pure string operation.

def foo(astr):
   return ''.join([d[v] for v in astr])

We could use this in a list comprehension or other iterator. But this is one case where np.vectorize does a nice job.

In [257]: f = np.vectorize(foo)
In [258]: f(val)
Out[258]:
array([['fhcg'],
       ['gfbh'],
       ['chxd'],
       ['cxca'],
       ['ihdh'],
       ['hdha'],
       ['abbx'],
       ['ggxa'],
       ['gicd']],
      dtype='<U4')

It does iterate on the array, so doesn't offer much of a speed advantage, but it takes care of 'broadcasting', handling the 2d array without extra work on our part.


Recommend

  • 5

    关于numpy.array和列表list的区别 Oldpan 2021年1月17日 0条评论 136次阅读 0人点赞 ...

  • 8

    Accelerate reading of NumPy array from files In the training process, I need to read array data from .npy file and get a part of it: import numpy as np data = np.load("sample1.npy") sou...

  • 10

    This article will discuss how to convert Pandas Dataframe to Numpy Array. Table of Contents A Dataframe is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.Data...

  • 5

    This article will discuss how to convert Numpy arrays to a Pandas DataFrame. Table of Contents A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using panda...

  • 4
    • www.bobobk.com 2 years ago
    • Cache

    python原生list数组与numpy的array

    python原生list数组与numpy的array 2021年12月22日 | 技术 在python中存储集合数据可以选择多种原生数据类型,包括list,array,tuple,dictionary四种类型....

  • 6
    • www.laravelcode.com 2 years ago
    • Cache

    Python Numpy Array Tutorial

    Python Numpy Array Tutorial   49 views   2 weeks ago Python Python is the most popular general purpose program...

  • 4

    In this article, We will learn how to check if a NumPy Array contains any NaN value in Python. Table of Contents What is a NaN value? The NaN stands for Not a Number, which is a numeric data type that can be interpre...

  • 6
    • thispointer.com 1 year ago
    • Cache

    Check if a value exists in a NumPy Array

    This tutorial will discuss about unique ways to check if a value exists in a NumPy array in Python. Table Of Contents Technique 1: Using “in” keyword Python provides an “in...

  • 4

    This tutorial will discuss about unique ways to check if all elements in a numpy array are equal to value. Table Of Contents Technique 1: Using all() method We can use the...

  • 8

    This tutorial will discuss about unique ways to check if any value in numpy array is negative in Python. Table Of Contents Method 1: Using numpy.any() The numpy module provides...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK