python libsvm學習1

1.任務1:
(1)代碼如下:

from numpy import *

def loadDataSet(fileName): #general function to parse tab -delimited floats
dataMat = [] #assume last column is target value
fr = open(fileName)
for line in fr.readlines():
curLine = line.strip().split(’\t’)
fltLine = map(float,curLine) #map all elements to float()
dataMat.append(list(fltLine))
return dataMat
(2)讀取數據截圖如下:

2.任務2:
該txt文件中每行的數據個數不同
3.任務3:
(1)
①方法1代碼:

from numpy import *

data = []
with open(“features_total.txt”, “r”) as mon0:
f1 = mon0.read()
data = f1.split()
count = 0
with open(“newData.txt”, “w”) as mon1:
for i in data:
if count == 20:
count = 0
mon1.writelines(’\n’)
count = count + 1
mon1.writelines(i + " ")

②方法一寫入另一個文件的文件內容截圖如下:

(2)
①方法二:

from numpy import *

data = []
with open(“features_total.txt”, “r”) as mon0:
f1 = mon0.read()
data = f1.split()
with open(“save.txt”, “w”) as mon1:
for word in data:
for i in range(20):
try:
if i < 19:
mon1.write(data[i] + ‘\t’)
else:
mon1.write(data[i] + ‘\n’)
except:
pass

②方法二寫入另一個文件的文件內容截圖如下:

4.任務4
(1)方法一:
①代碼如下:

import sys
import pandas as pd

dataframe = pd.read_csv(“510050sh.csv”,"“gbk”")
print(dataframe)
dataframe.to_csv(“csvread1.txt”,index = False)

②寫入另一個文件的文件內容截圖如下:

(2)方法2:
①代碼如下:
import csv
with open(“510050sh.csv”,“r”,newline="") as csv_in_file:
with open(“csvread1.txt”,‘w’,newline="") as csv_out_file:
file_reader = csv.reader(csv_in_file)
file_writer = csv.writer(csv_out_file)
for row_list in file_reader:
print(row_list)
file_writer.writerow(row_list)

②:寫入另一個文件的文件內容截圖如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章