最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

對(duì)python .txt文件讀取及數(shù)據(jù)處理方法總結(jié)

 更新時(shí)間:2018年04月23日 10:01:55   作者:007lizhen  
下面小編就為大家分享一篇對(duì)python .txt文件讀取及數(shù)據(jù)處理方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

1、處理包含數(shù)據(jù)的文件

最近利用Python讀取txt文件時(shí)遇到了一個(gè)小問題,就是在計(jì)算兩個(gè)np.narray()類型的數(shù)組時(shí),出現(xiàn)了以下錯(cuò)誤:

TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U3') dtype('<U3') dtype('<U3')

作為一個(gè)Python新手,遇到這個(gè)問題后花費(fèi)了挺多時(shí)間,在網(wǎng)上找了許多大神們寫的例子,最后終于解決了。

總結(jié)如下:

(1)出現(xiàn)此問題的原因是:目的是想計(jì)算兩個(gè)數(shù)組間的差值,但數(shù)組中的元素不是數(shù)據(jù)類型(float或int等),而是str類型的。

(2)解決方法:在為空數(shù)組添加數(shù)據(jù)過程中,將每個(gè)數(shù)據(jù)強(qiáng)制轉(zhuǎn)化為float型。

如將“character.append(dataSet[i][:-1])”修改為“ character.append([float(tk) for tk in dataSet[i][:-1]])”

現(xiàn)將利用Python讀取txt文件的過程總結(jié)如下:

python版本為python3.6

(1)函數(shù)定義,存放于Function.py文件中:

from numpy import *
import random
#讀取數(shù)據(jù)函數(shù),返回list類型的訓(xùn)練數(shù)據(jù)集和測(cè)試數(shù)據(jù)集
def loadData(fileName): 
 trainingData=[]
 testData=[]
 with open(fileName) as txtData:
 lines=txtData.readlines()
 for line in lines:
  lineData=line.strip().split(',') #去除空白和逗號(hào)“,”
  if random.random()<0.7:  #數(shù)據(jù)集分割比例
  trainingData.append(lineData) #訓(xùn)練數(shù)據(jù)集
  else:
  testData.append(lineData) #測(cè)試數(shù)據(jù)集
 return trainingData,testData
#輸入數(shù)據(jù)為list類型,分割數(shù)據(jù)集,分割為特征和標(biāo)簽兩部分,返回?cái)?shù)據(jù)為np.narray類型
def splitData(dataSet): 
 character=[]
 label=[]
 for i in range(len(dataSet)):
 character.append([float(tk) for tk in dataSet[i][:-1]])
 label.append(dataSet[i][-1])
 return array(character),array(label)

(2)實(shí)現(xiàn)兩個(gè)數(shù)組間的減法,存放于main.py文件中:

#__author__=='qustl_000'
#-*- coding: utf-8 -*-
import numpy as np
import Function
fileName="1.txt"
trainingData,testData=Function.loadData(fileName)
trainingCharacter,trainingLabel=Function.splitData(trainingData)
testCharacter,testLabel=Function.splitData(testData)
diff1=np.tile(testCharacter[0],(len(trainingCharacter),1))-trainingCharacter
print('測(cè)試數(shù)據(jù)集的一條數(shù)據(jù),擴(kuò)充到與訓(xùn)練數(shù)據(jù)集同維:')
print(np.tile(testCharacter[0],(len(trainingCharacter),1)))
print('訓(xùn)練數(shù)據(jù)集:')
print(trainingCharacter)
print('作差后的結(jié)果:')
print(diff1)

(3)運(yùn)行結(jié)果:

測(cè)試數(shù)據(jù)集的一條數(shù)據(jù),擴(kuò)充到與訓(xùn)練數(shù)據(jù)集同維:
[[ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]
 [ 1.5 60. ]]
訓(xùn)練數(shù)據(jù)集:
[[ 1.5 40. ]
 [ 1.5 50. ]
 [ 1.6 40. ]
 [ 1.6 50. ]
 [ 1.6 60. ]
 [ 1.6 70. ]
 [ 1.7 60. ]
 [ 1.7 70. ]
 [ 1.7 80. ]
 [ 1.8 60. ]
 [ 1.8 80. ]
 [ 1.8 90. ]
 [ 1.9 90. ]]
作差后的結(jié)果:
[[ 0. 20. ]
 [ 0. 10. ]
 [ -0.1 20. ]
 [ -0.1 10. ]
 [ -0.1 0. ]
 [ -0.1 -10. ]
 [ -0.2 0. ]
 [ -0.2 -10. ]
 [ -0.2 -20. ]
 [ -0.3 0. ]
 [ -0.3 -20. ]
 [ -0.3 -30. ]
 [ -0.4 -30. ]]

