Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Machine learning (ML) is a powerful tool for data analysis and prediction. Implementing ML models on a Linux system can be highly efficient due to the robust nature of the Linux environment and the availability of powerful open-source tools. This article will guide you through the process of setting up a machine learning environment on Linux, creating a simple ML model, and executing it via the command line.
Before we begin, ensure you have the following:
First, update your package list and install Python and pip, the Python package manager.
sudo apt update
sudo apt install python3 python3-pip -y
Next, install virtualenv to create isolated Python environments.
sudo pip3 install virtualenv
Create a virtual environment for your ML project.
mkdir ml_project
cd ml_project
virtualenv venv
source venv/bin/activate
Install essential Python libraries for machine learning such as numpy
, pandas
, scikit-learn
, and matplotlib
.
pip install numpy pandas scikit-learn matplotlib
Let's create a simple linear regression model using the scikit-learn
library. Create a file named linear_regression.py
and add the following code:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Generate some sample data
np.random.seed(0)
X = 2.5 * np.random.randn(100) + 1.5 # Array of 100 values
res = 1.5 * np.random.randn(100) # Generate 100 residual terms
y = 2 + 0.3 * X + res # Actual values of Y
# Reshape X for sklearn
X = X.reshape(-1, 1)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Create a linear regression model
model = LinearRegression()
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Plot the results
plt.scatter(X_test, y_test, color='black')
plt.plot(X_test, y_pred, color='blue', linewidth=3)
plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression')
plt.show()
To run your script, simply execute the following command in your terminal:
python linear_regression.py
This will generate a scatter plot of the test data and a line representing the linear regression model.
By following these steps, you have successfully set up a machine learning environment on a Linux system, created a simple linear regression model, and executed it via the command line. This foundational setup can be expanded to more complex models and larger datasets as you continue your journey in machine learning.