Using the following Python imports, answer the following questions: import numpy as np…
- User
Using the following Python imports, answer the following questions:
import numpy as np np.random.seed(42) import os import pandas as pd from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.pipeline import Pipeline from sklearn.ensemble import RandomForestClassifier,BaggingClassifier from sklearn.tree import DecisionTreeClassifier from sklearn.decomposition import PCA from sklearn.preprocessing import StandardScaler from sklearn.metrics import classification_report, accuracy_score, f1_score, recall_score, precision_score, confusion_matrix import time
A) Read "normal.csv" as a pandas dataframe "normalData", and print out the shape of the normal dataset.
B) Read "anomalous.csv" as a pandas dataframe "anomalousData", and print out the shape of the nomalous dataset.
- Assistant
To execute tasks A and B in Python, you'd need to use the
pandaslibrary to read the CSV files into dataframes and then print out their shape. I'll provide you with the code to accomplish these tasks, although I can't actually run the code without the files being accessible. Make sure the CSV files "normal.csv" and "anomalous.csv" are in the same directory as your script, or provide the full path to where they are stored.# A) Load "normal.csv" as a pandas dataframe and print out its shape. normalData = pd.read_csv("normal.csv") print("Shape of the normal dataset:", normalData.shape) # B) Load "anomalous.csv" as a pandas dataframe and print out its shape. anomalousData = pd.read_csv("anomalous.csv") print("Shape of the anomalous dataset:", anomalousData.shape)When you run this code, it will print out the number of rows and columns (shape) of each dataframe respectively. If the files aren't present in the directory where you're executing the script, you will get a
FileNotFoundError. Make sure to handle the files' paths correctly depending on where they are located relative to your script. - User
Now, using the same inputs, answer the next few questions:
A) Complete the myTrainTestSplit function, which takes as input two dataframes consisting of the normal and anomalous datasets, reserves 30 percent of each dataframe for testing and returns 4 variables, Xtrain, Xtest, ytrain, and ytest.
Note: you can use
concatfrom pandas library to concatenade trainig sets from two dataframes, and test sets from two dataframes.B) Apply a Decision Tree model for classifying the events as normal or anomalous. Fill in the myDecisionTree function, which accepts as input the training set and returns a fully trained model.
C) Apply a Bagging model that consists of 10 base decision trees for classifying the events as normal or anomalous. Fill in the myBagging function, which accepts as input the training set and returns a fully trained model.
- Assistant
- User
Now, using the same inputs, answer the next few questions:
A) Complete the myTrainTestSplit function, which takes as input two dataframes consisting of the normal and anomalous datasets, reserves 30 percent of each dataframe for testing and returns 4 variables, Xtrain, Xtest, ytrain, and ytest.
Note: you can use concat from pandas library to concatenade trainig sets from two dataframes, and test sets from two dataframes.
B) Apply a Decision Tree model for classifying the events as normal or anomalous. Fill in the myDecisionTree function, which accepts as input the training set and returns a fully trained model.
C) Apply a Bagging model that consists of 10 base decision trees for classifying the events as normal or anomalous. Fill in the myBagging function, which accepts as input the training set and returns a fully trained model.
D) Apply a Random Forest model that consists of 10 base decision trees for classifying the events as normal or anomalous. Fill in the myRandomForest function, which accepts as input the training set and returns a fully trained model.
- Assistant