Confusion matrix in Google colab is cutted off - python

I tried to visualize my Confusion matrix by the following code:
from mlxtend.plotting import plot_confusion_matrix
import matplotlib.pyplot as plt
import numpy as np
import sklearn as skplt
import scikitplot as skplt
skplt.metrics.plot_confusion_matrix(y_val, autokeras_predictions, figsize = (5, 5), title= 'My confusionmatrix' )
plt.figure(figsize = (10,7))
But it cuts off my confusion matrix above and below. (See picture)
Can anyone help me? Thanks!

I had the same problem.
I'm using Anaconda on Windows.
For me, the following resolved the problem:
from mlxtend.plotting import plot_confusion_matrix
...
multiclass = confusion_matrix(test_y, predictions)
class_names = ['16QAM', '32QAM', '64QAM', 'BPSK', 'QPSK', '8PSK']
fig, ax = plot_confusion_matrix(conf_mat=multiclass, colorbar=True,
show_absolute=False, show_normed=True, class_names=class_names)
-->ax.margins(2,2) #just change the values til adjust to your screen.
plt.show()

Related

How do I show every 15 classes and all the numbers in the boxes in my confusion matrix?

This is my code so far, my goal is that I want my confusion matrix to show all the numbers in each box. Hope this is not too obvious and thanks in advance.
`
import numpy as np
from sklearn.metrics import confusion_matrix, f1_score, roc_curve, precision_score, recall_score, accuracy_score, roc_auc_score
from sklearn import metrics
from mlxtend.plotting import plot_confusion_matrix
from string import digits
import pandas as pd
import matplotlib.pyplot as plt
y_actual = pd.Series([0,0,21,0,23,0,0,0,0,0,0,0,0,0,25], name='Actual')
y_pred = pd.Series([0,0,21,0,23,0,0,0,0,0,0,0,0,0,25], name='Predicted')
confm=pd.crosstab(y_actual,y_pred,margins=True)
df_conf_norm = confm.div(confm.sum(axis=1), axis="index")
def plot_confusion_matrix(conf_mat=confm,values_format = 'd', title='Matriz de Confusion', cmap=plt.cm.hot_r,show_normed=True):
plt.matshow(confm, cmap=cmap)
plt.colorbar()
show_absolute=True
tick_marks = np.arange(len(confm.columns))
plt.xticks(tick_marks, confm.columns, rotation=45)
plt.yticks(tick_marks, confm.index)
plt.ylabel(confm.index.name)
plt.xlabel(confm.columns.name)
plt.show()
confm = pd.crosstab(y_actual, y_pred)
plot_confusion_matrix(confm)
`
I tried to look for an answer here but the ones I've stumbled upon didn't work.

Adjust size of ConfusionMatrixDisplay (ScikitLearn)

How to set the size of the figure ploted by ScikitLearn's Confusion Matrix?
import numpy as np
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
cm = confusion_matrix(np.arange(25), np.arange(25))
cmp = ConfusionMatrixDisplay(cm, display_labels=np.arange(25))
cmp.plot()
The code above shows this figure, which is too tight:
You can send a matplotlib.axes object to the .plot method of sklearn.metrics.ConfusionMatrixDisplay. Set the size of the figure in matplotlib.pyplot.subplots first.
import numpy as np
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
import matplotlib.pyplot as plt
cm = confusion_matrix(np.arange(25), np.arange(25))
cmp = ConfusionMatrixDisplay(cm, display_labels=np.arange(25))
fig, ax = plt.subplots(figsize=(10,10))
cmp.plot(ax=ax)
I was looking for how to adjust the colorbar as someone pointed out in the commentaries in the answer offered by #Raphael and now want to add how to made this.
I used the properties of ConfusionMatrixDisplay and guided by this answer modified the code to:
import numpy as np
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
import matplotlib.pyplot as plt
cm = confusion_matrix(np.arange(25), np.arange(25))
cmp = ConfusionMatrixDisplay(cm, display_labels=np.arange(25))
fig, ax = plt.subplots(figsize=(10,10))
# Deactivate default colorbar
cmp.plot(ax=ax, colorbar=False)
# Adding custom colorbar
cax = fig.add_axes([ax.get_position().x1+0.01,ax.get_position().y0,0.02,ax.get_position().height])
plt.colorbar(cmp.im_, cax=cax)

