목록Numpy (57)
Note
How to convert a 1d array of tuples to a 2d numpy array? # Input: url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_1d = np.genfromtxt(url, delimiter=',', dtype=None) # Solution # 1: Convert each row to a list and get the first 4 items iris_2d = np.array([row.tolist()[:4] for row in iris_1d]) iris_2d[:4] # 2: Import only the first 4 columns from source url iri..
How to extract a particular column from 1D array of tuples? # Input: url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_1d = np.genfromtxt(url, delimiter=',', dtype=None) print(iris_1d.shape) # output #> (150,) # Solution: species = np.array([row[4] for row in iris_1d]) species[:5] array([b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-se..
How to import a dataset with numbers and texts keeping the text intact in python numpy? # Solution url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris = np.genfromtxt(url, delimiter=',', dtype='object') names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species') # Print the first 3 rows iris[:3] # output array([[b'5.1', b'3.5', b'1.4', b'0.2', ..
How to print the full numpy array without truncating # Input np.set_printoptions(threshold=6) a = np.arange(15) # Solution np.set_printoptions(threshold=np.nan) a # output array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])