Convert list to matrix Python numpy
NumPy - Array From Existing DataAdvertisements Previous Page Next Page In this chapter, we will discuss how to create an array from existing data. Show numpy.asarrayThis function is similar to numpy.array except for the fact that it has fewer parameters. This routine is useful for converting Python sequence into ndarray. numpy.asarray(a, dtype = None, order = None)The constructor takes the following parameters.
The following examples show how you can use the asarray function. Example 1Live Demo # convert list to ndarray
import numpy as np
x = [1,2,3]
a = np.asarray(x)
print a
Its output would be as follows [1 2 3]Example 2Live Demo # dtype is set
import numpy as np
x = [1,2,3]
a = np.asarray(x, dtype = float)
print a
Now, the output would be as follows Example 3Live Demo # ndarray from tuple
import numpy as np
x = (1,2,3)
a = np.asarray(x)
print a
Its output would be [1 2 3]Example 4Live Demo # ndarray from list of tuples
import numpy as np
x = [(1,2,3),(4,5)]
a = np.asarray(x)
print a
Here, the output would be as follows [(1, 2, 3) (4, 5)]numpy.frombufferThis function interprets a buffer as one-dimensional array. Any object that exposes the buffer interface is used as parameter to return an ndarray. numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)The constructor takes the following parameters.
ExampleThe following examples demonstrate the use of frombuffer function. Live Demo import numpy as np
s = 'Hello World'
a = np.frombuffer(s, dtype = 'S1')
print a
Here is its output ['H' 'e' 'l' 'l' 'o' ' ' 'W' 'o' 'r' 'l' 'd']numpy.fromiterThis function builds an ndarray object from any iterable object. A new one-dimensional array is returned by this function. numpy.fromiter(iterable, dtype, count = -1)Here, the constructor takes the following parameters.
The following examples show how to use the built-in range() function to return a list object. An iterator of this list is used to form an ndarray object. Example 1Live Demo # create list object using range function
import numpy as np
list = range(5)
print list
Its output is as follows Example 2Live Demo # obtain iterator object from list
import numpy as np
list = range(5)
it = iter(list)
# use iterator to create ndarray
x = np.fromiter(it, dtype = float)
print x
Now, the output would be as follows [0. 1. 2. 3. 4.]Useful Video CoursesVideo Python Data Science basics with Numpy, Pandas and Matplotlib63 Lectures 6 hours Abhilash Nelson More Detail Video Data Analysis using NumPy and PandasMost Popular 19 Lectures 8 hours DATAhill Solutions Srinivas Reddy More Detail Video Numpy with Python12 Lectures 3 hours DATAhill Solutions Srinivas Reddy More Detail Video Basics Data Science with Numpy, Pandas and Matplotlib10 Lectures 2.5 hours Akbar Khan More Detail Video NumPy For Data Science & Machine Learning:: From Beginner To Advanced Level20 Lectures 2 hours Pruthviraja L More Detail Video Pandas Crash Course for begineers : Numpy + Pandas + MatplotlibFeatured 63 Lectures 6 hours Anmol More Detail Previous Page Print Page Next Page Advertisements |