數(shù)據(jù)集如下:

1.5,40,thin
1.5,50,fat
1.5,60,fat
1.6,40,thin
1.6,50,thin
1.6,60,fat
1.6,70,fat
1.7,50,thin
1.7,60,thin
1.7,70,fat
1.7,80,fat
1.8,60,thin
1.8,70,thin
1.8,80,fat
1.8,90,fat
1.9,80,thin
1.9,90,fat

2、處理文本文件,如情感識(shí)別類的文件

在進(jìn)行文本的情感分類時(shí),從電影評(píng)論數(shù)據(jù)集網(wǎng)站上下載數(shù)據(jù)集后,發(fā)現(xiàn)數(shù)據(jù)集中存在許多不需要的符號(hào)。截取部分包含多余字符的數(shù)據(jù)如下:

下載數(shù)據(jù)集后,所有txt文件存放在兩個(gè)文件夾:“neg”(包含消極評(píng)論)和“pos”(包含積極地評(píng)論)中。

兩者的存放目錄如下:“F:\Self_Learning\機(jī)器學(xué)習(xí)\python\Bayes\review_polarity\txt_sentoken”。后面需要用到文件路徑,此路徑可根據(jù)自己存放目錄修改。

主要涉及到的python操作有:多余字符的刪除、文件夾中多文件的操作。

2.1 多余字符的刪除

首先,我們要?jiǎng)h除多余的符號(hào),獲得干凈的數(shù)據(jù)。

經(jīng)過查找資料,知道刪除一條文本數(shù)據(jù)中不需要的符號(hào),可以通過re.sub(chara,newChara,data)函數(shù)實(shí)現(xiàn),其中chara是需要?jiǎng)h除的字符,newChara是刪除字符后相應(yīng)位置的替換字符,data是需要操作的數(shù)據(jù)。比如下面的代碼,指的是刪除lines中包含的前面列出的字符,并用空白替換:

lineString = re.sub("[\n\.\!\/_\-$%^*(+\"\')]+|[+—()?【】“”!:,;.?、~@#¥%…&*()0123456789]+", " ", lines)

2.2 python對(duì)多文件的操作

下面的程序中,pathDirPos指的是所有積極評(píng)論的txt文件所在的目錄,在此指的是“F:\Self_Learning\機(jī)器學(xué)習(xí)\python\Bayes\review_polarity\txt_sentoken\pos”。child就是獲得的每個(gè)txt文件全名。

 for allDir in pathDirPos:
 child = os.path.join('%s' % allDir)

2.3 電影評(píng)論數(shù)據(jù)集預(yù)處理

下面給出對(duì)于電影評(píng)論數(shù)據(jù)集的預(yù)處理程序(python3.6).

'''獲取數(shù)據(jù),并去除數(shù)據(jù)中的多余符號(hào),返回list類型的數(shù)據(jù)集'''
def loadData(pathDirPos,pathDirNeg):
 posAllData = [] # 積極評(píng)論
 negAllData = [] # 消極評(píng)論
 # 積極評(píng)論
 for allDir in pathDirPos:
 lineDataPos = []
 child = os.path.join('%s' % allDir)
 filename = r"review_polarity/txt_sentoken/pos/" + child
 with open(filename) as childFile:
  for lines in childFile:
  lineString = re.sub("[\n\.\!\/_\-$%^*(+\"\')]+|[+—()?【】“”!:,;.?、~@#¥%…&*()0123456789]+", " ", lines)
  line = lineString.split(' ') #用空白分割每個(gè)文件中的數(shù)據(jù)集(此時(shí)還包含許多空白字符)
  for strc in line:
   if strc != "" and len(strc) > 1: #刪除空白字符,并篩選出長(zhǎng)度大于1的單詞
   lineDataPos.append(strc)
  posAllData.append(lineDataPos)
 # 消極評(píng)論
 for allDir in pathDirNeg:
 lineDataNeg = []
 child = os.path.join('%s' % allDir)
 filename = r"review_polarity/txt_sentoken/neg/" + child
 with open(filename) as childFile:
  for lines in childFile:
  lineString = re.sub("[\n\.\!\/_\-$%^*(+\"\')]+|[+—()?【】“”!:,;.?、~@#¥%…&*()0123456789]+", " ", lines)
  line = lineString.split(' ') #用空白分割每個(gè)文件中的數(shù)據(jù)集(此時(shí)還包含許多空白字符)
  for strc in line:
   if strc != "" and len(strc) > 1: #刪除空白字符,并篩選出長(zhǎng)度大于1的單詞
   lineDataNeg.append(strc)
  negAllData.append(lineDataNeg)
 return posAllData,negAllData
'''劃分?jǐn)?shù)據(jù)集,將數(shù)據(jù)集劃分為訓(xùn)練數(shù)據(jù)和測(cè)試數(shù)據(jù),參數(shù)splitPara為分割比例'''
def splitDataSet(pathDirPos,pathDirNeg,splitPara):
 trainingData=[]
 testData=[]
 traingLabel=[]
 testLabel=[]
 posData,negData=loadData(pathDirPos,pathDirNeg)
 pos_len=len(posData)
 neg_len=len(negData)
 #操作積極評(píng)論數(shù)據(jù)
 for i in range(pos_len):
 if(random.random()<splitPara):
  trainingData.append(posData[i])
  traingLabel.append(1)
 else:
  testData.append(posData[i])
  testLabel.append(1)
 for j in range(neg_len):
 if(random.random()<splitPara):
  trainingData.append(negData[j])
  traingLabel.append(0)
 else:
  testData.append(negData[j])
  testLabel.append(0)
 return trainingData,traingLabel,testData,testLabel

以上這篇對(duì)python .txt文件讀取及數(shù)據(jù)處理方法總結(jié)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 理解Django 中Call Stack機(jī)制的小Demo

    理解Django 中Call Stack機(jī)制的小Demo

    這篇文章主要介紹了理解Django 中Call Stack 機(jī)制的小Demo,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Python中plt.imshow(image)無法顯示圖片的解決

    Python中plt.imshow(image)無法顯示圖片的解決

    這篇文章主要介紹了Python中plt.imshow(image)無法顯示圖片的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python切片操作方法的實(shí)例總結(jié)

    python切片操作方法的實(shí)例總結(jié)

    所謂切片就是在某個(gè)數(shù)據(jù)里提取需要的部分,提取到的是某個(gè)索引下的值,或者索引區(qū)間的值,下面這篇文章主要給大家介紹了關(guān)于python切片操作方法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • python通過SSH登陸linux并操作的實(shí)現(xiàn)

    python通過SSH登陸linux并操作的實(shí)現(xiàn)

    這篇文章主要介紹了python通過SSH登陸linux并操作的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Python圖像讀寫方法對(duì)比

    Python圖像讀寫方法對(duì)比

    這篇文章主要介紹了Python圖像讀寫方法對(duì)比的相關(guān)資料,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11
  • Python上級(jí)目錄文件導(dǎo)入的幾種方法(from.import)

    Python上級(jí)目錄文件導(dǎo)入的幾種方法(from.import)

    有時(shí)候我們可能需要import另一個(gè)路徑下的python文件,下面這篇文章主要給大家介紹了關(guān)于Python上級(jí)目錄文件導(dǎo)入的幾種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Python如何使用Scapy實(shí)現(xiàn)端口探測(cè)

    Python如何使用Scapy實(shí)現(xiàn)端口探測(cè)

    Scapy 是一款使用純Python編寫的跨平臺(tái)網(wǎng)絡(luò)數(shù)據(jù)包操控工具,它能夠處理和嗅探各種網(wǎng)絡(luò)數(shù)據(jù)包,本文主要介紹了Python如何使用使用Scapy實(shí)現(xiàn)端口探測(cè),有需要的可以參考下
    2023-10-10
  • 利用python實(shí)現(xiàn)可視化大屏

    利用python實(shí)現(xiàn)可視化大屏

    這篇文章主要介紹了利用python實(shí)現(xiàn)可視化大屏,文章圍繞主題展開對(duì)如何利用python實(shí)現(xiàn)可視化大屏,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)和工作有所幫助
    2022-03-03
  • 樹莓派4B安裝Tensorflow的方法步驟

    樹莓派4B安裝Tensorflow的方法步驟

    這篇文章主要介紹了樹莓派4B安裝Tensorflow的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • python pandas消除空值和空格以及 Nan數(shù)據(jù)替換方法

    python pandas消除空值和空格以及 Nan數(shù)據(jù)替換方法

    今天小編就為大家分享一篇python pandas消除空值和空格以及 Nan數(shù)據(jù)替換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10

最新評(píng)論

蓬莱市| 上林县| 敖汉旗| 塔城市| 高淳县| 芦山县| 古交市| 南召县| 江津市| 加查县| 湘西| 铜陵市| 岳阳县| 冕宁县| 綦江县| 贵州省| 咸丰县| 呼玛县| 鹿泉市| 石景山区| 曲阳县| 清水县| 阿克苏市| 镇赉县| 南宫市| 庄浪县| 河间市| 西和县| 兴宁市| 天台县| 泾源县| 安福县| 辽阳市| 永仁县| 五莲县| 和林格尔县| 陇南市| 安陆市| 天镇县| 长沙县| 香河县|