黑客业务网 / 更新:2024-05-03 / 评分:4.2 评价:57060

```python import numpy as np import pandas as pd import pickle from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split, cross_val_score # Loading the dataset data = pd.read_csv('creditcard.csv') # Dropping the ID column data = data.drop('ID', axis=1) # Converting the Time column to datetime data['Time'] = pd.to_datetime(data['Time']) # Extracting features from the Time column data['Hour'] = data['Time'].dt.hour data['Date'] = data['Time'].dt.date # Dropping the Time column data = data.drop('Time', axis=1) # Scaling the numerical features scaler = StandardScaler() data_scaled = scaler.fit_transform(data) # Splitting the data into train and test sets X_train, X_test, y_train, y_test = train_test_split(data_scaled, data['Class'], test_size=0.2) # Training the logistic regression model model = LogisticRegression() model.fit(X_train, y_train) # Saving the model pickle.dump(model, open('creditcard_fraud_detection_model.pkl', 'wb')) # Evaluating the model print(f'Model score on the test set: {model.score(X_test, y_test)}') print(f'Cross-validation score: {cross_val_score(model, data_scaled, data["Class"], cv=5).mean()}') ```