Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import seaborn as sns from sklearn.preprocessing import StandardScaler from sklearn.decomposition import FactorAnalysis from sklearn.impute import SimpleImputer from factor _ analyzer import Rotator # Load the

import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import FactorAnalysis
from sklearn.impute import SimpleImputer
from factor_analyzer import Rotator
# Load the data
df = pd.read_csv('C:\\Users\\tingc\\Desktop\\Final report\\FinalData_Final.csv', encoding='latin1')
# Selecting only numerical columns for factor analysis
numerical_columns = df.select_dtypes(include=[np.number]).columns.tolist()
# Dropping the 'mrt_flow' column if it exists in the numerical columns
if 'mrt_flow' in numerical_columns:
numerical_columns.remove('mrt_flow')
# Update DataFrame to use only the selected numerical columns
df_numerical = df[numerical_columns]
# Imputing missing values
imputer = SimpleImputer(strategy='median')
df_numerical_imputed = imputer.fit_transform(df_numerical)
# Standardizing the data
scaler = StandardScaler()
df_numerical_scaled = scaler.fit_transform(df_numerical_imputed)
# Performing factor analysis
fa = FactorAnalysis(n_components=5, random_state=0)
fa_components = fa.fit_transform(df_numerical_scaled)
# Creating DataFrame for factor analysis results
fa_df = pd.DataFrame(fa_components, columns=[f'Factor{i+1}' for i in range(fa.n_components)])
# Factor loadings before rotation
loadings_before_rotation = fa.components_
# Applying Varimax rotation
rotator = Rotator(method='varimax')
loadings_rotated = rotator.fit_transform(loadings_before_rotation)
Above is the code for factor analysis, now I need to use the results(first 5 factors before rotated & first 5 factors after rotated) to predict y('mrt_flow'), using the SVM method(training:0.9,testing:0.1), and use MAE to evaluate the performance. please give me the complete code(not the steps but the code that can run it directly). Thank you very much!:)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions