Question
import pandas as pd import matplotlib import matplotlib.pyplot as plt import seaborn as sns import numpy as np from sklearn.manifold import TSNE from IPython.core.interactiveshell import
import pandas as pd
import matplotlib import matplotlib.pyplot as plt import seaborn as sns import numpy as np from sklearn.manifold import TSNE from IPython.core.interactiveshell import InteractiveShell import warnings from sklearn.preprocessing import StandardScaler from sklearn.manifold import TSNE warnings.filterwarnings('ignore') pd.options.display.max_columns = None
def fix_header(data): new_header = data.iloc[0] # take the first row for the header data = data[1:] # take the data without the header row data.rows = new_header # set the header row as the df header data.rename(columns={'default payment next month':'DEFAULTER'}, inplace=True) # change column name return data
train = pd.read_csv('train.csv') test = pd.read_csv('test.csv') train['Type'] = 'train' test['Type'] = 'test' train.drop(['Type'],axis = 1, inplace=True) test.drop(['Type'],axis = 1, inplace=True) train = train.astype(float) test = test.astype(float) df = pd.concat([train,test],axis=0) df.rename(columns={'default payment next month':'DEFAULTER'}, inplace=True)
df2 = train[train.DEFAULTER == 0].sample(n = 1000) df3 = train[train.DEFAULTER == 1].sample(n = 1000) df4 = pd.concat([df2, df3], axis = 0)
#Scale features to improve the training ability of TSNE. standard_scaler = StandardScaler() df4_std = standard_scaler.fit_transform(df4)
#Set y equal to the target values. y = df4.DEFAULTER
tsne = TSNE(n_components=2, random_state=0) x_test_2d = tsne.fit_transform(df4_std)
#Build the scatter plot with the two types of transactions. color_map = {0:'red', 1:'blue'} plt.figure() for idx, cl in enumerate(np.unique(y)): plt.scatter(x = x_test_2d[y==cl,0], y = x_test_2d[y==cl,1], c = color_map[idx], label = cl) plt.xlabel('X in t-SNE') plt.ylabel('Y in t-SNE') plt.legend(loc='upper right') plt.title('t-SNE visualization of train data') plt.show()
My python code is above and this is the error that comes out. Even thought defaulter is in my columns when i check, it does come out. how to fix this error?
In[13]: df.columns
Out[13]:
Index(['ID', 'LIMIT_BAL', 'SEX', 'EDUCATION', 'MARRIAGE', 'AGE', 'PAY_0', 'PAY_2', 'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6', 'BILL_AMT1', 'BILL_AMT2', 'BILL_AMT3', 'BILL_AMT4', 'BILL_AMT5', 'BILL_AMT6', 'PAY_AMT1', 'PAY_AMT2', 'PAY_AMT3', 'PAY_AMT4', 'PAY_AMT5', 'PAY_AMT6', 'DEFAULTER', 'Type'], dtype='object')
AttributeError: 'DataFrame' object has no attribute 'DEFAULTER'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started