Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4618924
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:21:19+00:00 2026-05-22T02:21:19+00:00

I have file *.data, which include data in this order: 2.5,10,U1 3,4.5,U1 3,9,U1 3.5,5.5,U1

  • 0

I have file *.data, which include data in this order:

2.5,10,U1
3,4.5,U1
3,9,U1
3.5,5.5,U1
3.5,8,U1
4,7.5,U1
4.5,3.5,U1
4.5,4.5,U1
4.5,6,U1
5,5,U1
5,7,U1
7,6.5,U1
3.5,9.5,U2
3.5,10.5,U2
4.5,8,U2
4.5,10.5,U2
5,9,U2
5.5,5.5,U2
5.5,7.5,U2

In this data(I have different types of data, this is just example where are just 2 classes…), is 2 classes: U1 and U2, and for every class there is 2 values…
What I need is to read this data and separate them to classes, in this case to U1 and U2…. Then after that I need to take from every class 2/3 data to new value(learning_set), and other 1/3 to other value(test_set).

I started with this code:

data = open('set.data', 'rt')                             
data_list=[]                                                   
border=2./3                                                  
data_list = [line.strip().split(',') for line in data]

learning_set=data_list[:int(round(len(data_list)*border))]
test_set=data_list[int(round(len(data_list)*border)):]

But there I take from all data 2/3 and 1/3, not from every class.

Many thanks for help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:21:20+00:00Added an answer on May 22, 2026 at 2:21 am

    You can filter your list after reading into two distinct subsets:

    data_list_1 = [(x,y,c) for (x,y,c) in data_list if c=='U1']
    data_list_2 = [(x,y,c) for (x,y,c) in data_list if c=='U2']
    

    Afterwards you can then construct two different learing sets and test sets as before but on the filtered lists, e.g.

    learning_set = data_list_1[:int(round(len(data_list_1)*border))] + data_list_2[:int(round(len(data_list_2)*border))]
    

    and same for test_set.

    Update: If you don’t know the classes before you can use the following code to first detect all classes and then loop over them.

    classes = set([t[-1] for t in data_list])
    
    learning_set = []
    test_set = []
    
    for cl in classes:
        data_list_filtered = [t for t in data_list if t[-1]==cl]
    
        learning_set += data_list_filtered[:int(round(len(data_list_filtered)*border))]
        test_set += data_list_filtered[int(round(len(data_list_filtered)*border)):]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have text file(test.data), which include(this is just example other files have more values....)
I have text file(test.data), which include some values and class name, for example 4.5,3.5,U1
I have this file 'gardens.php' , which pulls data from a table called 'generalinfo'
I have a file data.base which looks like: 1234 XXXX 4321 XXXX 9884 ZZZZ
I have a data file which is comprised of rows of data, newline separated.
I have a data file, which I need to read. I know to read
I have a text file which contains data seperated by '|'. I need to
I have a txt file which contains data in the following format: X1 Y1
I have a flash file which loads data from JSON and parses it. It
I have an xml file(from federal government's data.gov) which I'm trying to read with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.