Appearance
question:Create a Python script to generate random numbers between 1 and 100.
answer:import random # Generate 10 random numbers between 1 and 100 for _ in range(10): print(random.randint(1, 100))
question:Design a function in Python to remove the vowels from a given string. text = Hello World
answer:def remove_vowels(text): """Remove the vowels from a given string.""" vowels = ["a", "e", "i", "o", "u"] result = "" for char in text: if char.lower() not in vowels: result += char return result # Output print(remove_vowels(text))
question:Write a Python program to calculate the total number of days between two given dates. First date: 20 Dec 2020 Second date: 10 Jan 2021
answer:from datetime import date def daysDifference(date1, date2): return (date2-date1).days date1 = date(2020, 12, 20) date2 = date(2021, 1, 10) print(daysDifference(date1, date2))
question:Create a neural network in Python to classify fruits into one of three categories (apples, oranges, and pears). Not applicable
answer:import numpy as np from keras.utils import to_categorical from keras.models import Sequential from keras.layers import Dense # Data x_data = np.array([1,2,3,4,5,6,7,8,9,10]) labels = ['apple', 'apple', 'orange', 'orange', 'pear', 'pear', 'apple', 'orange', 'pear', 'apple'] # One-hot encoding y_data = to_categorical([labels.index(label) for label in labels]) # Model building model = Sequential() model.add(Dense(units=3, activation='softmax', input_dim=1)) model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy']) model.fit(x_data, y_data, batch_size=10, epochs=100, verbose=2) # Predictions predictions = model.predict(x_data) # Print predictions for (x, prediction) in zip(x_data, predictions): print('x={0} -- Predicted class={1}'.format(x, np.argmax(prediction)))