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)

②:写入另一个文件的文件内容截图如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值