#๐Ÿ”’ Help using Machine Learning on python

19 messages ยท Page 1 of 1 (latest)

brittle sandal
#

Hello ! I need help using machine learning AI with python

broken ibexBOT
#

@brittle sandal

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.

brittle sandal
#

I have a project where i'm using a CSV table containing DNA sequences

#

I made it that if a person is stressed, there is more likely to be certain sequences in their dna

#

And if a person has damaged TTAGGGC, he is more likely to have mutations

#

CODE FOR DATA SET GENERATION

#
import pandas as pd
import numpy as np
from adngen import generate_sequence

# Set a seed for reproducibility
np.random.seed(0)

# Number of samples
n_samples = 20000

# Generate 'Stress_Level' and 'Mutation_Possibility' columns
stress_level = np.random.randint(200, 10000, n_samples)
mutation_possibility = np.random.randint(200, 10000, n_samples)

# Generate 'Health_Status' column
health_status = np.random.choice(['Healthy', 'Stressed', 'Mutation_Possible'], n_samples)

# Generate 'DNA_Sequence' column
dna_sequence = [generate_sequence(1000, stress, mutation) for stress, mutation in zip(stress_level, mutation_possibility)]
# Create a DataFrame
df = pd.DataFrame({
    'Stress_Level': stress_level,
    'Mutation_Possibility': mutation_possibility,
    'Health_Status': health_status,
    'DNA_Sequence': dna_sequence
})

# Save the DataFrame to a CSV file
df.to_csv('health_data.csv', index=False)```
#

CODE FOR ADN GENERATION

#
import random

# Define the DNA bases
bases = ['A', 'T', 'C', 'G']


# Function to generate a random DNA sequence of a given length
def generate_sequence(length, stress_probability, mutation_probability):

    # Une chance sur 1000 d'avoir un TTAGCG [Stress]
    if random.randint(0, stress_probability) == 0:
        seq = 'TTAGGG' + 'ATGCAT' + ''.join(random.choice(bases) for _ in range(length - 13))
        seq += 'TTAGGG' + 'ATGCAT' + ''.join(random.choice(bases) for _ in range(length - 13))
        seq += 'TTAGGG' + 'ATGCAT' + ''.join(random.choice(bases) for _ in range(length - 13))
        seq += 'TTAGGG' + 'ATGCAT' + ''.join(random.choice(bases) for _ in range(length - 13))
        seq += 'TTAGGG' + 'ATGCAT' + ''.join(random.choice(bases) for _ in range(length - 13))
        return seq

    if random.randint(0, mutation_probability) == 0:
        return 'TTAGCG' + ''.join(random.choice(bases) for _ in range(length - 6))
    else:
        return 'TTAGGG' + ''.join(random.choice(bases) for _ in range(length - 6))


if __name__ == '__main__':
    Normal_telomeres_probability = 5000
    Normal_stress_probability = 1000

    with open('sane_person.adn', 'w') as f:
        for _ in range(50000):
            f.write(generate_sequence(100, Normal_stress_probability, Normal_telomeres_probability))

    with open('stressed.adn', 'w') as f:
        for _ in range(50000):
            f.write(generate_sequence(100, 200, 1000))

    with open('mutation_possible.adn', 'w') as f:
        for _ in range(50000):
            f.write(generate_sequence(100, 1000, 2000))

#

CODE FOR MACHINE LEARNIONG

#
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.feature_extraction.text import CountVectorizer
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
from adngen import generate_sequence


def generate_kmers(sequence, k=8):
    return [sequence[i:i + k] for i in range(len(sequence) - k + 1)]

# Create a Sequential model
model = Sequential()

# Initialize a CountVectorizer
vectorizer = CountVectorizer()
# Load the dataset
df = pd.read_csv('/home/ec2-user/SageMaker/health_data.csv')



# Generate k-mers for each DNA sequence
df['DNA_Sequence'] = df['DNA_Sequence'].apply(generate_kmers)

# Convert lists of k-mers into strings
df['DNA_Sequence'] = df['DNA_Sequence'].apply(' '.join)



# Fit the CountVectorizer to the DNA sequences and transform the sequences
X = vectorizer.fit_transform(df['DNA_Sequence']).toarray()

# Encode the Health_Status
y = pd.factorize(df['Health_Status'])[0]

# Convert the labels to categorical
y = to_categorical(y)

# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)



# Add an input layer and a hidden layer with 10 neurons
model.add(Dense(10, input_dim=X_train.shape[1], activation='relu'))

# Add an output layer with 3 neurons (since we have 3 classes)
model.add(Dense(3, activation='softmax'))



# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Train the model
model.fit(X_train, y_train, epochs=50, batch_size=10)

# Evaluate the model
loss, accuracy = model.evaluate(X_test, y_test)
print(f'Accuracy: {accuracy}')
#


# Now the model is ready to make predictions on new data
# For example, we can predict the health status of a new DNA sequence
new_sequence = generate_sequence(1000, 1000, 1000)
new_sequence_kmers = ' '.join(generate_kmers(new_sequence))
new_sequence_encoded = vectorizer.transform([new_sequence_kmers]).toarray()
y_pred = model.predict(new_sequence_encoded)
predicted_health_status = pd.factorize(df['Health_Status'])[1][np.argmax(y_pred)]
print(f'Predicted Health Status: {predicted_health_status}')

# stress person
new_sequence = generate_sequence(1000, 200, 1000)
new_sequence_kmers = ' '.join(generate_kmers(new_sequence))
new_sequence_encoded = vectorizer.transform([new_sequence_kmers]).toarray()
y_pred = model.predict(new_sequence_encoded)
predicted_health_status = pd.factorize(df['Health_Status'])[1][np.argmax(y_pred)]
print(f'Predicted Health Status: {predicted_health_status}')

# mutation person
new_sequence = generate_sequence(1000, 1000, 2000)
new_sequence_kmers = ' '.join(generate_kmers(new_sequence))
new_sequence_encoded = vectorizer.transform([new_sequence_kmers]).toarray()
y_pred = model.predict(new_sequence_encoded)
predicted_health_status = pd.factorize(df['Health_Status'])[1][np.argmax(y_pred)]
print(f'Predicted Health Status: {predicted_health_status}')
#

And i keep getting very bad confidence levels

#

so if you know why that is, could you please help me try and fix it ?

#

(i know that my approach isn't very sciency i'm not a biology student)

broken ibexBOT
#

@brittle sandal

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.