Trouble creating scatter plot

I'm having trouble using the scatter to create a scatter plot. Can someone help me? I've highlighted the line causing the error:
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import numpy as np
from sklearn.preprocessing import StandardScaler
data = pd.read_csv('vetl8.csv')
df = pd.DataFrame(data=data)
clusterNum = 3
X = df.iloc[:, 1:].values
X = np.nan_to_num(X)
Clus_dataSet = StandardScaler().fit_transform(X)
k_means = KMeans(init="k-means++", n_clusters=clusterNum, n_init=12)
k_means.fit(X)
labels = k_means.labels_
df["Labels"] = labels
df.to_csv('dfkmeans.csv')
plt.scatter(df[2], df[1], c=labels) **#Here**
plt.xlabel('K', fontsize=18)
plt.ylabel('g', fontsize=16)
plt.show()
#data set correct
You are close, just a minor adjustment to access the x-y columns by number should fix it:
plt.scatter(df[df.columns[2]], df[df.columns[1]], c=df["Labels"])

How to build effective K-means algoritham?

I have written a simple K-mean algorithm, But I am finding difficulty to explore it cluster by cluster.
Github Link: https://github.com/AkshayBayas/Machine-learning-/blob/master/K-Means%20algorithm.ipynb
Code:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
%pylab
Df = pd.read_csv('Kdata.csv')
from sklearn.cluster import KMeans
KModule = KMeans()
K_model = KModule.fit(Df)
K_result = K_model.predict(Df)
centers = K_model.cluster_centers_
K_model.labels_
plt.scatter (x1,x2, c = K_model.labels_, cmap = 'rainbow' )
Can anyone help?
No idea what you mean by "explore cluster by cluster".
If you don't specify the number of clusters, by default it is 8, so if you start with 3 like the code below, you can separate them. Also you need to set it as categoric, the cluster, so it will not be colored on a continuous scale:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
Df = pd.read_csv('Kdata.csv')
from sklearn.cluster import KMeans
KModule = KMeans(n_clusters=3)
K_model = KModule.fit(Df)
K_result = K_model.predict(Df)
Df['cluster'] = pd.Categorical(K_model.labels_)
sns.scatterplot("V1","V2",data=Df,hue='cluster',cmap = 'rainbow' )
Df.plot.scatter("V1","V2",c='cluster',cmap = 'rainbow')

PyPlot does not plot image

I created the following test code, and the code runs fine. But the plot does not appear when executed. Did I miss something? I use pyplot to create the plots. When I use plt.savefig("test.png") the chart is created and saved.
import numpy
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
from studentRegression import studentReg
from class_vis import prettyPicture, output_image
from ages_net_worth import ageNetWorthData
ages_train, ages_test, net_worths_train, net_worth_test = ageNetWorthData()
plt.clf()
plt.scatter(ages_train, net_worths_train, color="b", label="train data")
plt.legend(loc=2)
plt.xlabel("ages")
plt.ylabel("net worths")
plt.show()
def ageNetWorthData():
random.seed(42)
numpy.random.seed(42)
ages = []
for ii in range(100):
ages.append( random.randint(20,65) )
net_worths = [ii * 6.25 + numpy.random.normal(scale=40.) for ii in ages]
### need massage list into a 2d numpy array to get it to work in LinearRegression
ages = numpy.reshape( numpy.array(ages), (len(ages), 1))
net_worths = numpy.reshape( numpy.array(net_worths), (len(net_worths), 1))
from sklearn.cross_validation import train_test_split
ages_train, ages_test, net_worths_train, net_worths_test = train_test_split(ages, net_worths)
return ages_train, ages_test, net_worths_train, net_worths_test
You are using a "non-interactive" backend (agg). Just remove the line:
matplotlib.use('agg')
You can check the docs here.

Categories

Resources