Untitled

mail@pastecode.io avatar
unknown
python
a year ago
2.1 kB
2
Indexable
Never
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(data.drop('csat', axis=1), data['csat'], test_size=0.2, random_state=42)

# Train a linear regression model for each language
languages = X_train['language'].unique()
models = {}
for language in languages:
    X_train_lang = X_train[X_train['language'] == language].drop('language', axis=1)
    y_train_lang = y_train[X_train['language'] == language]
    model_lang = LinearRegression().fit(X_train_lang, y_train_lang)
    models[language] = model_lang

# Encode language as categorical variable for decision tree and random forest regressors
X_train = pd.get_dummies(X_train, columns=['language'])
X_test = pd.get_dummies(X_test, columns=['language'])

# Train other regression models
models['DecisionTree'] = DecisionTreeRegressor().fit(X_train, y_train)
models['RandomForest'] = RandomForestRegressor().fit(X_train, y_train)

# Evaluate the performance of each model on the testing set
results = {}
for name, model in models.items():
    if name == 'DecisionTree' or name == 'RandomForest':
        y_pred = model.predict(X_test)
    else:
        lang = name
        X_test_lang = X_test[X_test[f'language_{lang}'] == 1].drop(f'language_{lang}', axis=1)
        y_test_lang = y_test[X_test[f'language_{lang}'] == 1]
        y_pred_lang = model.predict(X_test_lang)
        y_pred = [y_pred_lang[i] if X_test.iloc[i][f'language_{lang}'] == 1 else None for i in range(len(X_test))]
    mse = mean_squared_error(y_test, y_pred)
    r2 = r2_score(y_test, y_pred)
    results[name] = {'MSE': mse, 'R2': r2}

# Print the results
print('Model\t\t\tMSE\t\tR2')
for name, result in results.items():
    print(f'{name}\t\t{result["MSE"]:.2f}\t\t{result["R2"]:.2f}')

# Select the best model based on R2 score
best_model_name = max(results, key=lambda x: results[x]['R2'])
best_model = models[best_model_name]
print(f'\nThe best model is {best_model_name} with an R2 score of {results[best_model_name]["R2"]:.2f}.')