text_plain_output_1.png
from sklearn.decomposition import PCA

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

dftrain = pd.read_csv('../input/tabular-playground-series-aug-2022/train.csv')

dftest = pd.read_csv('../input/tabular-playground-series-aug-2022/test.csv')

dftotal = dftrain.append(dftest)

dftotal.reset_index(inplace=True)

dftotal.columns

dftotal.isnull().sum()

dftotal.isnull().sum()

dftotal = dftotal[dftotal['loading'].notna()]

f = plt.figure(figsize=(20, 10))
sns.heatmap(dftotal.corr());

corrtable = dftotal.corr()

import numpy as np
from sklearn.model_selection import train_test_split
dftrainset = dftotal[~dftotal['failure'].isnull()]
dftestset = dftotal[dftotal['failure'].isnull()]
X = dftrainset[['id', 'product_code', 'loading', 'attribute_0', 'attribute_1', 'attribute_2', 'attribute_3', 'measurement_0', 'measurement_1', 'measurement_2', 'measurement_3', 'measurement_4', 'measurement_5', 'measurement_6', 'measurement_7', 'measurement_8', 'measurement_9', 'measurement_10', 'measurement_11', 'measurement_12', 'measurement_13', 'measurement_14', 'measurement_15', 'measurement_16', 'measurement_17']]
y = dftrainset['failure']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

from sklearn.decomposition import PCA
pca = PCA(n_components=4)
X = dftotal[['loading', 'attribute_0', 'attribute_1', 'attribute_2', 'attribute_3', 'measurement_0', 'measurement_1', 'measurement_2', 'measurement_3', 'measurement_4', 'measurement_5', 'measurement_6', 'measurement_7', 'measurement_8', 'measurement_9', 'measurement_10', 'measurement_11', 'measurement_12', 'measurement_13', 'measurement_14', 'measurement_15', 'measurement_16', 'measurement_17']]
y = dftotal['failure']
principalComponents = pca.fit_transform(X)
principalDf = pd.DataFrame(data=principalComponents, columns=['principal component 1', 'principal component 2', 'principal component 3', 'principal component 4'])
dfpcatotal = pd.concat([principalDf, dftrainset[['failure', 'id', 'product_code']]], axis=1)

dfpcatotal.head()