0% found this document useful (0 votes)
9 views

np plot linear regression torch

Uploaded by

donruffcorn
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

np plot linear regression torch

Uploaded by

donruffcorn
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#import torch

import numpy as np
#import simsimd; print(simsimd.get_capabilities())

arr = np.array((1, 2, 3, 4, 5))


print(arr)
print(np.__version__)
arr = np.array([1, 2, 3, 4, 5])

print(arr)

print(type(arr))
sc = np.array((42,34) )
arr = np.array([1,8, 3, 4, 5])

print(sc)
print(type(sc))

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr)
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)

arr = np.array([1, 2, 3, 4], ndmin=2)

print(arr)
print('number of dimensions :', arr.ndim)

arr = np.array([1, 2, 3, 4])

print(arr[2] + arr[3])
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('2nd element on 1st row: ', arr[0, 1])

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5])
arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[4:])

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[-3:-1])

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[::5])

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])


print(arr[1, 1:4])
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

print(arr[0:2, 2])

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

print(arr[0:2, 1:4])

arr = np.array([1, 2, 3, 4])

print(arr.dtype)

arr = np.array(['apple', 'banana', 'cherry'])

print(arr.dtype)
arr = np.array(["apple", "banana", "cherry"])

print(arr.dtype)

arr = np.array(['999', 2, 3, 4], dtype='S')

print(arr)
print(arr.dtype)

arr = np.array([1.9, 2.1, 3.1])

rrr = arr.astype('i')

print(rrr)
print(rrr.dtype)

arr = np.array([1, 0, 88])

nr = arr.astype(bool)

print(nr)

arr = np.array([1, 2, 3, 4, 5])

x = arr.copy()
y = arr.view()

print(x.base)
print(y.base)

import timeit

def test(n):
return sum(range(n))

n = 10000
loop = 10000

result = timeit.timeit(lambda: test(n), number=loop)


print(result/loop )
print(test(101) )
# [3 4]
# [1 1 5 5]
# #np.convolve(A, K) if K = np.array([1, 2, 3]) or K = np.array([1, 2, 3, 0, 0])
K = np.array([1, 2, 3])
K = np.array([1, 2, 3, 0, 0])
A=[4,3]
K= [1,1,5,5]
c1 =np.convolve(A, K,'valid')
print(c1)
arr = np.array([1, 2, 3, 4], ndmin=5)

print(arr)
print('shape of array :', arr.shape)

from scipy.special import softmax


arr1 = np.array([3,1.75,-2,0.5] )
#[.95,.85,.12,.62]) [0.72709942 0.20831747 0.00489916 0.05968395]
arr2 = softmax(arr1, axis=0)
#arr3= Sigmoid(arr1)
print (arr2)

import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize

# Create numpy data arrays


'''
# Comment out original data
#x = np.array([821,576,473,377,326])
#y = np.array([255,235,208,166,157])
'''

# Re-calculate x values as a percentage of the first (maximum)


# original x value above
x = np.array([1.000,0.702,0.576,0.459,0.397])

# Recalculate y values as a percentage of their respective x values


# from original data above
y = np.array([0.311,0.408,0.440,0.440,0.482])

def sigmoid(p,x):
x0,y0,c,k=p
y = c / (1 + np.exp(-k*(x-x0))) + y0
return y

plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.show()
Z=y
Z = np.array([3,1.75,-2,0.5] )
#[.95,.85,.12,.62]) [0.72709942 0.20831747 0.00489916 0.05968395]
A = 1 / (1 + (np.exp((-Z))))
#print (A)

import numpy as np #always remember to import numpy


import time
t0 = time.time() #.process_time()
#array1
tmatrix1 = [1.,2.,3.,4.,5.]
#array2
tmatrix2 = [6.,7.,8.,9.,10.]
#dot matrix
for i in range(1000): np.dot(tmatrix1,tmatrix2)
result = timeit.timeit(lambda: np.dot(tmatrix1,tmatrix2), number=1000)

t1 = time.time() #.process_time()

print('timeit',result)
print (t1-t0)
print ("dot operation = ." + "\n Computation time = " + str(1000*(t1 - t0)) +
"ms")

t0 = time.time() #.process_time()
tmatrix1 = [1.,2.,3.,4.,5.]
#array2
tmatrix2 = [6.,7.,8.,9.,10.]
for j in range(1000):
dot = 0
for i in range(len(tmatrix1)):
dot+= tmatrix1[i]*tmatrix2[i]
t1 = time.time() #.process_time()
print (t1-t0)
print ("dot operation = " + str(dot) + "\n Computation time = " + str(1000*(t1 -
t0)) + "ms")

import numpy as np
import matplotlib.pyplot as plt

def sig(x):
return 1/(1 + np.exp(-x))

x = np.linspace(-10, 10, 50)


#print(x)
p = sig(x)
p = softmax(x, axis=0)
#print(p)
plt.xlabel("x")
plt.ylabel("Sigmoid(x)")
plt.plot(x, p)
plt.show()

from scipy.stats import linregress

# Sample data
x = np.array([1, 200, 30, 4, 5])
y = [2, 300, 55, 7, 11]

# Perform linear regression


slope, intercept, r_value, p_value, std_err = linregress(x, y)

print(f"Slope: {slope}")
print(f"Intercept: {intercept}")
plt.plot(x, y,'bo')
plt.plot(x, slope*x+intercept, 'r-')
#plt.show()
plt.show()

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 2.0, 4.0, 6.0, 8.0, 10.0,
20.0, 40.0, 60.0, 80.0])

y = np.array([0.50505332505407008, 1.1207373784533172, 2.1981844719020001,


3.1746209003398689, 4.2905482471260044, 6.2816226678076958,
11.073788414382639, 23.248479770546009, 32.120462301367183,
44.036117671229206, 54.009003143831116, 102.7077685684846,
185.72880217806673, 256.12183145545811, 301.97120103079675])

# Our model is y = a * x, so things are quite simple, in this case...


# x needs to be a column vector instead of a 1D vector for this, however.
x = x[:,np.newaxis]
a, _, _, _ = np.linalg.lstsq(x, y)

print(a)
plt.plot(x, y, 'bo')
plt.plot(x, y)
#plt.plot(x, x,'go')
plt.plot(x, a*x, 'r-')
plt.show()

You might also like