How to implement a plot of the regression model in my code? - python

I have a little school project and would like to show the plot of the function in any way, maybe like this:
I know that my code is probaply bad, and if you have any iprovements just throw them at me.
This is the code I have worked on so far... I coded the data into the program by hand.
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn import linear_model
Xtrain = np.array([[15, 15, 20, 30, 20, 20],
[10, 10, 15, 25, 15, 15],
[20, 20, 25, 35, 25, 25],
[20, 20, 30, 20, 30, 20],
[15, 15, 25, 15, 25, 15],
[25, 25, 35, 25, 35, 25],
[30, 30, 30, 30, 10, 10],
[25, 25, 25, 25, 10, 10],
[35, 25, 35, 35, 15, 15],
[20, 20, 30, 25, 30, 25],
[15, 15, 25, 20, 25, 20],
[25, 25, 35, 30, 35, 30],
[10, 10, 15, 25, 30, 20],
[10, 10, 10, 20, 25, 15],
[20, 20, 20, 30, 35, 25],
[20, 25, 25, 20, 30, 20],
[15, 20, 20, 15, 25, 15],
[25, 30, 30, 25, 35, 25]])
ytrain = np.array([20, 15, 25, 20, 15, 25, 15, 10, 20, 20, 15, 25, 15, 10, 20, 20, 15, 25])
lr = LogisticRegression().fit(Xtrain, ytrain)
yhat = lr.predict(Xtrain)
print (accuracy_score(ytrain, yhat))

The problem is your Xtrain (in other words your Xaxis) is composed of 6 variables. That means it is 6 dimensional. On top of that there is a Y dimension of ytrain. A total of 7 dimensions. It will be very hard to visualize 7 dimensions on a 2D diagram. However suppose you want to plot the first column in the Xtrain with respect to Ytrain and plot it on top of it the predicted yhat, you can do as below. However please note this will not serve your original purpose of plotting the full Xtrain.
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn import linear_model
import matplotlib.pyplot as plt
Xtrain = np.array([[15, 15, 20, 30, 20, 20],
[10, 10, 15, 25, 15, 15],
[20, 20, 25, 35, 25, 25],
[20, 20, 30, 20, 30, 20],
[15, 15, 25, 15, 25, 15],
[25, 25, 35, 25, 35, 25],
[30, 30, 30, 30, 10, 10],
[25, 25, 25, 25, 10, 10],
[35, 25, 35, 35, 15, 15],
[20, 20, 30, 25, 30, 25],
[15, 15, 25, 20, 25, 20],
[25, 25, 35, 30, 35, 30],
[10, 10, 15, 25, 30, 20],
[10, 10, 10, 20, 25, 15],
[20, 20, 20, 30, 35, 25],
[20, 25, 25, 20, 30, 20],
[15, 20, 20, 15, 25, 15],
[25, 30, 30, 25, 35, 25]])
ytrain = np.array([20, 15, 25, 20, 15, 25, 15, 10, 20, 20, 15, 25, 15, 10, 20, 20, 15, 25])
lr = LogisticRegression().fit(Xtrain, ytrain)
yhat = lr.predict(Xtrain)
plt.scatter(x=Xtrain[:,0],y=ytrain,color="blue")
plt.scatter(x=Xtrain[:,0],y=yhat,color="red")
plt.show()
The output is as below. The predicted and observed values are very close in this case. Please let me know, if my explanation made any sense or if I read the problem requirement completely wrong.

Related

Coverting Int64index to list or accessing list of lists

