#π 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
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!
Add a py after the three backticks.
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
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
i imagine pandas a no go ? 
what is that π
library for data science
nope it's an assignment π’
Your paste is too long, and couldn't be uploaded.
i can ye
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
\t is a tab escape character
tab referring to the key on ur keyboard
this what file looks like on ur end ?
yep
For removing negative numbers, you could go evaluate through them all and use the absolute function on them
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
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.