| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- # Import necessary libraries
- import pandas as pd
- from sklearn.model_selection import train_test_split
- from sklearn.preprocessing import LabelEncoder
- from sklearn.ensemble import RandomForestClassifier
- from sklearn.metrics import accuracy_score, classification_report
- # Sample data (replace it with your actual dataset)
- data = {
- 'Amount': [100, 50, 200, 150, 75],
- 'Merchant': ['StoreA', 'StoreB', 'StoreA', 'StoreC', 'StoreB'],
- 'Date': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05'],
- 'TransactionType': ['Bensin', 'Matur', 'Bensin', 'Rafmagn', 'Matur']
- }
- df = pd.DataFrame(data)
- # Preprocessing and Feature Engineering
- le_merchant = LabelEncoder()
- df['Merchant'] = le_merchant.fit_transform(df['Merchant'])
- df['Date'] = pd.to_datetime(df['Date'])
- df['DayOfWeek'] = df['Date'].dt.dayofweek
- df['IsWeekend'] = df['DayOfWeek'].isin([5, 6]).astype(int)
- # Drop unnecessary columns
- df = df.drop(['Date', 'DayOfWeek'], axis=1)
- # Define features and target variable
- X = df.drop('TransactionType', axis=1)
- y = df['TransactionType']
- # 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)
- # Train a RandomForestClassifier (replace with your preferred model)
- model = RandomForestClassifier(random_state=42)
- model.fit(X_train, y_train)
- # Make predictions on the test set
- y_pred = model.predict(X_test)
- # Evaluate the model
- accuracy = accuracy_score(y_test, y_pred)
- print(f'Accuracy: {accuracy:.2f}')
- # Display classification report
- print('Classification Report:')
- print(classification_report(y_test, y_pred))
|