I have a list of lists but because of the Int64Index I cannot access it. Is there a way to access individual values or make it into a normal list?
data_exp = pd.read_csv(path+'/exp.csv')
exp_list=[]
for i in range (1,n+1):
check=data_exp.apply(lambda x: True if x['Set No.']==i else False, axis=1)
temp=[data_exp[check==True].index+1]
exp_list.append(temp)
del temp
display(exp_list)
The for loop just sort values based on a condition. The output is good but it is the format which is problamatic.
Gives me out put as follows:-
[[Int64Index([8, 11, 17, 20, 21, 27, 29, 36, 37, 38], dtype='int64')],
[Int64Index([1, 3, 7, 10, 14, 31, 33, 34, 35], dtype='int64')],
[Int64Index([5, 9, 12, 15, 19, 23, 25, 26, 28, 32], dtype='int64')],
[Int64Index([2, 4, 6, 13, 16, 18, 22, 24, 30, 39, 40], dtype='int64')]]
Thanks in advance
I'm not quite sure what you're doing to get the list of Int64Indexes, but you can access the numpy array underlying the index with the values property:
from pandas import Int64Index
l = [[Int64Index([8, 11, 17, 20, 21, 27, 29, 36, 37, 38], dtype='int64')],
[Int64Index([1, 3, 7, 10, 14, 31, 33, 34, 35], dtype='int64')],
[Int64Index([5, 9, 12, 15, 19, 23, 25, 26, 28, 32], dtype='int64')],
[Int64Index([2, 4, 6, 13, 16, 18, 22, 24, 30, 39, 40], dtype='int64')]]
print(l[0][0].values[0])

Create Image Patches Using Numpy

Let's assume that I have input of 4x4 image with 3 channels with following pixel values:
And I want to make it to 12 x 9 matrix of image patches like this (using 2x2 kernel on a 4x4 image):
How can I achieve this using numpy?
Thank you for your help.
Assuming 4x4x3 as input and 12x9 as output
from scipy.signal import convolve
import numpy as np
# creating the 4x4x3 input image
a = np.arange( 1,16+1).reshape(4,4)
b = np.arange(17,32+1).reshape(4,4)
c = np.arange(33,48+1).reshape(4,4)
i_4x4x3 = np.dstack((a, b, c))
# creating four 2x2 kernels
mask_tl = np.array([0,0,0,1]).reshape(2,2)
mask_tr = np.array([0,0,1,0]).reshape(2,2)
mask_bl = np.array([0,1,0,0]).reshape(2,2)
mask_br = np.array([1,0,0,0]).reshape(2,2)
mask_tl = mask_tl[:,:,None]
mask_tr = mask_tr[:,:,None]
mask_bl = mask_bl[:,:,None]
mask_br = mask_br[:,:,None]
# convolving the input with all four kernels
tl = convolve(i_4x4x3, mask_tl, mode='valid')
tr = convolve(i_4x4x3, mask_tr, mode='valid')
bl = convolve(i_4x4x3, mask_bl, mode='valid')
br = convolve(i_4x4x3, mask_br, mode='valid')
i = np.dstack((
tl.reshape(-1,3),
tr.reshape(-1,3),
bl.reshape(-1,3),
br.reshape(-1,3)))
i=i.reshape(i.shape[0],-1).transpose()
display(a,b,c)
display(i)
Output:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
array([[17, 18, 19, 20],
[21, 22, 23, 24],
[25, 26, 27, 28],
[29, 30, 31, 32]])
array([[33, 34, 35, 36],
[37, 38, 39, 40],
[41, 42, 43, 44],
[45, 46, 47, 48]])
array([[ 1, 2, 3, 5, 6, 7, 9, 10, 11],
[ 2, 3, 4, 6, 7, 8, 10, 11, 12],
[ 5, 6, 7, 9, 10, 11, 13, 14, 15],
[ 6, 7, 8, 10, 11, 12, 14, 15, 16],
[17, 18, 19, 21, 22, 23, 25, 26, 27],
[18, 19, 20, 22, 23, 24, 26, 27, 28],
[21, 22, 23, 25, 26, 27, 29, 30, 31],
[22, 23, 24, 26, 27, 28, 30, 31, 32],
[33, 34, 35, 37, 38, 39, 41, 42, 43],
[34, 35, 36, 38, 39, 40, 42, 43, 44],
[37, 38, 39, 41, 42, 43, 45, 46, 47],
[38, 39, 40, 42, 43, 44, 46, 47, 48]])

Is it possible to put numbers on top of a matplot histogram?

