File size: 2,039 Bytes
a9f7c2e
b31d818
 
 
 
 
 
 
 
a9f7c2e
b31d818
 
494ed19
b31d818
 
 
 
494ed19
b31d818
 
 
a9f7c2e
b31d818
 
a9f7c2e
b31d818
 
 
 
 
 
a9f7c2e
b31d818
 
 
 
 
a9f7c2e
b31d818
 
a9f7c2e
b31d818
 
a9f7c2e
b31d818
 
 
a9f7c2e
b31d818
 
a9f7c2e
b31d818
 
494ed19
b31d818
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
import joblib

# Load the data
data = pd.read_csv('sugar_cane_data.csv')

# Data preprocessing
data['Farm'] = data['Farm'].astype(str)
data['Variety'] = data['Variety'].fillna('Unknown')
data['Age'] = data['Age'].fillna('Unknown')

# Split features and target variables
X = data[['Farm', 'Variety', 'Age']]
y = data[['Brix', 'Purity', 'Pol', 'RS']]

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create preprocessing steps
preprocessor = ColumnTransformer(
    transformers=[
        ('num', StandardScaler(), []),
        ('cat', OneHotEncoder(handle_unknown='ignore'), ['Farm', 'Variety', 'Age'])
    ])

# Create a pipeline with preprocessor and random forest regressor
model = Pipeline([
    ('preprocessor', preprocessor),
    ('regressor', RandomForestRegressor(n_estimators=100, random_state=42))
])

# Fit the model
model.fit(X_train, y_train)

# Make predictions on the test set
y_pred = model.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse}")
print(f"R-squared Score: {r2}")

# Save the model
joblib.dump(model, 'sugar_cane_model.joblib')

# Function to make predictions for new data
def predict_sugar_cane_properties(farm, variety, age):
    new_data = pd.DataFrame({'Farm': [farm], 'Variety': [variety], 'Age': [age]})
    prediction = model.predict(new_data)
    return {
        'Brix': prediction[0][0],
        'Purity': prediction[0][1],
        'Pol': prediction[0][2],
        'RS': prediction[0][3]
    }

# Example usage
print(predict_sugar_cane_properties('01-18', 'CP69', 'P'))