9 examples of 'convert list to numpy array' in Python

Every line of 'convert list to numpy array' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
308def to_array_(v):
309 return v.toArray().tolist()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

17def to_nd_float_array(list_obj):
18 return np.array(list_obj, dtype=np.float32)
162def _convert_to_numpy(data):
163 """Returns tuple data with all elements converted to numpy ndarrays"""
164 if data is None:
165 return None
166 elif isinstance(data, tuple):
167 return tuple([_convert_to_numpy(d) for d in data])
168 elif isinstance(data, np.ndarray):
169 return data
170 elif isinstance(data, (pd.DataFrame, pd.Series)):
171 return data.to_numpy()
172 else:
173 raise Exception("Unsupported type %s" % str(type(data)))
95def to_list(v):
96 if v is not None and np.isscalar(v):
97 return [v]
98 else:
99 return v
151def list_of_pairs_to_numpy(l):
152 return np.asarray([x[1] for x in l], np.float32), np.asarray([x[0] for x in l], np.int)
55def vec_to_array(vector: np.ndarray) -> np.ndarray:
56 """
57 Converts a 1D vector to 2D array with the length of the input vector
58
59 for example
60 >>> x = np.linspace(0, 1, 10)
61 >>> x.shape
62 (10,)
63 >>> vec_to_array(x).shape
64 (10, 1)
65
66 :param vector:
67 :return:
68 """
69 return vector.reshape((len(vector), 1))
356def to_ndarray(data, copy=True):
357 if copy:
358 cp = lambda x: np.copy(x)
359 else:
360 cp = lambda x: x
361 if str(type(data)) == "<class 'chainer.variable.Variable'>":
362 return cp(data.num)
363 elif isinstance(data, np.ndarray):
364 return cp(data)
365 elif isinstance(data, numbers.Number):
366 return data
367 elif isinstance(data, collections.Iterable):
368 return np.asarray(data)
369 else:
370 raise ValueError('Unknown type of data {}. Cannot add to list'
371 .format(type(data)))
20def scalar4_vec_to_np(array):
21 npa = np.empty((len(array), 4))
22 for i, e in enumerate(array):
23 npa[i,0] = e.x
24 npa[i,1] = e.y
25 npa[i,2] = e.z
26 npa[i,3] = e.w
27 return npa
69def lst_2_array():
70 """
71 list, tuple to array
72 :return: none
73 """
74 tp = (1, 2, 3)
75 lst = [[1, 2], [3, 4]]
76 print np.array(lst).shape
77 # (2L, 2L)
78 print np.array(lst)
79 # [[1 2]
80 # [3 4]]
81 print np.asarray(lst)
82 # [[1 2]
83 # [3 4]]
84 print np.asarray(tp)

Related snippets