#πŸ”’ please help me about this

50 messages Β· Page 1 of 1 (latest)

true dew
#

Create a program for linear regression method with the following conditions:
I) input (not necessarily using the input function, can be inputted directly to the source code):

  1. given x and y/f(x) arrays for all
  2. x for the unknown f(x) for Interpolation
    II) process (conditions to be met by the program):
  3. use numerical methods for solving systems of linear equations
    III) output (results to be displayed):
  4. equation of the curve for Regression
  5. plot the given data set and equation of the curve/plane on the same graph for regression
  6. value of f(x) for Interpolation
slender summitBOT
#

@true dew

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

true dew
#

import numpy as np
import matplotlib.pyplot as plt

def linear_regression(x, y):
n = len(x)
x_mean = np.mean(x)
y_mean = np.mean(y)
xy_mean = np.mean(x * y)
x_squared_mean = np.mean(x ** 2)

# Slope (m) and intercept (b) of the regression line
m = (xy_mean - x_mean * y_mean) / (x_squared_mean - x_mean ** 2)
b = y_mean - m * x_mean

return m, b

def interpolate(x, y, x_interpolate):
m, b = linear_regression(x, y)
y_interpolate = m * x_interpolate + b
return y_interpolate

def plot_regression_line(x, y, m, b):
plt.scatter(x, y, color="red", label="Data points")
plt.plot(x, m * x + b, color="blue", label="Regression line")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Linear Regression")
plt.legend()
plt.show()

Given data

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

Interpolation point

x_interpolate = 6

Calculate regression line equation

m, b = linear_regression(x_given, y_given)
print("Equation of the regression line: y =", m, "* x +", b)

Plot the given data set and regression line

plot_regression_line(x_given, y_given, m, b)

Interpolation

y_interpolate = interpolate(x_given, y_given, x_interpolate)
print("Interpolated value of f(x) at x =", x_interpolate, "is", y_interpolate)

slender summitBOT
#

Hey @true dew!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
true dew
#

line 2 is wrong why is that

#
import matplotlib.pyplot as plt

def linear_regression(x, y):
    n = len(x)
    x_mean = np.mean(x)
    y_mean = np.mean(y)
    xy_mean = np.mean(x * y)
    x_squared_mean = np.mean(x ** 2)

    # Slope (m) and intercept (b) of the regression line
    m = (xy_mean - x_mean * y_mean) / (x_squared_mean - x_mean ** 2)
    b = y_mean - m * x_mean

    return m, b

def interpolate(x, y, x_interpolate):
    m, b = linear_regression(x, y)
    y_interpolate = m * x_interpolate + b
    return y_interpolate

def plot_regression_line(x, y, m, b):
    plt.scatter(x, y, color="red", label="Data points")
    plt.plot(x, m * x + b, color="blue", label="Regression line")
    plt.xlabel("X")
    plt.ylabel("Y")
    plt.title("Linear Regression")
    plt.legend()
    plt.show()

# Given data
x_given = np.array([1, 2, 3, 4, 5])
y_given = np.array([2, 3, 4, 5, 6])

# Interpolation point
x_interpolate = 6

# Calculate regression line equation
m, b = linear_regression(x_given, y_given)
print("Equation of the regression line: y =", m, "* x +", b)

# Plot the given data set and regression line
plot_regression_line(x_given, y_given, m, b)

# Interpolation
y_interpolate = interpolate(x_given, y_given, x_interpolate)
print("Interpolated value of f(x) at x =", x_interpolate, "is", y_interpolate)```
surreal wing
#

What’s line 2? The import?

true dew
#

here

surreal wing
#

Ah

true dew
#

im just boomer

surreal wing
#

Did you install the matplotlib library?

true dew
#

nope

#

can i change that?

surreal wing
#

pip install matplotlib

#

Enter this in your terminal

true dew
#

okok

#

this is my homework btw

#

haha

surreal wing
#

Guessing this is Windows?

#

If not, then this: pip3 install matplotlib

true dew
#

yea

#

Create a program for linear regression method with the following conditions:
I) input (not necessarily using the input function, can be inputted directly to the source code):

  1. given x and y/f(x) arrays for all
  2. x for the unknown f(x) for Interpolation
    II) process (conditions to be met by the program):
  3. use numerical methods for solving systems of linear equations
    III) output (results to be displayed):
  4. equation of the curve for Regression
  5. plot the given data set and equation of the curve/plane on the same graph for regression
  6. value of f(x) for Interpolation
#

can you help me abt this my code is correct/

#

?

#

@surreal wing

#

def linear_regression(x, y):
    n = len(x)
    x_mean = np.mean(x)
    y_mean = np.mean(y)
    xy_mean = np.mean(x * y)
    x_squared_mean = np.mean(x ** 2)

    m = (xy_mean - x_mean * y_mean) / (x_squared_mean - x_mean ** 2)
    b = y_mean - m * x_mean

    return m, b

def interpolate_linear_regression(x_known, y_known, x_interpolate):
    m, b = linear_regression(x_known, y_known)
    y_interpolate = m * x_interpolate + b
    return y_interpolate

# Example usage
x_known = [1.0, 2.0, 3.0, 4.0]
y_known = [2.0, 3.0, 5.0, 7.0]
x_interpolate = 2.5

result = interpolate_linear_regression(x_known, y_known, x_interpolate)
print(f"The interpolated value at x = {x_interpolate} is {result}")```
slender summitBOT
#

Hey @true dew!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
true dew
#

can you help me this one

#

heres the error

surreal wing
#

What's line 25 here?

true dew
#

result = interpolate_linear_regression(x_known, y_known, x_interpolate)

#

here

#

m, b = linear_regression(x_known, y_known)

here the 16

surreal wing
#

Here, x_know is a list of doubles (aka decimals), instead of integers (int).

true dew
#

can you put it in the code cant understand you mate

surreal wing
#

Not on my computer right now

#

But the fix is simple

true dew
#

okok

#

can u guide me

surreal wing
#

Change x_known into an np.array

#

Same with y_known

#

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

#

Something like that

twilit eagle
#

`import numpy as np

def linear_regression(x, y):
n = len(x)
x_mean = np.mean(x)
y_mean = np.mean(y)
xy_mean = np.mean(np.array(x) * np.array(y))
x_squared_mean = np.mean(np.array(x) ** 2)

m = (xy_mean - x_mean * y_mean) / (x_squared_mean - x_mean ** 2)
b = y_mean - m * x_mean

return m, b

def interpolate_linear_regression(x_known, y_known, x_interpolate):
m, b = linear_regression(x_known, y_known)
y_interpolate = m * x_interpolate + b
return y_interpolate

Example usage

x_known = [1.0, 2.0, 3.0, 4.0]
y_known = [2.0, 3.0, 5.0, 7.0]
x_interpolate = 2.5

result = interpolate_linear_regression(x_known, y_known, x_interpolate)
print(f"The interpolated value at x = {x_interpolate} is {result}")`

#

Please check

#

@true dew worked?

true dew
#

yea tnxx

slender summitBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.