Question: PythonProgramming - Need to color code data points on a scatterplot. WILL UPVOTE I'm looking to color code my data points on a scatterplot in
PythonProgramming - Need to color code data points on a scatterplot. WILL UPVOTE
I'm looking to color code my data points on a scatterplot in python. My code is shown below. It outputs the scatterplot and the legend to show "Benign" data points to be color coded as red and "Malignant" data points to be color coded as green, but my scatterplot is showing all points as green.
Can you please help debug my code? WILL UPVOTE!!
Here is the python code:
import matplotlib.pyplot as plt import numpy as np from numpy.linalg import svd from sklearn import datasets import pandas as pd
cancer = datasets.load_breast_cancer()
df = pd.DataFrame(cancer.data, columns = cancer.feature_names) df['target'] = cancer.target df['diagnosis'] = np.array([cancer.target_names[i] for i in cancer.target]) df.head()
from sklearn.preprocessing import StandardScaler
X_scaled = StandardScaler().fit_transform(cancer.data) features = X_scaled.T cov_matrix = np.cov(features)
values, vectors = np.linalg.eig(cov_matrix)
explained_variance = [] for i in range(len(values)): explained_variance.append(values[i] / np.sum(values)) projected_1 = X_scaled.dot(vectors.T[0]) projected_2 = X_scaled.dot(vectors.T[1])
res = pd.DataFrame(projected_1, columns = ['Principal Component 1']) res['Principal Component 2'] = projected_2 res['Y'] = df['diagnosis']
#plt.scatter(res['Principal Component 1'], res['Principal Component 2'])
a = res['Principal Component 1'] b = res['Principal Component 2']
plt.figure() plt.figure(figsize = (10,10)) plt.xticks(fontsize = 12) plt.yticks(fontsize = 14) plt.xlabel('Principal Component 1',fontsize = 20) plt.ylabel('Principal Component 2',fontsize = 20) plt.title("Principal Component Analysis of Wisconsin Breast Cancer Dataset",fontsize = 20) targets = ['Benign', 'Malignant'] colors = ['r', 'g'] for target, color in zip(targets, colors): indicesToKeep = df['diagnosis'] == target plt.scatter(res['Principal Component 1'], res['Principal Component 2'], c = color, s = 50)
plt.legend(targets, prop={'size': 15})
BOLD PART OF THE CODE IS WHERE I NEED HELP!!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
