Showing posts with label Normalization. Show all posts
Showing posts with label Normalization. Show all posts

Saturday, February 10, 2024

CHAPTER 12 AI BASED WASHING MACHINE CONTROL

Designing a machine learning model to fully control a washing machine involves several steps, including data collection, preprocessing, model training, and deployment. Here's a simplified example using Tensor Flow to demonstrate the process:
Data Collection: 
Collect data from sensors such as temperature sensors, water level sensors, and load sensors. You'll need labeled data indicating the optimal settings for each type of fabric and level of dirtiness.

Preprocessing: 
Prepare the data for training by normalizing features, handling missing values, and splitting it into training and testing sets.

Model Training: Train a machine learning model, such as a neural network, to predict the optimal settings based on input features like fabric type, dirtiness level, and previous washing history.

Model Evaluation: Evaluate the model's performance on the testing dataset to ensure it generalizes well to unseen data.

Deployment: Deploy the trained model to the washing machine's control system, allowing it to make real-time decisions based on sensor inputs.

Whole Program:

import numpy as np from sklearn.linear_model import LinearRegression # Get user input for fabric type and dirtiness level fabric_type = int(input("Enter fabric type (1 for cotton, 2 for silk, 3 for wool): ")) dirtiness_level = int(input("Enter dirtiness level (1 for low, 2 for medium, 3 for high): ")) # Preprocess user input # Map fabric type to one-hot encoding fabric_type_encoding = np.zeros(3) fabric_type_encoding[fabric_type - 1] = 1 # Normalize dirtiness level dirtiness_level_normalized = (dirtiness_level - 1) / 2.0 # Scale to range [0, 1] # Combine features user_input_features = np.concatenate([fabric_type_encoding, [dirtiness_level_normalized]]) # Repeat the input to match the number of samples in the target values user_input_features_repeated = np.tile(user_input_features, (3, 1)) # Pre-trained model weights (you need to have pre-trained weights saved) # Assuming we have pre-trained weights as a list of arrays pretrained_weights = np.array([ [0.1, 0.2, 0.3, 0.4], # Water Level [0.2, 0.3, 0.4,0.5], # Temperature [0.5, 0.6, 0.7,0.8] # Duration ]) # Define a simple linear regression model model = LinearRegression() # Fit the model to the data model.fit(user_input_features_repeated, pretrained_weights) # Predict optimal settings for user input predicted_settings = model.predict(user_input_features.reshape(1, -1)) print("Predicted Settings:") print("Water Level:", predicted_settings[0][0]) print("Temperature:", predicted_settings[0][1]) print("Duration:", predicted_settings[0][2])


Output

Enter fabric type (1 for cotton, 2 for silk, 3 for wool): 3
Enter dirtiness level (1 for low, 2 for medium, 3 for high): 1
Predicted Settings:
Water Level: 0.26666666666666666
Temperature: 0.3666666666666667
Duration: 0.4666666666666666

Let's go through each line of the code and explain its purpose:

import numpy as np from sklearn.linear_model import LinearRegression
  • import numpy as np: Imports the NumPy library and assigns it the alias np. NumPy is used for numerical operations and array manipulation.
  • from sklearn.linear_model import LinearRegression: Imports the LinearRegression class from scikit-learn's linear_model module. This class is used to fit linear regression models.

# Get user input for fabric type and dirtiness level
fabric_type = int(input("Enter fabric type (1 for cotton, 2 for silk, 3 for wool): "))
dirtiness_level = int(input("Enter dirtiness level (1 for low, 2 for medium, 3 for high): "))


  • input("Enter fabric type (1 for cotton, 2 for silk, 3 for wool): "): Prompts the user to enter the fabric type and reads the input as a string.
  • int(...): Converts the string input to an integer.
  • fabric_type = ...: Assigns the integer value entered by the user to the variable fabric_type.
  • Similarly, dirtiness_level = ... reads and assigns the user input for the dirtiness level.

# Preprocess user input # Map fabric type to one-hot encoding fabric_type_encoding = np.zeros(3) fabric_type_encoding[fabric_type - 1] = 1


  • np.zeros(3): Creates a NumPy array of zeros with length 3. This array will be used to represent the one-hot encoding for fabric type.
  • fabric_type_encoding[fabric_type - 1] = 1: Sets the element corresponding to the fabric type entered by the user to 1, indicating the presence of that fabric type in the one-hot encoding.

# Normalize dirtiness level dirtiness_level_normalized = (dirtiness_level - 1) / 2.0 # Scale to range [0, 1]



(dirtiness_level - 1) / 2.0: Normalizes the dirtiness level entered by the user to a value between 0 and 1. This is done by subtracting 1 from the entered level (to make it 0-indexed) and then dividing by 2.

# Combine features user_input_features = np.concatenate([fabric_type_encoding, [dirtiness_level_normalized]])

np.concatenate([...]): Combines the one-hot encoded fabric type and the normalized dirtiness level into a single array. This array represents the features used for prediction.


# Repeat the input to match the number of samples in the target values user_input_features_repeated = np.tile(user_input_features, (3, 1))

np.tile(user_input_features, (3, 1)): Repeats the single input features three times to match the number of samples in the target values. This ensures that both input features and target values have the same number of samples.

# Define a simple linear regression model model = LinearRegression()

model = LinearRegression(): Creates an instance of the LinearRegression class, which represents the linear regression model.

# Fit the model to the data model.fit(user_input_features_repeated, pretrained_weights)


model.fit(...): Fits the linear regression model to the training data. The user_input_features_repeated are the input features, and pretrained_weights are the target values.

# Predict optimal settings for user input predicted_settings = model.predict(user_input_features.reshape(1, -1))


model.predict(...): Uses the trained model to make predictions on the input features. The reshaping is done to ensure compatibility with the predict method, which expects a 2D array.


print("Predicted Settings:") print("Water Level:", predicted_settings[0][0]) print("Temperature:", predicted_settings[0][1]) print("Duration:", predicted_settings[0][2])

Prints the predicted settings for the user input. Each line prints one predicted setting (water level, temperature, duration) along with its corresponding value.

CHAPTER 18 EXPLORING THERMODYNAMICS WITH PYTHON: UNDERSTANDING CARNOT'S THEOREM AND MORE

  Python is a versatile programming language that can be used to simulate and analyze various physical phenomena, including thermal physics ...