#πŸ”’ Need help with a function that checks the 5th column inside a .txt file and removes all negative v

35 messages Β· Page 1 of 1 (latest)

junior stream
#

In the 5th column of the .txt file there's 11 negative values but when I run my code it says that it removed 285 values
(ps I dont know how to link the .txt file nor how to copy paste the code so it'd be a huge help if anyone could lmk πŸ™‚

heady owlBOT
#

@junior stream

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.

#

Hey @junior stream!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
junior stream
#
import numpy as np
import matplotlib.pyplot as plt
from remove_negatives import remove_negatives

#Task 5a
data = np.genfromtxt('EMGdata.txt', delimiter='\t', skip_header=1)

time = data[:, 0]
channels = data[:, 1:]

#Task 5b
fig, axs = plt.subplots(2, 1, figsize=(10, 8))

axs[0].plot(time, channels[:, 0], label="Channel 1")
axs[0].plot(time, channels[:, 1], label="Channel 2")
axs[0].plot(time, channels[:, 2], label="Channel 3")
axs[0].plot(time, channels[:, 3], label="Channel 4")

axs[0].set_title("All Channels vs Time")
axs[0].set_xlabel("Time (seconds)")
axs[0].set_ylabel("Muscle Activity (mV)")
axs[0].legend()

#Task 5d (Using the remove_negative function on channel 4 to ontain the cleaned electrical activity and corresponding time values)
clean_time, clean_channel4, num_removed, num_remaining = remove_negatives(time, channels[:, 3])

#Number of removed and remaining values
print(f"Removed {num_removed} values from Channel 4.")
print(f"Remaining {num_remaining} values in Channel 4.")

axs[1].plot(time, channels[:, 0], 'ro', label="Channel 1")
axs[1].plot(time, channels[:, 1], 'ro', label="Channel 2")
axs[1].plot(time, channels[:, 2], 'ro', label="Channel 3")
axs[1].plot(clean_time, clean_channel4, 'go', label="Channel 4 (Cleaned)")

# Adding cleaned data info to the subplot title
axs[1].set_title(f"Cleaned EMG: removed = {num_removed}, remaining = {num_remaining}")
axs[1].set_xlabel("Time (seconds)")
axs[1].set_ylabel("Muscle Activity (mV)")
axs[1].legend()

plt.tight_layout()
plt.show()
#
import numpy as np

#Task 5c
def remove_negatives(data1, data2):
    """
    The remove negatives function cleans provided data of negative numbers by removing them
    and provides information about the cleaning of this data.

    Args:
    data1: vector of provided data to clean by removal of negative numbers
    data2: vector of provided data that corresponds to data1 (if data1 is the independent variables
           data2 would be the dependent variable, and vice versa)

    Returns:
    clean_data1: vector of provided data1 with negative numbers removed
    clean_data2: vector of provided data2 with elements that corresponds with negative numbers removed
    num_negative: The number of negative numbers in data1 (the number of removed elements)
    num_non_negative: The number of non-negative numbers in data1 (number of remaining elements)
    """
    # Create lists to hold the cleaned data
    clean_data1 = []
    clean_data2 = []
    
    num_negative = 0
    num_non_negative = 0

    for i in range(len(data1)):
        if data2[i] >= 0: 
            clean_data1.append(data1[i])
            clean_data2.append(data2[i])
            num_non_negative += 1
        else:
            num_negative += 1

    return clean_data1, clean_data2, num_negative, num_non_negative
upbeat mica
#

i imagine pandas a no go ? pithink

junior stream
#

what is that πŸ’€

upbeat mica
#

library for data science

junior stream
#

nope it's an assignment 😒

upbeat mica
#

rip

#

can u send the file ?

junior stream
heady owlBOT
junior stream
#

can you see it>

#

?

upbeat mica
#

i can ye

upbeat mica
# junior stream

are u sure data = np.genfromtxt('EMGdata.txt', delimiter='\t', skip_header=1) works for parsing this into a 2d array of floats

#

cause i dont see any tabs in file u sent

junior stream
#

tabs?

#

sry I'm new

upbeat mica
#

tab referring to the key on ur keyboard

junior stream
#

ohh

#

yea it does

upbeat mica
junior stream
#

yep

distant stratus
#

For removing negative numbers, you could go evaluate through them all and use the absolute function on them

junior stream
#

lemme try

#

nope I still get the same result

distant stratus
#

What’s the result?

#

Also, you can pip install pandas, if you give me the OS, I can type up some commands to install pip and pandas.

Pip is Pythons Packaging Index Manager, essentially, it manages all your libraries Python uses

heady owlBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. 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.