import pandas as pd import matplotlib import matplotlib.pyplot as plt import sea
ID: 3708547 • Letter: I
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 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()
Out[13]:
Explanation / Answer
Code looks fine but if you are still getting the error, try following options:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.