Vignana-Jyothi / kp-gen-ai

MIT License
0 stars 0 forks source link

[Learn] Jupyternotebook #17

Open head-iie-vnr opened 3 months ago

head-iie-vnr commented 3 months ago
head-iie-vnr commented 3 months ago

Jupyter Basics

High-Level Overview of Jupyter

Jupyter is an open-source project that provides tools for interactive computing across multiple programming languages. It's widely used for data analysis, visualization, machine learning, and more. Here are some high-level details about Jupyter:

1. Jupyter Notebook

2. JupyterLab

3. JupyterHub

4. Jupyter Kernels

5. Jupyter Extensions

Benefits of Using Jupyter

Common Use Cases

Getting Started with Jupyter

  1. Installation: Install Jupyter using pip:
    pip install notebook
  2. Running a Notebook: Start the Jupyter Notebook server:
    jupyter notebook
  3. Creating and Using Notebooks: Use the Notebook Dashboard to create new notebooks, run cells, and visualize results.

Jupyter has become a cornerstone tool for data scientists, researchers, and educators, enabling an interactive and collaborative approach to computing and analysis.

head-iie-vnr commented 3 months ago

Basic Operations:

Example:

Here’s a simple example of a Jupyter Notebook workflow:

  1. Create a new code cell and write a simple Python code:

    print("Hello, Jupyter!")
  2. Run the cell by pressing Shift + Enter.

  3. Create a new Markdown cell and write some Markdown text:

    # My First Jupyter Notebook
    This is a simple example of a Jupyter Notebook.
  4. Run the Markdown cell to render the text.

Useful Tips:

Would you like help with a specific task or need more detailed instructions on a particular aspect of using Jupyter Notebooks?

head-iie-vnr commented 3 months ago

To perform sentiment analysis using a pipeline, you can use the Hugging Face library transformers, which provides pre-trained models for various NLP tasks. Below is a step-by-step guide to achieve this in a Jupyter Notebook:

Step 1: Install Necessary Libraries

First, ensure you have the required libraries installed. You can install transformers using pip:

!pip install transformers

Step 2: Import Libraries and Create a Pipeline

In your Jupyter Notebook, import the necessary libraries and create a pipeline for sentiment analysis:

from transformers import pipeline

# Create a sentiment-analysis pipeline
sentiment_analysis = pipeline('sentiment-analysis')

Step 3: Perform Sentiment Analysis

Use the pipeline to analyze the sentiment of a given text:

# Function to analyze sentiment using pipeline
def analyze_sentiment_pipeline(text):
    result = sentiment_analysis(text)[0]
    return result

# Test the function
sample_text = "I love sunny days, but I hate getting sunburned."
result = analyze_sentiment_pipeline(sample_text)
print(f"Sentiment Analysis of '{sample_text}':")
print(f"Label: {result['label']}, Score: {result['score']:.2f}")

Full Jupyter Notebook Example

Here's a complete example of a Jupyter Notebook for sentiment analysis using a pipeline:

# Sentiment Analysis with Hugging Face Transformers

In this notebook, we will perform sentiment analysis using the Hugging Face Transformers library.
# Install Transformers
!pip install transformers
# Import the necessary library
from transformers import pipeline

# Create a sentiment-analysis pipeline
sentiment_analysis = pipeline('sentiment-analysis')
# Function to analyze sentiment using pipeline
def analyze_sentiment_pipeline(text):
    result = sentiment_analysis(text)[0]
    return result
# Test the function with a sample text
sample_text = "I love sunny days, but I hate getting sunburned."
result = analyze_sentiment_pipeline(sample_text)
print(f"Sentiment Analysis of '{sample_text}':")
print(f"Label: {result['label']}, Score: {result['score']:.2f}")

Running the Notebook

  1. Copy and paste each code block into separate cells in your Jupyter Notebook.
  2. Run each cell sequentially to execute the code.

This will output the sentiment label (e.g., "POSITIVE" or "NEGATIVE") and the confidence score of the sample text.

Would you like to add more features or need further assistance with this example?