Why does my PCA change every time I run the code in python?

Viewed 204

I imputed my dataframe of any missing values with the median of each feature and scaled using StandardScaler(). I ran regular kneighbors with n=3 and the accuracy stays consistent.

Now I am to do the PCA of the resulting dataset with n_components=4 and apply K-neighbors with 3 neighbors. However, every time I run my code, the PCA dataset and kneighbors accuracy changes every time I run the program but the master dataset itself doesn't change. I even tried using first 4 features of the dataset when applying kneighbors and even that is inconsistent.

data = pd.read_csv('dataset.csv')
y = merged['Life expectancy at birth (years)']

X_train, X_test, y_train, y_test = train_test_split(data,
                                                    y,
                                                    train_size=0.7, 
                                                    test_size=0.3, 
                                                    random_state=200)
  
for i in range(len(features)):
    featuredata = X_train.iloc[:,i]
    fulldata = data.iloc[:,i]
    
    fulldata.fillna(featuredata.median(), inplace=True)
    
    data.iloc[:,i] = fulldata


scaler = preprocessing.StandardScaler().fit(X_train)
data = scaler.transform(data)

If I apply KNeighbors here, it runs fine, and my accuracy score remains the same.

pcatest = PCA(n_components=4)

pca_data = pcatest.fit_transform(data)

X_train, X_test, y_train, y_test = train_test_split(pca_data,
                                                    y,
                                                    train_size=0.7, 
                                                    test_size=0.3)

pca = neighbors.KNeighborsClassifier(n_neighbors=3)
pca.fit(X_train, y_train)
y_pred_pca = pca.predict(X_test)
pca_accuracy = accuracy_score(y_test, y_pred_pca)

However, my pca_accuracy score changes every time I run the code. What can I do to make it set and consistent?

first4_data = data[:,:4]

X_train, X_test, y_train, y_test = train_test_split(first4_data,
                                                    y,
                                                    train_size=0.7, 
                                                    test_size=0.3)

first4 = neighbors.KNeighborsClassifier(n_neighbors=3)
first4.fit(X_train, y_train)
y_pred_first4 = first4.predict(X_test)
first4_accuracy = accuracy_score(y_test, y_pred_first4)

I am only taking the first 4 features/columns and the data should remain the same, but for some reason, the accuracy score changes everytime I run it.

1 Answers

You need to give random_statea value in train_test_split otherwise everytime you run it without specifying random_state, you will get a different result. What happens is that every time you split your data, you do it in different ways, unless you specify a random state, or lack there of. It's the equivalent of seed() in R.

Related