목록Numpy (57)
Note
How to do probabilistic sampling in numpy? # Import iris keeping the text column intact url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris = np.genfromtxt(url, delimiter=',', dtype='object') # Solution # Get the species column species = iris[:, 4] # 1: Generate Probablistically np.random.seed(100) a = np.array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'..
How to create a new column from existing columns of a numpy array? # Input url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_2d = np.genfromtxt(url, delimiter=',', dtype='object') # Solution # Compute volume sepallength = iris_2d[:, 0].astype('float') petallength = iris_2d[:, 2].astype('float') volume = (np.pi * petallength * (sepallength**2))/3 # Introduce ne..
How to convert a numeric to a categorical (text) array? # Input 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') # Bin petallength petal_length_bin = np.digitize(iris[:, 2].astype('float'), [0, 3, 5, 10]) # Map it to respective cat..
How to find the count of unique values in a numpy array? # Import iris keeping the text column intact 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') # Solution # Extract the species column as an array species = np.array([row.toli..
How to replace all missing values with 0 in a numpy array? # Input url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3]) iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan # Solution iris_2d[np.isnan(iris_2d)] = 0 iris_2d[:4] # output array([[ 5.1, 3.5, 1.4, 0. ]..