File size: 21,157 Bytes
821537b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"id": "460d90da-b986-4c1c-8a66-eab144b0ba8d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Started Fetching Data\n",
"Failed to fetch data, retrying. Attempt 1/10\n",
"Failed to fetch data, retrying. Attempt 1/10\n",
"Fetched data for all the Pages.\n"
]
}
],
"source": [
"import requests\n",
"import time\n",
"\n",
"import random\n",
"pages = [\n",
" random.randint(1, 968000015)\n",
" for _ in range(500)\n",
" ]\n",
"# print(pages)\n",
"\n",
"base_url = \"https://datasets-server.huggingface.co/rows\"\n",
"params = {\n",
" \"dataset\": \"tiiuae/falcon-refinedweb\",\n",
" \"config\": \"default\",\n",
" \"split\": \"train\",\n",
" }\n",
"# response = requests.get(base_url, params=params)\n",
"# response.raise_for_status()\n",
"# for row in response.json()[\"rows\"]:\n",
"# content = row[\"row\"][\"content\"]\n",
"num_rows_per_page = 100\n",
"retry_limit = 10\n",
"retry_delay = 5\n",
"Falcon = []\n",
"\n",
"print('Started Fetching Data')\n",
"def fetch_data_for_page(page):\n",
" params[\"offset\"] = page\n",
" params[\"limit\"] = num_rows_per_page\n",
" attempt = 0\n",
" while attempt < retry_limit:\n",
" try:\n",
" response = requests.get(base_url, params=params)\n",
" response.raise_for_status() # This will raise an HTTPError if the HTTP request returned an unsuccessful status code\n",
" for row in response.json()[\"rows\"]:\n",
" content = row[\"row\"][\"content\"]\n",
" Falcon.append(content)\n",
" len(Falcon)\n",
" #print(f\"Fetched data for all the Pages.\")\n",
" break\n",
" except requests.exceptions.HTTPError as e:\n",
" attempt += 1\n",
" print(\n",
" f\"Failed to fetch data, retrying. Attempt {attempt}/{retry_limit}\"\n",
" )\n",
" if attempt < retry_limit:\n",
" time.sleep(retry_delay) # Wait before the next retry\n",
" else:\n",
" print(\n",
" \"Maximum retry limit reached. Unable to fetch data.\"\n",
" )\n",
" raise\n",
"\n",
"for page in pages:\n",
" fetch_data_for_page(page)\n",
"\n",
"print(f\"Fetched data for all the Pages.\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "f8f3baf1-5480-450b-a456-174a5c114d3e",
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"\n",
"# Open the CSV file for writing\n",
"with open(\"FalconData2.csv\", \"w\", newline=\"\") as csvfile:\n",
" # Create a CSV writer object\n",
" writer = csv.writer(csvfile)\n",
"\n",
" # Write the header row\n",
" writer.writerow([\"Text\"])\n",
"\n",
" # Write each element in the list as a row in the CSV file\n",
" for element in Falcon:\n",
" writer.writerow([element])\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "ea47c936-2c2b-4414-ba57-74fb6827ec0a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of duplicate rows: 5\n",
" Text\n",
"522 Name:\n",
"11746 Description.\\nReviews\\nThere are no reviews yet.\n",
"17606 Description.\\nReviews\\nThere are no reviews yet.\n",
"30436 NaN\n",
"42549 !\\n\n"
]
}
],
"source": [
"import pandas as pd\n",
"\n",
"# Read the CSV file into a pandas DataFrame\n",
"df = pd.read_csv(\"FalconData2.csv\")\n",
"\n",
"# Check for duplicate rows\n",
"duplicate_rows = df[df.duplicated()]\n",
"\n",
"# Print the number of duplicate rows\n",
"print(f\"Number of duplicate rows: {len(duplicate_rows)}\")\n",
"\n",
"# Print the duplicate rows\n",
"print(duplicate_rows)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "f4178cd6-747f-4e05-a9bf-17b97f959e06",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Text</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>[…]\\nM&S bank […]\\nLowest unsecured loan rate...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>JavaScript seems to be disabled in your browse...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>CMTech has designed a game to foster social in...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>A Storyteller's Point of View\\nMy\\nWriting\\nLe...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>mspu.us was registered 1 decade 3 years ago. I...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Text\n",
"0 […]\\nM&S bank […]\\nLowest unsecured loan rate...\n",
"1 JavaScript seems to be disabled in your browse...\n",
"2 CMTech has designed a game to foster social in...\n",
"3 A Storyteller's Point of View\\nMy\\nWriting\\nLe...\n",
"4 mspu.us was registered 1 decade 3 years ago. I..."
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "264548c1-4cf4-441f-a433-2f5d57861dc4",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Text</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>49995</th>\n",
" <td>Alumni in Action: Grace Heyne Lybrand\\nWhen Gr...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>49996</th>\n",
" <td>This.\\n51.351813 -105.220438\\n12 replies on “L...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>49997</th>\n",
" <td>VIDEO 1: Panel discussion with John Nichols, a...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>49998</th>\n",
" <td>The Prototype DA-2A made its first flight on M...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>49999</th>\n",
" <td>default search action\\nBibTeX record journals/...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Text\n",
"49995 Alumni in Action: Grace Heyne Lybrand\\nWhen Gr...\n",
"49996 This.\\n51.351813 -105.220438\\n12 replies on “L...\n",
"49997 VIDEO 1: Panel discussion with John Nichols, a...\n",
"49998 The Prototype DA-2A made its first flight on M...\n",
"49999 default search action\\nBibTeX record journals/..."
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.tail()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "3f215b09-8050-4477-860c-d3ed0a19f45d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of Words:\n",
"0 65\n",
"1 79\n",
"2 287\n",
"3 302\n",
"4 130\n",
" ... \n",
"49995 64\n",
"49996 325\n",
"49997 58\n",
"49998 623\n",
"49999 67\n",
"Name: Text, Length: 50000, dtype: int64\n",
"Smallest Row:\n",
"Text This\n",
"Name: 270, dtype: object\n",
"\n",
"Largest Row:\n",
"Text MAMMALS\\n400. Abu Jafar, M.Z., and C. Hays-Sha...\n",
"Name: 33020, dtype: object\n"
]
}
],
"source": [
"# Calculate the word count for each row without storing it as a column\n",
"word_counts = df['Text'].apply(lambda x: len(str(x).split()))\n",
"\n",
"\n",
"print(\"Number of Words:\")\n",
"print(word_counts)\n",
"\n",
"# print(\"Smallest Count\")\n",
"# print(word_counts.min())\n",
"\n",
"# print(\"Largest Count\")\n",
"# print(word_counts.max())\n",
"\n",
"# Find the row with the smallest word count\n",
"smallest_row = df.loc[word_counts.idxmin()]\n",
"\n",
"# Find the row with the largest word count\n",
"largest_row = df.loc[word_counts.idxmax()]\n",
"\n",
"# Display the smallest and largest rows\n",
"print(\"Smallest Row:\")\n",
"print(smallest_row)\n",
"\n",
"print(\"\\nLargest Row:\")\n",
"print(largest_row)\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "be5a87a8-cfee-4f63-992e-8fa1d4a5cdbb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text NaN\n",
"Name: 30436, dtype: object"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"target_row=30436\n",
"specific_row = df.iloc[target_row]\n",
"specific_row"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "e97d9e18-eaa0-4a1b-96ab-c89a0f4c738d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Text The old wireline Bell telephone system was bui...\n",
"Name: 19995, dtype: object\n"
]
}
],
"source": [
"print(specific_row)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "940ef35f-7517-403d-9f42-73760182dcaa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Text The old wireline Bell telephone system was bui...\n"
]
}
],
"source": [
"print(specific_row.to_string())"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "915ac669-718f-47f5-b175-a5f928b407db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"57\n"
]
}
],
"source": [
"print(len(specific_row.to_string()))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "ab5ee254-9ba7-496b-97c7-3b6185c21971",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training set size: 49000\n",
"Validation set size: 1000\n"
]
}
],
"source": [
"# import pandas as pd\n",
"\n",
"# # Load the dataset\n",
"# df = pd.read_csv(\"FalconData2.csv\")\n",
"\n",
"# # Calculate the index to split the data at the last 10%\n",
"# split_index = int(len(df) * 0.980)\n",
"\n",
"# # Split the data into training and validation sets\n",
"# train_df = df.iloc[:split_index] # First 90% for training\n",
"# validation_df = df.iloc[split_index:] # Last 10% for validation\n",
"\n",
"# # Display the sizes of the training and validation sets\n",
"# print(f\"Training set size: {len(train_df)}\")\n",
"# print(f\"Validation set size: {len(validation_df)}\")\n",
"\n",
"# # Optionally, save the datasets to new CSV files\n",
"# train_df.to_csv(\"FalconData_train2.csv\", index=False)\n",
"# validation_df.to_csv(\"FalconData_validation2.csv\", index=False)\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "7a16fb10-40cd-4668-b363-57ca64819ad3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of rows removed due to NaN values: 2\n",
"Training set size: 48998\n",
"Validation set size: 1000\n"
]
}
],
"source": [
"import pandas as pd\n",
"\n",
"# Load the dataset\n",
"df = pd.read_csv(\"FalconData2.csv\")\n",
"\n",
"# Check for NaN values and remove rows with NaN values\n",
"# df = df.dropna()\n",
"original_length = len(df)\n",
"\n",
"df = df.dropna()\n",
"\n",
"removed_rows = original_length - len(df)\n",
"print(f\"Number of rows removed due to NaN values: {removed_rows}\")\n",
"\n",
"# Calculate the index to split the data at the last 2%\n",
"split_index = int(len(df) * 0.98)\n",
"\n",
"# Split the data into training and validation sets\n",
"train_df = df.iloc[:split_index] # First 98% for training\n",
"validation_df = df.iloc[split_index:] # Last 2% for validation\n",
"\n",
"# Display the sizes of the training and validation sets\n",
"print(f\"Training set size: {len(train_df)}\")\n",
"print(f\"Validation set size: {len(validation_df)}\")\n",
"\n",
"# Save the datasets to new CSV files\n",
"train_df.to_csv(\"FalconData_train2.csv\", index=False)\n",
"validation_df.to_csv(\"FalconData_validation2.csv\", index=False)\n"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "55d929c5-c198-4a91-b31d-65dd83fa00d2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of duplicate rows: 4\n",
" Text\n",
"522 Name:\n",
"11745 Description.\\nReviews\\nThere are no reviews yet.\n",
"17605 Description.\\nReviews\\nThere are no reviews yet.\n",
"42547 !\\n\n"
]
}
],
"source": [
"# Read the CSV file into a pandas DataFrame\n",
"df1 = pd.read_csv(\"FalconData_train2.csv\")\n",
"\n",
"# Check for duplicate rows\n",
"duplicate_rows1 = df1[df1.duplicated()]\n",
"\n",
"# Print the number of duplicate rows\n",
"print(f\"Number of duplicate rows: {len(duplicate_rows1)}\")\n",
"\n",
"# Print the duplicate rows\n",
"print(duplicate_rows1)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "3cc404d9-e85e-48ff-aa34-750ebe3e3d3c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Text</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>[…]\\nM&S bank […]\\nLowest unsecured loan rate...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>JavaScript seems to be disabled in your browse...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>CMTech has designed a game to foster social in...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>A Storyteller's Point of View\\nMy\\nWriting\\nLe...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>mspu.us was registered 1 decade 3 years ago. I...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Text\n",
"0 […]\\nM&S bank […]\\nLowest unsecured loan rate...\n",
"1 JavaScript seems to be disabled in your browse...\n",
"2 CMTech has designed a game to foster social in...\n",
"3 A Storyteller's Point of View\\nMy\\nWriting\\nLe...\n",
"4 mspu.us was registered 1 decade 3 years ago. I..."
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1.head()"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "641c606f-6f7f-4097-a8de-a9f6be0047b1",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Text</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>48995</th>\n",
" <td>A Chenango County man was charged Wednesday wi...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>48996</th>\n",
" <td>2-Tone Black Personalized Embroidered One Init...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>48997</th>\n",
" <td>NARAL Pro-Choice America PAC Endorses Colleen ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>48998</th>\n",
" <td>Posts Tagged by Thomas Paine\\nAEI Hosts Peter ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>48999</th>\n",
" <td>Pantry feeds families in need\\n- Details\\n- Ca...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Text\n",
"48995 A Chenango County man was charged Wednesday wi...\n",
"48996 2-Tone Black Personalized Embroidered One Init...\n",
"48997 NARAL Pro-Choice America PAC Endorses Colleen ...\n",
"48998 Posts Tagged by Thomas Paine\\nAEI Hosts Peter ...\n",
"48999 Pantry feeds families in need\\n- Details\\n- Ca..."
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1.tail()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8f7dbf6-5d74-4f8f-85d0-e890a5b8d152",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|