import matplotlib.pyplot as plt
import numpy as np
randomnums = np.random.normal(loc=9,scale=6, size=400).astype(int)+15
Output:
array([25, 22, 19, 26, 24, 9, 19, 32, 30, 25, 29, 17, 21, 14, 17, 27, 27,
28, 17, 17, 20, 21, 16, 28, 20, 24, 15, 20, 20, 13, 33, 21, 30, 27,
8, 22, 24, 25, 23, 13, 24, 20, 16, 32, 15, 26, 34, 16, 21, 21, 28,
22, 23, 18, 20, 22, 23, 22, 23, 26, 22, 25, 19, 29, 14, 27, 21, 23,
24, 19, 25, 15, 22, 23, 19, 19, 23, 21, 22, 17, 25, 15, 24, 25, 23 ...
h = sorted(randomnums)
plt.hist(h,density=False)
plt.show()
Output:
From my research I found only how to plot numbers on top of a bar chart, but what I want is to plot on top of a histogram chart. Is it possible?
An adapted version of the answer I linked in the comments of the question. Thanks a lot for the suggestions in the comments below this post!
import matplotlib.pyplot as plt
import numpy as np
h = np.random.normal(loc=9,scale=6, size=400).astype(int)+15
fig, ax = plt.subplots(figsize=(16, 10))
ax.hist(h, density=False)
for rect in ax.patches:
height = rect.get_height()
ax.annotate(f'{int(height)}', xy=(rect.get_x()+rect.get_width()/2, height),
xytext=(0, 5), textcoords='offset points', ha='center', va='bottom')
...gives e.g.
See also: matplotlib.axes.Axes.annotate.

How do I initialize a numpy array starting at a particular number?

I can initialize a numpy array and reshape it at the time of creation.
test = np.arange(32).reshape(4, 8)
which produces this:
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31]])
... but I'd like to know how to start the sequential numbering at a given point, say at 13 rather than at 0. How is that done in numpy?
I've looked for answers and found something somewhat similar but it seems there would be a numpy command to do this.
arange takes an optional start argument.
start = 13 # Any number works here
np.arange(start, start + 32).reshape(4, 8)
# array([[13, 14, 15, 16, 17, 18, 19, 20],
# [21, 22, 23, 24, 25, 26, 27, 28],
# [29, 30, 31, 32, 33, 34, 35, 36],
# [37, 38, 39, 40, 41, 42, 43, 44]])

Dynamic Arrays in Python using numpy

travel_mat1= numpy.array([[23,23,20,24,28,12,17,10],[11,27,17,19,24,18,23,7],
[17,26,22,13,18,29,30,18],[22,21,28,7,18,29,30,18],[27,16,33,36,10,23,26,25],
[31,13,36,14,26,23,20,27],[34,7,33,20,35,17,14,24],[28,13,27,26,37,11,10,18],
[25,17,33,28,34,10,12,15]])
I need to change the size of array dynamically with no loss of actual data in the array. Means, I need to have a virtual dynamic array.
The above array Travel_mat1 is a 9X8 matrix. So if i need a 8X7 size matrix from Travel_mat1, it should look like:
([[23,23,20,24,28,12,17],[11,27,17,19,24,18,23],[17,26,22,13,18,29,30],
[22,21,28,7,18,29], [27,16,33,36,10,23,26],[31,13,36,14,26,23,20],
[34,7,33,20,35,17,14],[28,13,27,26,37,11,10]]).
Means, I need to reduce a row and a column in this case. How can I do this in python?
You can use numpy.delete:
>>> numpy.delete(numpy.delete(travel_mat1, 8, 0), 7, 1)
array([[23, 23, 20, 24, 28, 12, 17],
[11, 27, 17, 19, 24, 18, 23],
[17, 26, 22, 13, 18, 29, 30],
[22, 21, 28, 7, 18, 29, 30],
[27, 16, 33, 36, 10, 23, 26],
[31, 13, 36, 14, 26, 23, 20],
[34, 7, 33, 20, 35, 17, 14],
[28, 13, 27, 26, 37, 11, 10]])

Categories