简体   繁体   中英

Python: How to create an array of strings using NumPy ndarray

I am trying to do handwritten digit recognition using K-Nearest Neighbours classification using scikit learn. I am creating my own dataset, and have a list of labels (where the labels are strings). The length of the list is 5000.

However, to do KNN classification, both the image data and labels have to be in numpy ndarrays. How can I convert a list of strings into a NumPy ndarray containing those strings? Any insights are appreciated.

just use the np.array() function as follows:

import numpy as np

string_list = ["hello", "world", "str3", "str4"]

string_ndarray = np.array(string_list)

print(string_ndarray)
print(type(string_ndarray))

Output:

['hello' 'world' 'str3' 'str4']
<class 'numpy.ndarray'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM